Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcut to change output layer #52

Open
Segmentoholic opened this issue Oct 17, 2024 · 1 comment
Open

Shortcut to change output layer #52

Segmentoholic opened this issue Oct 17, 2024 · 1 comment

Comments

@Segmentoholic
Copy link

Hello,

is it possible to set a shortcut to change output layer file of Geo-SAM faster?

Thank you for your help in advance!

@Fanchengyan
Copy link
Collaborator

Fanchengyan commented Oct 18, 2024

This feature might not be essential. You can modify the code as needed. Binding a shortcut key is straightforward. Define it in tools/widgetTool.py:

Geo-SAM/tools/widgetTool.py

Lines 236 to 260 in c2c8b86

self.shortcut_clear = QShortcut(QKeySequence(Qt.Key_C), self.wdg_sel)
self.shortcut_undo = QShortcut(QKeySequence(Qt.Key_Z), self.wdg_sel)
self.shortcut_save = QShortcut(QKeySequence(Qt.Key_S), self.wdg_sel)
self.shortcut_hover_mode = QShortcut(QKeySequence(Qt.Key_P), self.wdg_sel)
self.shortcut_tab = QShortcut(QKeySequence(Qt.Key_Tab), self.wdg_sel)
self.shortcut_undo_sam_pg = QShortcut(
QKeySequence(QKeySequence.Undo), self.wdg_sel
)
# connect shortcuts
self.shortcut_clear.activated.connect(self.clear_layers)
self.shortcut_undo.activated.connect(self.undo_last_prompt)
self.shortcut_save.activated.connect(self.save_shp_file)
self.shortcut_hover_mode.activated.connect(self.toggle_hover_mode)
self.shortcut_tab.activated.connect(self.loop_prompt_type)
self.shortcut_undo_sam_pg.activated.connect(self.undo_sam_polygon)
# set context for shortcuts to application
# this will make shortcuts work even if the widget is not focused
self.shortcut_clear.setContext(Qt.ApplicationShortcut)
self.shortcut_undo.setContext(Qt.ApplicationShortcut)
self.shortcut_save.setContext(Qt.ApplicationShortcut)
self.shortcut_hover_mode.setContext(Qt.ApplicationShortcut)
self.shortcut_tab.setContext(Qt.ApplicationShortcut)
self.shortcut_undo_sam_pg.setContext(Qt.ApplicationShortcut)

And then unbind it in the unload function:

Geo-SAM/tools/widgetTool.py

Lines 382 to 399 in c2c8b86

def unload(self):
"""Unload actions when plugin is closed"""
self.clear_layers(clear_extent=True)
if hasattr(self, "shortcut_tab"):
self.disconnect_safely(self.shortcut_tab)
if hasattr(self, "shortcut_undo_sam_pg"):
self.disconnect_safely(self.shortcut_undo_sam_pg)
if hasattr(self, "shortcut_clear"):
self.disconnect_safely(self.shortcut_clear)
if hasattr(self, "shortcut_undo"):
self.disconnect_safely(self.shortcut_undo)
if hasattr(self, "shortcut_save"):
self.disconnect_safely(self.shortcut_save)
if hasattr(self, "shortcut_hover_mode"):
self.disconnect_safely(self.shortcut_hover_mode)
if hasattr(self, "wdg_sel"):
self.disconnect_safely(self.wdg_sel.MapLayerComboBox.layerChanged)
self.iface.removeDockWidget(self.wdg_sel)

To find the shortcut key binding, use QT Designer to open ui/Selector.ui. Click the button you want to bind, check its objectName

image

and find the corresponding connect function in tools/widgetTool.py (self.set_vector_layer in this case).

self.wdg_sel.MapLayerComboBox.layerChanged.connect(self.set_vector_layer)

To enable modification, you need to reload the plugin. You can install the Plugin Reloader plugin to reload the plugin without restarting QGIS.

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants