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 #ifndef _RADIOSWITCHWIDGET_H_
22 #define _RADIOSWITCHWIDGET_H_
24 #include "radiowidget.h"
26 #include "simulator.h"
30 #include <QToolButton>
32 class RadioSwitchWidget
: public RadioWidget
38 explicit RadioSwitchWidget(Board::SwitchType type
= Board::SWITCH_3POS
, QWidget
* parent
= Q_NULLPTR
, Qt::WindowFlags f
= Qt::WindowFlags()) :
39 RadioWidget(parent
, f
),
44 explicit RadioSwitchWidget(Board::SwitchType type
, const QString
& labelText
, int value
= 0, QWidget
* parent
= Q_NULLPTR
, Qt::WindowFlags f
= Qt::WindowFlags()) :
45 RadioWidget(labelText
, value
, parent
, f
),
53 m_type
= RADIO_WIDGET_SWITCH
;
55 m_slider
= new QSlider();
56 m_slider
->setOrientation(Qt::Vertical
);
57 m_slider
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Preferred
);
58 m_slider
->setInvertedAppearance(true);
59 m_slider
->setInvertedControls(true);
60 m_slider
->setTickPosition(QSlider::TicksBothSides
);
61 m_slider
->setPageStep(1);
62 m_slider
->setMinimum((swType
== Board::SWITCH_3POS
? -1 : 0));
63 m_slider
->setMaximum(1);
64 m_slider
->setTickInterval(1);
65 m_slider
->setSingleStep(1);
66 m_slider
->setValue(m_value
);
68 if (swType
== Board::SWITCH_TOGGLE
) {
69 QToolButton
* lockBtn
= new QToolButton(this);
70 lockBtn
->setIcon(Simulator::SimulatorIcon("toggle_lock"));
71 lockBtn
->setIconSize(QSize(8, 8));
72 lockBtn
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
73 lockBtn
->setToolButtonStyle(Qt::ToolButtonIconOnly
);
74 lockBtn
->setAutoRaise(true);
75 lockBtn
->setCheckable(true);
76 lockBtn
->setToolTip(tr("Latch/unlatch the momentary switch."));
78 QWidget
* container
= new QWidget(this);
79 container
->setFixedHeight(56);
80 container
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
81 QVBoxLayout
* cl
= new QVBoxLayout(container
);
82 cl
->setContentsMargins(0, 0, 0, 0);
84 cl
->addWidget(m_slider
, 1, Qt::AlignHCenter
);
85 cl
->addWidget(lockBtn
, 0, Qt::AlignHCenter
);
89 connect(lockBtn
, &QToolButton::toggled
, this, &RadioSwitchWidget::setToggleLocked
);
90 connect(m_slider
, &QSlider::sliderReleased
, this, &RadioSwitchWidget::onMomentaryReleased
);
91 connect(m_slider
, &QSlider::valueChanged
, this, &RadioSwitchWidget::onMomentaryReleased
);
92 connect(this, &RadioWidget::flagsChanged
, lockBtn
, &QToolButton::setChecked
);
95 m_slider
->setFixedHeight(56);
99 connect(m_slider
, &QSlider::valueChanged
, this, &RadioWidget::setValue
);
100 connect(this, &RadioWidget::valueChanged
, m_slider
, &QSlider::setValue
);
106 int val
= m_slider
->value() - (swType
== Board::SWITCH_3POS
|| m_slider
->value() == 1 ? 0 : 1);
110 void setToggleLocked(bool lock
)
112 if (m_flags
!= (quint16
)lock
) {
113 setFlags((quint16
)lock
);
125 void onMomentaryReleased()
127 if (!m_flags
&& !m_slider
->isSliderDown())
128 QTimer::singleShot(500, this, SLOT(onMomentaryTimeout()));
131 void onMomentaryTimeout()
138 Board::SwitchType swType
;
145 #endif // _RADIOSWITCHWIDGET_H_