Debian Codegraph @

src/tools/classic_tools/tool_arc.py

raw symbols SCIP
1 # tool_arc.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
19 from .abstract_classic_tool import AbstractClassicTool
20 from .utilities_paths import utilities_add_arrow_triangle
21
22 class ToolArc(AbstractClassicTool):
23 __gtype_name__ = 'ToolArc'
24
25 def __init__(self, window, **kwargs):
26 super().__init__('arc', _("Curve"), 'tool-arc-symbolic', window)
27 self.use_operator = True
28
29 # Default values
30 self._cap_id = cairo.LineCap.ROUND
31 self._1st_segment = None
32 self._dashes_type = 'none'
33 self._arrow_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_enum('arrow-type', self._arrow_type)
39 self.add_tool_action_boolean('pencil-outline', self._use_outline)
40
41 def give_back_control(self, preserve_selection):
42 self._1st_segment = None
43 self.restore_pixbuf()
44
45 ############################################################################
46 # Options ##################################################################
47
48 def set_active_shape(self):
49 state_as_string = self.get_option_value('line_shape')
50 if state_as_string == 'thin':
51 self._cap_id = cairo.LineCap.BUTT
52 else:
53 self._cap_id = cairo.LineCap.ROUND
54
55 def get_options_label(self):
56 return _("Curve options")
57
58 def get_editing_tips(self):
59 self._use_outline = self.get_option_value('pencil-outline')
60 self._dashes_type = self.get_option_value('dashes-type')
61 self._arrow_type = self.get_option_value('arrow-type')
62 self.set_active_shape()
63 is_arrow = self._arrow_type != 'none'
64 use_dashes = self._dashes_type != 'none'
65
66 label_segments = self.label + " - "
67 if self._1st_segment is not None:
68 label_segments += _("Draw a second segment to complete the curve")
69 else:
70 label_segments = None
71
72 label_options = self.label + " - "
73 if is_arrow and use_dashes:
74 label_options += _("Dashed arrow")
75 elif is_arrow:
76 label_options += _("Arrow")
77 elif use_dashes:
78 label_options += _("Dashed")
79 else:
80 label_options = None
81
82 if self.get_image().get_mouse_is_pressed():
83 label_modifier_alt = None
84 else:
85 label_modifier_alt = self.label + " - " + \
86 _("Press <Alt> to toggle the 'outline' option")
87
88 full_list = [label_segments, label_options, label_modifier_alt]
89 return list(filter(None, full_list))
90
91 ############################################################################
92
93 def on_press_on_area(self, event, surface, event_x, event_y):
94 self.set_common_values(event.button, event_x, event_y)
95
96 self.update_modifier_state(event.state)
97 if 'ALT' in self._modifier_keys:
98 self._use_outline = not self._use_outline
99
100 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
101 cairo_context = self.get_context()
102 if self._1st_segment is None:
103 cairo_context.move_to(self.x_press, self.y_press)
104 cairo_context.line_to(event_x, event_y)
105 else:
106 cairo_context.move_to(self._1st_segment[0], self._1st_segment[1])
107 cairo_context.curve_to(self._1st_segment[2], self._1st_segment[3], \
108 self.x_press, self.y_press, event_x, event_y)
109 self._path = cairo_context.copy_path()
110
111 if not render:
112 return
113 operation = self.build_operation(event_x, event_y)
114 self.do_tool_operation(operation)
115
116 def on_release_on_area(self, event, surface, event_x, event_y):
117 if self._1st_segment is None:
118 self._1st_segment = (self.x_press, self.y_press, event_x, event_y)
119 return
120 else:
121 cairo_context = self.get_context()
122 cairo_context.move_to(self._1st_segment[0], self._1st_segment[1])
123 cairo_context.curve_to(self._1st_segment[2], self._1st_segment[3], \
124 self.x_press, self.y_press, event_x, event_y)
125 self._1st_segment = None
126
127 self._path = cairo_context.copy_path()
128 operation = self.build_operation(event_x, event_y)
129 self.apply_operation(operation)
130
131 ############################################################################
132
133 def build_operation(self, event_x, event_y):
134 operation = {
135 'tool_id': self.id,
136 'rgba': self.main_color,
137 'rgba2': self.secondary_color,
138 'antialias': self._use_antialias,
139 'operator': self._operator,
140 'line_width': self.tool_width,
141 'line_cap': self._cap_id,
142 'dashes': self._dashes_type,
143 'arrow': self._arrow_type,
144 'outline': self._use_outline,
145 'path': self._path,
146 'x_release': event_x,
147 'y_release': event_y,
148 'x_press': self.x_press,
149 'y_press': self.y_press
150 }
151 return operation
152
153 def do_tool_operation(self, operation):
154 cairo_context = self.start_tool_operation(operation)
155
156 cairo_context.set_operator(operation['operator'])
157 line_width = operation['line_width']
158 cairo_context.set_line_width(line_width)
159 self.set_dashes_and_cap(cairo_context, line_width, \
160 operation['dashes'], operation['line_cap'])
161
162 if operation['arrow'] == 'double':
163 for pts in operation['path']:
164 # how to do without a for???
165 if(pts[0] == cairo.PathDataType.MOVE_TO):
166 x1 = pts[1][0]
167 y1 = pts[1][1]
168 else:
169 x2 = pts[1][0]
170 y2 = pts[1][1]
171 utilities_add_arrow_triangle(cairo_context, x1, y1, x2, y2, line_width)
172
173 cairo_context.append_path(operation['path'])
174
175 if operation['arrow'] != 'none':
176 x1 = operation['x_press']
177 y1 = operation['y_press']
178 x2 = operation['x_release']
179 y2 = operation['y_release']
180 utilities_add_arrow_triangle(cairo_context, x2, y2, x1, y1, line_width)
181
182 if operation['outline']:
183 cairo_context.set_source_rgba(*operation['rgba2'])
184 cairo_context.set_line_width(line_width * 1.2 + 2)
185 cairo_context.stroke_preserve()
186
187 cairo_context.set_line_width(line_width)
188 cairo_context.set_source_rgba(*operation['rgba'])
189 cairo_context.stroke()
190
191 ############################################################################
192 ################################################################################
193