Reduce the QSwitch class to only what's needed
This commit is contained in:
+52
-131
@@ -8,12 +8,8 @@
|
|||||||
File History:
|
File History:
|
||||||
Created: 2020-05-03 [0.4.5]
|
Created: 2020-05-03 [0.4.5]
|
||||||
|
|
||||||
This class is based on Stack Overflow code by Stefan Scherfke, based
|
The core code of this class is based on Stack Overflow example by
|
||||||
again on contribution by IMAN4K, published under license CC BY-SA 4.0.
|
Stefan Scherfke: https://stackoverflow.com/a/51825815/5825851
|
||||||
https://stackoverflow.com/a/51825815/5825851
|
|
||||||
|
|
||||||
The above code has been modified to integrate with novelWriter, and
|
|
||||||
re-released under compatible GPLv3 (see creativecommons.org).
|
|
||||||
|
|
||||||
This file is a part of novelWriter
|
This file is a part of novelWriter
|
||||||
Copyright 2020, Veronica Berglyd Olsen
|
Copyright 2020, Veronica Berglyd Olsen
|
||||||
@@ -50,55 +46,9 @@ class QSwitch(QAbstractButton):
|
|||||||
|
|
||||||
self.setCheckable(True)
|
self.setCheckable(True)
|
||||||
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||||
|
self.setFixedWidth(40)
|
||||||
self._trackRadius = 10
|
self.setFixedHeight(20)
|
||||||
self._thumbRadius = 8
|
self._offset = 10
|
||||||
|
|
||||||
self._margin = max(0, self._thumbRadius - self._trackRadius)
|
|
||||||
self._baseOffset = max(self._thumbRadius, self._trackRadius)
|
|
||||||
self._endOffset = {
|
|
||||||
True: lambda: self.width() - self._baseOffset,
|
|
||||||
False: lambda: self._baseOffset,
|
|
||||||
}
|
|
||||||
self._offset = self._baseOffset
|
|
||||||
|
|
||||||
palette = self.palette()
|
|
||||||
if self._thumbRadius > self._trackRadius:
|
|
||||||
self._trackColor = {
|
|
||||||
True: palette.highlight(),
|
|
||||||
False: palette.dark(),
|
|
||||||
}
|
|
||||||
self._thumbColor = {
|
|
||||||
True: palette.highlight(),
|
|
||||||
False: palette.light(),
|
|
||||||
}
|
|
||||||
self._textColor = {
|
|
||||||
True: palette.highlightedText().color(),
|
|
||||||
False: palette.dark().color(),
|
|
||||||
}
|
|
||||||
self._thumbText = {
|
|
||||||
True: '',
|
|
||||||
False: '',
|
|
||||||
}
|
|
||||||
self._trackOpacity = 0.5
|
|
||||||
else:
|
|
||||||
self._thumbColor = {
|
|
||||||
True: palette.highlightedText(),
|
|
||||||
False: palette.light(),
|
|
||||||
}
|
|
||||||
self._trackColor = {
|
|
||||||
True: palette.highlight(),
|
|
||||||
False: palette.dark(),
|
|
||||||
}
|
|
||||||
self._textColor = {
|
|
||||||
True: palette.highlight().color(),
|
|
||||||
False: palette.dark().color(),
|
|
||||||
}
|
|
||||||
self._thumbText = {
|
|
||||||
True: nwUnicode.U_CHECK,
|
|
||||||
False: nwUnicode.U_MULT,
|
|
||||||
}
|
|
||||||
self._trackOpacity = 1.0
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -111,8 +61,8 @@ class QSwitch(QAbstractButton):
|
|||||||
return self._offset
|
return self._offset
|
||||||
|
|
||||||
@offset.setter
|
@offset.setter
|
||||||
def offset(self, value):
|
def offset(self, theOffset):
|
||||||
self._offset = value
|
self._offset = theOffset
|
||||||
self.update()
|
self.update()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -120,114 +70,85 @@ class QSwitch(QAbstractButton):
|
|||||||
# Getters and Setters
|
# Getters and Setters
|
||||||
##
|
##
|
||||||
|
|
||||||
def trackRadius(self):
|
def setChecked(self, isChecked):
|
||||||
return self._trackRadius
|
"""Overload setChecked to also alter the offset.
|
||||||
|
"""
|
||||||
def setTrackRadius(self, theValue):
|
super().setChecked(isChecked)
|
||||||
if isinstance(theValue, int):
|
if isChecked:
|
||||||
if theValue > 0:
|
self.offset = 30
|
||||||
self._trackRadius = theValue
|
else:
|
||||||
return
|
self.offset = 10
|
||||||
raise ValueError("trackRadius must be an integer > 0")
|
|
||||||
self.update()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def thumbRadius(self):
|
|
||||||
return self._thumbRadius
|
|
||||||
|
|
||||||
def setThumbRadius(self, theValue):
|
|
||||||
if isinstance(theValue, int):
|
|
||||||
if theValue > 0:
|
|
||||||
self._thumbRadius = theValue
|
|
||||||
return
|
|
||||||
raise ValueError("thumbRadius must be an integer > 0")
|
|
||||||
self.update()
|
|
||||||
return
|
|
||||||
|
|
||||||
def setChecked(self, checked):
|
|
||||||
super().setChecked(checked)
|
|
||||||
self.offset = self._endOffset[checked]()
|
|
||||||
|
|
||||||
def sizeHint(self):
|
|
||||||
return QSize(
|
|
||||||
4 * self._trackRadius + 2 * self._margin,
|
|
||||||
2 * self._trackRadius + 2 * self._margin,
|
|
||||||
)
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Events
|
# Events
|
||||||
##
|
##
|
||||||
|
|
||||||
def resizeEvent(self, event):
|
def resizeEvent(self, theEvent):
|
||||||
"""Overload resize to ensure correct offset.
|
"""Overload resize to ensure correct offset.
|
||||||
"""
|
"""
|
||||||
super().resizeEvent(event)
|
super().resizeEvent(theEvent)
|
||||||
self.offset = self._endOffset[self.isChecked()]()
|
if self.isChecked():
|
||||||
|
self.offset = 30
|
||||||
|
else:
|
||||||
|
self.offset = 10
|
||||||
return
|
return
|
||||||
|
|
||||||
def paintEvent(self, event):
|
def paintEvent(self, event):
|
||||||
"""Drawing the switch itself.
|
"""Drawing the switch itself.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
qPaint = QPainter(self)
|
qPaint = QPainter(self)
|
||||||
qPaint.setRenderHint(QPainter.Antialiasing, True)
|
qPaint.setRenderHint(QPainter.Antialiasing, True)
|
||||||
qPaint.setPen(Qt.NoPen)
|
qPaint.setPen(Qt.NoPen)
|
||||||
trackOpacity = self._trackOpacity
|
|
||||||
thumbOpacity = 1.0
|
qPalette = self.palette()
|
||||||
textOpacity = 1.0
|
if self.isChecked():
|
||||||
if self.isEnabled():
|
trackBrush = qPalette.highlight()
|
||||||
trackBrush = self._trackColor[self.isChecked()]
|
thumbBrush = qPalette.highlightedText()
|
||||||
thumbBrush = self._thumbColor[self.isChecked()]
|
textColor = qPalette.highlight().color()
|
||||||
textColor = self._textColor[self.isChecked()]
|
thumbText = nwUnicode.U_CHECK
|
||||||
else:
|
else:
|
||||||
trackOpacity *= 0.8
|
trackBrush = qPalette.dark()
|
||||||
trackBrush = self.palette().shadow()
|
thumbBrush = qPalette.light()
|
||||||
thumbBrush = self.palette().mid()
|
textColor = qPalette.dark().color()
|
||||||
textColor = self.palette().shadow().color()
|
thumbText = nwUnicode.U_MULT
|
||||||
|
|
||||||
|
if self.isEnabled():
|
||||||
|
trackOpacity = 1.0
|
||||||
|
else:
|
||||||
|
trackOpacity = 0.6
|
||||||
|
|
||||||
qPaint.setBrush(trackBrush)
|
qPaint.setBrush(trackBrush)
|
||||||
qPaint.setOpacity(trackOpacity)
|
qPaint.setOpacity(trackOpacity)
|
||||||
qPaint.drawRoundedRect(
|
qPaint.drawRoundedRect(0, 0, 40, 20, 10, 10)
|
||||||
self._margin,
|
|
||||||
self._margin,
|
|
||||||
self.width() - 2*self._margin,
|
|
||||||
self.height() - 2*self._margin,
|
|
||||||
self._trackRadius,
|
|
||||||
self._trackRadius,
|
|
||||||
)
|
|
||||||
qPaint.setBrush(thumbBrush)
|
qPaint.setBrush(thumbBrush)
|
||||||
qPaint.setOpacity(thumbOpacity)
|
qPaint.drawEllipse(self.offset - 8, 2, 16, 16)
|
||||||
qPaint.drawEllipse(
|
|
||||||
self.offset - self._thumbRadius,
|
|
||||||
self._baseOffset - self._thumbRadius,
|
|
||||||
2*self._thumbRadius,
|
|
||||||
2*self._thumbRadius,
|
|
||||||
)
|
|
||||||
qPaint.setPen(textColor)
|
|
||||||
qPaint.setOpacity(textOpacity)
|
|
||||||
theFont = qPaint.font()
|
theFont = qPaint.font()
|
||||||
theFont.setPixelSize(1.5*self._thumbRadius)
|
theFont.setPixelSize(12)
|
||||||
|
qPaint.setPen(textColor)
|
||||||
qPaint.setFont(theFont)
|
qPaint.setFont(theFont)
|
||||||
qPaint.drawText(
|
qPaint.drawText(
|
||||||
QRectF(
|
QRectF(self.offset - 8, 2, 16, 16),
|
||||||
self.offset - self._thumbRadius,
|
Qt.AlignCenter, thumbText
|
||||||
self._baseOffset - self._thumbRadius,
|
|
||||||
2*self._thumbRadius,
|
|
||||||
2*self._thumbRadius,
|
|
||||||
),
|
|
||||||
Qt.AlignCenter,
|
|
||||||
self._thumbText[self.isChecked()],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def mouseReleaseEvent(self, event):
|
def mouseReleaseEvent(self, event):
|
||||||
"""Animate the switch.
|
"""Animate the switch on mouse release.
|
||||||
"""
|
"""
|
||||||
super().mouseReleaseEvent(event)
|
super().mouseReleaseEvent(event)
|
||||||
if event.button() == Qt.LeftButton:
|
if event.button() == Qt.LeftButton:
|
||||||
doAnim = QPropertyAnimation(self, b'offset', self)
|
doAnim = QPropertyAnimation(self, b'offset', self)
|
||||||
doAnim.setDuration(120)
|
doAnim.setDuration(120)
|
||||||
doAnim.setStartValue(self.offset)
|
doAnim.setStartValue(self.offset)
|
||||||
doAnim.setEndValue(self._endOffset[self.isChecked()]())
|
if self.isChecked():
|
||||||
|
doAnim.setEndValue(30)
|
||||||
|
else:
|
||||||
|
doAnim.setEndValue(10)
|
||||||
doAnim.start()
|
doAnim.start()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user