Skip to content

Commit

Permalink
Crop mode is now automatically turned on/off when crop menu is opened…
Browse files Browse the repository at this point in the history
…/closed
  • Loading branch information
Steffenhir committed Mar 29, 2024
1 parent 5d09eaa commit 49ec537
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
37 changes: 27 additions & 10 deletions graxpert/ui/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions graxpert/ui/left_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion graxpert/ui/ui_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 49ec537

Please sign in to comment.