dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
treeitem.h
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  *
30  * Additional note on redistribution: The copyright and license notices above
31  * must be maintained in each individual source file that is a derivative work
32  * of this source file; otherwise redistribution is prohibited.
33  */
34 
35 #ifndef TREEITEM_H
36 #define TREEITEM_H
37 
38 #include "uavobjects/uavobject.h"
42 #include <QtCore/QList>
43 #include <QtCore/QLinkedList>
44 #include <QtCore/QMap>
45 #include <QtCore/QVariant>
46 #include <QtCore/QTime>
47 #include <QtCore/QTimer>
48 #include <QtCore/QObject>
49 #include <QtCore/QDebug>
50 
51 class TreeItem;
52 
53 /*
54 * Small utility class that handles the higlighting of
55 * tree grid items.
56 * Basicly it maintains all items due to be restored to
57 * non highlighted state in a linked list.
58 * A timer traverses this list periodically to find out
59 * if any of the items should be restored. All items are
60 * updated withan expiration timestamp when they expires.
61 * An item that is beeing restored is removed from the
62 * list and its removeHighlight() method is called. Items
63 * that are not expired are left in the list til next time.
64 * Items that are updated during the expiration time are
65 * left untouched in the list. This reduces unwanted emits
66 * of signals to the repaint/update function.
67 */
68 class HighLightManager : public QObject
69 {
70  Q_OBJECT
71 public:
72  // Constructor taking the checking interval in ms.
73  HighLightManager(long checkingInterval);
74 
75  // This is called when an item has been set to
76  // highlighted = true.
77  bool add(TreeItem *itemToAdd);
78 
79 signals:
80  void updateHighlight(TreeItem *);
81 
82 private slots:
83  // Timer callback method.
84  void checkItemsExpired();
85 
86 private:
87  // The timer checking highlight expiration.
88  QTimer m_expirationTimer;
89 
90  // The sets holding all items due to be updated.
91  QSet<TreeItem *> m_itemsToHighlight;
92  QSet<TreeItem *> m_itemsWaiting;
93  QSet<TreeItem *> m_itemsToUnhighlight;
94 };
95 
96 class TreeItem : public QObject
97 {
98  Q_OBJECT
99 protected:
101 
102 public:
103  TreeItem(const QList<QVariant> &data, TreeItem *parent = nullptr);
104  TreeItem(const QVariant &data, TreeItem *parent = nullptr);
105  virtual ~TreeItem();
106 
107  virtual void appendChild(TreeItem *child);
108  virtual void removeChild(TreeItem *child);
109  void insertChild(TreeItem *child);
110 
111  TreeItem *getChild(int index);
112  inline QList<TreeItem *> treeChildren() const { return m_children; }
113  int childCount() const;
114  int columnCount() const;
115  QVariant data(int column = 1) const;
116  QString description() { return m_description; }
117  void setDescription(QString d)
118  {
119  d = d.trimmed().toHtmlEscaped();
120  if (!d.isEmpty()) {
121  // insert html tags to make this rich text so Qt will take care of wrapping
122  d.prepend("<span style='font-style: normal'>");
123  d.remove("@Ref", Qt::CaseInsensitive);
124  d.append("</span>");
125  m_description = d;
126  }
127  }
128  // only column 1 (TreeItem::dataColumn) is changed with setData currently
129  // other columns are initialized in constructor
130  virtual void setData(QVariant value, int column = 1);
131  int row() const;
132  TreeItem *parent() { return m_parent; }
133  void setParentTree(TreeItem *parent) { m_parent = parent; }
134  inline virtual bool isEditable() { return false; }
135  virtual void update();
136  virtual void apply();
137 
138  inline bool highlighted() { return m_highlight; }
139  void setHighlight();
140 
141  inline bool changed() { return m_changed; }
142  inline bool updatedOnly() { return m_updated; }
143  inline virtual bool getIsPresentOnHardware() const { return isPresentOnHardware; }
144  virtual void setIsPresentOnHardware(bool value)
145  {
146  foreach (TreeItem *item, treeChildren()) {
147  item->setIsPresentOnHardware(value);
148  }
149  isPresentOnHardware = value;
150  }
151  inline void setChanged(bool changed) { m_changed = changed; }
152  void setUpdatedOnly(bool updated);
153  void setUpdatedOnlyParent();
154  virtual void setHighlightManager(HighLightManager *mgr);
155 
156  virtual void removeHighlight();
157 
158  int nameIndex(QString name)
159  {
160  for (int i = 0; i < childCount(); ++i) {
161  if (name < getChild(i)->data(0).toString())
162  return i;
163  }
164  return childCount();
165  }
166 
167  TreeItem *findChildByName(QString name)
168  {
169  foreach (TreeItem *child, m_children) {
170  if (name == child->data(0).toString()) {
171  return child;
172  }
173  }
174  return nullptr;
175  }
176 
177  virtual bool isDefaultValue() const { return true; }
178 
179 protected:
180  bool childrenAreDefaultValue() const {
181  bool ret = true;
182  for (const auto item : treeChildren())
183  ret &= item->isDefaultValue();
184  return ret;
185  }
186 
187 private slots:
188 
189 private:
190  QList<TreeItem *> m_children;
191  // m_data contains: [0] property name, [1] value, [2] unit
192  QList<QVariant> m_data;
193  QString m_description;
194  TreeItem *m_parent;
195  bool m_highlight;
196  bool m_changed;
197  bool m_updated;
198  HighLightManager *m_highlightManager;
199  static int m_highlightTimeMs;
200 
201 public:
202  static const int dataColumn = 1;
203 };
204 
205 class DataObjectTreeItem;
206 class MetaObjectTreeItem;
207 
208 class TopTreeItem : public TreeItem
209 {
210  Q_OBJECT
211 public:
213  : TreeItem(data, parent)
214  {
215  }
216  TopTreeItem(const QVariant &data, TreeItem *parent = nullptr)
217  : TreeItem(data, parent)
218  {
219  }
220 
221  void addObjectTreeItem(quint32 objectId, DataObjectTreeItem *oti)
222  {
223  m_objectTreeItemsPerObjectIds[objectId] = oti;
224  }
225 
227  {
228  return m_objectTreeItemsPerObjectIds.contains(objectId)
229  ? m_objectTreeItemsPerObjectIds[objectId]
230  : 0;
231  }
232 
233  void addMetaObjectTreeItem(quint32 objectId, MetaObjectTreeItem *oti)
234  {
235  m_metaObjectTreeItemsPerObjectIds[objectId] = oti;
236  }
237 
239  {
240  return m_metaObjectTreeItemsPerObjectIds.contains(objectId)
241  ? m_metaObjectTreeItemsPerObjectIds[objectId]
242  : 0;
243  }
246 
247 private:
248  QMap<quint32, DataObjectTreeItem *> m_objectTreeItemsPerObjectIds;
249  QMap<quint32, MetaObjectTreeItem *> m_metaObjectTreeItemsPerObjectIds;
250 };
251 
252 class ObjectTreeItem : public TreeItem
253 {
254  Q_OBJECT
255 public:
257  : TreeItem(data, parent)
258  , m_obj(nullptr)
259  {
260  }
261  ObjectTreeItem(const QVariant &data, TreeItem *parent = nullptr)
262  : TreeItem(data, parent)
263  , m_obj(nullptr)
264  {
265  }
266  virtual void setObject(UAVObject *obj)
267  {
268  m_obj = obj;
270  }
271  inline UAVObject *object() { return m_obj; }
272 
273 private:
274  UAVObject *m_obj;
275 };
276 
278 {
279  Q_OBJECT
280 public:
282  : ObjectTreeItem(data, parent)
283  {
284  setObject(obj);
285  }
286  MetaObjectTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent = nullptr)
287  : ObjectTreeItem(data, parent)
288  {
289  setObject(obj);
290  }
291 };
292 
294 {
295  Q_OBJECT
296 public:
298  : ObjectTreeItem(data, parent)
299  {
300  }
301  DataObjectTreeItem(const QVariant &data, TreeItem *parent = nullptr)
302  : ObjectTreeItem(data, parent)
303  {
304  }
305  virtual void apply() override
306  {
307  foreach (TreeItem *child, treeChildren()) {
308  MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
309  if (!metaChild)
310  child->apply();
311  }
312  }
313  virtual void update() override
314  {
315  foreach (TreeItem *child, treeChildren()) {
316  MetaObjectTreeItem *metaChild = dynamic_cast<MetaObjectTreeItem *>(child);
317  if (!metaChild)
318  child->update();
319  }
320  }
321  virtual void appendChild(TreeItem *child) override
322  {
325  }
326  virtual void setObject(UAVObject *obj) override
327  {
328  UAVDataObject *dobj = dynamic_cast<UAVDataObject *>(obj);
329  if (dobj) {
331  connect(dobj, QOverload<UAVDataObject *>::of(&UAVDataObject::presentOnHardwareChanged),
334  } else {
335  foreach (TreeItem *item, treeChildren()) {
336  DataObjectTreeItem *inst = dynamic_cast<DataObjectTreeItem *>(item);
337  if (!inst)
338  item->setIsPresentOnHardware(false);
339  }
340  isPresentOnHardware = false;
341  }
342  }
343  virtual void setIsPresentOnHardware(bool value) override
344  {
345  foreach (TreeItem *item, treeChildren()) {
346  if (!dynamic_cast<DataObjectTreeItem *>(item))
347  item->setIsPresentOnHardware(value);
348  }
349  isPresentOnHardware = value;
350  }
351 
352  virtual bool isDefaultValue() const override { return childrenAreDefaultValue(); }
353 
354 
355 protected slots:
357  {
358  foreach (TreeItem *item, treeChildren()) {
360  }
362  }
363 };
364 
366 {
367  Q_OBJECT
368 public:
370  : DataObjectTreeItem(data, parent)
371  {
372  setObject(obj);
373  }
374  InstanceTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent = nullptr)
375  : DataObjectTreeItem(data, parent)
376  {
377  setObject(obj);
378  }
379  virtual void apply() { TreeItem::apply(); }
380  virtual void update() { TreeItem::update(); }
381 protected slots:
383  {
384  foreach (TreeItem *item, treeChildren()) {
386  }
388  bool disable = true;
389  if (parent()) {
390  if (!isPresentOnHardware)
391  parent()->setIsPresentOnHardware(false);
392  else {
393  foreach (TreeItem *item, parent()->treeChildren()) {
394  InstanceTreeItem *inst = dynamic_cast<InstanceTreeItem *>(item);
395  if (inst && inst->getIsPresentOnHardware())
396  disable = false;
397  }
398  parent()->setIsPresentOnHardware(!disable);
399  }
400  }
401  }
402 };
403 
405 {
406  Q_OBJECT
407 public:
409  : TreeItem(data, parent)
410  {
411  }
412  ArrayFieldTreeItem(const QVariant &data, TreeItem *parent = nullptr)
413  : TreeItem(data, parent)
414  {
415  }
416 
417  virtual bool isDefaultValue() const override { return childrenAreDefaultValue(); }
418 };
419 
420 #endif // TREEITEM_H
virtual void doRefreshHiddenObjects(UAVDataObject *dobj)
Definition: treeitem.h:382
MetaObjectTreeItem * findMetaObjectTreeItemByObjectId(quint32 objectId)
Definition: treeitem.h:238
TreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.cpp:99
void updateHighlight(TreeItem *)
void insertChild(TreeItem *child)
Definition: treeitem.cpp:138
DataObjectTreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:297
virtual void setIsPresentOnHardware(bool value)
Definition: treeitem.h:144
bool add(TreeItem *itemToAdd)
Definition: treeitem.cpp:51
bool highlighted()
Definition: treeitem.h:138
TopTreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:212
InstanceTreeItem(UAVObject *obj, const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:369
virtual void apply()
Definition: treeitem.h:379
int columnCount() const
Definition: treeitem.cpp:163
QList< MetaObjectTreeItem * > getMetaObjectItems()
Definition: treeitem.cpp:241
virtual void update() override
Definition: treeitem.h:313
bool updatedOnly()
Definition: treeitem.h:142
for i
Definition: OPPlots.m:140
UAVObject * object()
Definition: treeitem.h:271
TreeItem * parent()
Definition: treeitem.h:132
int row() const
Definition: treeitem.cpp:155
DataObjectTreeItem(const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:301
virtual bool isEditable()
Definition: treeitem.h:134
virtual void setIsPresentOnHardware(bool value) override
Definition: treeitem.h:343
virtual void removeChild(TreeItem *child)
Definition: treeitem.cpp:133
virtual void setObject(UAVObject *obj) override
Definition: treeitem.h:326
virtual void apply()
Definition: treeitem.cpp:184
bool isPresentOnHardware
Definition: treeitem.h:100
QString getDescription()
Definition: uavobject.cpp:139
virtual void removeHighlight()
Definition: treeitem.cpp:231
void setParentTree(TreeItem *parent)
Definition: treeitem.h:133
ObjectTreeItem(const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:261
ObjectTreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:256
QList< TreeItem * > treeChildren() const
Definition: treeitem.h:112
TreeItem * findChildByName(QString name)
Definition: treeitem.h:167
void addObjectTreeItem(quint32 objectId, DataObjectTreeItem *oti)
Definition: treeitem.h:221
virtual bool getIsPresentOnHardware() const
Definition: treeitem.h:143
virtual bool isDefaultValue() const override
Definition: treeitem.h:417
virtual void setObject(UAVObject *obj)
Definition: treeitem.h:266
virtual void setHighlightManager(HighLightManager *mgr)
Definition: treeitem.cpp:236
void setUpdatedOnlyParent()
Definition: treeitem.cpp:221
bool getIsPresentOnHardware() const
QList< DataObjectTreeItem * > getDataObjectItems()
Definition: treeitem.cpp:246
ArrayFieldTreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:408
virtual void appendChild(TreeItem *child)
Definition: treeitem.cpp:127
MetaObjectTreeItem(UAVObject *obj, const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.h:281
InstanceTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:374
bool childrenAreDefaultValue() const
Definition: treeitem.h:180
ArrayFieldTreeItem(const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:412
virtual void setData(QVariant value, int column=1)
Definition: treeitem.cpp:173
TreeItem * getChild(int index)
Definition: treeitem.cpp:145
QVariant data(int column=1) const
Definition: treeitem.cpp:168
virtual void update()
Definition: treeitem.cpp:178
DataObjectTreeItem * findDataObjectTreeItemByObjectId(quint32 objectId)
Definition: treeitem.h:226
void setChanged(bool changed)
Definition: treeitem.h:151
virtual void appendChild(TreeItem *child) override
Definition: treeitem.h:321
void setDescription(QString d)
Definition: treeitem.h:117
virtual void update()
Definition: treeitem.h:380
void addMetaObjectTreeItem(quint32 objectId, MetaObjectTreeItem *oti)
Definition: treeitem.h:233
void setHighlight()
Definition: treeitem.cpp:193
int childCount() const
Definition: treeitem.cpp:150
virtual bool isDefaultValue() const override
Definition: treeitem.h:352
void setUpdatedOnly(bool updated)
Definition: treeitem.cpp:209
virtual bool isDefaultValue() const
Definition: treeitem.h:177
virtual void apply() override
Definition: treeitem.h:305
int nameIndex(QString name)
Definition: treeitem.h:158
virtual void doRefreshHiddenObjects(UAVDataObject *dobj)
Definition: treeitem.h:356
MetaObjectTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:286
QString description()
Definition: treeitem.h:116
TopTreeItem(const QVariant &data, TreeItem *parent=nullptr)
Definition: treeitem.h:216
virtual ~TreeItem()
Definition: treeitem.cpp:122
static const int dataColumn
Definition: treeitem.h:202
void presentOnHardwareChanged(UAVDataObject *)
bool changed()
Definition: treeitem.h:141
HighLightManager(long checkingInterval)
Definition: treeitem.cpp:40