aboutsummaryrefslogtreecommitdiffstats
path: root/gr-qtgui/grc/qtgui_chooser.block.yml
blob: 4206d705f27fbd0cbfd800627bf705935d1b8b38 (plain) (blame)
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
id: variable_qtgui_chooser
label: QT GUI Chooser
flags: [ show_id, python ]

parameters:
-   id: label
    label: Label
    dtype: string
    hide: ${ ('none' if label else 'part') }
-   id: type
    label: Type
    dtype: enum
    default: int
    options: [real, int, string, raw]
    option_labels: [Float, Integer, String, Any]
    hide: part
-   id: num_opts
    label: Num Options
    dtype: enum
    default: '3'
    options: ['0', '1', '2', '3', '4', '5']
    option_labels: [List, '1', '2', '3', '4', '5']
-   id: options
    label: Options
    dtype: raw
    default: '[0, 1, 2]'
    hide: ${ ('all' if int(num_opts) else 'none') }
-   id: labels
    label: Labels
    dtype: raw
    default: '[]'
    hide: ${ ('all' if int(num_opts) else 'none') }
-   id: value
    label: Option 0 (Default)
    dtype: ${ type }
    default: '0'
    hide: ${ ('none' if int(num_opts) > 0 else 'all') }
-   id: label0
    label: Label 0
    dtype: string
    hide: ${ (int(num_opts) > 0) and (label0 and 'none' or 'part') or 'all' }
-   id: option1
    label: Option 1
    dtype: ${ type }
    default: '1'
    hide: ${ ('none' if int(num_opts) > 1 else 'all') }
-   id: label1
    label: Label 1
    dtype: string
    hide: ${ (int(num_opts) > 1) and (label1 and 'none' or 'part') or 'all' }
-   id: option2
    label: Option 2
    dtype: ${ type }
    default: '2'
    hide: ${ ('none' if int(num_opts) > 2 else 'all') }
-   id: label2
    label: Label 2
    dtype: string
    hide: ${ (int(num_opts) > 2) and (label2 and 'none' or 'part') or 'all' }
-   id: option3
    label: Option 3
    dtype: ${ type }
    default: '3'
    hide: ${ ('none' if int(num_opts) > 3 else 'all') }
-   id: label3
    label: Label 3
    dtype: string
    hide: ${ (int(num_opts) > 3) and (label3 and 'none' or 'part') or 'all' }
-   id: option4
    label: Option 4
    dtype: ${ type }
    default: '4'
    hide: ${ ('none' if int(num_opts) > 4 else 'all') }
-   id: label4
    label: Label 4
    dtype: string
    hide: ${ (int(num_opts) > 4) and (label4 and 'none' or 'part') or 'all' }
-   id: widget
    label: Widget
    dtype: enum
    default: combo_box
    options: [combo_box, radio_buttons]
    option_labels: [Combo Box, Radio Buttons]
    hide: part
-   id: orient
    label: Orientation
    dtype: enum
    default: Qt.QVBoxLayout
    options: [Qt.QHBoxLayout, Qt.QVBoxLayout]
    option_labels: [Horizontal, Vertical]
    hide: ${ ('part' if widget == 'radio_buttons' else 'all') }
-   id: gui_hint
    label: GUI Hint
    dtype: gui_hint
    hide: part
value: ${ value }

templates:
    imports: |-
        from PyQt5 import Qt
        from PyQt5.QtCore import QObject, pyqtSlot
    var_make: self.${id} = ${id} = ${value}
    callbacks:
    - self.set_${id}(${value})
    - self._${id}_callback(${id})
    make: |-
        <%
            all_options = [ value, option1, option2, option3, option4 ][:int(num_opts)]
            all_labels = [ label0, label1, label2, label3, label4 ][:int(num_opts)]
        %>\
        # Create the options list
        % if int(num_opts):
        self._${id}_options = (\
        % for ch in all_options:
        ${ch}, \
        % endfor
        )
        % else:
        self._${id}_options = ${options}
        % endif
        # Create the labels list
        % if int(num_opts):
        self._${id}_labels = (\
        % for lbl in all_labels:
        % if lbl:
        ${lbl}, \
        % else:
        self._${id}_options[${i}], \
        % endif
        % endfor
        )
        % elif labels:
        self._${id}_labels = ${labels}
        % else:
        self._${id}_map(str, self._${id}_options)
        % endif
        # Create the combo box
        % if widget == 'combo_box':
        <%
            win = 'self._%s_tool_bar'%id
        %>\
        ${win} = Qt.QToolBar(self)
        ${win}.addWidget(Qt.QLabel(${(label if (len(label) - 2 > 0) else repr(id))} + ": "))
        self._${id}_combo_box = Qt.QComboBox()
        ${win}.addWidget(self._${id}_combo_box)
        for _label in self._${id}_labels: self._${id}_combo_box.addItem(_label)
        self._${id}_callback = lambda i: Qt.QMetaObject.invokeMethod(self._${id}_combo_box, "setCurrentIndex", Qt.Q_ARG("int", self._${id}_options.index(i)))
        self._${id}_callback(self.${id})
        self._${id}_combo_box.currentIndexChanged.connect(
            lambda i: self.set_${id}(self._${id}_options[i]))
        % endif
        # Create the radio buttons
        % if widget == 'radio_buttons':
        <%
            win = 'self._%s_group_box'%id
        %>\
        ${win} = Qt.QGroupBox(${(label if (len(label) - 2 > 0) else repr(id))} + ": ")
        self._${id}_box = ${orient}()
        class variable_chooser_button_group(Qt.QButtonGroup):
            def __init__(self, parent=None):
                Qt.QButtonGroup.__init__(self, parent)
            @pyqtSlot(int)
            def updateButtonChecked(self, button_id):
                self.button(button_id).setChecked(True)
        self._${id}_button_group = variable_chooser_button_group()
        ${win}.setLayout(self._${id}_box)
        for i, _label in enumerate(self._${id}_labels):
            radio_button = Qt.QRadioButton(_label)
            self._${id}_box.addWidget(radio_button)
            self._${id}_button_group.addButton(radio_button, i)
        self._${id}_callback = lambda i: Qt.QMetaObject.invokeMethod(self._${id}_button_group, "updateButtonChecked", Qt.Q_ARG("int", self._${id}_options.index(i)))
        self._${id}_callback(self.${id})
        self._${id}_button_group.buttonClicked[int].connect(
            lambda i: self.set_${id}(self._${id}_options[i]))
        % endif
        ${gui_hint() % win}

documentation: |-
    This block creates a variable with enumerated options. The gui widget is implemented as a combo box or radio button group. Leave the label blank to use the variable id as the label.

    Choose the number of options available to your chooser. When the label is left blank, the option will be used as the label. Set the number of options to "list" to enter a single list of options and labels. When the labels is an empty list, the options will be used as the label.

    The GUI hint can be used to position the widget within the application. The hint is of the form [tab_id@tab_index]: [row, col, row_span, col_span]. Both the tab specification and the grid position are optional.

file_format: 1