1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

LP-567 factorize common code up into FieldTreeItem

derived classes are simpler
This commit is contained in:
Philippe Renon 2018-03-12 22:31:25 +01:00
parent 6ded0d0b7a
commit 9f71b941ed

View File

@ -73,8 +73,40 @@ public:
return parent()->isKnown(); return parent()->isKnown();
} }
void setData(QVariant value, int column)
{
QVariant currentValue = fieldToData();
setChanged(currentValue != value);
TreeItem::setData(value, column);
}
void update()
{
bool updated = false;
if (!changed()) {
QVariant currentValue = fieldToData();
if (data() != currentValue) {
updated = true;
TreeItem::setData(currentValue);
}
}
if (changed() || updated) {
setHighlight(true);
}
}
void apply()
{
m_field->setValue(dataToField(), m_index);
setChanged(false);
}
protected: protected:
virtual QVariant fieldToData() const = 0;
virtual QVariant dataToField() const = 0;
int m_index; int m_index;
UAVObjectField *m_field; UAVObjectField *m_field;
}; };
@ -90,16 +122,6 @@ public:
FieldTreeItem(index, data, field, parent), m_enumOptions(field->getOptions()) FieldTreeItem(index, data, field, parent), m_enumOptions(field->getOptions())
{} {}
void setData(QVariant value, int column)
{
QStringList options = m_field->getOptions();
QVariant tmpValue = m_field->getValue(m_index);
int tmpValIndex = options.indexOf(tmpValue.toString());
setChanged(tmpValIndex != value);
TreeItem::setData(value, column);
}
QString enumOptions(int index) QString enumOptions(int index)
{ {
if ((index < 0) || (index >= m_enumOptions.length())) { if ((index < 0) || (index >= m_enumOptions.length())) {
@ -108,25 +130,21 @@ public:
return m_enumOptions.at(index); return m_enumOptions.at(index);
} }
void apply() QVariant fieldToData() const
{
int value = data().toInt();
QStringList options = m_field->getOptions();
m_field->setValue(options[value], m_index);
setChanged(false);
}
void update()
{ {
QStringList options = m_field->getOptions(); QStringList options = m_field->getOptions();
QVariant value = m_field->getValue(m_index); QVariant value = m_field->getValue(m_index);
int valIndex = options.indexOf(value.toString()); int valIndex = options.indexOf(value.toString());
if (data() != valIndex || changed()) { return valIndex;
TreeItem::setData(valIndex); }
setHighlight(true);
} QVariant dataToField() const
{
int value = data().toInt();
QStringList options = m_field->getOptions();
return options[value];
} }
QWidget *createEditor(QWidget *parent) const QWidget *createEditor(QWidget *parent) const
@ -207,6 +225,16 @@ public:
} }
} }
QVariant fieldToData() const
{
return m_field->getValue(m_index).toInt();
}
QVariant dataToField() const
{
return data().toInt();
}
QWidget *createEditor(QWidget *parent) const QWidget *createEditor(QWidget *parent) const
{ {
QSpinBox *editor = new QSpinBox(parent); QSpinBox *editor = new QSpinBox(parent);
@ -231,28 +259,6 @@ public:
spinBox->setValue(value.toInt()); spinBox->setValue(value.toInt());
} }
void setData(QVariant value, int column)
{
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(value, column);
}
void apply()
{
m_field->setValue(data().toInt(), m_index);
setChanged(false);
}
void update()
{
int value = m_field->getValue(m_index).toInt();
if (data() != value || changed()) {
TreeItem::setData(value);
setHighlight(true);
}
}
private: private:
int m_minValue; int m_minValue;
int m_maxValue; int m_maxValue;
@ -267,26 +273,14 @@ public:
FloatFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, bool scientific = false, TreeItem *parent = 0) : FloatFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, bool scientific = false, TreeItem *parent = 0) :
FieldTreeItem(index, data, field, parent), m_useScientificNotation(scientific) {} FieldTreeItem(index, data, field, parent), m_useScientificNotation(scientific) {}
void setData(QVariant value, int column) QVariant fieldToData() const
{ {
setChanged(m_field->getValue(m_index) != value); return m_field->getValue(m_index).toDouble();
TreeItem::setData(value, column);
} }
void apply() QVariant dataToField() const
{ {
m_field->setValue(data().toDouble(), m_index); return data().toDouble();
setChanged(false);
}
void update()
{
double value = m_field->getValue(m_index).toDouble();
if (data() != value || changed()) {
TreeItem::setData(value);
setHighlight(true);
}
} }
QWidget *createEditor(QWidget *parent) const QWidget *createEditor(QWidget *parent) const
@ -345,6 +339,16 @@ public:
FieldTreeItem(index, data, field, parent) FieldTreeItem(index, data, field, parent)
{} {}
QVariant fieldToData() const
{
return toHexString(m_field->getValue(m_index));
}
QVariant dataToField() const
{
return toUInt(data());
}
QWidget *createEditor(QWidget *parent) const QWidget *createEditor(QWidget *parent) const
{ {
QLineEdit *lineEdit = new QLineEdit(parent); QLineEdit *lineEdit = new QLineEdit(parent);
@ -368,28 +372,6 @@ public:
lineEdit->setText(value.toString()); lineEdit->setText(value.toString());
} }
void setData(QVariant value, int column)
{
setChanged(m_field->getValue(m_index) != toUInt(value));
TreeItem::setData(value, column);
}
void apply()
{
m_field->setValue(toUInt(data()), m_index);
setChanged(false);
}
void update()
{
QVariant value = toHexString(m_field->getValue(m_index));
if (data() != value || changed()) {
TreeItem::setData(value);
setHighlight(true);
}
}
private: private:
QVariant toHexString(QVariant value) const QVariant toHexString(QVariant value) const
{ {
@ -418,6 +400,16 @@ public:
FieldTreeItem(index, data, field, parent) FieldTreeItem(index, data, field, parent)
{} {}
QVariant fieldToData() const
{
return toChar(m_field->getValue(m_index));
}
QVariant dataToField() const
{
return toUInt(data());
}
QWidget *createEditor(QWidget *parent) const QWidget *createEditor(QWidget *parent) const
{ {
QLineEdit *lineEdit = new QLineEdit(parent); QLineEdit *lineEdit = new QLineEdit(parent);
@ -441,27 +433,6 @@ public:
lineEdit->setText(value.toString()); lineEdit->setText(value.toString());
} }
void setData(QVariant value, int column)
{
setChanged(m_field->getValue(m_index) != toUInt(value));
TreeItem::setData(value, column);
}
void apply()
{
m_field->setValue(toUInt(data()), m_index);
setChanged(false);
}
void update()
{
QVariant value = toChar(m_field->getValue(m_index));
if (data() != value || changed()) {
TreeItem::setData(value);
setHighlight(true);
}
}
private: private:
QVariant toChar(QVariant value) const QVariant toChar(QVariant value) const
{ {