Skip to content

Commit

Permalink
Merge pull request #57 from claassenlab/45_move_the_view_of_the_viewbox
Browse files Browse the repository at this point in the history
45 Move the view of the viewbox to a given position
  • Loading branch information
mariehartung authored Jul 20, 2024
2 parents 3bf7ce3 + 49c73ef commit f6f9fdb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
41 changes: 41 additions & 0 deletions cellpose/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions cellpose/gui/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ def mainmenu(parent):
parent.saveFlows.triggered.connect(lambda: io._save_flows(parent))
file_menu.addAction(parent.saveFlows)
parent.saveFlows.setEnabled(False)


# Save settings action from main

parent.saveSettings = QAction("Save Settings as .&json", parent)
parent.saveSettings.setShortcut("Ctrl+J")
parent.saveSettings.triggered.connect(lambda: io._save_settings(parent))
file_menu.addAction(parent.saveSettings)
parent.saveSettings.setEnabled(True)

"""
This creates a new menu item for the minimap that the user can activate.
It is deactivated by default and has to be checked.
Expand All @@ -78,14 +86,7 @@ def mainmenu(parent):
parent.minimapWindow.setChecked(False)
parent.minimapWindow.triggered.connect(parent.minimap_window)
file_menu.addAction(parent.minimapWindow)

# Save settings action from main

parent.saveSettings = QAction("Save Settings as .&json", parent)
parent.saveSettings.setShortcut("Ctrl+J")
parent.saveSettings.triggered.connect(lambda: io._save_settings(parent))
file_menu.addAction(parent.saveSettings)
parent.saveSettings.setEnabled(True)

def editmenu(parent):
main_menu = parent.menuBar()
Expand Down Expand Up @@ -163,3 +164,5 @@ def helpmenu(parent):
openTrainHelp = QAction("Training instructions", parent)
openTrainHelp.triggered.connect(parent.train_help_window)
help_menu.addAction(openTrainHelp)


0 comments on commit f6f9fdb

Please sign in to comment.