| 1 | |
| 2 | # The tools |
| 3 | |
| 4 | >See tools-class-diagram.dia |
| 5 | |
| 6 | The tools are managed by a bunch of files in the `src/tools` directory. |
| 7 | |
| 8 | ## State pattern |
| 9 | |
| 10 | >The relationship between the window and the tools is a |
| 11 | **[State](https://en.wikipedia.org/wiki/State_pattern)** design pattern. |
| 12 | |
| 13 | >All tools inherit from **abstract** classes defining empty methods. Then, the |
| 14 | active tool's methods are called from the currently active image's code (or the |
| 15 | window's code) regardless of what tool is active. |
| 16 | |
| 17 | First of all, `src/tools/abstract_tool.py` defines how the tool will be added in |
| 18 | the UI, provides several wrappers to add options, to access the pixbufs, to add |
| 19 | an operation to the edition history, etc. |
| 20 | |
| 21 | The main thing here are the methods that react to clicks on the canvas (starting |
| 22 | a click, moving the pointer, releasing the click). |
| 23 | |
| 24 | Then, an other layer of abstract classes is used, depending on the subcategory a |
| 25 | tool is in: |
| 26 | |
| 27 | #### Classic tools |
| 28 | |
| 29 | These tools draw on the main pixbuf using **`cairo`**, though it can be more |
| 30 | complex. |
| 31 | |
| 32 | `abstract_classic_tool.py` manages only a few things like anti-aliasing, and |
| 33 | setting what color is the main or the secondary one depending on the selected |
| 34 | colors and the mouse button you clicked. |
| 35 | |
| 36 | #### Selection tools |
| 37 | |
| 38 | They translates the user's input into operations using the image's |
| 39 | **selection_manager**. These operations are quite complex, and are almost |
| 40 | entirely managed in `abstract_select.py`. |
| 41 | |
| 42 | #### Transformation tools |
| 43 | |
| 44 | They (scale/crop/rotate/filters/…) can be applied to the selection pixbuf or the |
| 45 | main pixbuf, and may use the image's `temp_pixbuf` attribute to store a preview |
| 46 | of their changes. These tools have to be explicitly applied by the user, using |
| 47 | a button in the [bottom bar](./design-optionsbars.md). |
| 48 | |
| 49 | `abstract_transform_tool.py` defines methods to preview and apply the changes. |
| 50 | |
| 51 | ---- |
| 52 | |
| 53 | Other shared features, when they don't depend on the image or the tool at all |
| 54 | (such as blurring, computing some paths, displaying an overlay on the image (for |
| 55 | the selection for example)), may be provided by one of the |
| 56 | `src/tools/utilities_*.py` files. |
| 57 | |
| 58 | ## Command pattern |
| 59 | |
| 60 | >The relationship between the history and the tools is probably an incorrect |
| 61 | **[Command](https://en.wikipedia.org/wiki/Command_pattern)** design pattern. |
| 62 | |
| 63 | All code triggered by a click will "end" by a method building a dict (an |
| 64 | associative array) where the value of the key `tool_id` is the id of the tool |
| 65 | building the dict, and the other values tell how to draw whatever the tool is |
| 66 | supposed to draw. |
| 67 | |
| 68 | This dict is internally named an "operation". If the operation should be applied |
| 69 | *(e.g. the click to draw a line with the pencil is released)*, the method |
| 70 | `apply_operation` is invoked (by the tool itself…) and it: |
| 71 | |
| 72 | - executes the operation on the image (whose `cairo.Surface` is, i suppose, the |
| 73 | receiver in this design pattern); |
| 74 | - adds the operation dict to [the image](./design-general.md) editing history. |
| 75 | The history may invoke `apply_operation` again later, if the user undoes and |
| 76 | redoes operations. |
| 77 | |
| 78 | If the operation should **not** be applied, the tool may run code to execute it |
| 79 | directly *(e.g. preview a line while the click isn't released yet)*. In this |
| 80 | case, the surface will be cleaned from the previewed bullshit before seriously |
| 81 | applying the final operation. |
| 82 |