Debian Codegraph @

src/tools/transform_tools/abstract_transform_tool.py

raw symbols SCIP
1 # abstract_transform_tool.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 gi.repository import Gtk, Gdk, GdkPixbuf
20
21 from .abstract_tool import AbstractAbstractTool
22 from .utilities_colors import utilities_gdk_rgba_to_normalized_array
23
24 class AbstractCanvasTool(AbstractAbstractTool):
25 __gtype_name__ = 'AbstractCanvasTool'
26
27 def __init__(self, tool_id, label, icon_name, window, **kwargs):
28 super().__init__(tool_id, label, icon_name, window)
29 self.menu_id = 1
30 self.centered_box = None
31 self.needed_width_for_long = 0
32 self.accept_selection = True
33 self.apply_to_selection = False
34 self._directions = ''
35
36 # Gdk.RGBA
37 self._expansion_rgba = None
38
39 def on_tool_selected(self, *args):
40 super().on_tool_selected()
41 self.apply_to_selection = self.selection_is_active()
42
43 def update_actions_state(self, *args):
44 # Changing this in on_tool_selected would be overridden by image.py
45 super().update_actions_state()
46
47 self.set_action_sensitivity('selection_delete', False)
48 self.set_action_sensitivity('selection_cut', False)
49 self.set_action_sensitivity('unselect', False)
50 self.set_action_sensitivity('select_all', False)
51
52 self.set_action_sensitivity('cancel_transform', True)
53 self.set_action_sensitivity('apply_transform', True)
54
55 def give_back_control(self, preserve_selection):
56 if not preserve_selection and self.selection_is_active():
57 self.on_apply_transform_tool_operation()
58 self.window.get_selection_tool().unselect_and_apply()
59 super().give_back_control(preserve_selection)
60
61 def _scroll_to_end(self, h_growth, v_growth):
62 if h_growth > 0:
63 if 'e' in self._directions:
64 self.window.action_go_last()
65 if v_growth > 0:
66 if 's' in self._directions:
67 self.window.action_go_bottom()
68 self.get_image().fake_scrollbar_update()
69
70 ############################################################################
71
72 def build_and_do_op(self):
73 operation = self.build_operation()
74 self.do_tool_operation(operation)
75
76 def temp_preview(self, is_selection, local_dx, local_dy):
77 """Part of the previewing methods shared by all transform tools."""
78 pixbuf = self.get_image().temp_pixbuf
79 cairo_context = self.get_context()
80 if is_selection:
81 cairo_context.set_source_surface(self.get_surface(), 0, 0)
82 cairo_context.paint()
83 x = self.get_selection().selection_x + local_dx
84 y = self.get_selection().selection_y + local_dy
85 Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, x, y)
86 cairo_context.paint()
87 else:
88 cairo_context.set_operator(cairo.Operator.SOURCE)
89 Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, 0, 0)
90 cairo_context.paint()
91 cairo_context.set_operator(cairo.Operator.OVER)
92 self.get_image().update()
93
94 ############################################################################
95
96 def on_apply_transform_tool_operation(self, *args):
97 self.restore_pixbuf()
98 operation = self.build_operation()
99 self.apply_operation(operation)
100 self.on_cancel_transform_tool_operation()
101
102 def on_cancel_transform_tool_operation(self, *args):
103 if self.apply_to_selection:
104 self.window.force_selection()
105 else:
106 self.window.back_to_previous()
107
108 def apply_operation(self, operation):
109 operation['is_preview'] = False
110 super().apply_operation(operation)
111 self.get_image().reset_temp()
112
113 def common_end_operation(self, op):
114 if op['is_preview']:
115 self.temp_preview(op['is_selection'], op['local_dx'], op['local_dy'])
116 else:
117 self.apply_temp(op['is_selection'], op['local_dx'], op['local_dy'])
118
119 def apply_temp(self, operation_is_selection, ldx, ldy):
120 new_pixbuf = self.get_image().temp_pixbuf.copy()
121 if operation_is_selection:
122 self.get_selection().update_from_transform_tool(new_pixbuf, ldx, ldy)
123 else:
124 self.get_image().set_main_pixbuf(new_pixbuf)
125 self.get_image().use_stable_pixbuf()
126
127 ############################################################################
128 # Direction of the cursor depending on its position ########################
129
130 def _set_directions(self, event_x, event_y, n_sizes):
131 """Set the directions of the user's future operation for tools such as
132 `scale` or `crop`.
133 It returns a boolean telling whether or not the directions changed since
134 the last call to this method."""
135 w_left = n_sizes['wl']
136 w_right = n_sizes['wr']
137 h_top = n_sizes['ht']
138 h_bottom = n_sizes['hb']
139 # print("set_directions", w_left, w_right, h_top, h_bottom)
140 directions = ''
141 if event_y < h_top:
142 directions += 'n'
143 elif event_y > h_bottom:
144 directions += 's'
145 if event_x < w_left:
146 directions += 'w'
147 elif event_x > w_right:
148 directions += 'e'
149
150 if self._directions == directions:
151 return False
152 else:
153 self._directions = directions
154 return True
155
156 def _set_cursor_name(self):
157 """Set the cursor name, depending on the previously set directions."""
158 if self._directions == '':
159 self.cursor_name = 'not-allowed'
160 else:
161 self.cursor_name = self._directions + '-resize'
162
163 def set_directional_cursor(self, event_x, event_y):
164 """Set the accurate cursor depending on the position of the pointer on
165 the canvas."""
166 n_sizes = self.get_image().get_nineths_sizes(self.apply_to_selection, \
167 self._x, self._y)
168 # if we're transforming the selection from its top and/or left, coords
169 # to decide the direction depend on local deltas (self._x and self._y)
170 if self._set_directions(event_x, event_y, n_sizes):
171 self._set_cursor_name()
172 self.window.set_cursor(True)
173
174 ############################################################################
175
176 def on_draw_above(self, area, cairo_context):
177 pass
178
179 def _draw_temp_pixbuf(self, cairo_context, x, y):
180 pixbuf = self.get_image().temp_pixbuf
181 Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, x, y)
182 if self.get_image().is_zoomed_surface_sharp():
183 cairo_context.get_source().set_filter(cairo.FILTER_NEAREST)
184 cairo_context.paint()
185
186 def get_deformed_surface(self, source_surface, coefs, prefill=False):
187 """Use cairo.Matrix to apply a transformation to `source_surface` using
188 the coefficients in `coefs` and return a new surface with the result."""
189 p_xx, p_yx, p_xy, p_yy, p_x0, p_y0 = coefs
190
191 source_w = source_surface.get_width()
192 source_h = source_surface.get_height()
193 w = p_xx * source_w + p_xy * source_h + p_x0 * 2
194 h = p_yx * source_w + p_yy * source_h + p_y0 * 2
195
196 new_surface = cairo.ImageSurface(cairo.Format.ARGB32, int(w), int(h))
197 cairo_context = cairo.Context(new_surface)
198
199 if prefill:
200 # TODO this exists only for the "skew" tool, and it shouldn't be
201 # here nor do this.
202 # TODO it should paint *around* the deformed surface after it's
203 # returned by the current method
204 color_array = utilities_gdk_rgba_to_normalized_array(self._expansion_rgba)
205 cairo_context.set_source_rgba(*color_array)
206 cairo_context.paint()
207
208 # m = cairo.Matrix(xx=1.0, yx=0.0, xy=0.0, yy=1.0, x0=0.0, y0=0.0)
209 m = cairo.Matrix(xx=p_xx, yx=p_yx, xy=p_xy, yy=p_yy, x0=p_x0, y0=p_y0)
210 try:
211 cairo_context.transform(m)
212 except:
213 self.show_error(_("Error: invalid values"))
214 return source_surface
215 cairo_context.set_source_surface(source_surface, 0, 0)
216 cairo_context.paint()
217 return new_surface
218
219 ############################################################################
220 # Options ##################################################################
221
222 def _update_expansion_rgba(self, event_btn=1):
223 """When the canvas grows, the color of the new pixels is parametrable"""
224 color_type = self.get_option_value('crop-expand')
225 self._force_expansion_rgba(color_type, event_btn)
226
227 def _force_expansion_rgba(self, color_type, event_btn=1):
228 if color_type == 'initial':
229 exp_rgba = self.get_image().get_initial_rgba()
230 elif color_type == 'secondary' and event_btn == 1:
231 exp_rgba = self.window.options_manager.get_right_color()
232 elif color_type == 'secondary' and event_btn == 3:
233 exp_rgba = self.window.options_manager.get_left_color()
234 else: # color_type == 'alpha':
235 exp_rgba = Gdk.RGBA(red=1.0, green=1.0, blue=1.0, alpha=0.0)
236 self._expansion_rgba = exp_rgba
237
238 ############################################################################
239 ################################################################################
240