summaryrefslogtreecommitdiff
path: root/gr-qtgui/python/qtgui/dialcontrol.py
blob: 2e19e182192973b9470ac4d9ec5f811b3e725b88 (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
#!/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 PyQt5.QtWidgets import QFrame, QVBoxLayout, QLabel
from PyQt5 import Qt
from PyQt5.QtCore import Qt as Qtc
from PyQt5.QtCore import QSize
from gnuradio import gr
import pmt

class LabeledDialControl(QFrame):
    def __init__(self, lbl='', parent=None, minimum=0, maximum=100, defaultvalue=0,
                 backgroundColor='default', changedCallback=None,
                 minsize=100, isFloat=False, scaleFactor=1, showvalue=False,
                 outputmsgname='value'):
        QFrame.__init__(self, parent)
        self.numberControl = DialControl(minimum, maximum, defaultvalue, backgroundColor,
                                         self.valChanged, changedCallback, minsize)

        layout = QVBoxLayout()

        self.outputmsgname = outputmsgname
        self.showvalue = showvalue
        self.isFloat = isFloat
        self.scaleFactor = scaleFactor
        self.lbl = lbl
        self.lblcontrol = QLabel(lbl, self)
        self.lblcontrol.setAlignment(Qtc.AlignCenter)

        if self.showvalue:
            textstr = self.buildTextStr(defaultvalue*self.scaleFactor)
            self.lblcontrol.setText(textstr)

        if len or self.showvalue:
            self.hasLabel = True
            layout.addWidget(self.lblcontrol)
        else:
            self.hasLabel = False

        layout.addWidget(self.numberControl)

        layout.setAlignment(Qtc.AlignCenter)
        self.setLayout(layout)
        self.show()

    def buildTextStr(self, new_value):
        textstr = ""
        if self.lbl:
            textstr = self.lbl + " - "

        if self.isFloat:
            textstr += "%.2f" % (new_value)
        else:
            textstr += str(new_value)

        return textstr

    def valChanged(self, new_value):
        if not self.showvalue:
            return

        if int(self.scaleFactor) != 1:
            new_value = new_value * self.scaleFactor

        textstr = self.buildTextStr(new_value)
        self.lblcontrol.setText(textstr)

class DialControl(Qt.QDial):
    def __init__(self, minimum=0, maximum=100, defaultvalue=0, backgroundColor='default',
                 lablelCallback=None, changedCallback=None, minsize=100):
        Qt.QDial.__init__(self)

        if backgroundColor != "default":
            self.setStyleSheet("background-color: " + backgroundColor + ";")

        self.minsize = minsize
        self.changedCallback = changedCallback
        self.lablelCallback = lablelCallback
        super().setMinimum(minimum)
        super().setMaximum(maximum)
        super().setValue(defaultvalue)
        super().valueChanged.connect(self.sliderMoved)

    def minimumSizeHint(self):
        return QSize(self.minsize, self.minsize)

    def sliderMoved(self):
        if self.changedCallback is not None:
            self.changedCallback(self.value())

        if self.lablelCallback is not None:
            self.lablelCallback(self.value())

class GrDialControl(gr.sync_block, LabeledDialControl):
    """
    This block creates a dial control. The control does control a
    variable which can be used for other items.  Leave the label
    blank to use the variable id as the label. The block also
    creates an optional message with the control value that
    can be used in message-based applications.

    Note: Dials only produce integer values, so the scale factor
    can be used with the min/max to adjust the output value to
    the desired range.  Think of the min/max as the increments,
    and the scale factor as the adjustment to get the values you want.
    """
    def __init__(self, lbl, parent, minimum, maximum, defaultvalue, backgroundColor='default',
                 varCallback=None, isFloat=False,
                 scaleFactor=1, minsize=100, showvalue=False, outputmsgname='value'):
        gr.sync_block.__init__(self, name="GrDialControl", in_sig=None, out_sig=None)
        LabeledDialControl.__init__(self, lbl, parent, minimum, maximum, defaultvalue,
                                    backgroundColor, self.valueChanged, minsize, isFloat,
                                    scaleFactor, showvalue)

        self.outputmsgname = outputmsgname
        self.varCallback = varCallback
        self.scaleFactor = scaleFactor
        self.isFloat = isFloat
        self.message_port_register_out(pmt.intern("value"))

    def valueChanged(self, new_value):
        if int(self.scaleFactor) != 1:
            new_value = new_value * self.scaleFactor

        if self.varCallback is not None:
            self.varCallback(new_value)

        if self.isFloat:
            self.message_port_pub(pmt.intern("value"), pmt.cons(pmt.intern(self.outputmsgname),
                                                                pmt.from_double(new_value)))
        else:
            self.message_port_pub(pmt.intern("value"), pmt.cons(pmt.intern(self.outputmsgname),
                                                                pmt.from_long(new_value)))