Debian Codegraph @

src/tools/transform_tools/tool_crop.py

raw symbols SCIP
1 # tool_crop.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 from gi.repository import Gdk, GdkPixbuf
19 from .abstract_transform_tool import AbstractCanvasTool
20 from .optionsbar_crop import OptionsBarCrop
21 from .utilities_overlay import utilities_show_handles_on_context
22 from .utilities_colors import utilities_rgba_to_hexadecimal, \
23 utilities_gdk_rgba_to_color_array
24
25 class ToolCrop(AbstractCanvasTool):
26 __gtype_name__ = 'ToolCrop'
27
28 def __init__(self, window):
29 super().__init__('crop', _("Crop"), 'tool-crop-symbolic', window)
30 self.cursor_name = 'not-allowed'
31 self.x_press = self.x_motion = 0
32 self.y_press = self.y_motion = 0
33 self._unclicked = True # locking operation execution to avoid inf. loops
34 self.add_tool_action_enum('crop-expand', 'initial')
35
36 def try_build_pane(self):
37 self.pane_id = 'crop'
38 self.window.options_manager.try_add_bottom_pane(self.pane_id, self)
39
40 def build_bottom_pane(self):
41 bar = OptionsBarCrop()
42 self.height_btn = bar.height_btn
43 self.width_btn = bar.width_btn
44 self.width_btn.connect('value-changed', self.on_width_changed)
45 self.height_btn.connect('value-changed', self.on_height_changed)
46 return bar
47
48 def get_options_label(self):
49 # The options of the "crop" tool are all about how to expand the canvas,
50 # hence this label.
51 return _("Expanding options")
52
53 def get_editing_tips(self):
54 label_direction = _("The sides you'll crop are hinted by the mouse pointer")
55
56 label_modifiers = None
57 if self.apply_to_selection:
58 label_action = _("Cropping the selection")
59 label_confirm = None
60 else:
61 label_action = _("Cropping or expanding the canvas")
62 label_confirm = self.label + " - " + \
63 _("Don't forget to confirm the operation!")
64 if not self.get_image().get_mouse_is_pressed():
65 label_modifiers = _("Press <Alt>, <Shift>, or both, to " + \
66 "quickly change the 'expand with' option")
67
68 full_list = [label_action, label_direction, label_confirm, label_modifiers]
69 return list(filter(None, full_list))
70
71 ############################################################################
72
73 def on_tool_selected(self, *args):
74 super().on_tool_selected()
75 self._x = 0
76 self._y = 0
77 if self.apply_to_selection:
78 self._init_if_selection()
79 else:
80 self._init_if_main()
81 self._update_expansion_rgba()
82 self.width_btn.set_value(self._original_width)
83 self.height_btn.set_value(self._original_height)
84 self.set_action_sensitivity('crop-expand', not self.apply_to_selection)
85 self.build_and_do_op()
86
87 def _init_if_selection(self):
88 self._original_width = self.get_selection_pixbuf().get_width()
89 self._original_height = self.get_selection_pixbuf().get_height()
90 self.width_btn.set_range(1, self._original_width)
91 self.height_btn.set_range(1, self._original_height)
92
93 def _init_if_main(self):
94 self._original_width = self.get_image().get_pixbuf_width()
95 self._original_height = self.get_image().get_pixbuf_height()
96 self.width_btn.set_range(1, 10 * self._original_width)
97 self.height_btn.set_range(1, 10 * self._original_height)
98
99 ############################################################################
100
101 def _get_width(self):
102 return self.width_btn.get_value_as_int()
103
104 def _get_height(self):
105 return self.height_btn.get_value_as_int()
106
107 def on_width_changed(self, *args):
108 if self._unclicked:
109 self.build_and_do_op()
110
111 def on_height_changed(self, *args):
112 if self._unclicked:
113 self.build_and_do_op()
114
115 def move_north(self, delta):
116 self.height_btn.set_value(self.height_btn.get_value() - delta)
117 self._y = self._y + delta
118
119 def move_south(self, delta):
120 self.height_btn.set_value(self.height_btn.get_value() + delta)
121
122 def move_east(self, delta):
123 self.width_btn.set_value(self.width_btn.get_value() + delta)
124
125 def move_west(self, delta):
126 self.width_btn.set_value(self.width_btn.get_value() - delta)
127 self._x = self._x + delta
128
129 ############################################################################
130
131 def on_unclicked_motion_on_area(self, event, surface):
132 self.set_directional_cursor(event.x, event.y)
133
134 def on_press_on_area(self, event, surface, event_x, event_y):
135 self.x_press = self.x_motion = event_x
136 self.y_press = self.y_motion = event_y
137 self._unclicked = False
138 self._update_expansion_rgba(event.button)
139
140 self.update_modifier_state(event.state)
141 if 'SHIFT' in self._modifier_keys and 'ALT' in self._modifier_keys:
142 self._force_expansion_rgba('secondary', event.button)
143 elif 'SHIFT' in self._modifier_keys:
144 self._force_expansion_rgba('alpha')
145 elif 'ALT' in self._modifier_keys:
146 self._force_expansion_rgba('initial')
147
148 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
149 delta_x = event_x - self.x_motion
150 delta_y = event_y - self.y_motion
151
152 if self._directions == '':
153 return
154 if 'n' in self._directions:
155 self.move_north(delta_y)
156 if 's' in self._directions:
157 self.move_south(delta_y)
158 if 'w' in self._directions:
159 self.move_west(delta_x)
160 if 'e' in self._directions:
161 self.move_east(delta_x)
162
163 if self.apply_to_selection:
164 self._x = max(0, self._x)
165 self._y = max(0, self._y)
166 width = min(self._original_width - self._x, self._get_width())
167 height = min(self._original_height - self._y, self._get_height())
168 self.width_btn.set_value(width)
169 self.height_btn.set_value(height)
170
171 self.x_motion = event_x
172 self.y_motion = event_y
173 # not adding the condition would be a better UX but slower to compute
174 if render and not self.apply_to_selection:
175 self.build_and_do_op()
176
177 def on_release_on_area(self, event, surface, event_x, event_y):
178 self._unclicked = True
179 self.build_and_do_op()
180 self._scroll_to_end(event_x - self.x_press, event_y - self.y_press)
181 self.window.set_cursor(False)
182
183 ############################################################################
184
185 def on_draw_above(self, area, cairo_context):
186 if self.apply_to_selection:
187 x1 = int(self._x)
188 y1 = int(self._y)
189 else:
190 x1 = 0
191 y1 = 0
192 x2 = x1 + self._get_width()
193 y2 = y1 + self._get_height()
194 x1, x2, y1, y2 = self.get_image().get_corrected_coords(x1, x2, y1, y2, \
195 self.apply_to_selection, False)
196 if not self.apply_to_selection:
197 self._draw_temp_pixbuf(cairo_context, x1, y1)
198 thickness = self.get_overlay_thickness()
199 utilities_show_handles_on_context(cairo_context, x1, x2, y1, y2, thickness)
200
201 ############################################################################
202
203 def build_selection_fit_operation(self):
204 """Special way to build an operation, not from the present crop tool,
205 but from the selection menu. The parameters are determined automatically
206 from the state of the selection manager."""
207 self._update_expansion_rgba()
208 s = self.get_selection()
209 new_x = min(0, s.selection_x)
210 new_y = min(0, s.selection_y)
211
212 s_width = max(0, s.selection_x) + s.selection_pixbuf.get_width()
213 new_width = max(-1 * new_x + self.get_main_pixbuf().get_width(), s_width)
214
215 s_height = max(0, s.selection_y) + s.selection_pixbuf.get_height()
216 new_height = max(-1 * new_y + self.get_main_pixbuf().get_height(), s_height)
217
218 operation = {
219 'tool_id': self.id,
220 'is_selection': False,
221 'is_preview': False,
222 'is_etf': True,
223 'local_dx': int(new_x),
224 'local_dy': int(new_y),
225 'width': new_width,
226 'height': new_height,
227 'rgba': self._expansion_rgba
228 }
229 return operation
230
231 def build_operation(self):
232 operation = {
233 'tool_id': self.id,
234 'is_selection': self.apply_to_selection,
235 'is_preview': True,
236 'is_etf': False,
237 'local_dx': int(self._x),
238 'local_dy': int(self._y),
239 'width': self._get_width(),
240 'height': self._get_height(),
241 'rgba': self._expansion_rgba
242 }
243 return operation
244
245 def do_tool_operation(self, operation):
246 self.start_tool_operation(operation)
247 x = operation['local_dx']
248 y = operation['local_dy']
249 width = operation['width']
250 height = operation['height']
251
252 rgba_array = utilities_gdk_rgba_to_color_array(operation['rgba'])
253 rgba_array[3] *= 255
254 hexa_rgba = utilities_rgba_to_hexadecimal(*rgba_array)
255
256 is_selection = operation['is_selection']
257 if is_selection:
258 source_pixbuf = self.get_selection_pixbuf()
259 else:
260 source_pixbuf = self.get_main_pixbuf()
261 self.get_image().set_temp_pixbuf(source_pixbuf.copy())
262 self._crop_temp_pixbuf(x, y, width, height, is_selection, hexa_rgba)
263 if operation['is_etf']:
264 # Case of an "expand to fit" action
265 s_pixbuf = self.get_selection_pixbuf()
266 self.get_selection().update_from_transform_tool(s_pixbuf, -1 * x, -1 * y)
267 self.common_end_operation(operation)
268
269 def _crop_temp_pixbuf(self, x, y, width, height, is_selection, hexa_rgba):
270 """Crop and/or expand the temp pixbuf according to given parameters."""
271
272 # Coordinates of the origin of the source pixbuf (temp_p)
273 src_x = max(x, 0)
274 src_y = max(y, 0)
275
276 # Coordinates of the origin of the destination pixbuf (new_pixbuf)
277 if is_selection:
278 dest_x = 0
279 dest_y = 0
280 else:
281 dest_x = max(-1 * x, 0)
282 dest_y = max(-1 * y, 0)
283
284 # Dotted lines == new sizes; plain lines == old sizes.
285 #
286 # If the origin has been cropped, `src` == the new coordinates, and
287 # `dest` == 0 (in the considered direction x or y):
288 #
289 # dest____________________
290 # | |
291 # | src------------------|
292 # | ⁝ |
293 # |___⁝___________________|
294 #
295 # With the selection, it's not possible to expand, so we're always in
296 # this first case, with `dest` == 0.
297 # Else (if it hasn't moved, or if it has been expanded) `src` has to be
298 # 0 (because the coordinates GdkPixbuf will use can't be negative), and
299 # `dest` is (-1 * the new coordinates):
300 #
301 # dest---------------------
302 # ⁝ ⁝
303 # ⁝ src__________________⁝
304 # ⁝ | |
305 # ⁝___|___________________|
306 #
307 # The sign inversion is completely artificial (again: it's because the
308 # coordinates GdkPixbuf will use can't be negative).
309
310 # Initialisation of an EMPTY pixbuf with the wanted size and color
311 new_pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, True, 8, \
312 width, height)
313 new_pixbuf.fill(hexa_rgba)
314
315 temp_p = self.get_image().temp_pixbuf
316 # The width/height we want (measured from the respective origins of the
317 # `src` and the `dest` rectangles)
318 min_w = min(width - dest_x, temp_p.get_width() - src_x)
319 min_h = min(height - dest_y, temp_p.get_height() - src_y)
320
321 # Copy an area of the source pixbuf `temp_p`; the area starts at `src_*`
322 # and has the dimensions `min_*`. It's painted on the destination pixbuf
323 # (`new_pixbuf`) starting at the coordinates `dest_*`.
324 temp_p.copy_area(src_x, src_y, min_w, min_h, new_pixbuf, dest_x, dest_y)
325 self.get_image().set_temp_pixbuf(new_pixbuf)
326
327 ############################################################################
328 ################################################################################
329