File: toggle_switch.py

package info (click to toggle)
superqt 0.7.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,320 kB
  • sloc: python: 9,108; makefile: 16; sh: 12
file content (67 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download | duplicates (2)
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
from qtpy import QtCore, QtGui
from qtpy.QtWidgets import QApplication, QStyle, QVBoxLayout, QWidget

from superqt import QToggleSwitch
from superqt.switch import QStyleOptionToggleSwitch

QSS_EXAMPLE = """
QToggleSwitch {
    qproperty-onColor: red;
    qproperty-handleSize: 12;
    qproperty-switchWidth: 30;
    qproperty-switchHeight: 16;
}
"""


class QRectangleToggleSwitch(QToggleSwitch):
    """A rectangle shaped toggle switch."""

    def drawGroove(
        self,
        painter: QtGui.QPainter,
        rect: QtCore.QRectF,
        option: QStyleOptionToggleSwitch,
    ) -> None:
        """Draw the groove of the switch."""
        painter.setPen(QtCore.Qt.PenStyle.NoPen)
        is_checked = option.state & QStyle.StateFlag.State_On
        painter.setBrush(option.on_color if is_checked else option.off_color)
        painter.setOpacity(0.8)
        painter.drawRect(rect)

    def drawHandle(self, painter, rect, option):
        """Draw the handle of the switch."""
        painter.drawRect(rect)


class QToggleSwitchWithText(QToggleSwitch):
    """A toggle switch with text on the handle."""

    def drawHandle(
        self,
        painter: QtGui.QPainter,
        rect: QtCore.QRectF,
        option: QStyleOptionToggleSwitch,
    ) -> None:
        super().drawHandle(painter, rect, option)

        text = "ON" if option.state & QStyle.StateFlag.State_On else "OFF"
        painter.setPen(QtGui.QPen(QtGui.QColor("black")))
        font = painter.font()
        font.setPointSize(5)
        painter.setFont(font)
        painter.drawText(rect, QtCore.Qt.AlignmentFlag.AlignCenter, text)


app = QApplication([])
widget = QWidget()
layout = QVBoxLayout(widget)
layout.addWidget(QToggleSwitch("original"))
switch_styled = QToggleSwitch("stylesheet")
switch_styled.setStyleSheet(QSS_EXAMPLE)
layout.addWidget(switch_styled)
layout.addWidget(QRectangleToggleSwitch("rectangle"))
layout.addWidget(QToggleSwitchWithText("with text"))
widget.show()
app.exec()