From 8cf5f75cf166598d9fc6aeb97e9bcd00e14fc39e Mon Sep 17 00:00:00 2001 From: DoinLakeFlyer Date: Sun, 12 Apr 2020 16:24:10 -0700 Subject: [PATCH] Update value visuals to support value ranges --- src/FlightMap/Widgets/InstrumentValue.cc | 98 ++++++++++++++++++++++- src/FlightMap/Widgets/InstrumentValue.h | 14 ++++ src/FlightMap/Widgets/ValuePageWidget.qml | 26 +++++- 3 files changed, 135 insertions(+), 3 deletions(-) diff --git a/src/FlightMap/Widgets/InstrumentValue.cc b/src/FlightMap/Widgets/InstrumentValue.cc index f1ea598f1..4d32d8897 100644 --- a/src/FlightMap/Widgets/InstrumentValue.cc +++ b/src/FlightMap/Widgets/InstrumentValue.cc @@ -65,11 +65,21 @@ InstrumentValue::InstrumentValue(Vehicle* activeVehicle, FontSize fontSize, QmlO } activeVehicleChanged(_activeVehicle); - connect(this, &InstrumentValue::rangeTypeChanged, this, &InstrumentValue::_resetRangeInfo); + + connect(this, &InstrumentValue::rangeTypeChanged, this, &InstrumentValue::_resetRangeInfo); + connect(this, &InstrumentValue::rangeTypeChanged, this, &InstrumentValue::_updateRanges); + connect(this, &InstrumentValue::rangeValuesChanged, this, &InstrumentValue::_updateRanges); + connect(this, &InstrumentValue::rangeColorsChanged, this, &InstrumentValue::_updateRanges); + connect(this, &InstrumentValue::rangeOpacitiesChanged, this, &InstrumentValue::_updateRanges); + connect(this, &InstrumentValue::rangeIconsChanged, this, &InstrumentValue::_updateRanges); } void InstrumentValue::activeVehicleChanged(Vehicle* activeVehicle) { + if (_fact) { + disconnect(_fact, &Fact::rawValueChanged, this, &InstrumentValue::_updateColor); + } + _activeVehicle = activeVehicle; _factGroupNames.clear(); @@ -94,12 +104,17 @@ void InstrumentValue::activeVehicleChanged(Vehicle* activeVehicle) _fact = factGroup->getFact(_factName); } emit factChanged(_fact); + + connect(_fact, &Fact::rawValueChanged, this, &InstrumentValue::_updateRanges); } + + _updateRanges(); } void InstrumentValue::setFact(const QString& factGroupName, const QString& factName) { if (_fact) { + disconnect(_fact, &Fact::rawValueChanged, this, &InstrumentValue::_updateRanges); _fact = nullptr; } @@ -129,6 +144,8 @@ void InstrumentValue::setFact(const QString& factGroupName, const QString& factN if (_fact) { _factGroupName = factGroupName; _factName = nonEmptyFactName; + + connect(_fact, &Fact::rawValueChanged, this, &InstrumentValue::_updateRanges); } else { _factName.clear(); _factGroupName.clear(); @@ -138,6 +155,8 @@ void InstrumentValue::setFact(const QString& factGroupName, const QString& factN emit factNameChanged (_factName); emit factGroupNameChanged (_factGroupName); emit factValueNamesChanged (_factValueNames); + + _updateRanges(); } void InstrumentValue::_setFontSize(FontSize fontSize) @@ -412,3 +431,80 @@ void InstrumentValue::removeRangeValue(int index) emit rangeOpacitiesChanged (_rangeOpacities); emit rangeIconsChanged (_rangeIcons); } + +void InstrumentValue::_updateRanges(void) +{ + _updateColor(); + _updateIcon(); + _updateOpacity(); +} + +void InstrumentValue::_updateColor(void) +{ + QColor newColor; + + int rangeIndex = -1; + + if (_rangeType == ColorRange && _fact) { + rangeIndex =_currentRangeIndex(_fact->rawValue().toDouble()); + } + if (rangeIndex != -1) { + newColor = _rangeColors[rangeIndex].value(); + } + + if (newColor != _currentColor) { + _currentColor = newColor; + emit currentColorChanged(_currentColor); + } +} + +void InstrumentValue::_updateOpacity(void) +{ + double newOpacity = 1.0; + + int rangeIndex = -1; + + if (_rangeType == OpacityRange && _fact) { + rangeIndex =_currentRangeIndex(_fact->rawValue().toDouble()); + } + if (rangeIndex != -1) { + newOpacity = _rangeOpacities[rangeIndex].toDouble(); + } + + if (!qFuzzyCompare(newOpacity, _currentOpacity)) { + _currentOpacity = newOpacity; + emit currentOpacityChanged(newOpacity); + } +} + +void InstrumentValue::_updateIcon(void) +{ + QString newIcon; + + int rangeIndex = -1; + + if (_rangeType == IconSelectRange && _fact) { + rangeIndex =_currentRangeIndex(_fact->rawValue().toDouble()); + } + if (rangeIndex != -1) { + newIcon = _rangeIcons[rangeIndex].toString(); + } + + if (newIcon != _currentIcon) { + _currentIcon = newIcon; + emit currentIconChanged(newIcon); + } +} + +int InstrumentValue::_currentRangeIndex(const QVariant& value) +{ + if (qIsNaN(value.toDouble())) { + return 0; + } + for (int i=0; i<_rangeValues.count(); i++) { + if (value.toDouble() <= _rangeValues[i].toDouble()) { + return i; + } + } + return _rangeValues.count(); +} diff --git a/src/FlightMap/Widgets/InstrumentValue.h b/src/FlightMap/Widgets/InstrumentValue.h index 6bda21b29..e389fc2b5 100644 --- a/src/FlightMap/Widgets/InstrumentValue.h +++ b/src/FlightMap/Widgets/InstrumentValue.h @@ -63,6 +63,9 @@ public: Q_PROPERTY(QVariantList rangeColors READ rangeColors WRITE setRangeColors NOTIFY rangeColorsChanged) Q_PROPERTY(QVariantList rangeIcons READ rangeIcons WRITE setRangeIcons NOTIFY rangeIconsChanged) Q_PROPERTY(QVariantList rangeOpacities READ rangeOpacities WRITE setRangeOpacities NOTIFY rangeOpacitiesChanged) + Q_PROPERTY(QColor currentColor MEMBER _currentColor NOTIFY currentColorChanged) + Q_PROPERTY(double currentOpacity MEMBER _currentOpacity NOTIFY currentOpacityChanged) + Q_PROPERTY(QString currentIcon MEMBER _currentIcon NOTIFY currentIconChanged) Q_INVOKABLE void setFact (const QString& factGroupName, const QString& factName); Q_INVOKABLE void clearFact (void); @@ -112,12 +115,20 @@ signals: void rangeColorsChanged (const QVariantList& rangeColors); void rangeIconsChanged (const QVariantList& rangeIcons); void rangeOpacitiesChanged (const QVariantList& rangeOpacities); + void currentColorChanged (const QColor& currentColor); + void currentOpacityChanged (double currentOpacity); + void currentIconChanged (const QString& currentIcon); private slots: void _resetRangeInfo (void); + void _updateRanges (void); private: void _setFontSize (FontSize fontSize); + int _currentRangeIndex (const QVariant& value); + void _updateColor (void); + void _updateIcon (void); + void _updateOpacity (void); Vehicle* _activeVehicle = nullptr; QmlObjectListModel* _rowModel = nullptr; @@ -131,6 +142,9 @@ private: IconPosition _iconPosition = IconLeft; QStringList _factGroupNames; QStringList _factValueNames; + QColor _currentColor; + double _currentOpacity = 1.0; + QString _currentIcon; // Ranges allow you to specifiy semantics to apply when a value is within a certain range. // The limits for each section of the range are specified in _rangeValues. With the first diff --git a/src/FlightMap/Widgets/ValuePageWidget.qml b/src/FlightMap/Widgets/ValuePageWidget.qml index 531a4aeaa..613cf04bb 100644 --- a/src/FlightMap/Widgets/ValuePageWidget.qml +++ b/src/FlightMap/Widgets/ValuePageWidget.qml @@ -166,15 +166,37 @@ Column { id: valueIcon height: _rgFontSizeTightHeights[object.fontSize] width: height - source: object.icon ? "/InstrumentValueIcons/" + object.icon : "" + source: icon sourceSize.height: height fillMode: Image.PreserveAspectFit mipmap: true smooth: true - color: qgcPal.text + color: object.isValidColor(object.currentColor) ? object.currentColor : qgcPal.text + opacity: object.currentOpacity visible: object.icon onWidthChanged: columnItem.recalcPositions() onHeightChanged: columnItem.recalcPositions() + + property string icon + readonly property string iconPrefix: "/InstrumentValueIcons/" + + function updateIcon() { + if (object.rangeType == InstrumentValue.IconSelectRange) { + icon = iconPrefix + object.currentIcon + } else if (object.icon) { + icon = iconPrefix + object.icon + } else { + icon = "" + } + } + + Connections { + target: object + onRangeTypeChanged: valueIcon.updateIcon() + onCurrentIconChanged: valueIcon.updateIcon() + onIconChanged: valueIcon.updateIcon() + } + Component.onCompleted: updateIcon(); } QGCLabel { -- 2.22.0