From 48a4cc28f50babb45ce43206191deefc28484c88 Mon Sep 17 00:00:00 2001 From: Mika Fischer Date: Thu, 16 Jun 2011 11:12:01 +0200 Subject: [PATCH] Implement global hotkeys (closes: #16) --- sloth/conf/default_config.py | 5 ++++ sloth/gui/labeltool.py | 47 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/sloth/conf/default_config.py b/sloth/conf/default_config.py index 2ee4ad2..9501c32 100644 --- a/sloth/conf/default_config.py +++ b/sloth/conf/default_config.py @@ -4,6 +4,11 @@ LABELS = ( ) HOTKEYS = ( + ('PgDown', lambda lt: lt.gotoNext(), 'Next image/frame'), + ('PgUp', lambda lt: lt.gotoPrevious(), 'Previous image/frame'), + ('Tab', lambda lt: lt.selectNextAnnotation(), 'Select next annotation'), + ('Shift+Tab', lambda lt: lt.selectPreviousAnnotation(), 'Select previous annotation'), + ('ESC', lambda lt: lt.exitInsertMode(), 'Exit insert mode'), ) # Defines the mapping from the annotation type to the visualization item. The diff --git a/sloth/gui/labeltool.py b/sloth/gui/labeltool.py index 151836d..82ead60 100755 --- a/sloth/gui/labeltool.py +++ b/sloth/gui/labeltool.py @@ -11,12 +11,16 @@ from sloth.gui.annotationscene import * from sloth.gui.frameviewer import * from sloth.gui.controlbuttons import * from sloth.conf import config +from sloth.core.utils import import_callable from sloth.annotations.model import * from sloth import APP_NAME, ORGANIZATION_DOMAIN, VERSION import okapy.videoio as okv GUIDIR=os.path.join(os.path.dirname(__file__)) +def bind(function, labeltool): + return lambda: function(labeltool) + class MainWindow(QMainWindow): def __init__(self, labeltool, parent=None): QMainWindow.__init__(self, parent) @@ -29,8 +33,6 @@ class MainWindow(QMainWindow): self.onAnnotationsLoaded() - self.initShortcuts() - # Slots def onPluginLoaded(self, action): self.ui.menuPlugins.addAction(action) @@ -60,30 +62,25 @@ class MainWindow(QMainWindow): if new_image.index() != self.treeview.currentIndex(): self.treeview.setCurrentIndex(new_image.index()) - def initShortcuts(self): - # TODO clean up, make configurable + def initShortcuts(self, HOTKEYS): self.shortcuts = [] - selectNextItem = QAction("Select next item", self) - selectNextItem.setShortcut(QKeySequence("Tab")) - selectNextItem.setEnabled(True) - selectNextItem.triggered.connect(self.scene.selectNextItem) - self.ui.menuPlugins.addAction(selectNextItem) - self.shortcuts.append(selectNextItem) + for hotkey in HOTKEYS: + assert len(hotkey) >= 2 + key = hotkey[0] + fun = hotkey[1] + desc = "" + if len(hotkey) > 2: + desc = hotkey[2] + if type(fun) == str: + fun = import_callable(fun) - selectPreviousItem = QAction("Select previous item", self) - selectPreviousItem.setShortcut(QKeySequence("Shift+Tab")) - selectPreviousItem.setEnabled(True) - selectPreviousItem.triggered.connect(lambda: self.scene.selectNextItem(True)) - self.ui.menuPlugins.addAction(selectPreviousItem) - self.shortcuts.append(selectPreviousItem) - - exitInsertMode = QAction("Exit insert mode", self) - exitInsertMode.setShortcut(QKeySequence("ESC")) - exitInsertMode.setEnabled(True) - exitInsertMode.triggered.connect(self.buttonarea.exitInsertMode) - self.ui.menuPlugins.addAction(exitInsertMode) - self.shortcuts.append(exitInsertMode) + hk = QAction(desc, self) + hk.setShortcut(QKeySequence(key)) + hk.setEnabled(True) + hk.triggered.connect(bind(fun, self.labeltool)) + self.ui.menuPlugins.addAction(hk) + self.shortcuts.append(hk) ### ### GUI/Application setup @@ -106,7 +103,9 @@ class MainWindow(QMainWindow): self.central_widget.setLayout(self.central_layout) self.setCentralWidget(self.central_widget) - self.buttonarea = ButtonArea(config.LABELS, config.HOTKEYS) + self.initShortcuts(config.HOTKEYS) + + self.buttonarea = ButtonArea(config.LABELS, ()) self.ui.dockAnnotationButtons.setWidget(self.buttonarea) self.buttonarea.stateChanged.connect(self.scene.setMode)