1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

OP-9 GCS/uavobjectbrowser: Initial support for highlight of recently updated objects, dummy testing with enums is possible.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@485 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
ephy 2010-04-12 05:54:54 +00:00 committed by ephy
parent 3581e37627
commit a57ce6bc43
5 changed files with 153 additions and 43 deletions

View File

@ -29,13 +29,14 @@
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) :
m_data(data),
m_parent(parent)
m_parent(parent),
m_highlight(false)
{
}
TreeItem::TreeItem(const QVariant &data, TreeItem *parent) :
m_parent(parent)
m_parent(parent),
m_highlight(false)
{
m_data << data << "" << "";
}
@ -84,7 +85,7 @@ QVariant TreeItem::data(int column) const
return m_data.value(column);
}
void TreeItem::setData(int column, QVariant value)
void TreeItem::setData(QVariant value, int column)
{
m_data.replace(column, value);
}

View File

@ -69,20 +69,27 @@ class TreeItem
QList<TreeItem*> children() const { return m_children; }
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
QVariant data(int column = 1) const;
// only column 1 is changed with setData currently
// other columns are initialized in constructor
virtual void setData(int column, QVariant value);
virtual void setData(QVariant value, int column = 1);
int row() const;
TreeItem *parent() { return m_parent; }
void setParent(TreeItem *parent) { m_parent = parent; }
virtual bool isEditable() { return false; }
inline virtual bool isEditable() { return false; }
virtual void update() {
foreach(TreeItem *child, m_children)
child->update();
}
virtual void setHighlight(bool highlight) { m_highlight = highlight; }
inline virtual bool highlight() { return m_highlight; }
private:
QList<TreeItem*> m_children;
// m_data contains: [0] property name, [1] value, and [2] unit
QList<QVariant> m_data;
TreeItem *m_parent;
bool m_highlight;
};
class TopTreeItem : public TreeItem
@ -109,8 +116,14 @@ private:
class DataObjectTreeItem : public TreeItem
{
public:
DataObjectTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
DataObjectTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
DataObjectTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) :
TreeItem(data, parent), m_obj(0), m_highlight(false) { }
DataObjectTreeItem(const QVariant &data, TreeItem *parent = 0) :
TreeItem(data, parent), m_obj(0), m_highlight(false) { }
void setObject(UAVObject *obj) { m_obj = obj; }
inline UAVObject *object() { return m_obj; }
private:
UAVObject *m_obj;
};
class MetaObjectTreeItem : public TreeItem
@ -118,13 +131,16 @@ class MetaObjectTreeItem : public TreeItem
public:
MetaObjectTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
MetaObjectTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
void update() { }
};
class InstanceTreeItem : public TreeItem
class InstanceTreeItem : public DataObjectTreeItem
{
public:
InstanceTreeItem(const QList<QVariant> &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
InstanceTreeItem(const QVariant &data, TreeItem *parent = 0) : TreeItem(data, parent) { }
InstanceTreeItem(UAVObject *obj, const QList<QVariant> &data, TreeItem *parent = 0) :
DataObjectTreeItem(data, parent) { setObject(obj); }
InstanceTreeItem(UAVObject *obj, const QVariant &data, TreeItem *parent = 0) :
DataObjectTreeItem(data, parent) { setObject(obj); }
};
class ArrayFieldTreeItem : public TreeItem
@ -163,14 +179,26 @@ public:
EnumFieldTreeItem(UAVObjectFieldEnum *field, int index, const QVariant &data,
TreeItem *parent = 0) :
FieldTreeItem(index, data, parent), enumOptions(field->getOptions()), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getSelectedIndex(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
apply(column);
}
void apply(int column) {
int value = data(column).toInt();
m_field->setSelectedIndex(value, m_index);
m_field->setSelectedIndex(value, m_index);
setChanged(false);
}
void update() {
int value = m_field->getSelectedIndex(m_index);
if (data() != value) {
setHighlight(true);
TreeItem::setData(value);
} else {
setHighlight(false);
}
}
bool isEnum() { return true; }
QStringList enumOptions;
private:
@ -196,9 +224,9 @@ public:
IntFieldTreeItem(index, data, QINT8MIN, QINT8MAX, parent), m_field(field) { }
Int8FieldTreeItem(UAVObjectFieldInt8 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QINT8MIN, QINT8MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toInt());
@ -214,9 +242,9 @@ public:
IntFieldTreeItem(index, data, QINT16MIN, QINT16MAX, parent), m_field(field) { }
Int16FieldTreeItem(UAVObjectFieldInt16 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QINT16MIN, QINT16MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toInt());
@ -232,9 +260,9 @@ public:
IntFieldTreeItem(index, data, QINT32MIN, QINT32MAX, parent), m_field(field) { }
Int32FieldTreeItem(UAVObjectFieldInt32 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QINT32MIN, QINT32MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
int value = data(column).toInt();
@ -251,9 +279,9 @@ public:
IntFieldTreeItem(index, data, QUINTMIN, QUINT8MAX, parent), m_field(field) { }
UInt8FieldTreeItem(UAVObjectFieldUInt8 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QUINTMIN, QUINT8MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toInt());
@ -269,9 +297,9 @@ public:
IntFieldTreeItem(index, data, QUINTMIN, QUINT16MAX, parent), m_field(field) { }
UInt16FieldTreeItem(UAVObjectFieldUInt16 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QUINTMIN, QUINT16MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toInt());
@ -287,9 +315,9 @@ public:
IntFieldTreeItem(index, data, QUINTMIN, QUINT32MAX, parent), m_field(field) { }
UInt32FieldTreeItem(UAVObjectFieldUInt32 *field, int index, const QVariant &data, TreeItem *parent = 0) :
IntFieldTreeItem(index, data, QUINTMIN, QUINT32MAX, parent), m_field(field) { }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toInt());
@ -307,9 +335,9 @@ public:
FloatFieldTreeItem(UAVObjectFieldFloat *field, int index, const QVariant &data, TreeItem *parent = 0) :
FieldTreeItem(index, data, parent), m_field(field) { }
bool isFloatType() { return true; }
void setData(int column, QVariant value) {
void setData(QVariant value, int column) {
setChanged(m_field->getValue(m_index) != value);
TreeItem::setData(column, value);
TreeItem::setData(value, column);
}
void apply(int column) {
m_field->setValue(data(column).toDouble(), m_index);

View File

@ -45,13 +45,12 @@ UAVObjectBrowserWidget::UAVObjectBrowserWidget(QWidget *parent) : QWidget(parent
m_model = new UAVObjectTreeModel();
m_browser->treeView->setModel(m_model);
m_browser->treeView->setColumnWidth(0, 300);
m_browser->treeView->setAllColumnsShowFocus(false);
m_browser->treeView->expandAll();
BrowserItemDelegate *m_delegate = new BrowserItemDelegate();
m_browser->treeView->setItemDelegate(m_delegate);
m_browser->treeView->setEditTriggers(QAbstractItemView::AllEditTriggers);
connect(m_browser->metaCheckBox, SIGNAL(toggled(bool)), this, SLOT(showMetaData(bool)));
showMetaData(m_browser->metaCheckBox->isChecked());
connect(m_browser->metaCheckBox, SIGNAL(toggled(bool)), this, SLOT(showMetaData(bool)));
connect(m_browser->saveSDButton, SIGNAL(clicked()), this, SLOT(saveSettings()));
connect(m_browser->readSDButton, SIGNAL(clicked()), this, SLOT(readSettings()));
}

View File

@ -33,6 +33,8 @@
#include "uavobjects/uavobjectfields.h"
#include "extensionsystem/pluginmanager.h"
#include <QtGui/QColor>
#include <QtCore/QTimer>
#include <QtCore/QSignalMapper>
#include <QtCore/QDebug>
UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent) :
@ -41,6 +43,8 @@ UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent) :
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
m_signalMapper = new QSignalMapper();
connect(m_signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(resetHighlightObject(QObject*)));
connect(objManager, SIGNAL(newObject(UAVObject*)), this, SLOT(newObject(UAVObject*)));
connect(objManager, SIGNAL(newInstance(UAVObject*)), this, SLOT(newObject(UAVObject*)));
@ -106,13 +110,16 @@ void UAVObjectTreeModel::addMetaObject(UAVMetaObject *obj, TreeItem *parent)
void UAVObjectTreeModel::addInstance(UAVObject *obj, TreeItem *parent)
{
connect(obj, SIGNAL(objectUpdatedAuto(UAVObject*)), this, SLOT(highlightUpdatedObject(UAVObject*)));
TreeItem *item;
if (!obj->isSingleInstance()) {
QString name = tr("Instance") + " " + QString::number(obj->getInstID());
item = new InstanceTreeItem(name);
parent->appendChild(item);
} else {
if (obj->isSingleInstance()) {
item = parent;
DataObjectTreeItem *p = static_cast<DataObjectTreeItem*>(parent);
p->setObject(obj);
} else {
QString name = tr("Instance") + " " + QString::number(obj->getInstID());
item = new InstanceTreeItem(obj, name);
parent->appendChild(item);
}
foreach (UAVObjectField *field, obj->getFields()) {
if (field->getNumElements() > 1) {
@ -258,14 +265,15 @@ QVariant UAVObjectTreeModel::data(const QModelIndex &index, int role) const
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
//
// if (index.column() == 1 && role == Qt::ForegroundRole) {
// FieldTreeItem *fieldItem = dynamic_cast<FieldTreeItem*>(item);
// if (fieldItem && fieldItem->isChanged())
// return QVariant(QColor(Qt::darkBlue));
// }
if (index.column() == 0 && role == Qt::BackgroundRole) {
DataObjectTreeItem *dataItem = dynamic_cast<DataObjectTreeItem*>(item);
if (dataItem && dataItem->highlight())
return QVariant(QColor(255, 230, 230));
}
if (index.column() == 1 && role == Qt::BackgroundRole) {
FieldTreeItem *fieldItem = dynamic_cast<FieldTreeItem*>(item);
if (fieldItem && fieldItem->highlight())
return QVariant(QColor(255, 230, 230));
if (fieldItem && fieldItem->isChanged())
return QVariant(QColor(230, 230, 255));
}
@ -287,7 +295,7 @@ QVariant UAVObjectTreeModel::data(const QModelIndex &index, int role) const
bool UAVObjectTreeModel::setData(const QModelIndex &index, const QVariant & value, int role)
{
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
item->setData(index.column(), value);
item->setData(value, index.column());
return true;
}
@ -315,3 +323,65 @@ QVariant UAVObjectTreeModel::headerData(int section, Qt::Orientation orientation
return QVariant();
}
void UAVObjectTreeModel::highlightUpdatedObject(UAVObject *object)
{
UAVDataObject *obj = qobject_cast<UAVDataObject*>(object);
if (!obj)
return;
DataObjectTreeItem * dobj = findDataObjectTreeItem(obj);
if (dobj) {
dobj->setHighlight(true);
dobj->update();
emit dataChanged(QModelIndex(), QModelIndex());
}
// any updates while highlighted will not reset timer
if (m_timerMap.contains(obj))
return;
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
m_timerMap.insert(obj, timer);
connect(timer, SIGNAL(timeout()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(timer, obj);
timer->start(500);
}
void UAVObjectTreeModel::resetHighlightObject(QObject *object)
{
UAVDataObject *obj = qobject_cast<UAVDataObject*>(object);
if (!obj)
return;
QTimer *timer = m_timerMap.value(obj);
delete timer;
m_timerMap.remove(obj);
DataObjectTreeItem * dobj = findDataObjectTreeItem(obj);
if (dobj) {
dobj->setHighlight(false);
dobj->update();
emit dataChanged(QModelIndex(), QModelIndex());
}
}
DataObjectTreeItem *UAVObjectTreeModel::findDataObjectTreeItem(UAVDataObject *obj)
{
TopTreeItem *root = obj->isSettings() ? m_settingsTree : m_nonSettingsTree;
foreach (TreeItem *child, root->children()) {
DataObjectTreeItem *dobj = dynamic_cast<DataObjectTreeItem*>(child);
if (dobj && dobj->object() && dobj->object()->isSingleInstance()) {
if(dobj->object() == obj) {
return dobj;
}
} else {
foreach (TreeItem *c, dobj->children()) {
DataObjectTreeItem *d = dynamic_cast<DataObjectTreeItem*>(c);
if (d && d->object() == obj)
return dobj;
}
}
}
return 0;
}

View File

@ -29,14 +29,18 @@
#define UAVOBJECTTREEMODEL_H
#include <QAbstractItemModel>
#include <QtCore/QMap>
class TreeItem;
class TopTreeItem;
class DataObjectTreeItem;
class UAVObject;
class UAVDataObject;
class UAVMetaObject;
class UAVObjectField;
class UAVObjectManager;
class QSignalMapper;
class QTimer;
class UAVObjectTreeModel : public QAbstractItemModel
{
@ -61,6 +65,11 @@ signals:
public slots:
void newObject(UAVObject *obj);
private slots:
void highlightUpdatedObject(UAVObject *obj);
void resetHighlightObject(QObject *obj);
private:
void addDataObject(UAVDataObject *obj);
void addMetaObject(UAVMetaObject *obj, TreeItem *parent);
@ -70,10 +79,13 @@ private:
void addInstance(UAVObject *obj, TreeItem *parent);
QString updateMode(quint8 updateMode);
void setupModelData(UAVObjectManager *objManager);
DataObjectTreeItem *findDataObjectTreeItem(UAVDataObject *obj);
TreeItem *rootItem;
TopTreeItem *m_settingsTree;
TopTreeItem *m_nonSettingsTree;
QSignalMapper *m_signalMapper;
QMap<UAVObject*,QTimer*> m_timerMap;
};