-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRadioGroup.cpp
More file actions
207 lines (179 loc) · 6.3 KB
/
RadioGroup.cpp
File metadata and controls
207 lines (179 loc) · 6.3 KB
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
// Copyright (c) 2014-2021 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <Pothos/Object/Containers.hpp>
#include <QGroupBox>
#include <QRadioButton>
#include <QBoxLayout>
#include <QMouseEvent>
#include <map>
/***********************************************************************
* |PothosDoc Radio Group
*
* The radio group widget allows for selection of a value
* from a group of radio buttons.
*
* |category /Widgets
* |keywords radio buttons
*
* |param title The name of the value displayed by this widget
* |default "My Radio Value"
* |widget StringEntry()
*
* |param direction The layout direction (horizontal or vertical).
* |default "TopToBottom"
* |option [Left to Right] "LeftToRight"
* |option [Right to Left] "RightToLeft"
* |option [Top to Bottom] "TopToBottom"
* |option [Bottom to Top] "BottomToTop"
* |preview disable
*
* |param value The initial selection for this radio group.
* |default 42
*
* |param options A list of options to choose from.
* Options take the form of an Object vector
* where each entry is another Object vector
* containing a displayable name, and a value for the option.
*
* |default [["Opt0", 42], ["Opt1", "xyz"]]
*
* |mode graphWidget
* |factory /widgets/radio_group()
* |setter setTitle(title)
* |setter setDirection(direction)
* |setter setOptions(options)
* |setter setValue(value)
**********************************************************************/
class RadioGroup : public QGroupBox, public Pothos::Block
{
Q_OBJECT
public:
static Block *make(void)
{
return new RadioGroup();
}
RadioGroup(void):
_layout(new QBoxLayout(QBoxLayout::TopToBottom, this))
{
this->setStyleSheet("QGroupBox {font-weight: bold;}");
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, widget));
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, value));
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, setTitle));
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, setDirection));
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, setValue));
this->registerCall(this, POTHOS_FCN_TUPLE(RadioGroup, setOptions));
this->registerSignal("valueChanged");
qRegisterMetaType<Pothos::Object>("Pothos::Object");
qRegisterMetaType<Pothos::ObjectVector>("Pothos::ObjectVector");
}
QWidget *widget(void)
{
return this;
}
void activate(void)
{
//emit current value when design becomes active
this->handleRadioChanged(true);
}
void setTitle(const QString &title)
{
QMetaObject::invokeMethod(this, "handleSetTitle", Qt::QueuedConnection, Q_ARG(QString, title));
}
void setDirection(const QString &direction)
{
if (direction == "LeftToRight") _layout->setDirection(QBoxLayout::LeftToRight);
if (direction == "RightToLeft") _layout->setDirection(QBoxLayout::RightToLeft);
if (direction == "TopToBottom") _layout->setDirection(QBoxLayout::TopToBottom);
if (direction == "BottomToTop") _layout->setDirection(QBoxLayout::BottomToTop);
}
Pothos::Object value(void) const
{
for (auto pair : _radioToOption)
{
if (pair.first->isChecked()) return pair.second;
}
return Pothos::Object();
}
void setValue(const Pothos::Object &value)
{
QMetaObject::invokeMethod(this, "__setValue", Qt::QueuedConnection, Q_ARG(Pothos::Object, value));
}
void setOptions(const Pothos::ObjectVector &options)
{
//validate first
for (const auto &option : options)
{
if (not option.canConvert(typeid(Pothos::ObjectVector))) throw Pothos::DataFormatException("RadioGroup::setOptions()", "entry is not ObjectVector");
auto optPair = option.convert<Pothos::ObjectVector>();
if (optPair.size() != 2) throw Pothos::DataFormatException("RadioGroup::setOptions()", "entry must be ObjectVector of size == 2");
if (not optPair.at(0).canConvert(typeid(QString))) throw Pothos::DataFormatException("RadioGroup::setOptions()", "entry[0] must be a string");
}
QMetaObject::invokeMethod(this, "__setOptions", Qt::QueuedConnection, Q_ARG(Pothos::ObjectVector, options));
}
public slots:
QVariant saveState(void) const
{
for (size_t i = 0; i < _radioToOption.size(); i++)
{
if (_radioToOption[i].first->isChecked()) return int(i);
}
return QVariant();
}
void restoreState(const QVariant &state)
{
const size_t index = state.toUInt();
if (index >= _radioToOption.size()) return;
__setValue(_radioToOption[index].second);
}
protected:
void mousePressEvent(QMouseEvent *event)
{
QGroupBox::mousePressEvent(event);
event->ignore(); //allows for dragging from QGroupBox title
}
private slots:
void __setOptions(const Pothos::ObjectVector &options)
{
auto oldValue = this->value();
this->clear();
for (const auto &option : options)
{
auto optPair = option.convert<Pothos::ObjectVector>();
auto title = optPair.at(0).convert<QString>();
auto value = optPair.at(1);
auto radio = new QRadioButton(title, this);
connect(radio, &QRadioButton::toggled, this, &RadioGroup::handleRadioChanged);
_radioToOption.push_back(std::make_pair(radio, value));
_layout->addWidget(radio);
}
this->setValue(oldValue);
}
void __setValue(const Pothos::Object &value)
{
for (auto pair : _radioToOption)
{
pair.first->setChecked(pair.second.equals(value));
}
}
void handleRadioChanged(const bool toggled)
{
if (not toggled) return;
this->emitSignal("valueChanged", this->value());
}
void handleSetTitle(const QString &title)
{
QGroupBox::setTitle(title);
}
private:
void clear(void)
{
for (auto pair : _radioToOption) delete pair.first;
_radioToOption.clear();
}
QBoxLayout *_layout;
std::vector<std::pair<QRadioButton *, Pothos::Object>> _radioToOption;
};
static Pothos::BlockRegistry registerRadioGroup(
"/widgets/radio_group", &RadioGroup::make);
#include "RadioGroup.moc"