-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ Submodules | |
|
||
msl.qt.loop_until_abort | ||
msl.qt.prompt | ||
|
||
msl.qt.toggle_switch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
msl\.qt\.toggle_switch module | ||
============================= | ||
|
||
.. automodule:: msl.qt.toggle_switch | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Example showing how to use the :obj:`ToggleSwitch <msl.qt.toggle_switch.ToggleSwitch>`. | ||
""" | ||
from PyQt5 import QtWidgets | ||
|
||
from msl.qt import application, ToggleSwitch | ||
|
||
|
||
def print_state(checked): | ||
print('The switch is {}'.format('on' if checked else 'off')) | ||
|
||
|
||
def main(): | ||
app = application() | ||
window = QtWidgets.QWidget() | ||
window.setWindowTitle('Toggle Switch Example') | ||
hbox = QtWidgets.QHBoxLayout() | ||
ts = ToggleSwitch(window) | ||
ts.toggled.connect(print_state) | ||
hbox.addWidget(ts) | ||
window.setLayout(hbox) | ||
window.show() | ||
app.exec_() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
A toggle switch QWidget. | ||
""" | ||
from PyQt5 import QtWidgets, QtCore, QtGui | ||
|
||
|
||
class ToggleSwitch(QtWidgets.QAbstractButton): | ||
|
||
def __init__(self, parent=None, checked_color='#009688', unchecked_color='#B4B4B4'): | ||
"""Constructs a toggle switch, |switch| | ||
.. |switch| image:: ../../docs/_static/toggle_switch.gif | ||
:scale: 50 % | ||
.. literalinclude:: ../../msl/examples/qt/toggle_switch.py | ||
Parameters | ||
---------- | ||
parent : :obj:`QWidget` | ||
The parent :obj:`QWidget`. | ||
checked_color : :obj:`str` or :obj:`QColor` | ||
The color to draw the switch when it is in the checked state. | ||
unchecked_color : :obj:`str` or :obj:`QColor` | ||
The color to draw the switch when it is **not** in the checked state. | ||
""" | ||
super(ToggleSwitch, self).__init__(parent) | ||
|
||
self._pad = 4 | ||
self._checked_brush = QtGui.QBrush(QtGui.QColor(checked_color)) | ||
self._unchecked_brush = QtGui.QBrush(QtGui.QColor(unchecked_color)) | ||
self.setCheckable(True) | ||
|
||
def paintEvent(self, event): | ||
"""Overrides the paintEvent method.""" | ||
diameter = self.height() - 2 * self._pad | ||
radius = diameter * 0.5 | ||
|
||
if self.isChecked(): | ||
brush = self._checked_brush | ||
x = self.width() - diameter - self._pad | ||
opacity = 0.3 | ||
else: | ||
brush = self._unchecked_brush | ||
x = self._pad | ||
opacity = 0.5 | ||
|
||
p = QtGui.QPainter(self) | ||
p.setPen(QtCore.Qt.NoPen) | ||
p.setRenderHint(QtGui.QPainter.Antialiasing, True) | ||
ellipse = QtCore.QRect(x, self._pad, diameter, diameter) | ||
w = max(diameter, self.width() - 2 * self._pad) | ||
rect = QtCore.QRect(self._pad, self._pad, w, diameter) | ||
if self.isEnabled(): | ||
p.setBrush(brush) | ||
p.setOpacity(opacity) | ||
p.drawRoundedRect(rect, radius, radius) | ||
p.setOpacity(1.0) | ||
p.drawEllipse(ellipse) | ||
else: | ||
p.setBrush(QtCore.Qt.black) | ||
p.setOpacity(0.12) | ||
p.drawRoundedRect(rect, radius, radius) | ||
p.setOpacity(1.0) | ||
p.setBrush(QtGui.QColor('#BDBDBD')) | ||
p.drawEllipse(ellipse) | ||
|
||
def enterEvent(self, event): | ||
"""Overrides the enterEvent method.""" | ||
self.setCursor(QtCore.Qt.PointingHandCursor) | ||
|
||
def sizeHint(self): | ||
"""Overrides the sizeHint method.""" | ||
return QtCore.QSize(2 * (self.height() + self._pad), self.height() + 2 * self._pad) |