From 9a4236dbfe32d7cc3fdf35c366d71f739a277640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moln=C3=A1r=20Csaba?= Date: Tue, 2 Apr 2019 21:07:26 +0200 Subject: [PATCH 1/2] Added Slider control with adjustable minimum and maximum values, adjustable knob color and simultaneous output into up to 6 items of an array. Can be used as a throttle knob. --- widgetui/panelitems/slider.cpp | 252 +++++++++++++++++++++++++++++++++ widgetui/panelitems/slider.h | 58 ++++++++ widgetui/widgetui.pro | 6 +- 3 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 widgetui/panelitems/slider.cpp create mode 100644 widgetui/panelitems/slider.h diff --git a/widgetui/panelitems/slider.cpp b/widgetui/panelitems/slider.cpp new file mode 100644 index 0000000..c0c36e8 --- /dev/null +++ b/widgetui/panelitems/slider.cpp @@ -0,0 +1,252 @@ +#include "slider.h" +#include +#include +#include +#include "../util/console.h" + +REGISTER_WITH_PANEL_ITEM_FACTORY(Slider,"adjustment/slider_vertical"); + +Slider::Slider(ExtPlanePanel *panel, ExtPlaneConnection *conn) : + PanelItem(panel, PanelItemTypeSwitch, PanelItemShapeCircular), + _client(this, typeName(), conn) { + conn->registerClient(&_client); + connect(&_client, SIGNAL(refChanged(QString,double)), this, SLOT(valueChanged(QString,double))); + _value = 1; + _change = 1; + _minimum = 1; + _maximum = 100; + _valueStartPoint = _minimum; + _knobcolor = Qt::red; + _index1set = true; + _index2set = false; + _index3set = false; + _index4set = false; + _index5set = false; + _index6set = false; + _ref = 0; + setSize(70,200); +} + +void Slider::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + setupPainter(painter); + float myHeight = height(); + float myWidth = width(); + painter->setClipRect(0, 0, myWidth, myHeight); + + // Slider + painter->save(); + painter->setPen(QPen(Qt::darkGray, (width() * 0.4f))); + painter->translate(width() / 2, 0); + painter->drawLine(0, 0, 0, height()); + painter->restore(); + + // Slider knob + float knobWidth = width(); // myWidth * 0.30f; + float knobHeight = myHeight * 0.20f; + float travel = myHeight - knobHeight; + int ypos = travel - (travel * ((_value - _minimum) / (_maximum - _minimum))); + painter->save(); + painter->setPen(_knobcolor); + painter->setBrush(_knobcolor); + painter->translate((width() / 2)-(knobWidth/2), 0); +// Uncomment this if you want a rectangular knob +// painter->drawRect(0, ypos, knobWidth, knobHeight); + painter->drawEllipse(0, ypos, knobWidth, knobHeight); + painter->restore(); + + PanelItem::paint(painter, option, widget); +} + +void Slider::storeSettings(QSettings &settings) { + PanelItem::storeSettings(settings); + settings.setValue("dataref", _refname); + settings.setValue("change", _change); + settings.setValue("minimum", _minimum); + settings.setValue("maximum", _maximum); + settings.setValue("knobcolor", _knobcolor.rgba()); + settings.setValue("index1set", _index1set); + settings.setValue("index2set", _index2set); + settings.setValue("index3set", _index3set); + settings.setValue("index4set", _index4set); + settings.setValue("index5set", _index5set); + settings.setValue("index6set", _index6set); +} + +void Slider::loadSettings(QSettings &settings) { + PanelItem::loadSettings(settings); + setRef(settings.value("dataref", "").toString()); + setChange(settings.value("change", 1).toDouble()); + setMinimum(settings.value("minimum", 1).toDouble()); + setMaximum(settings.value("maximum", 100).toDouble()); + + QColor knobcolor; + knobcolor.setRgba(settings.value("knobcolor").toUInt()); + setKnobColor(knobcolor); + + setIndex1Set(settings.value("index1set", true).toBool()); + setIndex2Set(settings.value("index2set", true).toBool()); + setIndex3Set(settings.value("index3set", true).toBool()); + setIndex4Set(settings.value("index4set", true).toBool()); + setIndex5Set(settings.value("index5set", true).toBool()); + setIndex6Set(settings.value("index6set", true).toBool()); + _valueStartPoint = _minimum; +} + +void Slider::createSettings(QGridLayout *layout) { + layout->addWidget(new QLabel("Dataref", layout->parentWidget())); + QLineEdit *refEdit = new QLineEdit(_refname, layout->parentWidget()); + connect(refEdit, SIGNAL(textChanged(QString)), this, SLOT(setRef(QString))); + layout->addWidget(refEdit); + + layout->addWidget(new QLabel("Change amount", layout->parentWidget())); + QLineEdit *changeEdit = new QLineEdit(QString::number(_change), layout->parentWidget()); + connect(changeEdit, SIGNAL(textChanged(QString)), this, SLOT(setChangeString(QString))); + layout->addWidget(changeEdit); + + createNumberInputSetting(layout,"Minimum", _minimum, SLOT(setMinimum(float))); + + createNumberInputSetting(layout,"Maximum", _maximum, SLOT(setMaximum(float))); + + createColorSetting(layout, "Knob Color", _knobcolor, SLOT(setKnobColor(QColor)) ); + + createCheckboxSetting( layout, "Write Array index 1", _index1set, SLOT(setIndex1Set(bool))); + createCheckboxSetting( layout, "Write Array index 2", _index2set, SLOT(setIndex2Set(bool))); + createCheckboxSetting( layout, "Write Array index 3", _index3set, SLOT(setIndex3Set(bool))); + createCheckboxSetting( layout, "Write Array index 4", _index4set, SLOT(setIndex4Set(bool))); + createCheckboxSetting( layout, "Write Array index 5", _index5set, SLOT(setIndex5Set(bool))); + createCheckboxSetting( layout, "Write Array index 6", _index6set, SLOT(setIndex6Set(bool))); + +} + +void Slider::applySettings() { + if(_ref) { + _ref->unsubscribe(); + _ref = 0; + } + if(!_refname.isEmpty()) + _ref = _client.subscribeDataRef(_refname, 0); +} + +void Slider::setChange(double ch) { + _change = ch; + update(); +} + +void Slider::setChangeString(QString ch) { + bool ok = false; + double chDouble = ch.toDouble(&ok); + if(ok) + setChange(chDouble); +} + +void Slider::setMinimum(float ch) { + _minimum = ch; + if (_minimum > _maximum) { + _minimum = _maximum; + } + update(); +} + +void Slider::setMaximum(float ch) { + _maximum = ch; + if (_maximum < _minimum) { + _maximum = _minimum; + } + update(); +} + +void Slider::setKnobColor(QColor ch) { + _knobcolor = ch; +} + +void Slider::setIndex1Set( bool input ) { + _index1set = input; +} + +void Slider::setIndex2Set( bool input ) { + _index2set = input; +} + +void Slider::setIndex3Set( bool input ) { + _index3set = input; +} + +void Slider::setIndex4Set( bool input ) { + _index4set = input; +} + +void Slider::setIndex5Set( bool input ) { + _index5set = input; +} + +void Slider::setIndex6Set( bool input ) { + _index6set = input; +} + +void Slider::setRef(QString txt) { + if(_ref) { + _ref->unsubscribe(); + _ref = 0; + } + _refname = txt; + update(); +} + +void Slider::mousePressEvent ( QGraphicsSceneMouseEvent * event ) { + if(isEditMode()) { + PanelItem::mousePressEvent(event); + } else { + dragStartPoint = event->pos(); + _valueStartPoint = _value; + } +} + +void Slider::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if(isEditMode()) { + PanelItem::mouseReleaseEvent(event); + } else { + update(); + } +} +void Slider::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + if(isEditMode()) { + PanelItem::mouseMoveEvent(event); + } else { + double changeAmount = (dragStartPoint - event->pos()).y(); + float travel = height() - (height() * 0.20f); + changeAmount = changeAmount * ((_maximum - _minimum) / travel); + _value = _valueStartPoint + changeAmount; + if (_value < _minimum) { + _value = _minimum; + } + if (_value > _maximum) { + _value = _maximum; + } + if(_ref) { + if (_index1set) { + _ref->setValue(QString::number(_value), 0); + } + if (_index2set) { + _ref->setValue(QString::number(_value), 1); + } + if (_index3set) { + _ref->setValue(QString::number(_value), 2); + } + if (_index4set) { + _ref->setValue(QString::number(_value), 3); + } + if (_index5set) { + _ref->setValue(QString::number(_value), 4); + } + if (_index6set) { + _ref->setValue(QString::number(_value), 5); + } + } + update(); + } +} + +void Slider::valueChanged(QString ref, double newValue) { + Q_ASSERT(ref==_refname); + _value = newValue; +} diff --git a/widgetui/panelitems/slider.h b/widgetui/panelitems/slider.h new file mode 100644 index 0000000..eb60815 --- /dev/null +++ b/widgetui/panelitems/slider.h @@ -0,0 +1,58 @@ +#ifndef SLIDER_H +#define SLIDER_H + +#include "panelitem.h" +#include + +/** + * A generic slider that can set values + */ +class Slider : public PanelItem +{ + Q_OBJECT +public: + Q_INVOKABLE Slider(ExtPlanePanel *panel, ExtPlaneConnection *conn); + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual QString typeName(); + virtual void storeSettings(QSettings &settings); + virtual void loadSettings(QSettings &settings); + virtual void createSettings(QGridLayout *layout); + virtual void applySettings(); +protected: + virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event ); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); +signals: + +public slots: + void setChange(double ca); + void setMinimum(float ca); + void setMaximum(float ca); + void setRef(QString ref); +private slots: + void valueChanged(QString ref, double newValue); + void setChangeString(QString ch); + void setIndex1Set(bool input); + void setIndex2Set(bool input); + void setIndex3Set(bool input); + void setIndex4Set(bool input); + void setIndex5Set(bool input); + void setIndex6Set(bool input); + void setKnobColor(QColor ch); +private: + ExtPlaneClient _client; + QString _refname; + double _value, _change, _valueStartPoint; + float _minimum, _maximum; + bool _index1set; + bool _index2set; + bool _index3set; + bool _index4set; + bool _index5set; + bool _index6set; + QColor _knobcolor; + ClientDataRef* _ref; + QPointF dragStartPoint; +}; + +#endif // SLIDER_H diff --git a/widgetui/widgetui.pro b/widgetui/widgetui.pro index f41c364..0c91c52 100644 --- a/widgetui/widgetui.pro +++ b/widgetui/widgetui.pro @@ -106,7 +106,8 @@ SOURCES += \ panelitems/engine_battery.cpp \ panelitems/genericlittlegauge.cpp \ panelitems/pfddisplay.cpp \ - panelitems/indicatordisplay.cpp + panelitems/indicatordisplay.cpp \ + panelitems/slider.cpp HEADERS += \ panelitems/panelitem.h \ @@ -169,7 +170,8 @@ HEADERS += \ panelitems/engine_battery.h \ panelitems/genericlittlegauge.h \ panelitems/pfddisplay.h \ - panelitems/indicatordisplay.h + panelitems/indicatordisplay.h \ + panelitems/slider.h FORMS += dialogs/settingsdialog.ui \ dialogs/edititemdialog.ui \ From eb5ec6ec228f0d6a6076d606c8220138054792aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moln=C3=A1r=20Csaba?= Date: Wed, 3 Apr 2019 16:46:24 +0200 Subject: [PATCH 2/2] Removed alternative logical operators --- widgetui/panelitems/engine_battery.cpp | 2 +- widgetui/panelitems/tankleveler.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/widgetui/panelitems/engine_battery.cpp b/widgetui/panelitems/engine_battery.cpp index 4cd9f1d..d729fe2 100644 --- a/widgetui/panelitems/engine_battery.cpp +++ b/widgetui/panelitems/engine_battery.cpp @@ -147,7 +147,7 @@ void EngineBattery::setMinValue(float mv){ } void EngineBattery::setMaxValue(float mv){ - if (mv > 0 and mv != valueMax && mv > valueMin){ + if (mv > 0 && mv != valueMax && mv > valueMin){ valueMax = (float)mv; this->update(); } diff --git a/widgetui/panelitems/tankleveler.cpp b/widgetui/panelitems/tankleveler.cpp index cb004ff..7f1aad9 100644 --- a/widgetui/panelitems/tankleveler.cpp +++ b/widgetui/panelitems/tankleveler.cpp @@ -159,7 +159,7 @@ void TankLeveler::setTankNumber(float val) { } void TankLeveler::setMaxValue(float mv){ - if (mv > 0 and mv != valueMax){ + if (mv > 0 && mv != valueMax){ valueMax = (int)mv; this->update(); }