From 49ec537186686e3a9e71ab6bd23fd996196f1fcc Mon Sep 17 00:00:00 2001 From: Steffen Hirtle Date: Fri, 29 Mar 2024 17:54:49 +0100 Subject: [PATCH] Crop mode is now automatically turned on/off when crop menu is opened/closed --- graxpert/ui/canvas.py | 37 +++++++++++++++++++++++++++---------- graxpert/ui/left_menu.py | 14 ++++++++++---- graxpert/ui/ui_events.py | 3 ++- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/graxpert/ui/canvas.py b/graxpert/ui/canvas.py index 065458b..c451040 100644 --- a/graxpert/ui/canvas.py +++ b/graxpert/ui/canvas.py @@ -119,7 +119,8 @@ def register_events(self): eventbus.add_listener(AppEvents.DISPLAY_TYPE_CHANGED, self.redraw_image) eventbus.add_listener(UiEvents.RESET_ZOOM_REQUEST, self.reset_zoom) eventbus.add_listener(UiEvents.DISPLAY_START_BADGE_REQUEST, self.on_display_start_badge_request) - eventbus.add_listener(UiEvents.TOGGLE_CROP_REQUEST, self.on_toggle_crop_request) + eventbus.add_listener(UiEvents.TURN_ON_CROP_MODE, self.on_turn_on_crop_mode) + eventbus.add_listener(UiEvents.TURN_OFF_CROP_MODE, self.on_turn_off_crop_mode) eventbus.add_listener(UiEvents.APPLY_CROP_REQUEST, self.on_apply_crop_request) # event handling @@ -142,9 +143,13 @@ def on_apply_crop_request(self, event=None): return graxpert.images.crop_all(self.startx, self.endx, self.starty, self.endy) + + self.startx = 0 + self.starty = 0 + self.endx = graxpert.images.get("Original").width + self.endy = graxpert.images.get("Original").height eventbus.emit(AppEvents.RESET_POITS_REQUEST) - self.crop_mode = False self.zoom_fit(graxpert.images.get(self.display_type.get()).width, graxpert.images.get(self.display_type.get()).height) self.redraw_points() @@ -395,8 +400,11 @@ def on_stretch_image_end(self, event=None): def on_stretch_image_error(self, event=None): self.show_loading_frame(False) - - def on_toggle_crop_request(self, event=None): + + def on_turn_on_crop_mode(self, event=None): + if self.crop_mode: + return + if graxpert.images.get("Original") is None: messagebox.showerror("Error", _("Please load your picture first.")) return @@ -405,14 +413,23 @@ def on_toggle_crop_request(self, event=None): self.starty = 0 self.endx = graxpert.images.get("Original").width self.endy = graxpert.images.get("Original").height - - if self.crop_mode: - self.crop_mode = False - else: - self.crop_mode = True - + + self.crop_mode = True + self.redraw_points() + + def on_turn_off_crop_mode(self, event=None): + if not self.crop_mode: + return + + self.startx = 0 + self.starty = 0 + self.endx = graxpert.images.get("Original").width + self.endy = graxpert.images.get("Original").height + + self.crop_mode = False self.redraw_points() + # widget logic def draw_image(self, pil_image, tags=None): if pil_image is None: diff --git a/graxpert/ui/left_menu.py b/graxpert/ui/left_menu.py index f166255..85d2517 100644 --- a/graxpert/ui/left_menu.py +++ b/graxpert/ui/left_menu.py @@ -106,24 +106,30 @@ def __init__(self, parent, **kwargs): self.place_children() eventbus.add_listener(UiEvents.SHOW_MENU_REQUEST, lambda e: self.hide() if not e == "CROP" else None) + eventbus.add_listener(UiEvents.SHOW_MENU_REQUEST, lambda e: eventbus.emit(UiEvents.TURN_OFF_CROP_MODE) if not e == "CROP" else None) def create_children(self): super().create_children() - self.cropmode_button = GraXpertButton(self.sub_frame, text=_("Crop mode on/off"), command=lambda: eventbus.emit(UiEvents.TOGGLE_CROP_REQUEST)) - self.cropapply_button = GraXpertButton(self.sub_frame, text=_("Apply crop"), command=lambda: eventbus.emit(UiEvents.APPLY_CROP_REQUEST)) + self.cropapply_button = GraXpertButton(self.sub_frame, + text=_("Apply crop"), + fg_color=ThemeManager.theme["Accent.CTkButton"]["fg_color"], + hover_color=ThemeManager.theme["Accent.CTkButton"]["hover_color"], + command=lambda: eventbus.emit(UiEvents.APPLY_CROP_REQUEST)) def setup_layout(self): super().setup_layout() def place_children(self): super().place_children() - self.cropmode_button.grid(column=1, row=0, pady=pady, sticky=tk.NSEW) - self.cropapply_button.grid(column=1, row=1, pady=pady, sticky=tk.NSEW) + self.cropapply_button.grid(column=1, row=0, pady=pady, sticky=tk.NSEW) def toggle(self): super().toggle() if self.show: eventbus.emit(UiEvents.SHOW_MENU_REQUEST, "CROP") + eventbus.emit(UiEvents.TURN_ON_CROP_MODE) + else: + eventbus.emit(UiEvents.TURN_OFF_CROP_MODE) class ExtractionMenu(CollapsibleMenuFrame): diff --git a/graxpert/ui/ui_events.py b/graxpert/ui/ui_events.py index 07de140..593fa02 100644 --- a/graxpert/ui/ui_events.py +++ b/graxpert/ui/ui_events.py @@ -7,7 +7,8 @@ class UiEvents(Enum): # menu requests SHOW_MENU_REQUEST = auto() # crop - TOGGLE_CROP_REQUEST = auto() + TURN_ON_CROP_MODE = auto() + TURN_OFF_CROP_MODE = auto() APPLY_CROP_REQUEST = auto() # right sidebar requests HELP_FRAME_TOGGLED = auto()