Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / pathactioneditor / treeitem.h
blob376b24163c6e831d88d6a34a513bc8e490a4e1a2
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/QVariant>
36 #include <QtCore/QObject>
39 class TreeItem : public QObject {
40 Q_OBJECT
41 public:
42 TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
43 TreeItem(const QVariant &data, TreeItem *parent = 0);
44 virtual ~TreeItem();
46 void appendChild(TreeItem *child);
47 void insert(int index, TreeItem *child);
49 TreeItem *child(int row);
50 inline QList<TreeItem *> treeChildren() const
52 return m_children;
54 int childCount() const;
55 int columnCount() const;
56 QVariant data(int column = 1) const;
57 QString description()
59 return m_description;
61 void setDescription(QString d) // Split around 40 characters
63 int idx = d.indexOf(" ", 40);
65 d.insert(idx, QString("<br>"));
66 d.remove("@Ref", Qt::CaseInsensitive);
67 m_description = d;
69 // only column 1 (TreeItem::dataColumn) is changed with setData currently
70 // other columns are initialized in constructor
71 virtual void setData(QVariant value, int column = 1);
72 int row() const;
73 TreeItem *parent()
75 return m_parent;
77 void setParentTree(TreeItem *parent)
79 m_parent = parent;
81 inline virtual bool isEditable()
83 return false;
85 virtual void update();
86 virtual void apply();
88 inline bool highlighted()
90 return m_highlight;
92 void setHighlight(bool highlight);
93 void setActive(bool highlight);
95 inline bool changed()
97 return m_changed;
99 inline void setChanged(bool changed)
101 m_changed = changed; if (changed) {
102 emit updateHighlight(this);
106 signals:
107 void updateHighlight(TreeItem *);
109 private slots:
110 void removeHighlight();
112 private:
113 QList<TreeItem *> m_children;
114 // m_data contains: [0] property name, [1] value, [2] unit
115 QList<QVariant> m_data;
116 QString m_description;
117 TreeItem *m_parent;
118 bool m_highlight;
119 bool m_changed;
120 public:
121 static const int dataColumn = 1;
122 private:
125 class TopTreeItem : public TreeItem {
126 Q_OBJECT
127 public:
128 TopTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
129 TopTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
131 QList<quint32> objIds()
133 return m_objIds;
135 void addObjId(quint32 objId)
137 m_objIds.append(objId);
139 void insertObjId(int index, quint32 objId)
141 m_objIds.insert(index, objId);
143 int nameIndex(QString name)
145 for (int i = 0; i < childCount(); ++i) {
146 if (name < child(i)->data(0).toString()) {
147 return i;
150 return childCount();
153 private:
154 QList<quint32> m_objIds;
157 class ObjectTreeItem : public TreeItem {
158 Q_OBJECT
159 public:
160 ObjectTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) :
161 TreeItem(data, parent), m_obj(0) {}
162 ObjectTreeItem(const QVariant &data, TreeItem *parent = 0) :
163 TreeItem(data, parent), m_obj(0) {}
164 void setObject(UAVObject *obj)
166 m_obj = obj; setDescription(obj->getDescription());
168 inline UAVObject *object()
170 return m_obj;
172 private:
173 UAVObject *m_obj;
176 class MetaObjectTreeItem : public ObjectTreeItem {
177 Q_OBJECT
178 public:
179 MetaObjectTreeItem(UAVObject *obj, const QList<QVariant> &data, TreeItem *parent = 0) :
180 ObjectTreeItem(data, parent)
182 setObject(obj);
184 MetaObjectTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent = 0) :
185 ObjectTreeItem(data, parent)
187 setObject(obj);
191 class DataObjectTreeItem : public ObjectTreeItem {
192 Q_OBJECT
193 public:
194 DataObjectTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) :
195 ObjectTreeItem(data, parent) {}
196 DataObjectTreeItem(const QVariant &data, TreeItem *parent = 0) :
197 ObjectTreeItem(data, parent) {}
198 virtual void apply()
200 foreach(TreeItem * child, treeChildren()) {
201 MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
203 if (!metaChild) {
204 child->apply();
208 virtual void update()
210 foreach(TreeItem * child, treeChildren()) {
211 MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
213 if (!metaChild) {
214 child->update();
220 class InstanceTreeItem : public DataObjectTreeItem {
221 Q_OBJECT
222 public:
223 InstanceTreeItem(UAVObject *obj, const QList<QVariant> &data, TreeItem *parent = 0) :
224 DataObjectTreeItem(data, parent)
226 setObject(obj);
228 InstanceTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent = 0) :
229 DataObjectTreeItem(data, parent)
231 setObject(obj);
233 virtual void apply()
235 TreeItem::apply();
237 virtual void update()
239 TreeItem::update();
243 class ArrayFieldTreeItem : public TreeItem {
244 Q_OBJECT
245 public:
246 ArrayFieldTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
247 ArrayFieldTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) {}
250 #endif // TREEITEM_H