FactValueSliderListModel.cc 3.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "FactValueSliderListModel.h"

#include <QDebug>
#include <QQmlEngine>
#include <QtMath>

#include <math.h>

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)
{
Don Gagne's avatar
Don Gagne committed
33
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
34 35 36 37 38 39 40 41
}

FactValueSliderListModel::~FactValueSliderListModel()
{
}

int FactValueSliderListModel::resetInitialValue(void)
{
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47
    if (_cValues > 0) {
        // Remove any old rows
        beginRemoveRows(QModelIndex(), 0, _cValues - 1);
        _cValues = 0;
        endRemoveRows();
    }
48 49 50 51 52 53 54 55

    _initialValue = _fact.cookedValue().toDouble();
    _initialValueRounded = qRound(_initialValue);
    if (qRound(_fact.rawIncrement()) == _fact.rawIncrement()) {
        _increment = qRound(_fact.cookedIncrement());
    } else {
        _increment = _fact.cookedIncrement();
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
56 57
    _cPrevValues = qMin((_initialValue - _fact.cookedMin().toDouble()), 100.0) / _increment;
    _cNextValues = qMin((_fact.cookedMax().toDouble() - _initialValue), 100.0) / _increment;
58 59 60 61 62 63 64
    _initialValueIndex = _cPrevValues;

    int totalValueCount = _cPrevValues + 1 + _cNextValues;
    beginInsertRows(QModelIndex(), 0, totalValueCount - 1);
    _cValues = totalValueCount;
    endInsertRows();

DonLakeFlyer's avatar
DonLakeFlyer committed
65 66
    emit initialValueAtPrecisionChanged();

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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<int, QByteArray> FactValueSliderListModel::roleNames(void) const
{
    QHash<int, QByteArray> 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();
}
DonLakeFlyer's avatar
DonLakeFlyer committed
131 132 133 134 135 136

double FactValueSliderListModel::initialValueAtPrecision(void)
{
    double precision = qPow(10, _fact.decimalPlaces());
    return qRound(_initialValue * precision) / precision;
}