summaryrefslogtreecommitdiff
path: root/gr-qtgui/python/qtgui/msgpushbutton.py
blob: 4865cc1eb9daeef38bb1d3624c3c7876fa475e5c (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
#!/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 import Qt
from gnuradio import gr
import pmt


class MsgPushButton(gr.sync_block, Qt.QPushButton):
    """
    This block creates a variable push button that creates a message
    when clicked. Leave the label blank to use the variable id as
    the label. You can define both the output message pmt name as
    well as the value and value type.
    """

    def __init__(self, lbl, msgName, msgValue, relBackColor, relFontColor):
        gr.sync_block.__init__(self, name="MsgPushButton",
                               in_sig=None, out_sig=None)
        Qt.QPushButton.__init__(self, lbl)

        self.lbl = lbl
        self.msgName = msgName
        self.msgValue = msgValue

        styleStr = ""
        if relBackColor != 'default':
            styleStr = "background-color: " + relBackColor + "; "

        if relFontColor:
            styleStr += "color: " + relFontColor + "; "

        self.setStyleSheet(styleStr)

        self.clicked[bool].connect(self.onBtnClicked)

        self.message_port_register_out(pmt.intern("pressed"))

    def onBtnClicked(self, pressed):
        if type(self.msgValue) == int:
            self.message_port_pub(pmt.intern("pressed"),
                                  pmt.cons(pmt.intern(self.msgName), pmt.from_long(self.msgValue)))
        elif type(self.msgValue) == float:
            self.message_port_pub(pmt.intern("pressed"),
                                  pmt.cons(pmt.intern(self.msgName), pmt.from_double(self.msgValue)))
        elif type(self.msgValue) == str:
            self.message_port_pub(pmt.intern("pressed"),
                                  pmt.cons(pmt.intern(self.msgName), pmt.intern(self.msgValue)))
        elif type(self.msgValue) == bool:
            self.message_port_pub(pmt.intern("pressed"),
                                  pmt.cons(pmt.intern(self.msgName), pmt.from_bool(self.msgValue)))