28 changed files with 402 additions and 258 deletions
@ -1,33 +0,0 @@ |
|||||||
/****************************************************************************
|
|
||||||
* |
|
||||||
* (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. |
|
||||||
* |
|
||||||
****************************************************************************/ |
|
||||||
|
|
||||||
|
|
||||||
/// @file
|
|
||||||
/// @author Don Gagne <don@thegagnes.com>
|
|
||||||
|
|
||||||
#include "FactValidator.h" |
|
||||||
|
|
||||||
FactValidator::FactValidator(QObject* parent) : |
|
||||||
QValidator(parent) |
|
||||||
{ |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
void FactValidator::fixup(QString& input) const |
|
||||||
{ |
|
||||||
Q_UNUSED(input); |
|
||||||
} |
|
||||||
|
|
||||||
FactValidator::State FactValidator::validate(QString& input, int& pos) const |
|
||||||
{ |
|
||||||
Q_UNUSED(input); |
|
||||||
Q_UNUSED(pos); |
|
||||||
|
|
||||||
return Acceptable; |
|
||||||
} |
|
@ -1,56 +0,0 @@ |
|||||||
/****************************************************************************
|
|
||||||
* |
|
||||||
* (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. |
|
||||||
* |
|
||||||
****************************************************************************/ |
|
||||||
|
|
||||||
|
|
||||||
/// @file
|
|
||||||
/// @author Don Gagne <don@thegagnes.com>
|
|
||||||
|
|
||||||
#ifndef FactValidator_H |
|
||||||
#define FactValidator_H |
|
||||||
|
|
||||||
#include <QValidator> |
|
||||||
|
|
||||||
class Fact; |
|
||||||
|
|
||||||
/// QML Validator for Facts (Work In Progress)
|
|
||||||
///
|
|
||||||
/// The validator uses the FactMetaData to impose restrictions on the input. It is used as follows:
|
|
||||||
/// @code{.unparsed}
|
|
||||||
/// TextInput {
|
|
||||||
/// validator: FactValidator { fact: parameters["RC_MAP_THROTTLE"]; }
|
|
||||||
/// }
|
|
||||||
/// @endcode
|
|
||||||
class FactValidator : public QValidator |
|
||||||
{ |
|
||||||
Q_OBJECT |
|
||||||
|
|
||||||
Q_PROPERTY(Fact* fact READ fact WRITE setFact) |
|
||||||
|
|
||||||
public: |
|
||||||
FactValidator(QObject* parent = NULL); |
|
||||||
|
|
||||||
// Property system methods
|
|
||||||
|
|
||||||
/// Read accessor for fact property
|
|
||||||
Fact* fact(void) { return _fact; } |
|
||||||
|
|
||||||
/// Write accessor for fact property
|
|
||||||
void setFact(Fact* fact) { _fact = fact; } |
|
||||||
|
|
||||||
/// Override from QValidator
|
|
||||||
virtual void fixup(QString& input) const; |
|
||||||
|
|
||||||
/// Override from QValidator
|
|
||||||
virtual State validate(QString& input, int& pos) const; |
|
||||||
|
|
||||||
private: |
|
||||||
Fact* _fact; ///< Fact that the validator is working on
|
|
||||||
}; |
|
||||||
|
|
||||||
#endif |
|
@ -0,0 +1,125 @@ |
|||||||
|
/****************************************************************************
|
||||||
|
* |
||||||
|
* (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) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
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<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(); |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
/****************************************************************************
|
||||||
|
* |
||||||
|
* (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. |
||||||
|
* |
||||||
|
****************************************************************************/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <QAbstractListModel> |
||||||
|
|
||||||
|
#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<int, QByteArray> 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; |
||||||
|
}; |
Loading…
Reference in new issue