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
) :
27 m_controlWidget(NULL
),
36 qRegisterMetaType
<RadioWidgetState
>();
38 setType(RADIO_WIDGET_NONE
);
41 RadioWidget::RadioWidget(RadioUiAction
* action
, QWidget
* parent
, Qt::WindowFlags f
) :
42 RadioWidget(parent
, f
)
47 RadioWidget::RadioWidget(const QString
& labelText
, int value
, QWidget
* parent
, Qt::WindowFlags f
) :
48 RadioWidget(parent
, f
)
51 setLabelText(labelText
);
54 void RadioWidget::setIndex(const int & index
)
59 void RadioWidget::setType(const RadioWidgetType
& type
)
64 void RadioWidget::setValue(const int & value
)
66 if (value
!= m_value
|| m_valueReset
) {
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
)
80 void RadioWidget::setFlags(const quint16
& flags
)
82 if (flags
!= m_flags
) {
84 emit
flagsChanged(flags
);
88 void RadioWidget::setShowLabel(const bool show
)
90 if (show
!= m_showLabel
) {
96 void RadioWidget::setLabelText(const QString
& labelText
, bool showLabel
)
98 m_labelText
= labelText
;
99 m_showLabel
= showLabel
;
103 void RadioWidget::setStateData(const RadioWidgetState
& state
)
105 if (state
.type
!= m_type
|| state
.index
!= m_index
)
109 setValue(state
.value
);
110 setFlags(state
.flags
);
113 void RadioWidget::changeVisibility(bool visible
)
115 static QSizePolicy oldSP
= sizePolicy();
118 setSizePolicy(oldSP
);
121 oldSP
= sizePolicy();
122 setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Ignored
);
126 void RadioWidget::setAction(RadioUiAction
* action
)
129 disconnect(m_action
, 0, this, 0);
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
148 int RadioWidget::getIndex() const
153 int RadioWidget::getType() const
158 QByteArray
RadioWidget::getStateData() const
161 QDataStream
stream(&ba
, QIODevice::WriteOnly
);
162 stream
<< getState();
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
176 void RadioWidget::onActionToggled(int index
, bool active
)
178 emit
valueChange(m_type
, index
, getValue());
182 void RadioWidget::addLayout()
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()
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
)
212 if (m_controlWidget
) {
213 m_gridLayout
->removeWidget(m_controlWidget
);
214 m_controlWidget
->deleteLater();
216 m_controlWidget
= 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
;
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
;
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
;