Skip to content

Commit

Permalink
improved spinbox UX
Browse files Browse the repository at this point in the history
  • Loading branch information
playduck committed Apr 4, 2020
1 parent af7b32e commit 25b3b5c
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions ListItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,45 @@ class SuperSpinner(QtWidgets.QLineEdit):

def __init__(self, parent=None):
super().__init__(parent)
self.setValidator(QtGui.QDoubleValidator(parent=self))
self.setMaxLength(5)
self.setReadOnly(True)
self.textChanged.connect(lambda t: self.valueChanged.emit(float(t)))
self.setValidator(QtGui.QDoubleValidator(0.0, 1000.0, Config.PRECISION, parent=self))

self.textChanged.connect(self.__handleChange)
self.setCursor(QtCore.Qt.SizeHorCursor)

self.beeingEdited = False
self.mouseStartPos = False
self.startValue = 0
self.value = 0
self.startValue = 0.0
self.value = 0.0

self.min = self.max = 0.0
self.setValue(0.0, True)

self.min = self.max = 0
def __handleChange(self, t):
if (self.beeingEdited or self.mouseStartPos) and t and float(t):
val = round(float(t), Config.PRECISION)
self.valueChanged.emit(val)

def setRange(self, min, max):
self.min = min
self.max = max

def setValue(self, val):
self.setText(str(val))
self.value = val
def setValue(self, val, override=False):
if not self.beeingEdited or override:
val = round(val, Config.PRECISION)
self.setText(str(val))
self.value = val

def focusInEvent(self, QFocusEvent):
self.beeingEdited = True
return super().focusInEvent(QFocusEvent)

def focusOutEvent(self, QFocusEvent):
self.beeingEdited = False
return super().focusOutEvent(QFocusEvent)

def mouseDoubleClickEvent(self, e):
self.setValue(0)
self.setValue(0.0, True)
self.valueChanged.emit(0.0)

def mousePressEvent(self, e):
super().mousePressEvent(e)
Expand All @@ -335,10 +352,10 @@ def mouseMoveEvent(self, e):
else:
multiplier = 0.1

valueOffset = round( (self.mouseStartPos - e.pos().x()) * multiplier, 3)
valueOffset = ( self.mouseStartPos - e.pos().x() ) * multiplier
value = self.startValue - valueOffset
value = min((self.max, max((self.min, value))))
self.setValue(value)
self.setValue(value, True)

def mouseReleaseEvent(self, e):
super().mouseReleaseEvent(e)
Expand Down

0 comments on commit 25b3b5c

Please sign in to comment.