QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
FactValueSliderListModel.cc
Go to the documentation of this file.
2#include "Fact.h"
4
5#include <QtCore/QtMath>
6
7QGC_LOGGING_CATEGORY(FactValueSliderListModelLog, "FactSystem.FactValueSliderListModel")
8
9FactValueSliderListModel::FactValueSliderListModel(const Fact &fact, QObject *parent)
10 : QAbstractListModel(parent)
11 , _fact(fact)
12{
13 // qCDebug(FactValueSliderListModelLog) << Q_FUNC_INFO << this;
14}
15
16FactValueSliderListModel::~FactValueSliderListModel()
17{
18 // qCDebug(FactValueSliderListModelLog) << Q_FUNC_INFO << this;
19}
20
21int FactValueSliderListModel::resetInitialValue()
22{
23 if (_cValues > 0) {
24 // Remove any old rows
25 beginRemoveRows(QModelIndex(), 0, _cValues - 1);
26 _cValues = 0;
27 endRemoveRows();
28 }
29
30 _initialValue = _fact.cookedValue().toDouble();
31 _initialValueAtPrecision = _valueAtPrecision(_initialValue);
32 if (qRound(_fact.rawIncrement()) == _fact.rawIncrement()) {
33 _increment = qRound(_fact.cookedIncrement());
34 } else {
35 _increment = _fact.cookedIncrement();
36 }
37 _cPrevValues = qMin((_initialValue - _fact.cookedMin().toDouble()) / _increment, 100.0);
38 _cNextValues = qMin((_fact.cookedMax().toDouble() - _initialValue) / _increment, 100.0);
39 _initialValueIndex = _cPrevValues;
40
41 const int totalValueCount = _cPrevValues + 1 + _cNextValues;
42 beginInsertRows(QModelIndex(), 0, totalValueCount - 1);
43 _cValues = totalValueCount;
44 endInsertRows();
45
47
48 return _initialValueIndex;
49}
50
51QVariant FactValueSliderListModel::data(const QModelIndex &index, int role) const
52{
53 Q_UNUSED(role);
54
55 if (!index.isValid()) {
56 return QVariant();
57 }
58
59 const int valueIndex = index.row();
60 if (valueIndex >= _cValues) {
61 return QVariant();
62 }
63
64 if (role == _valueRole) {
65 double value;
66 const int cIncrementCount = valueIndex - _initialValueIndex;
67 if (cIncrementCount == 0) {
68 value = _initialValue;
69 } else {
70 value = initialValueAtPrecision() + (cIncrementCount * _increment);
71 }
72 return QVariant(_valueAtPrecision(value));
73 } else if (role == _valueIndexRole) {
74 return QVariant::fromValue(valueIndex);
75 } else {
76 return QVariant();
77 }
78}
79
80QHash<int, QByteArray> FactValueSliderListModel::roleNames() const
81{
82 QHash<int, QByteArray> hash;
83
84 hash[_valueRole] = "value";
85 hash[_valueIndexRole] = "valueIndex";
86
87 return hash;
88}
89
90double FactValueSliderListModel::valueAtModelIndex(int index) const
91{
92 return data(createIndex(index, 0), _valueRole).toDouble();
93}
94
95int FactValueSliderListModel::valueIndexAtModelIndex(int index) const
96{
97 return data(createIndex(index, 0), _valueIndexRole).toInt();
98}
99
100double FactValueSliderListModel::_valueAtPrecision(double value) const
101{
102 const double precision = qPow(10, _fact.decimalPlaces());
103 return (qRound(value * precision) / precision);
104}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Provides a list model of values for incrementing/decrementing the value of a Fact.
A Fact is used to hold a single value within the system.
Definition Fact.h:19