[Simulator] Asynchronous SimulatorInterface & a few new features. (#4738)
[opentx.git] / companion / src / simulation / widgets / radioknobwidget.h
blobd66eddf186a07bbcdf1d25fa6505046cf62aa38f
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 #ifndef _RADIOKNOBWIDGET_H_
22 #define _RADIOKNOBWIDGET_H_
24 #include "radiowidget.h"
25 #include "boards.h"
27 #include <QDial>
28 #include <QMouseEvent>
29 #include <QToolTip>
30 #include <math.h>
32 class RadioKnobWidget : public RadioWidget
34 Q_OBJECT
36 public:
38 explicit RadioKnobWidget(Board::PotType type = Board::POT_WITH_DETENT, QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
39 RadioWidget(parent, f),
40 m_potType(type)
42 init();
44 explicit RadioKnobWidget(Board::PotType type, const QString & labelText, int value = 0, QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
45 RadioWidget(labelText, value, parent, f),
46 m_potType(type)
48 init();
51 void init()
53 m_type = RADIO_WIDGET_KNOB;
55 KnobWidget * pot = new KnobWidget(m_potType, this);
56 pot->setValue(m_value);
58 connect(pot, &KnobWidget::valueChanged, this, &RadioWidget::setValue);
59 connect(this, &RadioWidget::valueChanged, pot, &KnobWidget::setValue);
61 setWidget(pot);
64 private:
66 Board::PotType m_potType;
68 class KnobWidget : public QDial
70 public:
72 explicit KnobWidget(Board::PotType type, QWidget * parent = 0):
73 QDial(parent)
75 m_toolTip = tr("<p>Value (input): <b>%1</b></p>");
77 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
78 setFixedSize(QSize(42, 42));
79 setNotchesVisible(true);
80 if (type == Board::POT_MULTIPOS_SWITCH) {
81 m_stepSize = 2048 / 5;
82 setMinimum(0);
83 setMaximum(2048);
84 // this is a bit of a hack to get the notch markers to display correctly
85 // the actual notches/value are constrained in setValue()
86 setSingleStep(m_stepSize / 10);
87 setPageStep(m_stepSize);
88 setNotchTarget(5.7);
90 else {
91 m_stepSize = 1;
92 m_toolTip.append(tr("Right-double-click to reset to center."));
93 setMinimum(-1024);
94 setMaximum(1024);
95 setPageStep(128);
96 setNotchTarget(64);
100 void setValue(int value)
102 if (m_stepSize > 1) {
103 value = value / m_stepSize * m_stepSize;
105 QDial::setValue(value);
109 bool event(QEvent *event)
111 if (event->type() == QEvent::ToolTip) {
112 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(event);
113 if (helpEvent) {
114 QToolTip::showText(helpEvent->globalPos(), m_toolTip.arg(this->value()));
115 return true;
118 return QWidget::event(event);
121 void mousePressEvent(QMouseEvent * event)
123 if (m_stepSize == 1 && event->button() == Qt::RightButton && event->type() == QEvent::MouseButtonDblClick) {
124 setValue(0);
125 event->accept();
126 return;
128 QDial::mousePressEvent(event);
131 void wheelEvent(QWheelEvent * event)
133 if (event->angleDelta().isNull())
134 return;
136 if (m_stepSize > 1) {
137 int numSteps = event->angleDelta().y() / 8 / 15 * m_stepSize; // one step per 15deg
138 setValue(value() + numSteps);
139 event->accept();
140 return;
142 QDial::wheelEvent(event);
145 quint16 m_stepSize;
146 QString m_toolTip;
152 #endif // _RADIOKNOBWIDGET_H_