diff --git a/nw/additions/qswitch.py b/nw/additions/qswitch.py index f5db98ff..9275c0c7 100644 --- a/nw/additions/qswitch.py +++ b/nw/additions/qswitch.py @@ -8,12 +8,8 @@ File History: Created: 2020-05-03 [0.4.5] - This class is based on Stack Overflow code by Stefan Scherfke, based - again on contribution by IMAN4K, published under license CC BY-SA 4.0. - 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). + The core code of this class is based on Stack Overflow example by + Stefan Scherfke: https://stackoverflow.com/a/51825815/5825851 This file is a part of novelWriter Copyright 2020, Veronica Berglyd Olsen @@ -50,55 +46,9 @@ class QSwitch(QAbstractButton): self.setCheckable(True) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) - - self._trackRadius = 10 - self._thumbRadius = 8 - - 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 + self.setFixedWidth(40) + self.setFixedHeight(20) + self._offset = 10 return @@ -111,8 +61,8 @@ class QSwitch(QAbstractButton): return self._offset @offset.setter - def offset(self, value): - self._offset = value + def offset(self, theOffset): + self._offset = theOffset self.update() return @@ -120,114 +70,85 @@ class QSwitch(QAbstractButton): # Getters and Setters ## - def trackRadius(self): - return self._trackRadius - - def setTrackRadius(self, theValue): - if isinstance(theValue, int): - if theValue > 0: - self._trackRadius = theValue - return - raise ValueError("trackRadius must be an integer > 0") - self.update() + def setChecked(self, isChecked): + """Overload setChecked to also alter the offset. + """ + super().setChecked(isChecked) + if isChecked: + self.offset = 30 + else: + self.offset = 10 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 ## - def resizeEvent(self, event): + def resizeEvent(self, theEvent): """Overload resize to ensure correct offset. """ - super().resizeEvent(event) - self.offset = self._endOffset[self.isChecked()]() + super().resizeEvent(theEvent) + if self.isChecked(): + self.offset = 30 + else: + self.offset = 10 return def paintEvent(self, event): """Drawing the switch itself. """ + qPaint = QPainter(self) qPaint.setRenderHint(QPainter.Antialiasing, True) qPaint.setPen(Qt.NoPen) - trackOpacity = self._trackOpacity - thumbOpacity = 1.0 - textOpacity = 1.0 - if self.isEnabled(): - trackBrush = self._trackColor[self.isChecked()] - thumbBrush = self._thumbColor[self.isChecked()] - textColor = self._textColor[self.isChecked()] + + qPalette = self.palette() + if self.isChecked(): + trackBrush = qPalette.highlight() + thumbBrush = qPalette.highlightedText() + textColor = qPalette.highlight().color() + thumbText = nwUnicode.U_CHECK else: - trackOpacity *= 0.8 - trackBrush = self.palette().shadow() - thumbBrush = self.palette().mid() - textColor = self.palette().shadow().color() + trackBrush = qPalette.dark() + thumbBrush = qPalette.light() + textColor = qPalette.dark().color() + thumbText = nwUnicode.U_MULT + + if self.isEnabled(): + trackOpacity = 1.0 + else: + trackOpacity = 0.6 qPaint.setBrush(trackBrush) qPaint.setOpacity(trackOpacity) - qPaint.drawRoundedRect( - self._margin, - self._margin, - self.width() - 2*self._margin, - self.height() - 2*self._margin, - self._trackRadius, - self._trackRadius, - ) + qPaint.drawRoundedRect(0, 0, 40, 20, 10, 10) + qPaint.setBrush(thumbBrush) - qPaint.setOpacity(thumbOpacity) - qPaint.drawEllipse( - self.offset - self._thumbRadius, - self._baseOffset - self._thumbRadius, - 2*self._thumbRadius, - 2*self._thumbRadius, - ) - qPaint.setPen(textColor) - qPaint.setOpacity(textOpacity) + qPaint.drawEllipse(self.offset - 8, 2, 16, 16) + theFont = qPaint.font() - theFont.setPixelSize(1.5*self._thumbRadius) + theFont.setPixelSize(12) + qPaint.setPen(textColor) qPaint.setFont(theFont) qPaint.drawText( - QRectF( - self.offset - self._thumbRadius, - self._baseOffset - self._thumbRadius, - 2*self._thumbRadius, - 2*self._thumbRadius, - ), - Qt.AlignCenter, - self._thumbText[self.isChecked()], + QRectF(self.offset - 8, 2, 16, 16), + Qt.AlignCenter, thumbText ) + return def mouseReleaseEvent(self, event): - """Animate the switch. + """Animate the switch on mouse release. """ super().mouseReleaseEvent(event) if event.button() == Qt.LeftButton: doAnim = QPropertyAnimation(self, b'offset', self) doAnim.setDuration(120) doAnim.setStartValue(self.offset) - doAnim.setEndValue(self._endOffset[self.isChecked()]()) + if self.isChecked(): + doAnim.setEndValue(30) + else: + doAnim.setEndValue(10) doAnim.start() return