From 512922c952f075681e045f62389bbf256bca153d Mon Sep 17 00:00:00 2001 From: DonLakeFlyer Date: Sun, 29 Apr 2018 16:53:16 -0700 Subject: [PATCH] New ListModel for Fact meta data base increment/decrement --- qgroundcontrol.pro | 4 +- src/FactSystem/FactValueSliderListModel.cc | 125 +++++++++++++++++++++ src/FactSystem/FactValueSliderListModel.h | 48 ++++++++ src/QGCApplication.cc | 2 + 4 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 src/FactSystem/FactValueSliderListModel.cc create mode 100644 src/FactSystem/FactValueSliderListModel.h diff --git a/qgroundcontrol.pro b/qgroundcontrol.pro index db70301c4..bb3b44142 100644 --- a/qgroundcontrol.pro +++ b/qgroundcontrol.pro @@ -1057,7 +1057,7 @@ HEADERS += \ src/FactSystem/FactGroup.h \ src/FactSystem/FactMetaData.h \ src/FactSystem/FactSystem.h \ - src/FactSystem/FactValidator.h \ + src/FactSystem/FactValueSliderListModel.h \ src/FactSystem/ParameterManager.h \ src/FactSystem/SettingsFact.h \ @@ -1067,7 +1067,7 @@ SOURCES += \ src/FactSystem/FactGroup.cc \ src/FactSystem/FactMetaData.cc \ src/FactSystem/FactSystem.cc \ - src/FactSystem/FactValidator.cc \ + src/FactSystem/FactValueSliderListModel.cc \ src/FactSystem/ParameterManager.cc \ src/FactSystem/SettingsFact.cc \ diff --git a/src/FactSystem/FactValueSliderListModel.cc b/src/FactSystem/FactValueSliderListModel.cc new file mode 100644 index 000000000..59adb5fd2 --- /dev/null +++ b/src/FactSystem/FactValueSliderListModel.cc @@ -0,0 +1,125 @@ +/**************************************************************************** + * + * (c) 2009-2016 QGROUNDCONTROL PROJECT + * + * QGroundControl is licensed according to the terms in the file + * COPYING.md in the root of the source code directory. + * + ****************************************************************************/ + +#include "FactValueSliderListModel.h" + +#include +#include +#include + +#include + +const int FactValueSliderListModel::_valueRole = Qt::UserRole; +const int FactValueSliderListModel::_valueIndexRole = Qt::UserRole + 1; + +FactValueSliderListModel::FactValueSliderListModel(Fact& fact, QObject* parent) + : QAbstractListModel (parent) + , _fact (fact) + , _cValues (0) + , _firstValueIndexInWindow (0) + , _initialValueIndex (0) + , _cPrevValues (0) + , _cNextValues (0) + , _initialValue (0) + , _initialValueRounded (0) + , _increment (0) +{ +} + +FactValueSliderListModel::~FactValueSliderListModel() +{ +} + +int FactValueSliderListModel::resetInitialValue(void) +{ + // Remove any old rows + beginRemoveRows(QModelIndex(), 0, _cValues - 1); + _cValues = 0; + endRemoveRows(); + + _initialValue = _fact.cookedValue().toDouble(); + _initialValueRounded = qRound(_initialValue); + if (qRound(_fact.rawIncrement()) == _fact.rawIncrement()) { + _increment = qRound(_fact.cookedIncrement()); + } else { + _increment = _fact.cookedIncrement(); + } + _cPrevValues = qMin((_initialValue - _fact.cookedMin().toDouble()), 1000.0) / _increment; + _cNextValues = qMin((_fact.cookedMax().toDouble() - _initialValue), 1000.0) / _increment; + _initialValueIndex = _cPrevValues; + + int totalValueCount = _cPrevValues + 1 + _cNextValues; + beginInsertRows(QModelIndex(), 0, totalValueCount - 1); + _cValues = totalValueCount; + endInsertRows(); + + return _initialValueIndex; +} + +int FactValueSliderListModel::rowCount(const QModelIndex& parent) const +{ + Q_UNUSED(parent); + + return _cValues; +} + +QVariant FactValueSliderListModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role); + + if (!index.isValid()) { + return QVariant(); + } + + int valueIndex = index.row(); + if (valueIndex >= _cValues) { + return QVariant(); + } + + if (role == _valueRole) { + double value; + int cIncrementCount = valueIndex - _initialValueIndex; + if (cIncrementCount == 0) { + value = _initialValue; + } else { + value = _initialValueRounded + (cIncrementCount * _increment); + } + double precision = qPow(10, _fact.decimalPlaces()); + double atPrecision = qRound(value * precision) / precision; + //qDebug() << value << precision << atPrecision << _fact.decimalPlaces() << _fact.name(); + return QVariant(atPrecision); + } else if (role == _valueIndexRole) { + return QVariant::fromValue(valueIndex); + } else { + return QVariant(); + } + + +} + +QHash FactValueSliderListModel::roleNames(void) const +{ + QHash hash; + + hash[_valueRole] = "value"; + hash[_valueIndexRole] = "valueIndex"; + + return hash; +} + +double FactValueSliderListModel::valueAtModelIndex(int index) +{ + return data(createIndex(index, 0), _valueRole).toDouble(); + +} + +int FactValueSliderListModel::valueIndexAtModelIndex(int index) +{ + return data(createIndex(index, 0), _valueIndexRole).toInt(); +} diff --git a/src/FactSystem/FactValueSliderListModel.h b/src/FactSystem/FactValueSliderListModel.h new file mode 100644 index 000000000..3f35b7156 --- /dev/null +++ b/src/FactSystem/FactValueSliderListModel.h @@ -0,0 +1,48 @@ +/**************************************************************************** + * + * (c) 2009-2016 QGROUNDCONTROL PROJECT + * + * QGroundControl is licensed according to the terms in the file + * COPYING.md in the root of the source code directory. + * + ****************************************************************************/ + +#pragma once + +#include + +#include "Fact.h" + +/// Provides a list model of values for incrementing/decrementing the value of a Fact +class FactValueSliderListModel : public QAbstractListModel +{ + Q_OBJECT + +public: + FactValueSliderListModel(Fact& fact, QObject* parent = NULL); + ~FactValueSliderListModel(); + + Q_INVOKABLE int resetInitialValue(void); + Q_INVOKABLE double valueAtModelIndex(int index); + Q_INVOKABLE int valueIndexAtModelIndex(int index); + +private: + // Overrides from QAbstractListModel + int rowCount(const QModelIndex & parent = QModelIndex()) const override; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override; + QHash roleNames(void) const override; + + Fact& _fact; + int _cValues; + int _firstValueIndexInWindow; + int _initialValueIndex; + int _cPrevValues; + int _cNextValues; + int _windowSize; + double _initialValue; + double _initialValueRounded; + double _increment; + + static const int _valueRole; + static const int _valueIndexRole; +}; diff --git a/src/QGCApplication.cc b/src/QGCApplication.cc index 6d4d797b5..8bfd850b3 100644 --- a/src/QGCApplication.cc +++ b/src/QGCApplication.cc @@ -83,6 +83,7 @@ #include "CameraCalc.h" #include "VisualMissionItem.h" #include "EditPositionDialogController.h" +#include "FactValueSliderListModel.h" #ifndef NO_SERIAL_LINK #include "SerialLink.h" @@ -368,6 +369,7 @@ void QGCApplication::_initCommon(void) qmlRegisterUncreatableType ("QGroundControl.Controllers", 1, 0, "GeoFenceController", "Reference only"); qmlRegisterUncreatableType("QGroundControl.Controllers", 1, 0, "RallyPointController", "Reference only"); qmlRegisterUncreatableType ("QGroundControl.Controllers", 1, 0, "VisualMissionItem", "Reference only"); + qmlRegisterUncreatableType("QGroundControl.FactControls", 1, 0, "FactValueSliderListModel","Reference only"); qmlRegisterType ("QGroundControl.Controllers", 1, 0, "ParameterEditorController"); qmlRegisterType ("QGroundControl.Controllers", 1, 0, "ESP8266ComponentController"); -- 2.22.0