Fix doc path
[opentx.git] / companion / src / simulation / widgets / radiowidget.cpp
blob9e4d9177ae4ad87ef603877c08c37756d4d916d8
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "radiouiaction.h"
22 #include "radiowidget.h"
24 RadioWidget::RadioWidget(QWidget * parent, Qt::WindowFlags f) :
25 QWidget(parent, f),
26 m_gridLayout(NULL),
27 m_controlWidget(NULL),
28 m_nameLabel(NULL),
29 m_action(NULL),
30 m_value(0),
31 m_flags(0),
32 m_valueReset(false),
33 m_showLabel(false),
34 m_labelText("")
36 qRegisterMetaType<RadioWidgetState>();
37 setIndex(-1);
38 setType(RADIO_WIDGET_NONE);
41 RadioWidget::RadioWidget(RadioUiAction * action, QWidget * parent, Qt::WindowFlags f) :
42 RadioWidget(parent, f)
44 setAction(action);
47 RadioWidget::RadioWidget(const QString & labelText, int value, QWidget * parent, Qt::WindowFlags f) :
48 RadioWidget(parent, f)
50 setValue(value);
51 setLabelText(labelText);
54 void RadioWidget::setIndex(const int & index)
56 m_index = index;
59 void RadioWidget::setType(const RadioWidgetType & type)
61 m_type = type;
64 void RadioWidget::setValue(const int & value)
66 if (value != m_value || m_valueReset) {
67 m_value = value;
68 m_valueReset = false;
69 emit valueChanged(value);
70 emit valueChange(m_type, m_index, value);
74 void RadioWidget::setValueQual(const RadioWidget::RadioWidgetType & type, const int & index, const int & value)
76 if (type == m_type && index == m_index)
77 setValue(value);
80 void RadioWidget::setFlags(const quint16 & flags)
82 if (flags != m_flags) {
83 m_flags = flags;
84 emit flagsChanged(flags);
88 void RadioWidget::setShowLabel(const bool show)
90 if (show != m_showLabel) {
91 m_showLabel = show;
92 addLabel();
96 void RadioWidget::setLabelText(const QString & labelText, bool showLabel)
98 m_labelText = labelText;
99 m_showLabel = showLabel;
100 addLabel();
103 void RadioWidget::setStateData(const RadioWidgetState & state)
105 if (state.type != m_type || state.index != m_index)
106 return;
108 m_valueReset = true;
109 setValue(state.value);
110 setFlags(state.flags);
113 void RadioWidget::changeVisibility(bool visible)
115 static QSizePolicy oldSP = sizePolicy();
116 setVisible(visible);
117 if (visible) {
118 setSizePolicy(oldSP);
120 else {
121 oldSP = sizePolicy();
122 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
126 void RadioWidget::setAction(RadioUiAction * action)
128 if (m_action) {
129 disconnect(m_action, 0, this, 0);
130 delete m_action;
131 m_action = NULL;
133 m_action = action;
135 if (m_action) {
136 if (m_action->getIndex() > -1)
137 setIndex(m_action->getIndex());
138 connect(m_action, &RadioUiAction::toggled, this, &RadioWidget::onActionToggled);
143 int RadioWidget::getValue() const
145 return m_value;
148 int RadioWidget::getIndex() const
150 return m_index;
153 int RadioWidget::getType() const
155 return m_type;
158 QByteArray RadioWidget::getStateData() const
160 QByteArray ba;
161 QDataStream stream(&ba, QIODevice::WriteOnly);
162 stream << getState();
163 return ba;
166 RadioWidget::RadioWidgetState RadioWidget::getState() const
168 return RadioWidgetState(quint8(m_type), qint8(m_index), qint16(m_value), quint16(m_flags));
171 RadioUiAction * RadioWidget::getAction() const
173 return m_action;
176 void RadioWidget::onActionToggled(int index, bool active)
178 emit valueChange(m_type, index, getValue());
182 void RadioWidget::addLayout()
184 if (m_gridLayout)
185 return;
187 m_gridLayout = new QGridLayout(this);
188 m_gridLayout->setContentsMargins(0, 0, 0, 0);
189 m_gridLayout->setVerticalSpacing(4);
190 m_gridLayout->setHorizontalSpacing(0);
193 void RadioWidget::addLabel()
195 addLayout();
196 if (m_nameLabel) {
197 m_gridLayout->removeWidget(m_nameLabel);
198 m_nameLabel->deleteLater();
200 if (m_showLabel && !m_labelText.isEmpty()) {
201 m_nameLabel = new QLabel(m_labelText, this);
202 m_nameLabel->setAlignment(Qt::AlignCenter);
203 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
204 m_gridLayout->addWidget(m_nameLabel, 1, 0, 1, 1, Qt::AlignTop);
205 m_gridLayout->setRowStretch(1, 0);
209 void RadioWidget::setWidget(QWidget * widget, Qt::Alignment align)
211 addLayout();
212 if (m_controlWidget) {
213 m_gridLayout->removeWidget(m_controlWidget);
214 m_controlWidget->deleteLater();
216 m_controlWidget = widget;
217 if (widget) {
218 m_gridLayout->addWidget(widget, 0, 0, 1, 1, align);
219 m_gridLayout->setRowStretch(0, 1);
223 QDataStream & operator << (QDataStream &out, const RadioWidget::RadioWidgetState & o)
225 out << o._version << o.type << o.index << o.value << o.flags;
226 return out;
229 QDataStream & operator >> (QDataStream &in, RadioWidget::RadioWidgetState & o)
231 if (o._version <= RADIO_WIDGET_STATE_VERSION)
232 in >> o._version >> o.type >> o.index >> o.value >> o.flags;
233 return in;
236 QDebug operator << (QDebug d, const RadioWidget::RadioWidgetState & o)
238 QDebugStateSaver saver(d);
239 d << "RadioWidget::RadioWidgetState: type=" << o.type << "; index=" << o.index
240 << "; value=" << o.value << "; flags=0x" << hex << o.flags;
241 return d;