2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup UAVObjectBrowserPlugin UAVObject Browser Plugin
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
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
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>
46 * Small utility class that handles the higlighting of
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
{
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
);
75 // Timer callback method.
76 void checkItemsExpired();
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.
89 class TreeItem
: public QObject
{
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);
99 void appendChild(TreeItem
*child
);
100 void insertChild(TreeItem
*child
);
102 TreeItem
*getChild(int index
) const;
103 inline QList
<TreeItem
*> treeChildren() const
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
);
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);
127 TreeItem
*parent() const
131 void setParentTree(TreeItem
*parent
)
135 inline virtual bool isEditable() const
139 virtual void update();
140 virtual void apply();
142 inline bool highlighted() const
146 void setHighlight(bool highlight
);
147 static void setHighlightTime(int time
)
149 m_highlightTimeMs
= time
;
152 inline bool changed() const
156 inline void setChanged(bool 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()) {
177 TreeItem
*findChildByName(QString name
) const
179 foreach(TreeItem
* child
, m_children
) {
180 if (name
== child
->data(0).toString()) {
187 static int maxHexStringLength(UAVObjectField::FieldType type
)
190 case UAVObjectField::INT8
:
193 case UAVObjectField::INT16
:
196 case UAVObjectField::INT32
:
199 case UAVObjectField::UINT8
:
202 case UAVObjectField::UINT16
:
205 case UAVObjectField::UINT32
:
213 void updateIsKnown(bool isKnown
)
215 if (isKnown
!= this->isKnown()) {
217 foreach(TreeItem
* child
, m_children
) {
218 child
->updateIsKnown(isKnown
);
220 emit
updateIsKnown(this);
223 virtual bool isKnown() const
229 void updateHighlight(TreeItem
*item
);
230 void updateIsKnown(TreeItem
*item
);
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
;
244 QTime m_highlightExpires
;
245 HighLightManager
*m_highlightManager
;
248 class DataObjectTreeItem
;
249 class MetaObjectTreeItem
;
251 class TopTreeItem
: public TreeItem
{
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();
280 QHash
<quint32
, DataObjectTreeItem
*> m_objectTreeItemsPerObjectIds
;
281 QHash
<quint32
, MetaObjectTreeItem
*> m_metaObjectTreeItemsPerObjectIds
;
284 class ObjectTreeItem
: public TreeItem
{
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()
303 return !m_obj
->isSettingsObject() || m_obj
->isKnown();
310 class MetaObjectTreeItem
: public ObjectTreeItem
{
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
)
322 return parent()->isKnown();
326 class DataObjectTreeItem
: public ObjectTreeItem
{
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
) {}
335 foreach(TreeItem
* child
, treeChildren()) {
336 MetaObjectTreeItem
*metaChild
= dynamic_cast<MetaObjectTreeItem
*>(child
);
343 virtual void update()
345 foreach(TreeItem
* child
, treeChildren()) {
346 MetaObjectTreeItem
*metaChild
= dynamic_cast<MetaObjectTreeItem
*>(child
);
355 class InstanceTreeItem
: public DataObjectTreeItem
{
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
)
368 virtual void update()
374 class ArrayFieldTreeItem
: public TreeItem
{
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;
384 return parent()->isKnown();
388 UAVObjectField
*m_field
;