From 6ded0d0b7a95c1d093fd64e1dff99b198e490379 Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Mon, 12 Mar 2018 22:29:33 +0100 Subject: [PATCH] LP-567 improve treeitem const correcteness --- .../plugins/uavobjectbrowser/fieldtreeitem.h | 50 +++++++++---------- .../src/plugins/uavobjectbrowser/treeitem.cpp | 4 +- .../src/plugins/uavobjectbrowser/treeitem.h | 23 +++++---- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/ground/gcs/src/plugins/uavobjectbrowser/fieldtreeitem.h b/ground/gcs/src/plugins/uavobjectbrowser/fieldtreeitem.h index df1247cf7..5a7ebe902 100644 --- a/ground/gcs/src/plugins/uavobjectbrowser/fieldtreeitem.h +++ b/ground/gcs/src/plugins/uavobjectbrowser/fieldtreeitem.h @@ -60,16 +60,15 @@ public: TreeItem(data, parent), m_index(index), m_field(field) {} - bool isEditable() + bool isEditable() const { 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() {} - virtual bool isKnown() + virtual QWidget *createEditor(QWidget *parent) const = 0; + virtual QVariant getEditorValue(QWidget *editor) const = 0; + virtual void setEditorValue(QWidget *editor, QVariant value) const = 0; + virtual bool isKnown() const { return parent()->isKnown(); } @@ -130,7 +129,7 @@ public: } } - QWidget *createEditor(QWidget *parent) + QWidget *createEditor(QWidget *parent) const { QComboBox *editor = new QComboBox(parent); @@ -142,14 +141,14 @@ public: return editor; } - QVariant getEditorValue(QWidget *editor) + QVariant getEditorValue(QWidget *editor) const { QComboBox *comboBox = static_cast(editor); return comboBox->currentIndex(); } - void setEditorValue(QWidget *editor, QVariant value) + void setEditorValue(QWidget *editor, QVariant value) const { QComboBox *comboBox = static_cast(editor); @@ -208,7 +207,7 @@ public: } } - QWidget *createEditor(QWidget *parent) + QWidget *createEditor(QWidget *parent) const { QSpinBox *editor = new QSpinBox(parent); @@ -217,7 +216,7 @@ public: return editor; } - QVariant getEditorValue(QWidget *editor) + QVariant getEditorValue(QWidget *editor) const { QSpinBox *spinBox = static_cast(editor); @@ -225,7 +224,7 @@ public: return spinBox->value(); } - void setEditorValue(QWidget *editor, QVariant value) + void setEditorValue(QWidget *editor, QVariant value) const { QSpinBox *spinBox = static_cast(editor); @@ -290,7 +289,7 @@ public: } } - QWidget *createEditor(QWidget *parent) + QWidget *createEditor(QWidget *parent) const { if (m_useScientificNotation) { QScienceSpinBox *editor = new QScienceSpinBox(parent); @@ -307,7 +306,7 @@ public: } } - QVariant getEditorValue(QWidget *editor) + QVariant getEditorValue(QWidget *editor) const { if (m_useScientificNotation) { QScienceSpinBox *spinBox = static_cast(editor); @@ -320,7 +319,7 @@ public: } } - void setEditorValue(QWidget *editor, QVariant value) + void setEditorValue(QWidget *editor, QVariant value) const { if (m_useScientificNotation) { QScienceSpinBox *spinBox = static_cast(editor); @@ -346,7 +345,7 @@ public: FieldTreeItem(index, data, field, parent) {} - QWidget *createEditor(QWidget *parent) + QWidget *createEditor(QWidget *parent) const { QLineEdit *lineEdit = new QLineEdit(parent); @@ -355,14 +354,14 @@ public: return lineEdit; } - QVariant getEditorValue(QWidget *editor) + QVariant getEditorValue(QWidget *editor) const { QLineEdit *lineEdit = static_cast(editor); return lineEdit->text(); } - void setEditorValue(QWidget *editor, QVariant value) + void setEditorValue(QWidget *editor, QVariant value) const { QLineEdit *lineEdit = static_cast(editor); @@ -392,7 +391,7 @@ public: } private: - QVariant toHexString(QVariant value) + QVariant toHexString(QVariant value) const { QString str; bool ok; @@ -400,7 +399,7 @@ private: return str.setNum(value.toUInt(&ok), 16).toUpper(); } - QVariant toUInt(QVariant str) + QVariant toUInt(QVariant str) const { bool ok; @@ -419,7 +418,7 @@ public: FieldTreeItem(index, data, field, parent) {} - QWidget *createEditor(QWidget *parent) + QWidget *createEditor(QWidget *parent) const { QLineEdit *lineEdit = new QLineEdit(parent); @@ -428,14 +427,14 @@ public: return lineEdit; } - QVariant getEditorValue(QWidget *editor) + QVariant getEditorValue(QWidget *editor) const { QLineEdit *lineEdit = static_cast(editor); return lineEdit->text(); } - void setEditorValue(QWidget *editor, QVariant value) + void setEditorValue(QWidget *editor, QVariant value) const { QLineEdit *lineEdit = static_cast(editor); @@ -463,14 +462,13 @@ public: setHighlight(true); } } - private: - QVariant toChar(QVariant value) + QVariant toChar(QVariant value) const { return value.toChar(); } - QVariant toUInt(QVariant str) + QVariant toUInt(QVariant str) const { return QVariant(str.toString().at(0).toLatin1()); } diff --git a/ground/gcs/src/plugins/uavobjectbrowser/treeitem.cpp b/ground/gcs/src/plugins/uavobjectbrowser/treeitem.cpp index 5b4d17702..627c427c1 100644 --- a/ground/gcs/src/plugins/uavobjectbrowser/treeitem.cpp +++ b/ground/gcs/src/plugins/uavobjectbrowser/treeitem.cpp @@ -161,7 +161,7 @@ void TreeItem::insertChild(TreeItem *child) child->setParentTree(this); } -TreeItem *TreeItem::getChild(int index) +TreeItem *TreeItem::getChild(int index) const { return m_children.value(index); } @@ -250,7 +250,7 @@ void TreeItem::setHighlightManager(HighLightManager *mgr) m_highlightManager = mgr; } -QTime TreeItem::getHiglightExpires() +QTime TreeItem::getHiglightExpires() const { return m_highlightExpires; } diff --git a/ground/gcs/src/plugins/uavobjectbrowser/treeitem.h b/ground/gcs/src/plugins/uavobjectbrowser/treeitem.h index ba7d4564f..481031d09 100644 --- a/ground/gcs/src/plugins/uavobjectbrowser/treeitem.h +++ b/ground/gcs/src/plugins/uavobjectbrowser/treeitem.h @@ -99,7 +99,7 @@ public: void appendChild(TreeItem *child); void insertChild(TreeItem *child); - TreeItem *getChild(int index); + TreeItem *getChild(int index) const; inline QList treeChildren() const { return m_children; @@ -107,12 +107,13 @@ public: int childCount() const; int columnCount() const; virtual QVariant data(int column = 1) const; - QString description() + QString description() const { return m_description; } - void setDescription(QString d) // Split around 40 characters + void setDescription(QString d) { + // Split around 40 characters int idx = d.indexOf(" ", 40); d.insert(idx, QString("
")); @@ -123,7 +124,7 @@ public: // other columns are initialized in constructor virtual void setData(QVariant value, int column = 1); int row() const; - TreeItem *parent() + TreeItem *parent() const { return m_parent; } @@ -131,14 +132,14 @@ public: { m_parent = parent; } - inline virtual bool isEditable() + inline virtual bool isEditable() const { return false; } virtual void update(); virtual void apply(); - inline bool highlighted() + inline bool highlighted() const { return m_highlight; } @@ -148,7 +149,7 @@ public: m_highlightTimeMs = time; } - inline bool changed() + inline bool changed() const { return m_changed; } @@ -159,11 +160,11 @@ public: virtual void setHighlightManager(HighLightManager *mgr); - QTime getHiglightExpires(); + QTime getHiglightExpires() const; virtual void removeHighlight(); - int nameIndex(QString name) + int nameIndex(QString name) const { for (int i = 0; i < childCount(); ++i) { if (name < getChild(i)->data(0).toString()) { @@ -173,7 +174,7 @@ public: return childCount(); } - TreeItem *findChildByName(QString name) + TreeItem *findChildByName(QString name) const { foreach(TreeItem * child, m_children) { if (name == child->data(0).toString()) { @@ -219,7 +220,7 @@ public: emit updateIsKnown(this); } } - virtual bool isKnown() + virtual bool isKnown() const { return true; }