Debian Codegraph @

src/tools/classic_tools/tool_line.py

raw symbols SCIP
1 # tool_line.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_add_arrow_triangle
21
22 class ToolLine(AbstractClassicTool):
23 __gtype_name__ = 'ToolLine'
24
25 def __init__(self, window, **kwargs):
26 super().__init__('line', _("Line"), 'tool-line-symbolic', window)
27 self.use_operator = True
28
29 self._use_outline = False
30 self._dashes_type = 'none'
31 self._arrow_type = 'none'
32 self._use_gradient = False
33 # Lock the tool to only draw orthogonal lines (multiples of 45°)
34 self._ortholock = False # Unrelated to Maïté's ortolan.
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_enum('arrow-type', self._arrow_type)
39 self.add_tool_action_boolean('use_gradient', self._use_gradient)
40 self.add_tool_action_boolean('pencil-outline', self._use_outline)
41 self.add_tool_action_boolean('line-ortholock', self._ortholock)
42 self._set_options_attributes() # Not optimal but more readable
43
44 def get_tooltip(self, event_x, event_y, motion_behavior):
45 if motion_behavior != 1:
46 return None # no line is being drawn
47
48 delta_x = abs(self.x_press - event_x)
49 delta_y = abs(self.y_press - event_y)
50 line1 = _("Width: %spx") % str(delta_x)
51 line2 = _("Height: %spx") % str(delta_y)
52 length = round(math.sqrt(delta_x * delta_x + delta_y * delta_y), 2)
53 return line1 + "\n" + line2 + "\n" + _("Length: %spx") % str(length)
54
55 ############################################################################
56
57 def _set_active_shape(self):
58 state_as_string = self.get_option_value('line_shape')
59 if state_as_string == 'thin':
60 self._cap_id = cairo.LineCap.BUTT
61 else:
62 self._cap_id = cairo.LineCap.ROUND
63
64 def get_options_label(self):
65 return _("Line options")
66
67 def _set_options_attributes(self):
68 self._use_outline = self.get_option_value('pencil-outline')
69 self._dashes_type = self.get_option_value('dashes-type')
70 self._arrow_type = self.get_option_value('arrow-type')
71 self._use_gradient = self.get_option_value('use_gradient')
72 self._ortholock = self.get_option_value('line-ortholock')
73 self._set_active_shape()
74
75 def get_editing_tips(self):
76 self._set_options_attributes()
77 is_arrow = self._arrow_type != 'none'
78 use_dashes = self._dashes_type != 'none'
79
80 label_options = self.label
81 if is_arrow and use_dashes:
82 label_options += " - " + _("Dashed arrow")
83 elif is_arrow:
84 label_options += " - " + _("Arrow")
85 elif use_dashes:
86 label_options += " - " + _("Dashed")
87 else:
88 label_options = None
89
90 label_modifier_shift = self.label + " - "
91 if self._ortholock:
92 label_modifier_shift += _("Press <Shift> to unlock the line direction")
93 else:
94 label_modifier_shift += _("Press <Shift> to lock the line direction")
95
96 label_modifier_alt = self.label + " - " + \
97 _("Press <Alt> to toggle the 'outline' option")
98
99 if self.get_image().get_mouse_is_pressed():
100 label_modifier_shift = None
101 label_modifier_alt = None
102
103 full_list = [label_options, label_modifier_shift, label_modifier_alt]
104 return list(filter(None, full_list))
105
106 ############################################################################
107
108 def on_press_on_area(self, event, surface, event_x, event_y):
109 self.set_common_values(event.button, event_x, event_y)
110
111 self.update_modifier_state(event.state)
112 if 'SHIFT' in self._modifier_keys:
113 self._ortholock = not self._ortholock
114 if 'ALT' in self._modifier_keys:
115 self._use_outline = not self._use_outline
116
117 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
118 if not render:
119 return
120 operation = self.build_operation(event_x, event_y)
121 self.do_tool_operation(operation)
122
123 def on_release_on_area(self, event, surface, event_x, event_y):
124 operation = self.build_operation(event_x, event_y)
125 self.apply_operation(operation)
126
127 ############################################################################
128
129 def build_operation(self, event_x, event_y):
130 operation = {
131 'tool_id': self.id,
132 'rgba': self.main_color,
133 'rgba2': self.secondary_color,
134 'antialias': self._use_antialias,
135 'operator': self._operator,
136 'line_width': self.tool_width,
137 'line_cap': self._cap_id,
138 'dashes': self._dashes_type,
139 'arrow': self._arrow_type,
140 'gradient': self._use_gradient,
141 'outline': self._use_outline,
142 'ortholock': self._ortholock,
143 'x_release': event_x,
144 'y_release': event_y,
145 'x_press': self.x_press,
146 'y_press': self.y_press
147 }
148 return operation
149
150 def do_tool_operation(self, operation):
151 cairo_context = self.start_tool_operation(operation)
152 cairo_context.set_operator(operation['operator'])
153 line_width = operation['line_width']
154 cairo_context.set_line_width(line_width)
155 x1 = operation['x_press']
156 y1 = operation['y_press']
157 x2 = operation['x_release']
158 y2 = operation['y_release']
159
160 if operation['ortholock']:
161 delta_x = abs(x1 - x2)
162 delta_y = abs(y1 - y2)
163 if delta_x > 2 * delta_y:
164 # Strictly horizontal line
165 y2 = y1
166 elif delta_x * 2 < delta_y:
167 # Strictly vertical line
168 x2 = x1
169 else:
170 # 45° line
171 delta45 = min(delta_x, delta_y)
172 x2 = x1 + delta45 if (x1 - x2 < 0) else x1 - delta45
173 y2 = y1 + delta45 if (y1 - y2 < 0) else y1 - delta45
174
175 self.set_dashes_and_cap(cairo_context, line_width, \
176 operation['dashes'], operation['line_cap'])
177
178 if operation['arrow'] == 'double':
179 utilities_add_arrow_triangle(cairo_context, x1, y1, x2, y2, line_width)
180
181 # We don't memorize the path because all coords are here anyway for the
182 # linear gradient and/or the arrow.
183 cairo_context.move_to(x1, y1)
184 cairo_context.line_to(x2, y2)
185
186 if operation['arrow'] != 'none':
187 utilities_add_arrow_triangle(cairo_context, x2, y2, x1, y1, line_width)
188
189 if operation['outline']:
190 cairo_context.set_source_rgba(*operation['rgba2'])
191 cairo_context.set_line_width(line_width * 1.2 + 2)
192 cairo_context.stroke_preserve()
193
194 if operation['gradient']:
195 pattern = cairo.LinearGradient(x1, y1, x2, y2)
196 pattern.add_color_stop_rgba(0.1, *operation['rgba'])
197 pattern.add_color_stop_rgba(0.9, *operation['rgba2'])
198 cairo_context.set_source(pattern)
199 else:
200 cairo_context.set_source_rgba(*operation['rgba'])
201 cairo_context.set_line_width(line_width)
202 cairo_context.stroke()
203
204 ############################################################################
205 ################################################################################
206