@@ -123,6 +123,7 @@ class FigureCanvasQT( QtGui.QWidget, FigureCanvasBase ):
123123 keyvald = { QtCore .Qt .Key_Control : 'control' ,
124124 QtCore .Qt .Key_Shift : 'shift' ,
125125 QtCore .Qt .Key_Alt : 'alt' ,
126+ QtCore .Qt .Key_Meta : 'super' ,
126127 QtCore .Qt .Key_Return : 'enter' ,
127128 QtCore .Qt .Key_Left : 'left' ,
128129 QtCore .Qt .Key_Up : 'up' ,
@@ -146,13 +147,37 @@ class FigureCanvasQT( QtGui.QWidget, FigureCanvasBase ):
146147 QtCore .Qt .Key_PageUp : 'pageup' ,
147148 QtCore .Qt .Key_PageDown : 'pagedown' ,
148149 }
150+
151+ # define the modifier keys which are to be collected on keyboard events.
152+ # format is: [(modifier_flag, modifier_name, equivalent_key)
153+ _modifier_keys = [
154+ (QtCore .Qt .MetaModifier , 'super' , QtCore .Qt .Key_Meta ),
155+ (QtCore .Qt .AltModifier , 'alt' , QtCore .Qt .Key_Alt ),
156+ (QtCore .Qt .ControlModifier , 'ctrl' , QtCore .Qt .Key_Control )
157+ ]
158+
159+ if sys .platform == 'darwin' :
160+ # in OSX, the control and super (aka cmd/apple) keys are switched, so
161+ # switch them back.
162+ keyvald .update ({
163+ QtCore .Qt .Key_Control : 'super' , # cmd/apple key
164+ QtCore .Qt .Key_Meta : 'control' ,
165+ })
166+
167+ _modifier_keys = [
168+ (QtCore .Qt .ControlModifier , 'super' , QtCore .Qt .Key_Control ),
169+ (QtCore .Qt .AltModifier , 'alt' , QtCore .Qt .Key_Alt ),
170+ (QtCore .Qt .MetaModifier , 'ctrl' , QtCore .Qt .Key_Meta ),
171+ ]
172+
149173 # map Qt button codes to MouseEvent's ones:
150174 buttond = {QtCore .Qt .LeftButton : 1 ,
151175 QtCore .Qt .MidButton : 2 ,
152176 QtCore .Qt .RightButton : 3 ,
153177 # QtCore.Qt.XButton1 : None,
154178 # QtCore.Qt.XButton2 : None,
155179 }
180+
156181 def __init__ ( self , figure ):
157182 if DEBUG : print ('FigureCanvasQt: ' , figure )
158183 _create_qApp ()
@@ -295,8 +320,7 @@ def _get_key( self, event ):
295320
296321 if key is not None :
297322 # prepend the ctrl, alt, super keys if appropriate (sorted in that order)
298- for modifier , Qt_key , prefix in [(QtCore .Qt .AltModifier , QtCore .Qt .Key_Alt , 'alt' ),
299- (QtCore .Qt .ControlModifier , QtCore .Qt .Key_Control , 'ctrl' )]:
323+ for modifier , prefix , Qt_key in self ._modifier_keys :
300324 if event .key () != Qt_key and int (event .modifiers ()) & modifier == modifier :
301325 key = '{}+{}' .format (prefix , key )
302326
@@ -368,6 +392,14 @@ def __init__( self, canvas, num ):
368392 self .canvas .setFocusPolicy ( QtCore .Qt .StrongFocus )
369393 self .canvas .setFocus ()
370394
395+ if sys .platform == 'darwin' :
396+ # to make a qt window pop up on top on osx, osascript can be used
397+ # this came from http://sourceforge.net/mailarchive/message.php?msg_id=23718545
398+ cmd = ("""/usr/bin/osascript -e 'tell app "Finder" to set """ + \
399+ """frontmost of process "%s" to true'""" ) % \
400+ os .path .basename (sys .executable )
401+ os .system (cmd )
402+
371403 QtCore .QObject .connect ( self .window , QtCore .SIGNAL ( 'destroyed()' ),
372404 self ._widgetclosed )
373405 self .window ._destroying = False
@@ -398,9 +430,9 @@ def __init__( self, canvas, num ):
398430 self .canvas .figure .show = lambda * args : self .window .show ()
399431
400432 def notify_axes_change ( fig ):
401- # This will be called whenever the current axes is changed
402- if self .toolbar is not None :
403- self .toolbar .update ()
433+ # This will be called whenever the current axes is changed
434+ if self .toolbar is not None :
435+ self .toolbar .update ()
404436 self .canvas .figure .add_axobserver ( notify_axes_change )
405437
406438 @QtCore .Slot ()
@@ -409,10 +441,10 @@ def _show_message(self,s):
409441 self .window .statusBar ().showMessage (s )
410442
411443 def full_screen_toggle (self ):
412- if self .window .isFullScreen ():
413- self .window .showNormal ()
414- else :
415- self .window .showFullScreen ()
444+ if self .window .isFullScreen ():
445+ self .window .showNormal ()
446+ else :
447+ self .window .showFullScreen ()
416448
417449 def _widgetclosed ( self ):
418450 if self .window ._destroying : return
0 commit comments