Debian Codegraph @

src/tools/transform_tools/tool_rotate.py

raw symbols SCIP
1 # tool_rotate.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 math
19 from gi.repository import Gdk
20 from .abstract_transform_tool import AbstractCanvasTool
21 from .optionsbar_rotate import OptionsBarRotate
22
23 class ToolRotate(AbstractCanvasTool):
24 __gtype_name__ = 'ToolRotate'
25
26 def __init__(self, window):
27 super().__init__('rotate', _("Rotate"), 'tool-rotate-symbolic', window)
28 self.cursor_name = 'pointer'
29 self.flip_h = False
30 self.flip_v = False
31 self.angle_press = 0
32
33 self.add_tool_action_simple('rotate-clockwise', self.on_right_clicked)
34 self.add_tool_action_simple('rotate-counter-cw', self.on_left_clicked)
35 self.add_tool_action_simple('rotate-flip-h', self.on_horizontal_clicked)
36 self.add_tool_action_simple('rotate-flip-v', self.on_vertical_clicked)
37
38 def try_build_pane(self):
39 self.pane_id = 'rotate'
40 self.window.options_manager.try_add_bottom_pane(self.pane_id, self)
41
42 def build_bottom_pane(self):
43 pane = OptionsBarRotate(self)
44 self.angle_btn = pane.angle_btn
45 self.angle_btn.connect('value-changed', self.on_angle_changed)
46 return pane
47
48 def get_options_label(self):
49 return _("Rotating options")
50
51 def get_editing_tips(self):
52 if self.apply_to_selection:
53 label_action = _("Rotating the selection")
54 label_confirm = None
55 else:
56 label_action = _("Rotating the canvas")
57 label_confirm = self.label + " - " + \
58 _("Don't forget to confirm the operation!")
59
60 # there is intentionally no `label_modifier_shift` because it would be
61 # too hard to explain the actions of each mouse button.
62
63 full_list = [label_action, label_confirm]
64 return list(filter(None, full_list))
65
66 def on_tool_selected(self, *args):
67 super().on_tool_selected()
68 self.flip_h = False
69 self.flip_v = False
70 self.angle_btn.set_value(0.0)
71 self.build_and_do_op() # Show the temp_pixbuf before any event
72 if self.apply_to_selection:
73 self.cursor_name = 'move'
74 # not the ideal cursor, but their is no ideal cursor for this
75 else:
76 self.cursor_name = 'pointer'
77 # the pane is updated by the window according to self.apply_to_selection
78
79 ############################################################################
80
81 def on_press_on_area(self, event, surface, event_x, event_y):
82 self.update_modifier_state(event.state)
83 if 'SHIFT' in self._modifier_keys:
84 if event.button == 1:
85 self.on_horizontal_clicked()
86 elif event.button == 3:
87 self.on_vertical_clicked()
88 return
89 elif not self.apply_to_selection:
90 if event.button == 1:
91 self.on_left_clicked()
92 elif event.button == 3:
93 self.on_right_clicked()
94 return
95
96 center_x, center_y = self.get_selection().get_center_coords()
97 delta_x0 = center_x - event_x
98 delta_y0 = center_y - event_y
99 press_as_degrees = (math.atan2(delta_x0, delta_y0) * 180) / math.pi
100 self.angle_press = self.get_angle() - int(press_as_degrees)
101
102 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
103 if not self.apply_to_selection:
104 return
105 center_x, center_y = self.get_selection().get_center_coords()
106 delta_x = center_x - event_x
107 delta_y = center_y - event_y
108 release_angle = ( math.atan2(delta_x, delta_y) * 180 ) / math.pi
109 self.angle_btn.set_value(int(release_angle) + self.angle_press)
110 if render:
111 operation = self.build_operation()
112 self.do_tool_operation(operation)
113
114 def on_release_on_area(self, event, surface, event_x, event_y):
115 self.on_motion_on_area(event, surface, event_x, event_y)
116
117 ############################################################################
118
119 def get_angle(self):
120 return self.angle_btn.get_value_as_int()
121
122 def on_right_clicked(self, *args):
123 angle = self.get_normalized_angle()
124 self.angle_btn.set_value(angle - 90)
125
126 def on_left_clicked(self, *args):
127 angle = self.get_normalized_angle()
128 self.angle_btn.set_value(angle + 90)
129
130 def on_vertical_clicked(self, *args):
131 self.flip_v = not self.flip_v
132 self.build_and_do_op()
133
134 def on_horizontal_clicked(self, *args):
135 self.flip_h = not self.flip_h
136 self.build_and_do_op()
137
138 def get_normalized_angle(self, *args):
139 angle = self.get_angle() % 360
140 angle = int(angle/90) * 90
141 return angle
142
143 def on_angle_changed(self, *args):
144 if self.get_angle() == 360 or self.get_angle() == -360:
145 self.angle_btn.set_value(0)
146 self.build_and_do_op()
147
148 ############################################################################
149
150 def on_draw_above(self, area, cairo_context):
151 x1 = 0
152 y1 = 0
153 x2 = x1 + self.get_image().temp_pixbuf.get_width()
154 y2 = y1 + self.get_image().temp_pixbuf.get_height()
155 x1, x2, y1, y2 = self.get_image().get_corrected_coords(x1, x2, y1, y2, \
156 self.apply_to_selection, False)
157 self._draw_temp_pixbuf(cairo_context, x1, y1)
158
159 ############################################################################
160
161 def build_operation(self):
162 operation = {
163 'tool_id': self.id,
164 'is_selection': self.apply_to_selection,
165 'is_preview': True,
166 'local_dx': 0,
167 'local_dy': 0,
168 'angle': self.get_angle(),
169 'flip_h': self.flip_h,
170 'flip_v': self.flip_v
171 }
172 return operation
173
174 def do_tool_operation(self, operation):
175 self.start_tool_operation(operation)
176 angle = operation['angle']
177 flip_h = operation['flip_h']
178 flip_v = operation['flip_v']
179 if operation['is_selection']:
180 source_pixbuf = self.get_selection_pixbuf()
181 else:
182 source_pixbuf = self.get_main_pixbuf()
183
184 if angle < 0:
185 angle += 360
186 gdk_rotation = int(angle / 90) * 90
187 cairo_rotation = angle % 90
188 # print('angle:', angle)
189 # print('gdk_rotation:', gdk_rotation)
190 # print('cairo_rotation:', cairo_rotation)
191 new_pixbuf = source_pixbuf
192
193 # Image flipping (horizontal or vertical "mirroring")
194 if flip_h:
195 new_pixbuf = new_pixbuf.flip(True)
196 if flip_v:
197 new_pixbuf = new_pixbuf.flip(False)
198
199 # Image rotation, using the method from GdkPixbuf.Pixbuf
200 new_pixbuf = new_pixbuf.rotate_simple(gdk_rotation)
201
202 # Image rotation, using methods from cairo.Context (only if needed)
203 if cairo_rotation != 0:
204 surface0 = Gdk.cairo_surface_create_from_pixbuf(new_pixbuf, 0, None)
205 surface0.set_device_scale(self.scale_factor(), self.scale_factor())
206 coefs = self._get_rotation_matrix(cairo_rotation, \
207 surface0.get_width(), surface0.get_height())
208 new_surface = self.get_deformed_surface(surface0, coefs)
209 new_pixbuf = Gdk.pixbuf_get_from_surface(new_surface, 0, 0, \
210 new_surface.get_width(), new_surface.get_height())
211
212 self.get_image().set_temp_pixbuf(new_pixbuf)
213 self.common_end_operation(operation)
214
215 def _get_rotation_matrix(self, angle, width, height):
216 """Transform an angle (in degrees) to the xx/yx/xy/yy coefs expected by
217 cairo. Due to previously performed modifications to the data, the angle
218 will be between 0 (excluded) and 90 (excluded)."""
219 rad = math.pi * angle / 180
220
221 xx = math.cos(rad)
222 xy = math.sin(rad)
223 yx = -1 * math.sin(rad)
224 yy = math.cos(rad)
225
226 x0 = max(0, height * yx)
227 y0 = max(0, width * xy)
228
229 return [xx, yx, xy, yy, x0, y0]
230
231 ############################################################################
232 ################################################################################
233