| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
| 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 |
|
| 50 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 273 |
src_x = max(x, 0) |
| 274 |
src_y = max(y, 0) |
| 275 |
|
| 276 |
|
| 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 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
| |
| 293 |
_| |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
⁝ |
| 303 |
_⁝ |
| 304 |
| |
| 305 |
_| |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 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 |
|
| 317 |
|
| 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 |
|
| 322 |
|
| 323 |
|
| 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 |
|