Debian Codegraph @

src/tools/transform_tools/tool_skew.py

raw symbols SCIP
1 # tool_skew.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
19 from .abstract_transform_tool import AbstractCanvasTool
20 from .optionsbar_skew import OptionsBarSkew
21 from .utilities_overlay import utilities_show_handles_on_context
22
23 class ToolSkew(AbstractCanvasTool):
24 __gtype_name__ = 'ToolSkew'
25
26 def __init__(self, window):
27 # This is the name of the tool changing rectangles into parallelograms.
28 # It's synonymous with tilt, slant, bend. If you need a reference to
29 # translate it, this is named after MS Paint's "Stretch/Skew" dialog.
30 super().__init__('skew', _("Skew"), 'tool-skew-symbolic', window)
31 self._x = 0
32 self._y = 0
33 self.add_tool_action_simple('skew-exists', self.select_flowbox_child)
34 self.add_tool_action_enum('crop-expand', 'initial')
35
36 def try_build_pane(self):
37 self.pane_id = 'skew'
38 self.window.options_manager.try_add_bottom_pane(self.pane_id, self)
39
40 def build_bottom_pane(self):
41 bar = OptionsBarSkew()
42 self.yx_spinbtn = bar.yx_spinbtn
43 self.xy_spinbtn = bar.xy_spinbtn
44 self.yx_spinbtn.connect('value-changed', self.on_coord_changed)
45 self.xy_spinbtn.connect('value-changed', self.on_coord_changed)
46 return bar
47
48 def get_options_model(self):
49 if self.apply_to_selection:
50 return None
51 else:
52 return super().get_options_model()
53
54 def get_options_label(self):
55 if self.apply_to_selection:
56 return super().get_options_label()
57 else:
58 return _("Skewing options")
59
60 def get_editing_tips(self):
61 label_direction = _("The directions of the deformation are hinted by the mouse pointer")
62
63 label_modifiers = None
64 if self.apply_to_selection:
65 label_action = _("Skewing the selection")
66 label_confirm = None
67 else:
68 label_action = _("Skewing the canvas")
69 label_confirm = self.label + " - " + \
70 _("Don't forget to confirm the operation!")
71 if not self.get_image().get_mouse_is_pressed():
72 label_modifiers = _("Press <Alt>, <Shift>, or both, to " + \
73 "quickly change the 'expand with' option")
74
75 full_list = [label_action, label_direction, label_confirm, label_modifiers]
76 return list(filter(None, full_list))
77
78 def on_tool_selected(self, *args):
79 super().on_tool_selected()
80 self.set_action_sensitivity('crop-expand', not self.apply_to_selection)
81 self._reset_values()
82
83 ############################################################################
84
85 def get_yx(self):
86 return self.yx_spinbtn.get_value_as_int()
87
88 def get_xy(self):
89 return self.xy_spinbtn.get_value_as_int()
90
91 def _set_cursor_name(self):
92 if self._directions == '':
93 self.cursor_name = 'not-allowed'
94 return
95 if self._directions == 'nw' or self._directions == 'se':
96 self.cursor_name = 'nwse-resize'
97 elif self._directions == 'ne' or self._directions == 'sw':
98 self.cursor_name = 'nesw-resize'
99 elif self._directions == 'n' or self._directions == 's':
100 self.cursor_name = 'ew-resize'
101 elif self._directions == 'e' or self._directions == 'w':
102 self.cursor_name = 'ns-resize'
103
104 def _get_width(self):
105 if self.apply_to_selection:
106 source_pixbuf = self.get_selection_pixbuf()
107 else:
108 source_pixbuf = self.get_main_pixbuf()
109 return source_pixbuf.get_width()
110
111 def _get_height(self):
112 if self.apply_to_selection:
113 source_pixbuf = self.get_selection_pixbuf()
114 else:
115 source_pixbuf = self.get_main_pixbuf()
116 return source_pixbuf.get_height()
117
118 def on_coord_changed(self, *args):
119 self.build_and_do_op()
120
121 def _reset_values(self, *args):
122 self._update_expansion_rgba()
123 self._yx = 0 # vertical deformation
124 self._xy = 0 # horizontal deformation
125 self.yx_spinbtn.set_value(0)
126 self.xy_spinbtn.set_value(0)
127 self.build_and_do_op()
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 = event_x
136 self.y_press = event_y
137 self._yx = self.get_yx() # vertical deformation
138 self._xy = self.get_xy() # horizontal deformation
139 # TODO répliquer ce que fait le scale avec son x2/y2 qui évite un effet
140 # flamby dégueulasse lié aux arrondis ?
141 self._update_expansion_rgba(event.button)
142
143 self.update_modifier_state(event.state)
144 if 'SHIFT' in self._modifier_keys and 'ALT' in self._modifier_keys:
145 self._force_expansion_rgba('secondary', event.button)
146 elif 'SHIFT' in self._modifier_keys:
147 self._force_expansion_rgba('alpha')
148 elif 'ALT' in self._modifier_keys:
149 self._force_expansion_rgba('initial')
150
151 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
152 if self._directions == '' or not render:
153 return
154
155 delta_x = event_x - self.x_press
156 delta_y = event_y - self.y_press
157
158 yx = self._yx
159 xy = self._xy
160 if 'n' in self._directions:
161 xy -= 100 * delta_x/self._get_width()
162 if 's' in self._directions:
163 xy += 100 * delta_x/self._get_width()
164 if 'w' in self._directions:
165 yx -= 100 * delta_y/self._get_height()
166 if 'e' in self._directions:
167 yx += 100 * delta_y/self._get_height()
168
169 self.yx_spinbtn.set_value(yx)
170 self.xy_spinbtn.set_value(xy)
171
172 def on_release_on_area(self, event, surface, event_x, event_y):
173 self.on_motion_on_area(event, surface, event_x, event_y)
174 self.build_and_do_op() # technically already done
175
176 ############################################################################
177
178 def on_draw_above(self, area, cairo_context):
179 x1 = 0
180 y1 = 0
181 scaled_xy = abs(self.get_xy()) * (self._get_height() / self._get_width())
182 scaled_yx = abs(self.get_yx()) * (self._get_width() / self._get_height())
183 p_xy = (scaled_xy + 100) / 100
184 p_yx = (scaled_yx + 100) / 100
185 x2 = x1 + self._get_width() * p_xy
186 y2 = y1 + self._get_height() * p_yx
187
188 x1, x2, y1, y2 = self.get_image().get_corrected_coords(x1, x2, y1, y2, \
189 self.apply_to_selection, False)
190 self._draw_temp_pixbuf(cairo_context, x1, y1)
191 thickness = self.get_overlay_thickness()
192 utilities_show_handles_on_context(cairo_context, x1, x2, y1, y2, thickness)
193
194 ############################################################################
195
196 def build_operation(self):
197 operation = {
198 'tool_id': self.id,
199 'is_selection': self.apply_to_selection,
200 'is_preview': True,
201 'local_dx': 0,
202 'local_dy': 0,
203 'yx': self.yx_spinbtn.get_value_as_int()/100,
204 'xy': self.xy_spinbtn.get_value_as_int()/100,
205 'rgba': self._expansion_rgba,
206 }
207 return operation
208
209 def do_tool_operation(self, operation):
210 self.start_tool_operation(operation)
211 if operation['is_selection']:
212 source_pixbuf = self.get_selection_pixbuf()
213 prefill = False
214 else:
215 source_pixbuf = self.get_main_pixbuf()
216 prefill = True
217 source_surface = Gdk.cairo_surface_create_from_pixbuf(source_pixbuf, 0, None)
218 source_surface.set_device_scale(self.scale_factor(), self.scale_factor())
219
220 xy = operation['xy']
221 x0 = 0.0
222 if xy < 0:
223 x0 = int(-1 * xy * source_surface.get_height())
224 yx = operation['yx']
225 y0 = 0.0
226 if yx < 0:
227 y0 = int(-1 * yx * source_surface.get_width())
228 coefs = [1.0, yx, xy, 1.0, x0, y0]
229
230 new_surface = self.get_deformed_surface(source_surface, coefs, prefill)
231 new_pixbuf = Gdk.pixbuf_get_from_surface(new_surface, 0, 0, \
232 new_surface.get_width(), new_surface.get_height())
233 self.get_image().set_temp_pixbuf(new_pixbuf)
234 self.common_end_operation(operation)
235
236 ############################################################################
237 ################################################################################
238