From d8e1d849f5d285cba3516c54b19376b2c49b306b Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Tue, 21 Jan 2014 23:08:13 +0100 Subject: [PATCH] OP-1183 UAVObjectBrowser now displays and allows editing of integers in hex format (when unit is set to hex) +review OPReview --- .../plugins/uavobjectbrowser/fieldtreeitem.h | 137 +++++++++++++++++- .../uavobjectbrowser/uavobjecttreemodel.cpp | 9 +- 2 files changed, 136 insertions(+), 10 deletions(-) diff --git a/ground/openpilotgcs/src/plugins/uavobjectbrowser/fieldtreeitem.h b/ground/openpilotgcs/src/plugins/uavobjectbrowser/fieldtreeitem.h index 31cf9ecb3..64e639eea 100644 --- a/ground/openpilotgcs/src/plugins/uavobjectbrowser/fieldtreeitem.h +++ b/ground/openpilotgcs/src/plugins/uavobjectbrowser/fieldtreeitem.h @@ -51,18 +51,23 @@ class FieldTreeItem : public TreeItem { Q_OBJECT public: + FieldTreeItem(int index, const QList &data, TreeItem *parent = 0) : TreeItem(data, parent), m_index(index) {} + FieldTreeItem(int index, const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent), m_index(index) {} + bool isEditable() { return true; } + virtual QWidget *createEditor(QWidget *parent) = 0; virtual QVariant getEditorValue(QWidget *editor) = 0; virtual void setEditorValue(QWidget *editor, QVariant value) = 0; virtual void apply() {} + protected: int m_index; }; @@ -70,12 +75,12 @@ protected: class EnumFieldTreeItem : public FieldTreeItem { Q_OBJECT public: - EnumFieldTreeItem(UAVObjectField *field, int index, const QList &data, - TreeItem *parent = 0) : + EnumFieldTreeItem(UAVObjectField *field, int index, const QList &data, TreeItem *parent = 0) : FieldTreeItem(index, data, parent), m_enumOptions(field->getOptions()), m_field(field) {} - EnumFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, - TreeItem *parent = 0) : + + EnumFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent = 0) : FieldTreeItem(index, data, parent), m_enumOptions(field->getOptions()), m_field(field) {} + void setData(QVariant value, int column) { QStringList options = m_field->getOptions(); @@ -85,6 +90,7 @@ public: setChanged(tmpValIndex != value); TreeItem::setData(value, column); } + QString enumOptions(int index) { if ((index < 0) || (index >= m_enumOptions.length())) { @@ -92,6 +98,7 @@ public: } return m_enumOptions.at(index); } + void apply() { int value = data(dataColumn).toInt(); @@ -100,6 +107,7 @@ public: m_field->setValue(options[value], m_index); setChanged(false); } + void update() { QStringList options = m_field->getOptions(); @@ -111,14 +119,16 @@ public: setHighlight(true); } } + QWidget *createEditor(QWidget *parent) { QComboBox *editor = new QComboBox(parent); // Setting ClickFocus lets the ComboBox stay open on Mac OSX. editor->setFocusPolicy(Qt::ClickFocus); - foreach(QString option, m_enumOptions) - editor->addItem(option); + foreach(QString option, m_enumOptions) { + editor->addItem(option); + } return editor; } @@ -135,6 +145,7 @@ public: comboBox->setCurrentIndex(value.toInt()); } + private: QStringList m_enumOptions; UAVObjectField *m_field; @@ -148,6 +159,7 @@ public: { setMinMaxValues(); } + IntFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent = 0) : FieldTreeItem(index, data, parent), m_field(field) { @@ -210,16 +222,19 @@ public: 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(dataColumn).toInt(), m_index); setChanged(false); } + void update() { int value = m_field->getValue(m_index).toInt(); @@ -241,18 +256,22 @@ class FloatFieldTreeItem : public FieldTreeItem { public: FloatFieldTreeItem(UAVObjectField *field, int index, const QList &data, bool scientific = false, TreeItem *parent = 0) : FieldTreeItem(index, data, parent), m_field(field), m_useScientificNotation(scientific) {} + FloatFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, bool scientific = false, TreeItem *parent = 0) : FieldTreeItem(index, data, parent), m_field(field), m_useScientificNotation(scientific) {} + void setData(QVariant value, int column) { setChanged(m_field->getValue(m_index) != value); TreeItem::setData(value, column); } + void apply() { m_field->setValue(data(dataColumn).toDouble(), m_index); setChanged(false); } + void update() { double value = m_field->getValue(m_index).toDouble(); @@ -303,9 +322,115 @@ public: spinBox->setValue(value.toDouble()); } } + private: UAVObjectField *m_field; bool m_useScientificNotation; }; +class HexFieldTreeItem : public FieldTreeItem { + Q_OBJECT +public: + HexFieldTreeItem(UAVObjectField *field, int index, const QList &data, TreeItem *parent = 0) : + FieldTreeItem(index, data, parent), m_field(field) + {} + + HexFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent = 0) : + FieldTreeItem(index, data, parent), m_field(field) + {} + + QWidget *createEditor(QWidget *parent) + { + QLineEdit *lineEdit = new QLineEdit(parent); + + lineEdit->setInputMask(QString(maxLength(), 'H')); + + return lineEdit; + } + + QVariant getEditorValue(QWidget *editor) + { + QLineEdit *lineEdit = static_cast(editor); + + return lineEdit->text(); + } + + void setEditorValue(QWidget *editor, QVariant value) + { + QLineEdit *lineEdit = static_cast(editor); + + lineEdit->setText(value.toString()); + } + + void setData(QVariant value, int column) + { + setChanged(m_field->getValue(m_index) != value); + TreeItem::setData(value, column); + } + + void apply() + { + m_field->setValue(toUInt(data(dataColumn)), m_index); + setChanged(false); + } + + void update() + { + QVariant value = toHexString(m_field->getValue(m_index)); + + if (data() != value || changed()) { + TreeItem::setData(value); + setHighlight(true); + } + } + +private: + UAVObjectField *m_field; + + QVariant toHexString(QVariant value) + { + QString str; + bool ok; + + return str.setNum(value.toUInt(&ok), 16).toUpper(); + } + + QVariant toUInt(QVariant str) + { + bool ok; + + return str.toString().toUInt(&ok, 16); + } + + int maxLength() + { + int maxLength = 0; + + switch (m_field->getType()) { + case UAVObjectField::INT8: + maxLength = 2; + break; + case UAVObjectField::INT16: + maxLength = 4; + break; + case UAVObjectField::INT32: + maxLength = 8; + break; + case UAVObjectField::UINT8: + maxLength = 2; + break; + case UAVObjectField::UINT16: + maxLength = 4; + break; + case UAVObjectField::UINT32: + maxLength = 8; + break; + default: + Q_ASSERT(false); + break; + } + return maxLength; + } +}; + #endif // FIELDTREEITEM_H diff --git a/ground/openpilotgcs/src/plugins/uavobjectbrowser/uavobjecttreemodel.cpp b/ground/openpilotgcs/src/plugins/uavobjectbrowser/uavobjecttreemodel.cpp index 1845f2c35..f1665bd59 100644 --- a/ground/openpilotgcs/src/plugins/uavobjectbrowser/uavobjecttreemodel.cpp +++ b/ground/openpilotgcs/src/plugins/uavobjectbrowser/uavobjecttreemodel.cpp @@ -228,7 +228,11 @@ void UAVObjectTreeModel::addSingleField(int index, UAVObjectField *field, TreeIt case UAVObjectField::UINT32: data.append(field->getValue(index)); data.append(field->getUnits()); - item = new IntFieldTreeItem(field, index, data); + if (field->getUnits().toLower() == "hex") { + item = new HexFieldTreeItem(field, index, data); + } else { + item = new IntFieldTreeItem(field, index, data); + } break; case UAVObjectField::FLOAT32: data.append(field->getValue(index)); @@ -351,9 +355,6 @@ QVariant UAVObjectTreeModel::data(const QModelIndex &index, int role) const return item->data(index.column()); } -// if (role == Qt::DecorationRole) -// return QIcon(":/core/images/openpilot_logo_128.png"); - if (role == Qt::ToolTipRole) { TreeItem *item = static_cast(index.internalPointer()); return item->description();