Debian Codegraph @

src/tools/selection_tools/select_free.py

raw symbols SCIP
1 # select_free.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_select import AbstractSelectionTool
20
21 class ToolFreeSelect(AbstractSelectionTool):
22 __gtype_name__ = 'ToolFreeSelect'
23
24 def __init__(self, window, **kwargs):
25 # Context: this is a tool to select an area according to a shape that
26 # can be freely defined by the user.
27 super().__init__('free_select', _("Free selection"), 'tool-select-free-symbolic', window)
28 self.closing_precision = 10
29 self.closing_x = 0.0
30 self.closing_y = 0.0
31 self.add_tool_action_simple('selection_close', self._force_close_shape)
32 self.set_action_sensitivity('selection_close', False)
33
34 def on_tool_unselected(self, *args):
35 super().on_tool_unselected()
36 self.set_action_sensitivity('selection_close', False)
37
38 def get_editing_tips(self):
39 tips = super().get_editing_tips()
40 future_path = self.get_selection().get_future_path()
41 if future_path is not None and not self.selection_is_active():
42 label_instruction = self.label + " - " + \
43 _("Click on the shape's first point to close it.")
44 tips.append(label_instruction)
45 return tips
46
47 ############################################################################
48
49 def press_define(self, event_x, event_y):
50 self._draw_shape(event_x, event_y)
51 self.set_action_sensitivity('selection_close', True)
52
53 def motion_define(self, event_x, event_y, render):
54 self._draw_shape(event_x, event_y, render)
55
56 def release_define(self, surface, event_x, event_y):
57 if self._draw_shape(event_x, event_y):
58 self.restore_pixbuf()
59 self.operation_type = 'op-define'
60 operation = self.build_operation()
61 self.apply_operation(operation)
62 self.set_action_sensitivity('selection_close', False)
63 else:
64 return # without updating the surface so the path is visible
65
66 ############################################################################
67
68 def _force_close_shape(self, *args):
69 self.release_define(None, self.closing_x, self.closing_y)
70
71 def _draw_shape(self, event_x, event_y, render=True):
72 """This method is specific to the 'free selection' mode. It returns a
73 boolean, true if the shape should be closed."""
74 cairo_context = self.get_context()
75 cairo_context.set_source_rgba(0.5, 0.5, 0.5, 0.5)
76 thickness = self.get_overlay_thickness()
77 cairo_context.set_dash([3 * thickness, 3 * thickness])
78 cairo_context.set_line_width(thickness)
79 if self.get_selection().get_future_path() is None:
80 self.closing_x = event_x
81 self.closing_y = event_y
82 cairo_context.move_to(event_x, event_y)
83 self._pre_load_path(cairo_context.copy_path())
84 return False
85 delta_x = max(event_x, self.closing_x) - min(event_x, self.closing_x)
86 delta_y = max(event_y, self.closing_y) - min(event_y, self.closing_y)
87 cairo_context.append_path(self.get_selection().get_future_path())
88 if (delta_x < self.closing_precision) and (delta_y < self.closing_precision):
89 cairo_context.close_path()
90 cairo_context.stroke_preserve()
91 self._pre_load_path(cairo_context.copy_path())
92 return True
93 else:
94 cairo_context.line_to(event_x, event_y)
95 cairo_context.stroke_preserve() # draw the line without closing the path
96 self._pre_load_path(cairo_context.copy_path())
97 if render:
98 self.non_destructive_show_modif()
99 return False
100
101 ############################################################################
102 ################################################################################
103