Debian Codegraph @

src/tools/classic_tools/tool_points.py

raw symbols SCIP
1 # tool_points.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
21 class ToolPoints(AbstractClassicTool):
22 __gtype_name__ = 'ToolPoints'
23
24 def __init__(self, window, **kwargs):
25 # Context: this is the name of a tool to draw little circles, crosses,
26 # or squares at precise points of the image, for example to draw a vague
27 # graph, or to highlight something in an image.
28 # A number can be added on the cross/circle/square to help captioning
29 # the elements of an image.
30 super().__init__('points', _("Points"), 'tool-points-symbolic', window)
31
32 self.add_tool_action_enum('points_type', 'cross')
33 self.add_tool_action_boolean('points_number', False)
34 self.add_tool_action_simple('points_reset_num', self._reset_number)
35 self.add_tool_action_simple('points_inc_num', self._increment_number)
36 self.add_tool_action_simple('points_dec_num', self._decrement_number)
37
38 self._next_number = 1
39 self._set_active_type() # Not optimal but more readable
40
41 def _set_active_type(self):
42 state_as_string = self.get_option_value('points_type')
43 if state_as_string == 'cross':
44 self._shape_label = _("Cross")
45 elif state_as_string == 'x-cross':
46 self._shape_label = _("X-shaped cross")
47 elif state_as_string == 'square':
48 self._shape_label = _("Square")
49 else:
50 self._shape_label = _("Circle")
51 self._points_type = state_as_string
52
53 self._use_number = self.get_option_value('points_number')
54 self.set_action_sensitivity('points_reset_num', self._use_number)
55 self.set_action_sensitivity('points_inc_num', self._use_number)
56 self.set_action_sensitivity('points_dec_num', self._use_number)
57
58 def _reset_number(self, *args):
59 self._next_number = 1
60 self.window.set_window_subtitles()
61
62 def _increment_number(self, *args):
63 self._next_number += 1
64 self.window.set_window_subtitles()
65
66 def _decrement_number(self, *args):
67 self._next_number -= 1
68 self.window.set_window_subtitles()
69
70 def get_options_label(self):
71 return _("Points options")
72
73 def get_editing_tips(self):
74 self._set_active_type()
75 label = self.label + " - " + self._shape_label
76 if self._use_number:
77 label += " - " + _("Next number: %s") % self._next_number
78 # A single label is returned so the next number is always visible
79 return [label]
80
81 ############################################################################
82
83 def on_press_on_area(self, event, surface, event_x, event_y):
84 self.set_common_values(event.button, event_x, event_y)
85
86 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
87 if render:
88 operation = self.build_operation(event_x, event_y)
89 self.do_tool_operation(operation)
90
91 def on_release_on_area(self, event, surface, event_x, event_y):
92 operation = self.build_operation(event_x, event_y)
93 self.apply_operation(operation)
94 if self._use_number:
95 self._increment_number()
96
97 ############################################################################
98
99 def build_operation(self, event_x, event_y):
100 if self._use_number:
101 number = self._next_number
102 else:
103 number = None
104 operation = {
105 'tool_id': self.id,
106 'rgba': self.main_color,
107 'rgba2': self.secondary_color, # for the font of the number
108 'antialias': self._use_antialias,
109 'line_width': self.tool_width,
110 'point_type': self._points_type,
111 'number': number,
112 'x': event_x,
113 'y': event_y,
114 }
115 return operation
116
117 def do_tool_operation(self, operation):
118 cairo_context = self.start_tool_operation(operation)
119 cairo_context.set_line_cap(cairo.LineCap.BUTT)
120 cairo_context.set_source_rgba(*operation['rgba'])
121
122 point_width = operation['line_width']
123 line_width = max(1, int(point_width / 4))
124 half_width = max(1, int(point_width / 2))
125 cairo_context.set_line_width(line_width)
126
127 x = operation['x']
128 y = operation['y']
129
130 point_type = operation['point_type']
131 if point_type == 'circle':
132 cairo_context.arc(x, y, half_width, 0.0, 2 * math.pi)
133 cairo_context.fill()
134 elif point_type == 'cross':
135 # Looks awful with small sizes, either with or without antialiasing
136 cairo_context.move_to(x, y - half_width)
137 cairo_context.line_to(x, y + half_width)
138 cairo_context.move_to(x - half_width, y)
139 cairo_context.line_to(x + half_width, y)
140 elif point_type == 'x-cross':
141 cairo_context.move_to(x - half_width, y - half_width)
142 cairo_context.line_to(x + half_width, y + half_width)
143 cairo_context.move_to(x + half_width, y - half_width)
144 cairo_context.line_to(x - half_width, y + half_width)
145 else: # if point_type == 'square':
146 cairo_context.set_line_width(point_width)
147 cairo_context.move_to(x, y - half_width)
148 cairo_context.line_to(x, y + half_width)
149
150 cairo_context.stroke() # without operator support, because it wouldn't
151 # make much sense, and the abstract method for them doesn't support
152 # changing the line width depending on the point type (like here).
153
154 number = operation['number']
155 if number is None:
156 return
157
158 # Text size
159 cairo_context.set_font_size(max(1, int(point_width * 0.8)))
160
161 # Coordinates
162 if number < 10:
163 num_x = x - line_width
164 elif number < 100:
165 num_x = x - line_width * 2
166 else:
167 num_x = x - line_width * 3
168 # XXX could be better with cairo.ScaledFont.text_extents()
169 num_y = y + line_width
170
171 # Outline
172 for dx in [-1, 1]:
173 for dy in [-1, 1]:
174 cairo_context.move_to(num_x + dx, num_y + dy)
175 cairo_context.show_text(str(number))
176 # XXX all my text insertions could use a "layer" like the brushes
177
178 # Text color
179 cairo_context.set_source_rgba(*operation['rgba2'])
180
181 # Cairo's "toy" text API (enough for numbers)
182 cairo_context.move_to(num_x, num_y)
183 cairo_context.show_text(str(number))
184
185 ############################################################################
186 ################################################################################
187