Debian Codegraph @

src/tools/transform_tools/tool_scale.py

raw symbols SCIP
1 # tool_scale.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 Gtk, GdkPixbuf, GLib
19 from .abstract_transform_tool import AbstractCanvasTool
20 from .optionsbar_scale import OptionsBarScale
21 from .utilities_overlay import utilities_show_handles_on_context
22
23 class ToolScale(AbstractCanvasTool):
24 __gtype_name__ = 'ToolScale'
25
26 def __init__(self, window):
27 super().__init__('scale', _("Scale"), 'tool-scale-symbolic', window)
28 self.cursor_name = 'not-allowed'
29
30 # depends on both the option AND the click coordinates
31 self._preserve_ratio = True
32
33 # the user inverted the ratio preservation behavior with a modifier key
34 self._ratio_is_inverted = False
35
36 # safety lock to reload asynchronously
37 self._reload_is_locked = False
38
39 # safety lock to set values in the spinbuttons
40 self._spinbtns_disabled = True
41
42 self._x = 0
43 self._y = 0
44 self._x2 = 0
45 self._y2 = 0
46 self.x_press = self.x_motion = None
47 self.y_press = self.y_motion = None
48 self.add_tool_action_enum('scale-proportions', 'corners')
49 self.add_tool_action_boolean('scale-ratio-spinbtns', False)
50 self.add_tool_action_enum('scale-unit', 'pixels')
51
52 def try_build_pane(self):
53 self.pane_id = 'scale'
54 self.window.options_manager.try_add_bottom_pane(self.pane_id, self)
55
56 def build_bottom_pane(self):
57 bar = OptionsBarScale(self)
58
59 self._width_btn = bar.width_btn
60 self._height_btn = bar.height_btn
61 self._width_btn.connect('value-changed', self.on_spinbtn_changed)
62 self._height_btn.connect('value-changed', self.on_spinbtn_changed)
63
64 self._w100_btn = bar.width_100_btn
65 self._h100_btn = bar.height_100_btn
66 self._w100_btn.connect('value-changed', self.on_spinbtn100_changed)
67 self._h100_btn.connect('value-changed', self.on_spinbtn100_changed)
68
69 return bar
70
71 ############################################################################
72
73 def get_options_label(self):
74 return _("Scaling options")
75
76 def get_editing_tips(self):
77 # The current method is called when options change, so i use it to:
78
79 # 1) update the visibility of the spinbuttons depending on which ones
80 # the user wants to use.
81 self._update_to_wanted_unit()
82
83 # 2) update the bullshit around ratio preservation actions
84 ratio_option = self.get_option_value('scale-proportions')
85 if not ratio_option == 'corners':
86 action = self.window.lookup_action('scale-ratio-spinbtns')
87 action.set_state(GLib.Variant.new_boolean(ratio_option == 'always'))
88 self.set_action_sensitivity('scale-ratio-spinbtns', ratio_option == 'corners')
89
90 if self.apply_to_selection:
91 label_action = _("Scaling the selection")
92 label_confirm = None
93 else:
94 label_action = _("Scaling the canvas")
95 label_confirm = self.label + " - " + \
96 _("Don't forget to confirm the operation!")
97
98 if self.get_image().get_mouse_is_pressed():
99 label_modifier_shift = None
100 else:
101 label_modifier_shift = _("Press <Shift> to quickly toggle the " + \
102 "'lock proportions' option")
103
104 # there is intentionally no `label_direction` because i expect the users
105 # to understand by themselves how it works.
106
107 full_list = [label_action, label_confirm, label_modifier_shift]
108 return list(filter(None, full_list))
109
110 def on_tool_selected(self, *args):
111 super().on_tool_selected()
112 self._x = 0
113 self._y = 0
114 self.x_press = self.x_motion = None
115 self.y_press = self.y_motion = None
116 self._w100_btn.set_value(100)
117 self._h100_btn.set_value(100)
118
119 width, height = self._get_original_size()
120 self.set_preserve_ratio()
121 self._ratio = width / height
122 self._spinbtns_disabled = False
123 self._width_btn.set_value(width)
124 self._height_btn.set_value(height)
125 self.build_and_do_op() # Ensure a correct preview
126
127 def _get_original_size(self):
128 if self.apply_to_selection:
129 original_pixbuf = self.get_selection_pixbuf()
130 else:
131 original_pixbuf = self.get_main_pixbuf()
132 return original_pixbuf.get_width(), original_pixbuf.get_height()
133
134 ############################################################################
135
136 def _update_to_wanted_unit(self):
137 want_pixels = self.get_option_value('scale-unit') == 'pixels'
138 if want_pixels:
139 self._width_btn.set_visible(True)
140 self._height_btn.set_visible(True)
141 self._w100_btn.set_visible(False)
142 self._h100_btn.set_visible(False)
143 else:
144 self._width_btn.set_visible(False)
145 self._height_btn.set_visible(False)
146 self._w100_btn.set_visible(True)
147 self._h100_btn.set_visible(True)
148
149 def set_preserve_ratio(self, for_spinbtns=False):
150 """Set whether or not `self._preserve_ratio` should be true. If it is,
151 AND that wasn't already the case before, this method sets the value of
152 `self._ratio` too.
153 The parameter is `True` if the request to set the ratio is triggered by
154 a direct interaction with a spinbutton: the method will not look at the
155 same options to decide what to do."""
156 former_setting = self._preserve_ratio
157
158 if for_spinbtns:
159 self._preserve_ratio = self.get_option_value('scale-ratio-spinbtns')
160 else:
161 setting = self.get_option_value('scale-proportions')
162 if setting == 'corners':
163 self._preserve_ratio = len(self._directions) != 1
164 else:
165 self._preserve_ratio = setting == 'always'
166
167 if self._ratio_is_inverted:
168 self._preserve_ratio = not self._preserve_ratio
169
170 if self._preserve_ratio == former_setting:
171 return
172 if self._preserve_ratio:
173 self._ratio = self._get_width() / self._get_height()
174
175 def _try_scale_dimensions(self):
176 if self._reload_is_locked:
177 return
178 self._reload_is_locked = True
179 GLib.timeout_add(100, self._unlock_reload, {})
180 self._async_try_scale_dimensions()
181
182 def _unlock_reload(self, content_params):
183 self._reload_is_locked = False
184 self._async_try_scale_dimensions()
185
186 def _async_try_scale_dimensions(self, data_dict={}):
187 """When the value in a spinbutton changes, adjust the values in the
188 spinbuttons if necessary, and build-and-do the corresponding tool
189 operation."""
190 self._spinbtns_disabled = True
191
192 if self._preserve_ratio:
193 temp_pixbuf = self.get_image().temp_pixbuf
194 existing_width = temp_pixbuf.get_width()
195 existing_height = temp_pixbuf.get_height()
196
197 new_width = self._get_width()
198 new_height = self._get_height()
199
200 if existing_width != new_width:
201 new_height = new_width / self._ratio
202 self._height_btn.set_value(new_height)
203 if existing_height != new_height:
204 new_width = new_height * self._ratio
205 self._width_btn.set_value(new_width)
206 # else:
207 # If the ratio isn't locked, the dimension should be applied without
208 # any change. Calculations to adjust the size are only useful when
209 # trying to preserve the image proportions.
210
211 # Update the "percentage" spinbuttons
212 original_width , original_height = self._get_original_size()
213 new_w100 = 100 * (self._get_width() / original_width)
214 new_h100 = 100 * (self._get_height() / original_height)
215 self._w100_btn.set_value(new_w100)
216 self._h100_btn.set_value(new_h100)
217
218 self._spinbtns_disabled = False
219 self.build_and_do_op()
220
221 ############################################################################
222 # Spinbuttons management ###################################################
223
224 def on_spinbtn_changed(self, *args):
225 if self._spinbtns_disabled:
226 return
227 if self.x_press is None:
228 # This means the user interacts with the spinbtn directly, instead
229 # of the surface.
230 self.set_preserve_ratio(True)
231 self._try_scale_dimensions()
232
233 def on_spinbtn100_changed(self, *args):
234 if self._spinbtns_disabled:
235 return
236 # This callback can only pass its guard clause when triggered from the
237 # spinbutton directly: i don't have to check the value of self.x_press
238 self.set_preserve_ratio(True)
239
240 original_width, original_height = self._get_original_size()
241 new_width = original_width * (self._w100_btn.get_value() / 100)
242 new_height = original_height * (self._h100_btn.get_value() / 100)
243 delta_x = self._get_width() - new_width
244 delta_y = self._get_height() - new_height
245 self._apply_deltas_to_spinbtns(new_width, new_height, delta_x, delta_y)
246
247 def _apply_deltas_to_spinbtns(self, new_width, new_height, dx=0, dy=0):
248 if self._preserve_ratio:
249 if abs(dy) > abs(dx):
250 self._height_btn.set_value(new_height)
251 else:
252 self._width_btn.set_value(new_width)
253 else:
254 self._height_btn.set_value(new_height)
255 self._width_btn.set_value(new_width)
256
257 def _get_width(self):
258 return self._width_btn.get_value_as_int()
259
260 def _get_height(self):
261 return self._height_btn.get_value_as_int()
262
263 ############################################################################
264
265 def on_unclicked_motion_on_area(self, event, surface):
266 self.set_directional_cursor(event.x, event.y)
267
268 def on_press_on_area(self, event, surface, event_x, event_y):
269 self.update_modifier_state(event.state)
270 if 'SHIFT' in self._modifier_keys:
271 # The value will be restored later in `on_release_on_area`
272 self._ratio_is_inverted = True
273
274 self.x_press = self.x_motion = event_x
275 self.y_press = self.y_motion = event_y
276 self._x2 = self._x + self._get_width()
277 self._y2 = self._y + self._get_height()
278 self.set_preserve_ratio()
279
280 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
281 if self._directions == '':
282 return
283 delta_x = event_x - self.x_motion
284 delta_y = event_y - self.y_motion
285 self.x_motion = event_x
286 self.y_motion = event_y
287
288 height = self._get_height()
289 width = self._get_width()
290 if 'n' in self._directions:
291 height -= delta_y
292 self._y = self._y + delta_y
293 if 's' in self._directions:
294 height += delta_y
295 if 'w' in self._directions:
296 width -= delta_x
297 self._x = self._x + delta_x
298 if 'e' in self._directions:
299 width += delta_x
300
301 if self.apply_to_selection and self._preserve_ratio:
302 if 'w' in self._directions:
303 self._x = self._x2 - width
304 if 'n' in self._directions:
305 self._y = self._y2 - height
306
307 self._apply_deltas_to_spinbtns(width, height, delta_x, delta_y)
308
309 def on_release_on_area(self, event, surface, event_x, event_y):
310 self.on_motion_on_area(event, surface, event_x, event_y)
311 self._ratio_is_inverted = False
312 self.build_and_do_op() # technically already done
313 self._scroll_to_end(event_x - self.x_press, event_y - self.y_press)
314
315 # Reset those to tell apart a spinbtn signals' possible origins
316 self.x_press = self.x_motion = None
317 self.y_press = self.y_motion = None
318
319 ############################################################################
320
321 def on_draw_above(self, area, cairo_context):
322 if self.apply_to_selection:
323 x1 = int(self._x)
324 y1 = int(self._y)
325 else:
326 x1 = 0
327 y1 = 0
328 x2 = x1 + self._get_width()
329 y2 = y1 + self._get_height()
330 x1, x2, y1, y2 = self.get_image().get_corrected_coords(x1, x2, y1, y2, \
331 self.apply_to_selection, False)
332 self._draw_temp_pixbuf(cairo_context, x1, y1)
333 thickness = self.get_overlay_thickness()
334 utilities_show_handles_on_context(cairo_context, x1, x2, y1, y2, thickness)
335
336 ############################################################################
337
338 def build_operation(self):
339 operation = {
340 'tool_id': self.id,
341 'is_selection': self.apply_to_selection,
342 'is_preview': True,
343 'local_dx': int(self._x),
344 'local_dy': int(self._y),
345 'width': self._get_width(),
346 'height': self._get_height()
347 }
348 return operation
349
350 def do_tool_operation(self, operation):
351 self.start_tool_operation(operation)
352 if operation['is_selection']:
353 source_pixbuf = self.get_selection_pixbuf()
354 else:
355 source_pixbuf = self.get_main_pixbuf()
356 self.get_image().set_temp_pixbuf(source_pixbuf.scale_simple( \
357 operation['width'], \
358 operation['height'], \
359 GdkPixbuf.InterpType.TILES))
360 self.common_end_operation(operation)
361
362 ############################################################################
363 ################################################################################
364