summaryrefslogtreecommitdiff
path: root/gr-qtgui/python/qtgui/toggleswitch.py
blob: a869e574de4969dce760da43a1cc6149aae23b30 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#

from gnuradio import gr
import pmt

from PyQt5.QtWidgets import QFrame, QHBoxLayout, QVBoxLayout, QLabel
from PyQt5.QtGui import QPainter, QBrush, QColor, QPen, QFontMetricsF
from PyQt5.QtCore import Qt as Qtc
from PyQt5.QtCore import QRect

class LabeledToggleSwitch(QFrame):
    # Positions: 1 = above, 2=below, 3=left, 4=right
    def __init__(self, lbl='', onColor='green', offColor='red', initialState=False,
                 maxSize=50, position=1, parent=None, callback=None, alignment=1, valignment=1):
        QFrame.__init__(self, parent)
        self.numberControl = ToggleSwitch(onColor, offColor, initialState, maxSize,
                                           parent, callback)

        if position < 3:
            layout = QVBoxLayout()
        else:
            layout = QHBoxLayout()

        self.lbl = lbl
        self.lblcontrol = QLabel(lbl, self)

        if position == 3: # left of switch
            self.lblcontrol.setAlignment(Qtc.AlignRight)
        elif position == 4: # right of switch
            self.lblcontrol.setAlignment(Qtc.AlignLeft)
        else:
            # Above or below
            self.lblcontrol.setAlignment(Qtc.AlignCenter)

        # add top or left
        if len:
            if position == 1 or position == 3:
                layout.addWidget(self.lblcontrol)

        layout.addWidget(self.numberControl)

        # Add bottom or right
        if len:
            if position == 2 or position == 4:
                layout.addWidget(self.lblcontrol)

        if alignment == 1:
            halign = Qtc.AlignCenter
        elif alignment == 2:
            halign = Qtc.AlignLeft
        else:
            halign = Qtc.AlignRight

        if valignment == 1:
            valign = Qtc.AlignVCenter
        elif valignment == 2:
            valign = Qtc.AlignTop
        else:
            valign = Qtc.AlignBottom

        layout.setAlignment(halign | valign)

        self.setLayout(layout)

        textfont = self.lblcontrol.font()
        metrics = QFontMetricsF(textfont)

        maxWidth = max((maxSize+4), (maxSize*2 + metrics.width(lbl)))
        maxHeight = max((maxSize/2+4), (maxSize/2 + metrics.height()+2))

        self.setMinimumSize(int(maxWidth), int(maxHeight))

        self.show()

    def setState(self, on_off):
        self.numberControl.setState(on_off)

class ToggleSwitch(QFrame):
    def __init__(self, onColor='green', offColor='red', initialState=False, maxSize=50,
                 parent=None, callback=None):
        QFrame.__init__(self, parent)

        self.maxSize = maxSize
        self.curState = initialState
        self.onColor = QColor(onColor)
        self.offColor = QColor(offColor)
        self.callback = callback
        self.setMinimumSize(maxSize, maxSize/2)
        self.setMaximumSize(maxSize, maxSize/2)

    def setState(self, on_off):
        self.curState = on_off
        if self.callback is not None:
            self.callback(on_off)

        super().update()

    def paintEvent(self, event):
        super().paintEvent(event)

        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        size = self.size()
        brush = QBrush()

        center_x = size.width()/2

        if self.curState:
            brush.setColor(self.onColor)
            painter.setPen(QPen(self.onColor, 0))
        else:
            brush.setColor(self.offColor)
            painter.setPen(QPen(self.offColor, 0))

        brush.setStyle(Qtc.SolidPattern)
        painter.setBrush(brush)

        # Draw the switch background
        centerRect = QRect(size.width()/4, 0, size.width()/2-4, size.height())
        painter.drawRect(centerRect)
        painter.drawEllipse(0, 0, size.height(), size.height())
        painter.drawEllipse(size.width()/2, 0, size.height(), size.height())

        # Draw the switch itself
        brush.setColor(QColor('white'))
        painter.setBrush(brush)
        painter.setPen(QPen(QColor('white'), 0))
        if self.curState:
            painter.drawEllipse(center_x+2, 2, size.height() - 4, size.height() - 4)
        else:
            painter.drawEllipse(2, 2, size.height() - 4, size.height() - 4)

    def mousePressEvent(self, event):
        if event.x() <= self.size().width()/2:
            self.setState(False)
        else:
            self.setState(True)

        super().update()

class GrToggleSwitch(gr.sync_block, LabeledToggleSwitch):
    """
    This block creates a modern toggle switch. The variable will take
    on one value or the other as set in the dialog. This button will
    also produce a state message matching the set values.
    """
    def __init__(self, callback, lbl, pressedReleasedDict, initialState=False,
                 onColor='green', offColor='silver', position=3, maxSize=50,
                 alignment=1, valignment=1, parent=None, outputmsgname='value'):
        gr.sync_block.__init__(self, name="ToggleSwitch", in_sig=None, out_sig=None)
        LabeledToggleSwitch.__init__(self, lbl, onColor, offColor, initialState,
                                     maxSize, position, parent, self.notifyUpdate,
                                     alignment, valignment)

        self.outputmsgname = outputmsgname
        self.pressReleasedDict = pressedReleasedDict
        self.callback = callback
        self.message_port_register_out(pmt.intern("state"))

    def notifyUpdate(self, new_val):
        if self.callback is not None:
            if new_val:
                self.callback(self.pressReleasedDict['Pressed'])
            else:
                self.callback(self.pressReleasedDict['Released'])

        if new_val:
            if type(self.pressReleasedDict['Pressed']) == bool:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_bool(self.pressReleasedDict['Pressed'])))
            elif type(self.pressReleasedDict['Pressed']) == int:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_long(self.pressReleasedDict['Pressed'])))
            elif type(self.pressReleasedDict['Pressed']) == float:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_double(self.pressReleasedDict['Pressed'])))
            else:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.intern(self.pressReleasedDict['Pressed'])))
        else:
            if type(self.pressReleasedDict['Released']) == bool:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_bool(self.pressReleasedDict['Released'])))
            elif type(self.pressReleasedDict['Released']) == int:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_long(self.pressReleasedDict['Released'])))
            elif type(self.pressReleasedDict['Released']) == float:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.from_double(self.pressReleasedDict['Released'])))
            else:
                self.message_port_pub(pmt.intern("state"),
                            pmt.cons(pmt.intern(self.outputmsgname),
                            pmt.intern(self.pressReleasedDict['Released'])))