11import pickle
22from logging import getLogger
33
4- from qtpy import QtCore , QtWidgets
4+ from qtpy import QtCore , QtWidgets , QtGui
55
66import bw2data as bd
77
@@ -27,6 +27,7 @@ def __init__(self, parent=None):
2727 self .setMenuBar (self .menu_bar )
2828
2929 self .connect_signals ()
30+ self .connect_shortcuts ()
3031
3132 def sync (self ):
3233 """
@@ -87,6 +88,12 @@ def connect_signals(self):
8788 # Keyboard shortcuts
8889 signals .project .changed .connect (self .sync )
8990
91+ def connect_shortcuts (self ):
92+ """Connect global keyboard shortcuts to their respective functions. Only called once during initialization."""
93+ for seq , func in _main_window_shortcuts .items ():
94+ shortcut = QtWidgets .QShortcut (QtGui .QKeySequence (seq ), self )
95+ shortcut .activated .connect (func )
96+
9097 def clearPanes (self ):
9198 for pane in self .panes ():
9299 pane .deleteLater ()
@@ -108,3 +115,17 @@ def dialog_on_exception(self, exception: Exception):
108115 f"An error occurred, check the logs for more information \n \n { str (exception )} " ,
109116 QtWidgets .QMessageBox .Ok ,
110117 )
118+
119+
120+ def global_shortcut (key_sequence ):
121+ """
122+ Decorator to register a global keyboard shortcut for the main window. Decorate a function with e.g.
123+ @global_shortcut("Ctrl+S") to register it as a shortcut. Also works on the run method of actions as long as the
124+ parameters of said action are taken care of.
125+ """
126+ def decorator (func ):
127+ _main_window_shortcuts [key_sequence ] = func
128+ return func
129+ return decorator
130+
131+ _main_window_shortcuts = {}
0 commit comments