-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspinBox.py
302 lines (193 loc) · 7.42 KB
/
spinBox.py
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from PySide import QtGui, QtCore
from PySide.QtCore import *
from PySide.QtGui import *
import math
import re
import math
import inspect, os
from simpleEval import simple_eval
MAX_QT_VAL = 999999999.0
MAX_QT_EXP = 10
class VEBaseSpinBox(QLineEdit):
def __init__(self, name, parent, *args, **kwargs):
super(VEBaseSpinBox, self).__init__(parent, *args, **kwargs)
self.m_trackStartPos = QPoint()
self.minimum = -MAX_QT_VAL
self.maximum = MAX_QT_VAL
self.decimals = 4
self.singleStep = 1.0
#self.m_last
#self.m_startValue
#bool self.m_dragging
#bool self.m_stepping
#bool self.m_steppingTimerConnected
self.m_steppingTimer = QTimer()
self.m_last = 0
self.m_startValue = 0
self.m_dragging= False
self.m_stepping= False
self.m_steppingTimerConnected= False
self.setFocusPolicy(Qt.StrongFocus)
self.setAlignment( Qt.AlignRight | Qt.AlignCenter )
#self.setKeyboardTracking( False )
self.m_steppingTimer.setSingleShot( True )
def stepBy(self, step):
print self.value()
self.setValue(str(float(self.value()) + self.singleStep))
def value(self):
return self.text()
def setValue(self, value):
self.setText(value)
def mousePressEvent(self, event ): #override*/
self.m_trackStartPos = event.pos()
self.m_startValue = self.value()
self.m_last = self.value()
self.initialOverrideCursor = Qt.SizeVerCursor
QApplication.setOverrideCursor( self.initialOverrideCursor )
event.accept()
def mouseReleaseEvent(self, event ): #override*/
if self.m_dragging :
self.m_dragging = False
self.resetPrecision()
self.emitInteractionEnd( True )
# [FE-6014]
self.deselect()
self.clearFocus() # [FE-6077] using self instead of self.clearFocus() makes the TAB key work.
else:
self.beginStepping()
self.mousePressEvent( event )
super(VEBaseSpinBox, self).mouseReleaseEvent(event)
QApplication.restoreOverrideCursor()
event.accept()
def mouseMoveEvent(self, event ): #override*/
if ( event.buttons() == Qt.LeftButton ):
if not self.m_dragging:
self.m_dragging = True
if ( self.m_stepping ):
self.m_stepping = False
self.m_steppingTimer.stop()
else:
self.emitInteractionBegin()
trackPos = event.pos()
deltaX = trackPos.x() - self.m_trackStartPos.x()
deltaXInInches = deltaX / self.logicalDpiX()
logBaseChangePerStep = self.implicitLogBaseChangePerStep()
# Slow down movement if Ctrl is pressed
if ( QApplication.keyboardModifiers() & Qt.ControlModifier ):
logBaseChangePerStep -= 2
else:
logBaseChangePerStep -= 1
stepMult = self.updateStep( deltaXInInches, logBaseChangePerStep )
nSteps = ( round( stepMult * ( self.m_trackStartPos.y() - trackPos.y() ) ) )
# While dragging, we want to do an absolute value offset,
# so reset to start value and then increment by abs step
#FabricUI.Util.QTSignalBlocker block( self )
self.setValue( self.m_startValue )
self.stepBy( nSteps )
event.accept()
def focusInEvent(self, event ): #override*/
# [FE-5997] inspired by http://stackoverflow.com/questions/5821802/qspinbox-inside-a-qscrollarea-how-to-prevent-spin-box-from-stealing-focus-when
self.setFocusPolicy(Qt.WheelFocus)
if ( event.reason() != Qt.PopupFocusReason ):
self.m_last = self.value()
super(VEBaseSpinBox, self).focusInEvent(event)
def focusOutEvent(self, event ): #override*/
# [FE-5997] inspired by http://stackoverflow.com/questions/5821802/qspinbox-inside-a-qscrollarea-how-to-prevent-spin-box-from-stealing-focus-when
self.setFocusPolicy(Qt.StrongFocus)
super(VEBaseSpinBox, self).focusOutEvent(event)
def keyPressEvent(self, event ): #override*/
if ( event.key() == Qt.Key_Up or event.key() == Qt.Key_Down ):
beginStepping()
elif ( event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter ):
# [FE-6025]
self.endStepping()
self.deselect()
self.clearFocus()
event.accept()
return
elif ( event.key() == Qt.Key_Escape ):
# [FE-6007]
self.setValue(self.m_last)
self.endStepping()
self.deselect()
self.clearFocus()
event.accept()
return
else:
self.endStepping()
super(VEBaseSpinBox, self).keyPressEvent(event)
def wheelEvent(self, event ): #override*/
# [FE-5997] inspired by http://stackoverflow.com/questions/5821802/qspinbox-inside-a-qscrollarea-how-to-prevent-spin-box-from-stealing-focus-when
if (not self.hasFocus()):
event.ignore()
return
self.beginStepping()
super(VEBaseSpinBox, self).wheelEvent(event)
def leaveEvent(self, event ): #override*/
self.endStepping()
super(VEBaseSpinBox, self).leaveEvent(event)
#def implicitLogBaseChangePerStep(self):
# return 0
#def updateStep(self, deltaXInInches, logBaseChangePerStep):
# return 0
#def resetPrecision(self ):
# pass
def beginStepping(self):
if ( not self.m_stepping ):
self.m_stepping = True
if ( not self.m_steppingTimerConnected ):
self.m_steppingTimerConnected = True
self.connectSteppingTimer( self.m_steppingTimer )
self.emitInteractionBegin()
# [pzion 20160125] Steps are bigger than dragging
self.updateStep( 0.0, self.implicitLogBaseChangePerStep() )
#assert( self.m_steppingTimerConnected )
# This is finely tuned but also personal preference:
# (pz's fav value: 600, em's fav value: 300)
self.m_steppingTimer.start( 300 )
def endStepping(self):
if ( self.m_stepping ):
self.m_stepping = False
self.m_steppingTimer.stop()
self.resetPrecision()
self.emitInteractionEnd( True )
def connectSteppingTimer(self, steppingTimer ):
return 0
def emitInteractionBegin(self):
return 0
def emitInteractionEnd(self, commit ):
return 0
def textFromValue(self, val ):
return QString.number( val )
def setSingleStep(self, val):
self.singleStep = val
def setDecimals(self, prec):
self.decimals = prec
def implicitLogBaseChangePerStep(self):
if ( self.minimum == -MAX_QT_VAL or self.maximum == +MAX_QT_VAL ):
return 0.0
return max(-6.0, log10( ( self.maximum() - self.minimum() ) / 20.0 ))
def updateStep(self, deltaXInInches, logBaseChangePerStep):
base10Exp = 0.5 * deltaXInInches + logBaseChangePerStep
velocity = pow( 10, base10Exp )
# Always step by a round-number
roundedBase10Exp = ( round( base10Exp ) )
changePerStep = pow( 10.0, roundedBase10Exp )
minChangePerStep = 0.00001
changePerStep = max( minChangePerStep, changePerStep )
maxChangePerStep = 100000
changePerStep = min( maxChangePerStep, changePerStep )
self.setSingleStep( changePerStep )
self.setDecimals( max( 0, -( math.trunc( base10Exp ) ) ) )
return max( 0.01, 0.5 * velocity / changePerStep)
def resetPrecision(self):
self.setDecimals( MAX_QT_EXP )
class Toto(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Toto, self).__init__()
self.spinBox = VEBaseSpinBox('X', self)
import sys
app = QtGui.QApplication(sys.argv)
view = Toto()
view.show()
sys.exit(app.exec_())