-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScopePy_keyboard.py
More file actions
270 lines (187 loc) · 7.62 KB
/
ScopePy_keyboard.py
File metadata and controls
270 lines (187 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 29 20:53:48 2015
@author: john
ScopePy Keyboard shortcut library
=====================================
Central place for defining keyboard shortcut infrastructure
Version
==============================================================================
$Revision:: 54 $
$Date:: 2015-06-28 10:25:47 -0400 (Sun, 2#$
$Author:: john $
==============================================================================
"""
#==============================================================================
#%% License
#==============================================================================
"""
Copyright 2015 John Bainbridge
This file is part of ScopePy.
ScopePy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ScopePy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ScopePy. If not, see <http://www.gnu.org/licenses/>.
"""
#==============================================================================
#%% Imports
#==============================================================================
# Standard library
import os
import logging
# Third party libraries
import numpy as np
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# My libraries
from ScopePy_utilities import minidir
#==============================================================================
#%% Logger
#==============================================================================
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Add do nothing handler
logger.addHandler(logging.NullHandler())
# create console handler and set level to debug
con = logging.StreamHandler()
con.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('[%(asctime)s:%(name)s:%(levelname)s]: %(message)s')
# add formatter to ch
con.setFormatter(formatter)
# add ch to logger
logger.addHandler(con)
#==============================================================================
#%% Constants
#==============================================================================
#==============================================================================
#%% Class definitions
#==============================================================================
class Shortcuts():
"""
Class for holding a collection of keyboard shortcut reference to the name
of an action. Basically it's a dictionary of keyboard shortcuts with some
extra functions
Example
----------
mainShortcuts = ShortCuts()
mainShortcuts['NewPlot'] = 'Ctrl+n'
shortCut = mainShortcuts['NewPlot']
shortCut = mainShortcuts.NewPlot
event = QKeyPressEvent()
action = mainShortcuts.getActionFromEvent(event)
"""
def __init__(self):
# Main dictionary for holding shortcuts
# key = action name
# item = QKeySequence
self.action2key = {}
# Reverse lookup dictionary
self.key2action = {}
def __str__(self):
lines = []
for action,keySeq in self.action2key.items():
lines.append("%s : %s" % (action,keySeq.toString()))
return '\n'.join(lines)
def __repr__(self):
return "Shortcuts() object"
def addShortCut(self,actionName,shortcut):
"""
Add new shortcut to internal dictionary
Inputs
-------
actionName : str
Name label for the action represented by this shortcut
shortcut : str or Qt key list
e.g. "Ctrl+t" or Qt.CTRL+Qt.Key_T
This will be converted into a QKeySequence
"""
# Convert shortcut to key sequence
if isinstance(shortcut,QKeySequence):
keySeq = shortcut
else:
# Anything else just QKeySequence it
keySeq = QKeySequence(shortcut)
# Add to dictionaries
self.action2key[actionName] = keySeq
self.key2action[self.action2key[actionName].toString().lower()] = actionName
# Try to add as an attribute
try:
setattr(self,actionName,self.action2key[actionName])
finally:
pass
def __getitem__(self,actionName):
assert actionName in self.action2key,"Shortcuts: Unknown action [%s]" % actionName
return self.action2key[actionName]
def __setitem__(self,actionName,shortcut):
self.addShortCut(actionName,shortcut)
def getActionFromEvent(self,event):
"""
Check if the event is the same as any of the shortcuts contained
in this Shortcuts() class
Example usage
---------------
>>> S = Shortcuts()
>>> S['new'] = 'Ctrl+n'
>>> event = QKeyEvent(QEvent.KeyPress,Qt.Key_N,Qt.ControlModifier)
>>> S.getActionFromEvent(event)
'new'
"""
# Convert event into a QKeySequence string
# ------------------------------------------
keySeq = QKeySequence(event.modifiers()|event.key()).toString().lower()
# Cross reference key sequence
# ------------------------------
if keySeq in self.key2action:
return self.key2action[keySeq]
else:
return
#==============================================================================
#%% Default Keyboard shortcuts
#==============================================================================
def makeDefaultKeyboardShortcuts():
"""
Make defaults and return in one collected variable
"""
# Programming note:
# --------------------
# Would really like to use the QKeySequence.StandardKey to generate the
# "standard" shortcuts like "Open" but doing this, for example
# QKeySequence(QKeySequence.Print)
# As given in the documentation crashes the program with no explanation
# Main window
# ----------
mainShortcuts = Shortcuts()
mainShortcuts['open'] = "Ctrl+o"
mainShortcuts['save'] = "Ctrl+s"
mainShortcuts['newPlot'] = "Ctrl+n"
mainShortcuts['newTab'] = "Ctrl+t"
mainShortcuts['config'] = "Ctrl+Alt+i"
mainShortcuts['panels'] = Qt.Key_Meta # windows key
# Plot
plotShortcuts = Shortcuts()
plotShortcuts['autoscale'] = 'Ctrl+A'
plotShortcuts['autoscale_x'] = 'Alt+X'
plotShortcuts['autoscale_y'] = 'Alt+Y'
plotShortcuts['pan_up'] = Qt.Key_Up
plotShortcuts['pan_down'] = Qt.Key_Down
plotShortcuts['pan_left'] = Qt.Key_Left
plotShortcuts['pan_right'] = Qt.Key_Right
plotShortcuts['zoom_in'] = 'alt+i'
plotShortcuts['zoom_out'] = 'alt+o'
plotShortcuts['scale_y_out'] = Qt.CTRL+Qt.Key_Up
plotShortcuts['scale_y_in'] = Qt.CTRL+Qt.Key_Down
plotShortcuts['scale_x_out'] = Qt.CTRL+Qt.Key_Right
plotShortcuts['scale_x_in'] = Qt.CTRL+Qt.Key_Left
# wholeTable = {'main':mainShortcuts,'plot':plotShortcuts}
wholeTable = minidir()
wholeTable['main'] = mainShortcuts
wholeTable['plot'] = plotShortcuts
return wholeTable