-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToggleButton.py
67 lines (53 loc) · 2.01 KB
/
ToggleButton.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ToggleButton.py
MIT License (c) Faure Systems <dev at faure dot systems>
Prop control toggle button.
"""
from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
class ToggleButton(QWidget):
publishMessage = pyqtSignal(str, str)
# __________________________________________________________________
def __init__(self,
caption_on,
caption_off,
variable,
sync_on,
sync_off,
action_on,
action_off,
topic):
super(ToggleButton, self).__init__()
self._caption_on = caption_on
self._caption_off = caption_off
self._variable = variable
self._sync_on = sync_on
self._sync_off = sync_off
self._action_on = action_on
self._action_off = action_off
self._topic = topic
self._pushButton = QPushButton(self._caption_off)
self._pushButton.setFocusPolicy(Qt.NoFocus)
main_layout = QHBoxLayout()
main_layout.setContentsMargins(0, 0, 0, 0)
main_layout.setSpacing(8)
main_layout.addWidget(self._pushButton)
self.setLayout(main_layout)
self._pushButton.released.connect(self.onPushButton)
# __________________________________________________________________
@pyqtSlot(dict)
def onDataReceived(self, variables):
if self._variable in variables:
if variables[self._variable] == self._sync_on:
self._pushButton.setText(self._caption_on)
else:
self._pushButton.setText(self._caption_off)
# __________________________________________________________________
@pyqtSlot()
def onPushButton(self):
if self._pushButton.text() == self._caption_on:
self.publishMessage.emit(self._topic, self._action_on)
else:
self.publishMessage.emit(self._topic, self._action_off)