LP-567 improve treeitem const correcteness
[librepilot.git] / ground / gcs / src / plugins / uavobjectbrowser / treeitem.h
blob481031d094644d966dcaa0fcfb88f9889a9575c4
1 /**
2 ******************************************************************************
4 * @file treeitem.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVObjectBrowserPlugin UAVObject Browser Plugin
9 * @{
10 * @brief The UAVObject Browser gadget plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifndef TREEITEM_H
29 #define TREEITEM_H
31 #include "uavobject.h"
32 #include "uavmetaobject.h"
33 #include "uavobjectfield.h"
34 #include <QtCore/QList>
35 #include <QtCore/QLinkedList>
36 #include <QtCore/QMap>
37 #include <QtCore/QVariant>
38 #include <QtCore/QTime>
39 #include <QtCore/QTimer>
40 #include <QtCore/QObject>
41 #include <QtCore/QDebug>
43 class TreeItem;
46 * Small utility class that handles the higlighting of
47 * tree grid items.
48 * Basicly it maintains all items due to be restored to
49 * non highlighted state in a linked list.
50 * A timer traverses this list periodically to find out
51 * if any of the items should be restored. All items are
52 * updated with an expiration timestamp when they expires.
53 * An item that is beeing restored is removed from the
54 * list and its removeHighlight() method is called. Items
55 * that are not expired are left in the list til next time.
56 * Items that are updated during the expiration time are
57 * left untouched in the list. This reduces unwanted emits
58 * of signals to the repaint/update function.
60 class HighLightManager : public QObject {
61 Q_OBJECT
62 public:
63 HighLightManager();
65 // This is called when an item has been set to
66 // highlighted = true.
67 bool add(TreeItem *itemToAdd);
69 // This is called when an item is set to highlighted = false;
70 bool remove(TreeItem *itemToRemove);
72 bool startTimer(QTime time);
74 private slots:
75 // Timer callback method.
76 void checkItemsExpired();
78 private:
79 // The timer checking highlight expiration.
80 QTimer m_expirationTimer;
82 // The collection holding all items due to be updated.
83 QSet<TreeItem *> m_items;
85 // Mutex to lock when accessing collection.
86 QMutex m_mutex;
89 class TreeItem : public QObject {
90 Q_OBJECT
91 public:
92 static const int TITLE_COLUMN = 0;
93 static const int DATA_COLUMN = 1;
95 TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
96 TreeItem(const QVariant &data, TreeItem *parent = 0);
97 virtual ~TreeItem();
99 void appendChild(TreeItem *child);
100 void insertChild(TreeItem *child);
102 TreeItem *getChild(int index) const;
103 inline QList<TreeItem *> treeChildren() const
105 return m_children;
107 int childCount() const;
108 int columnCount() const;
109 virtual QVariant data(int column = 1) const;
110 QString description() const
112 return m_description;
114 void setDescription(QString d)
116 // Split around 40 characters
117 int idx = d.indexOf(" ", 40);
119 d.insert(idx, QString("<br>"));
120 d.remove("@Ref", Qt::CaseInsensitive);
121 m_description = d;
123 // only column 1 (TreeItem::dataColumn) is changed with setData currently
124 // other columns are initialized in constructor
125 virtual void setData(QVariant value, int column = 1);
126 int row() const;
127 TreeItem *parent() const
129 return m_parent;
131 void setParentTree(TreeItem *parent)
133 m_parent = parent;
135 inline virtual bool isEditable() const
137 return false;
139 virtual void update();
140 virtual void apply();
142 inline bool highlighted() const
144 return m_highlight;
146 void setHighlight(bool highlight);
147 static void setHighlightTime(int time)
149 m_highlightTimeMs = time;
152 inline bool changed() const
154 return m_changed;
156 inline void setChanged(bool changed)
158 m_changed = changed;
161 virtual void setHighlightManager(HighLightManager *mgr);
163 QTime getHiglightExpires() const;
165 virtual void removeHighlight();
167 int nameIndex(QString name) const
169 for (int i = 0; i < childCount(); ++i) {
170 if (name < getChild(i)->data(0).toString()) {
171 return i;
174 return childCount();
177 TreeItem *findChildByName(QString name) const
179 foreach(TreeItem * child, m_children) {
180 if (name == child->data(0).toString()) {
181 return child;
184 return 0;
187 static int maxHexStringLength(UAVObjectField::FieldType type)
189 switch (type) {
190 case UAVObjectField::INT8:
191 return 2;
193 case UAVObjectField::INT16:
194 return 4;
196 case UAVObjectField::INT32:
197 return 8;
199 case UAVObjectField::UINT8:
200 return 2;
202 case UAVObjectField::UINT16:
203 return 4;
205 case UAVObjectField::UINT32:
206 return 8;
208 default:
209 Q_ASSERT(false);
211 return 0;
213 void updateIsKnown(bool isKnown)
215 if (isKnown != this->isKnown()) {
216 m_changed = false;
217 foreach(TreeItem * child, m_children) {
218 child->updateIsKnown(isKnown);
220 emit updateIsKnown(this);
223 virtual bool isKnown() const
225 return true;
228 signals:
229 void updateHighlight(TreeItem *item);
230 void updateIsKnown(TreeItem *item);
232 private slots:
234 private:
235 static int m_highlightTimeMs;
236 QList<TreeItem *> m_children;
238 // m_data contains: [0] property name, [1] value, [2] unit
239 QList<QVariant> m_data;
240 QString m_description;
241 TreeItem *m_parent;
242 bool m_highlight;
243 bool m_changed;
244 QTime m_highlightExpires;
245 HighLightManager *m_highlightManager;
248 class DataObjectTreeItem;
249 class MetaObjectTreeItem;
251 class TopTreeItem : public TreeItem {
252 Q_OBJECT
253 public:
254 TopTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
255 TopTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
257 void addObjectTreeItem(quint32 objectId, DataObjectTreeItem *oti)
259 m_objectTreeItemsPerObjectIds[objectId] = oti;
262 DataObjectTreeItem *findDataObjectTreeItemByObjectId(quint32 objectId)
264 return m_objectTreeItemsPerObjectIds.value(objectId, 0);
267 void addMetaObjectTreeItem(quint32 objectId, MetaObjectTreeItem *oti)
269 m_metaObjectTreeItemsPerObjectIds[objectId] = oti;
272 MetaObjectTreeItem *findMetaObjectTreeItemByObjectId(quint32 objectId)
274 return m_metaObjectTreeItemsPerObjectIds.value(objectId, 0);
277 QList<MetaObjectTreeItem *> getMetaObjectItems();
279 private:
280 QHash<quint32, DataObjectTreeItem *> m_objectTreeItemsPerObjectIds;
281 QHash<quint32, MetaObjectTreeItem *> m_metaObjectTreeItemsPerObjectIds;
284 class ObjectTreeItem : public TreeItem {
285 Q_OBJECT
286 public:
287 ObjectTreeItem(const QList<QVariant> &data, UAVObject *object, TreeItem *parent = 0) :
288 TreeItem(data, parent), m_obj(object)
290 setDescription(m_obj->getDescription());
292 ObjectTreeItem(const QVariant &data, UAVObject *object, TreeItem *parent = 0) :
293 TreeItem(data, parent), m_obj(object)
295 setDescription(m_obj->getDescription());
297 inline UAVObject *object()
299 return m_obj;
301 bool isKnown()
303 return !m_obj->isSettingsObject() || m_obj->isKnown();
306 private:
307 UAVObject *m_obj;
310 class MetaObjectTreeItem : public ObjectTreeItem {
311 Q_OBJECT
312 public:
313 MetaObjectTreeItem(UAVObject *object, const QList<QVariant> &data, TreeItem *parent = 0) :
314 ObjectTreeItem(data, object, parent)
316 MetaObjectTreeItem(UAVObject *object, const QVariant &data, TreeItem *parent = 0) :
317 ObjectTreeItem(data, object, parent)
320 bool isKnown()
322 return parent()->isKnown();
326 class DataObjectTreeItem : public ObjectTreeItem {
327 Q_OBJECT
328 public:
329 DataObjectTreeItem(const QList<QVariant> &data, UAVObject *object, TreeItem *parent = 0) :
330 ObjectTreeItem(data, object, parent) {}
331 DataObjectTreeItem(const QVariant &data, UAVObject *object, TreeItem *parent = 0) :
332 ObjectTreeItem(data, object, parent) {}
333 virtual void apply()
335 foreach(TreeItem * child, treeChildren()) {
336 MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
338 if (!metaChild) {
339 child->apply();
343 virtual void update()
345 foreach(TreeItem * child, treeChildren()) {
346 MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
348 if (!metaChild) {
349 child->update();
355 class InstanceTreeItem : public DataObjectTreeItem {
356 Q_OBJECT
357 public:
358 InstanceTreeItem(UAVObject *object, const QList<QVariant> &data, TreeItem *parent = 0) :
359 DataObjectTreeItem(data, object, parent)
361 InstanceTreeItem(UAVObject *object, const QVariant &data, TreeItem *parent = 0) :
362 DataObjectTreeItem(data, object, parent)
364 virtual void apply()
366 TreeItem::apply();
368 virtual void update()
370 TreeItem::update();
374 class ArrayFieldTreeItem : public TreeItem {
375 Q_OBJECT
376 public:
377 ArrayFieldTreeItem(UAVObjectField *field, const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent), m_field(field)
379 ArrayFieldTreeItem(UAVObjectField *field, const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent), m_field(field)
381 QVariant data(int column) const;
382 bool isKnown()
384 return parent()->isKnown();
387 private:
388 UAVObjectField *m_field;
391 #endif // TREEITEM_H