Debian Codegraph @

src/tools/classic_tools/tool_pencil.py

raw symbols SCIP
1 # tool_pencil.py
2 #
3 # Copyright 2018-2023 Romain F. T.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import cairo, math
19 from .abstract_classic_tool import AbstractClassicTool
20 from .utilities_paths import utilities_smooth_path
21
22 class ToolPencil(AbstractClassicTool):
23 __gtype_name__ = 'ToolPencil'
24
25 def __init__(self, window, **kwargs):
26 super().__init__('pencil', _("Pencil"), 'tool-pencil-symbolic', window)
27 self.use_operator = True
28
29 self._path = None
30 self._shape_label = _("Round")
31 self._cap_id = cairo.LineCap.ROUND
32 self._join_id = cairo.LineCap.ROUND
33 self._dashes_type = 'none'
34 self._use_outline = False
35
36 self.add_tool_action_enum('line_shape', 'round')
37 self.add_tool_action_enum('dashes-type', self._dashes_type)
38 self.add_tool_action_boolean('pencil-outline', self._use_outline)
39
40 def _set_active_shape(self, *args):
41 state_as_string = self.get_option_value('line_shape')
42 if state_as_string == 'thin':
43 self._cap_id = cairo.LineCap.BUTT
44 self._join_id = cairo.LineJoin.BEVEL
45 self._shape_label = _("Square")
46 else:
47 self._cap_id = cairo.LineCap.ROUND
48 self._join_id = cairo.LineJoin.ROUND
49 self._shape_label = _("Round")
50
51 def get_options_label(self):
52 return _("Pencil options")
53
54 def get_editing_tips(self):
55 self._dashes_type = self.get_option_value('dashes-type')
56 use_dashes = self._dashes_type != 'none'
57 self._use_outline = self.get_option_value('pencil-outline')
58 self._set_active_shape()
59
60 if use_dashes:
61 label_options = self.label + " - " + _("Dashed")
62 else:
63 label_options = None
64
65 if self.get_image().get_mouse_is_pressed():
66 label_modifier_alt = None
67 if label_options is None:
68 label_options = self.label
69 else:
70 label_modifier_alt = self.label + " - " + \
71 _("Press <Alt> to toggle the 'outline' option")
72
73 full_list = [label_options, label_modifier_alt]
74 return list(filter(None, full_list))
75
76 ############################################################################
77
78 def on_press_on_area(self, event, surface, event_x, event_y):
79 self.set_common_values(event.button, event_x, event_y)
80 self._path = None
81
82 self.update_modifier_state(event.state)
83 if 'ALT' in self._modifier_keys:
84 self._use_outline = not self._use_outline
85
86 def _add_point(self, event_x, event_y):
87 cairo_context = self.get_context()
88 if self._path is None:
89 cairo_context.move_to(self.x_press, self.y_press)
90 else:
91 cairo_context.append_path(self._path)
92 cairo_context.line_to(event_x, event_y)
93 self._path = cairo_context.copy_path()
94
95 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
96 self._add_point(event_x, event_y)
97 if not render:
98 return
99 operation = self.build_operation()
100 self.do_tool_operation(operation)
101
102 def on_release_on_area(self, event, surface, event_x, event_y):
103 self._add_point(event_x, event_y)
104
105 if self.x_press == event_x and self.y_press == event_y:
106 # Special case when the pointer hasn't moved: in order to "force"
107 # cairo to draw a pixel, a tiny segment is added artifically
108 float_x, float_y = self.get_image().get_event_coords(event, False)
109 decimals_x = float_x - event_x
110 if decimals_x > 0:
111 delta_x = 0.20
112 else:
113 delta_x = -0.20
114 decimals_y = float_y - event_y
115 if decimals_y > 0:
116 delta_y = 0.20
117 else:
118 delta_y = -0.20
119 self._add_point(event_x + delta_x, event_y + delta_y)
120
121 operation = self.build_operation()
122 self.apply_operation(operation)
123
124 ############################################################################
125
126 def build_operation(self):
127 operation = {
128 'tool_id': self.id,
129 'rgba': self.main_color,
130 'rgba2': self.secondary_color,
131 'antialias': self._use_antialias,
132 'smooth': not self.get_image().is_zoomed_surface_sharp(),
133 'outline': self._use_outline,
134 'operator': self._operator,
135 'line_width': self.tool_width,
136 'line_cap': self._cap_id,
137 'line_join': self._join_id,
138 'dashes': self._dashes_type,
139 'path': self._path
140 }
141 return operation
142
143 def do_tool_operation(self, operation):
144 if operation['path'] is None:
145 return
146 cairo_context = self.start_tool_operation(operation)
147
148 cairo_context.set_operator(operation['operator'])
149 line_width = operation['line_width']
150 self.set_dashes_and_cap(cairo_context, line_width, \
151 operation['dashes'], operation['line_cap'])
152 cairo_context.set_line_join(operation['line_join']) # XXX useless?
153
154 if operation['smooth']:
155 utilities_smooth_path(cairo_context, operation['path'])
156 else:
157 cairo_context.append_path(operation['path'])
158
159 if operation['outline']:
160 cairo_context.set_source_rgba(*operation['rgba2'])
161 cairo_context.set_line_width(line_width * 1.2 + 2)
162 cairo_context.stroke_preserve()
163
164 cairo_context.set_source_rgba(*operation['rgba'])
165 cairo_context.set_line_width(line_width)
166 cairo_context.stroke()
167
168 ############################################################################
169 ################################################################################
170