From 02b15db3ce2400e51231ebbc54d7597bc6d5c6e1 Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Tue, 9 Jul 2024 17:26:33 +0200 Subject: [PATCH 1/8] Created first version of the method used to navigate on the minimap --- cellpose/gui/gui.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index c79699b2..e6ef18a6 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -386,6 +386,43 @@ def minimap_closed(self): if hasattr(self, 'minimap'): del self.minimap + def center_view_on_position(self, normalized_x, normalized_y): + """ + Centers the view on the given normalized coordinates (x, y). + This will be used to navigate using the minimap window. + This will also give us the coordinates. + + Args: + normalized_x (float): Normalized x-coordinate (0.0 to 1.0). + normalized_y (float): Normalized y-coordinate (0.0 to 1.0). + """ + # Get the size of the image + img_height = self.img.image.shape[0] + img_width = self.img.image.shape[1] + + # Calculate the actual pixel coordinates + target_x = normalized_x * img_width + target_y = normalized_y * img_height + + # Get the size of the viewbox (p0) + viewbox_width = self.p0.width() + viewbox_height = self.p0.height() + # methods using vieboxes (might be useful later): + # in gui: make_viewbox, in guiparts ViewBoxNoRightDrag + + # Calculate the new top-left corner of the viewbox to center the target position + new_x = target_x - viewbox_width // 2 + new_y = target_y - viewbox_height // 2 + # might have to be adjusted + + # Ensure the new top-left corner does not go out of the image bounds + new_x = max(0, min(new_x, img_width - viewbox_width)) + new_y = max(0, min(new_y, img_height - viewbox_height)) + + # Set the new viewbox position + self.p0.setGeometry(new_x, new_y, viewbox_width, viewbox_height) + self.p0.update() # Refresh the view + def make_buttons(self): self.boldfont = QtGui.QFont("Arial", 11, QtGui.QFont.Bold) From ad19fe8709a2b37a949a50d5159b5d51e1e3f077 Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Wed, 10 Jul 2024 22:13:25 +0200 Subject: [PATCH 2/8] Modified method to change the range of the image displayed in the viewbox --- cellpose/gui/gui.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index e6ef18a6..85c1bbc5 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -357,6 +357,7 @@ def minimap_window(self): if self.minimap_window_instance is not None: self.minimap_window_instance.deleteLater() self.minimap_window_instance = None + self.center_view_on_position(0.5, 0.5) #test def close_minimap_window(self): """ @@ -404,12 +405,46 @@ def center_view_on_position(self, normalized_x, normalized_y): target_x = normalized_x * img_width target_y = normalized_y * img_height + # Get the current view range of the viewbox. + # This tells us which part of the image is currently displayed. + view_range = self.p0.viewRange() + + # Extract the x and y ranges + x_range = view_range[0] + y_range = view_range[1] + + # Get the center position of the view in the image: + x_center = (x_range[0] + x_range[1]) / 2 + y_center = (y_range[0] + y_range[1]) / 2 + # not needed as of now, might still be useful though + + # Calculate the current zoom level + zoom_x = (x_range[1] - x_range[0]) / img_width + zoom_y = (y_range[1] - y_range[0]) / img_height + + # Calculate the width and height of the view range based on the zoom level + view_width = img_width * zoom_x + view_height = img_height * zoom_y + + # Calculate the new view range, centered on the target position + new_x_range = [max(0, min(target_x - view_width / 2, img_width - view_width)), + max(view_width, min(target_x + view_width / 2, img_width))] + new_y_range = [max(0, min(target_y - view_height / 2, img_height - view_height)), + max(view_height, min(target_y + view_height / 2, img_height))] + + # Set the new view range to the ViewBox + self.p0.setXRange(*new_x_range, padding=0) + self.p0.setYRange(*new_y_range, padding=0) + + """ + # old code; probably not needed anymore + # Get the size of the viewbox (p0) viewbox_width = self.p0.width() viewbox_height = self.p0.height() # methods using vieboxes (might be useful later): # in gui: make_viewbox, in guiparts ViewBoxNoRightDrag - + # Calculate the new top-left corner of the viewbox to center the target position new_x = target_x - viewbox_width // 2 new_y = target_y - viewbox_height // 2 @@ -421,7 +456,14 @@ def center_view_on_position(self, normalized_x, normalized_y): # Set the new viewbox position self.p0.setGeometry(new_x, new_y, viewbox_width, viewbox_height) + + # Move the view of the viewbox + self.p0.translateBy(new_x, new_y) + self.p0.update() # Refresh the view + # moves the border of the viewbox as of now (not good xD) + """ + def make_buttons(self): From 1fc44db4e7c8b0ea31bcd7a619bf341b7e02f2ea Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Sat, 13 Jul 2024 12:38:23 +0200 Subject: [PATCH 3/8] Adapted the code to prevent the image from jumping to the upper left corner if zoomed out --- cellpose/gui/gui.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index 85c1bbc5..74dc09bf 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -427,10 +427,10 @@ def center_view_on_position(self, normalized_x, normalized_y): view_height = img_height * zoom_y # Calculate the new view range, centered on the target position - new_x_range = [max(0, min(target_x - view_width / 2, img_width - view_width)), - max(view_width, min(target_x + view_width / 2, img_width))] - new_y_range = [max(0, min(target_y - view_height / 2, img_height - view_height)), - max(view_height, min(target_y + view_height / 2, img_height))] + new_x_range = [target_x - view_width / 2, + target_x + view_width / 2] + new_y_range = [target_y - view_height / 2, + target_y + view_height / 2] # Set the new view range to the ViewBox self.p0.setXRange(*new_x_range, padding=0) @@ -458,7 +458,7 @@ def center_view_on_position(self, normalized_x, normalized_y): self.p0.setGeometry(new_x, new_y, viewbox_width, viewbox_height) # Move the view of the viewbox - self.p0.translateBy(new_x, new_y) + self.p0.translateBy(x=new_x, y=new_y) self.p0.update() # Refresh the view # moves the border of the viewbox as of now (not good xD) From 23ca886c13c2771cb749c084ce8f18309c33be6b Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Sat, 13 Jul 2024 12:39:56 +0200 Subject: [PATCH 4/8] Removed unused code --- cellpose/gui/gui.py | 42 +++--------------------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index 74dc09bf..69c3eac7 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -413,11 +413,6 @@ def center_view_on_position(self, normalized_x, normalized_y): x_range = view_range[0] y_range = view_range[1] - # Get the center position of the view in the image: - x_center = (x_range[0] + x_range[1]) / 2 - y_center = (y_range[0] + y_range[1]) / 2 - # not needed as of now, might still be useful though - # Calculate the current zoom level zoom_x = (x_range[1] - x_range[0]) / img_width zoom_y = (y_range[1] - y_range[0]) / img_height @@ -427,44 +422,13 @@ def center_view_on_position(self, normalized_x, normalized_y): view_height = img_height * zoom_y # Calculate the new view range, centered on the target position - new_x_range = [target_x - view_width / 2, - target_x + view_width / 2] - new_y_range = [target_y - view_height / 2, - target_y + view_height / 2] + new_x_range = [target_x - view_width / 2, target_x + view_width / 2] + new_y_range = [target_y - view_height / 2, target_y + view_height / 2] # Set the new view range to the ViewBox self.p0.setXRange(*new_x_range, padding=0) self.p0.setYRange(*new_y_range, padding=0) - """ - # old code; probably not needed anymore - - # Get the size of the viewbox (p0) - viewbox_width = self.p0.width() - viewbox_height = self.p0.height() - # methods using vieboxes (might be useful later): - # in gui: make_viewbox, in guiparts ViewBoxNoRightDrag - - # Calculate the new top-left corner of the viewbox to center the target position - new_x = target_x - viewbox_width // 2 - new_y = target_y - viewbox_height // 2 - # might have to be adjusted - - # Ensure the new top-left corner does not go out of the image bounds - new_x = max(0, min(new_x, img_width - viewbox_width)) - new_y = max(0, min(new_y, img_height - viewbox_height)) - - # Set the new viewbox position - self.p0.setGeometry(new_x, new_y, viewbox_width, viewbox_height) - - # Move the view of the viewbox - self.p0.translateBy(x=new_x, y=new_y) - - self.p0.update() # Refresh the view - # moves the border of the viewbox as of now (not good xD) - """ - - def make_buttons(self): self.boldfont = QtGui.QFont("Arial", 11, QtGui.QFont.Bold) @@ -1932,7 +1896,7 @@ def update_plot(self): self.win.show() self.show() - + def update_layer(self): if self.masksOn or self.outlinesOn: From 484cd4ed766aa8483fdbbd7b6b56033c981a4479 Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Mon, 15 Jul 2024 16:50:51 +0200 Subject: [PATCH 5/8] removed test --- cellpose/gui/gui.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index 21ed8bdb..86a2fd5a 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -355,7 +355,6 @@ def minimap_window(self): if self.minimap_window_instance is not None: self.minimap_window_instance.deleteLater() self.minimap_window_instance = None - self.center_view_on_position(0.5, 0.5) #test def minimap_closed(self): """ From cb3d10b32dfd355e785016ec61df558481b20e7b Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Mon, 15 Jul 2024 18:16:18 +0200 Subject: [PATCH 6/8] changed the description of the method --- cellpose/gui/gui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index 86a2fd5a..06281dd6 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -370,7 +370,7 @@ def center_view_on_position(self, normalized_x, normalized_y): """ Centers the view on the given normalized coordinates (x, y). This will be used to navigate using the minimap window. - This will also give us the coordinates. + The zoom level will be maintained. Args: normalized_x (float): Normalized x-coordinate (0.0 to 1.0). From 903bc6b437bfef3c5c4c4585cc72796c29e1cd5d Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Wed, 17 Jul 2024 17:51:01 +0200 Subject: [PATCH 7/8] Extended explanation to include parameter p0 (viewbox) --- cellpose/gui/gui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index 06281dd6..07e36bec 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -384,7 +384,7 @@ def center_view_on_position(self, normalized_x, normalized_y): target_x = normalized_x * img_width target_y = normalized_y * img_height - # Get the current view range of the viewbox. + # Get the current view range of the view box p0. # This tells us which part of the image is currently displayed. view_range = self.p0.viewRange() From 49c73efee3111ba385c1f7c8a1259c805f9e2008 Mon Sep 17 00:00:00 2001 From: Marie Hartung Date: Sat, 20 Jul 2024 10:41:44 +0200 Subject: [PATCH 8/8] Restored function that was lost in the last merge-commit --- cellpose/gui/gui.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cellpose/gui/gui.py b/cellpose/gui/gui.py index ee1b3b07..04f46f0e 100644 --- a/cellpose/gui/gui.py +++ b/cellpose/gui/gui.py @@ -363,6 +363,47 @@ def minimap_closed(self): except Exception as e: print(f"An error occurred while closing the minimap: {e}") + def center_view_on_position(self, normalized_x, normalized_y): + """ + Centers the view on the given normalized coordinates (x, y). + This will be used to navigate using the minimap window. + The zoom level will be maintained. + Args: + normalized_x (float): Normalized x-coordinate (0.0 to 1.0). + normalized_y (float): Normalized y-coordinate (0.0 to 1.0). + """ + # Get the size of the image + img_height = self.img.image.shape[0] + img_width = self.img.image.shape[1] + + # Calculate the actual pixel coordinates + target_x = normalized_x * img_width + target_y = normalized_y * img_height + + # Get the current view range of the view box p0. + # This tells us which part of the image is currently displayed. + view_range = self.p0.viewRange() + + # Extract the x and y ranges + x_range = view_range[0] + y_range = view_range[1] + + # Calculate the current zoom level + zoom_x = (x_range[1] - x_range[0]) / img_width + zoom_y = (y_range[1] - y_range[0]) / img_height + + # Calculate the width and height of the view range based on the zoom level + view_width = img_width * zoom_x + view_height = img_height * zoom_y + + # Calculate the new view range, centered on the target position + new_x_range = [target_x - view_width / 2, target_x + view_width / 2] + new_y_range = [target_y - view_height / 2, target_y + view_height / 2] + + # Set the new view range to the ViewBox + self.p0.setXRange(*new_x_range, padding=0) + self.p0.setYRange(*new_y_range, padding=0) + def make_buttons(self): self.boldfont = QtGui.QFont("Arial", 11, QtGui.QFont.Bold)