1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

Changed the way change hilight of three cells works to bring down the

number of QTimer instances and the number of redraws to hopefully
increase performance.
Changed so that when a child gets hilighted it will hilight its parent.
This commit is contained in:
Fredrik Arvidsson 2012-06-16 17:38:05 +02:00
parent c1689b2205
commit d03282d9b0
4 changed files with 102 additions and 12 deletions

View File

@ -27,6 +27,45 @@
#include "treeitem.h" #include "treeitem.h"
HighLightManager::HighLightManager(long checkingInterval)
{
m_expirationTimer.start(checkingInterval);
connect(&m_expirationTimer, SIGNAL(timeout()), this, SLOT(checkItemsExpired()));
}
bool HighLightManager::add(TreeItem *itemToAdd)
{
QMutexLocker locker(&m_listMutex);
if(!m_itemsList.contains(itemToAdd))
{
m_itemsList.append(itemToAdd);
return true;
}
return false;
}
bool HighLightManager::remove(TreeItem *itemToRemove)
{
QMutexLocker locker(&m_listMutex);
return m_itemsList.removeOne(itemToRemove);
}
void HighLightManager::checkItemsExpired()
{
QMutexLocker locker(&m_listMutex);
QMutableLinkedListIterator<TreeItem*> iter(m_itemsList);
QTime now = QTime::currentTime();
while(iter.hasNext())
{
TreeItem* item = iter.next();
if(item->getHiglightExpires() < now)
{
item->removeHighlight();
iter.remove();
}
}
}
int TreeItem::m_highlightTimeMs = 500; int TreeItem::m_highlightTimeMs = 500;
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) : TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) :
@ -36,7 +75,6 @@ TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) :
m_highlight(false), m_highlight(false),
m_changed(false) m_changed(false)
{ {
connect(&m_timer, SIGNAL(timeout()), this, SLOT(removeHighlight()));
} }
TreeItem::TreeItem(const QVariant &data, TreeItem *parent) : TreeItem::TreeItem(const QVariant &data, TreeItem *parent) :
@ -46,7 +84,6 @@ TreeItem::TreeItem(const QVariant &data, TreeItem *parent) :
m_changed(false) m_changed(false)
{ {
m_data << data << "" << ""; m_data << data << "" << "";
connect(&m_timer, SIGNAL(timeout()), this, SLOT(removeHighlight()));
} }
TreeItem::~TreeItem() TreeItem::~TreeItem()
@ -112,17 +149,34 @@ void TreeItem::setHighlight(bool highlight) {
m_highlight = highlight; m_highlight = highlight;
m_changed = false; m_changed = false;
if (highlight) { if (highlight) {
if (m_timer.isActive()) { m_highlightExpires = QTime::currentTime().addMSecs(m_highlightTimeMs);
m_timer.stop(); if(m_highlightManager->add(this))
} {
m_timer.setSingleShot(true);
m_timer.start(m_highlightTimeMs);
}
emit updateHighlight(this); emit updateHighlight(this);
}
}
else if(m_highlightManager->remove(this))
{
emit updateHighlight(this);
}
if(m_parent)
{
m_parent->setHighlight(highlight);
}
} }
void TreeItem::removeHighlight() { void TreeItem::removeHighlight() {
m_highlight = false; m_highlight = false;
update(); //update();
emit updateHighlight(this); emit updateHighlight(this);
} }
void TreeItem::setHighlightManager(HighLightManager *mgr)
{
m_highlightManager = mgr;
}
QTime TreeItem::getHiglightExpires()
{
return m_highlightExpires;
}

View File

@ -32,10 +32,31 @@
#include "uavmetaobject.h" #include "uavmetaobject.h"
#include "uavobjectfield.h" #include "uavobjectfield.h"
#include <QtCore/QList> #include <QtCore/QList>
#include <QtCore/QLinkedList>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QTCore/QTime>
#include <QtCore/QTimer> #include <QtCore/QTimer>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QDebug>
class TreeItem;
class HighLightManager : public QObject
{
Q_OBJECT
public:
HighLightManager(long checkingInterval);
bool add(TreeItem* itemToAdd);
bool remove(TreeItem* itemToRemove);
private slots:
void checkItemsExpired();
private:
QTimer m_expirationTimer;
QLinkedList<TreeItem*> m_itemsList;
QMutex m_listMutex;
};
class TreeItem : public QObject class TreeItem : public QObject
{ {
@ -77,11 +98,16 @@ public:
inline bool changed() { return m_changed; } inline bool changed() { return m_changed; }
inline void setChanged(bool changed) { m_changed = changed; } inline void setChanged(bool changed) { m_changed = changed; }
virtual void setHighlightManager(HighLightManager* mgr);
QTime getHiglightExpires();
virtual void removeHighlight();
signals: signals:
void updateHighlight(TreeItem*); void updateHighlight(TreeItem*);
private slots: private slots:
void removeHighlight();
private: private:
QList<TreeItem*> m_children; QList<TreeItem*> m_children;
@ -91,7 +117,8 @@ private:
TreeItem *m_parent; TreeItem *m_parent;
bool m_highlight; bool m_highlight;
bool m_changed; bool m_changed;
QTimer m_timer; QTime m_highlightExpires;
HighLightManager* m_highlightManager;
public: public:
static const int dataColumn = 1; static const int dataColumn = 1;
private: private:

View File

@ -46,7 +46,7 @@ UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent) :
{ {
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>(); UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
m_highlightManager = new HighLightManager(300);
connect(objManager, SIGNAL(newObject(UAVObject*)), this, SLOT(newObject(UAVObject*))); connect(objManager, SIGNAL(newObject(UAVObject*)), this, SLOT(newObject(UAVObject*)));
connect(objManager, SIGNAL(newInstance(UAVObject*)), this, SLOT(newObject(UAVObject*))); connect(objManager, SIGNAL(newInstance(UAVObject*)), this, SLOT(newObject(UAVObject*)));
@ -56,6 +56,7 @@ UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent) :
UAVObjectTreeModel::~UAVObjectTreeModel() UAVObjectTreeModel::~UAVObjectTreeModel()
{ {
delete m_highlightManager;
delete m_rootItem; delete m_rootItem;
} }
@ -67,9 +68,12 @@ void UAVObjectTreeModel::setupModelData(UAVObjectManager *objManager)
m_rootItem = new TreeItem(rootData); m_rootItem = new TreeItem(rootData);
m_settingsTree = new TopTreeItem(tr("Settings"), m_rootItem); m_settingsTree = new TopTreeItem(tr("Settings"), m_rootItem);
m_settingsTree->setHighlightManager(m_highlightManager);
m_rootItem->appendChild(m_settingsTree); m_rootItem->appendChild(m_settingsTree);
m_nonSettingsTree = new TopTreeItem(tr("Data Objects"), m_rootItem); m_nonSettingsTree = new TopTreeItem(tr("Data Objects"), m_rootItem);
m_nonSettingsTree->setHighlightManager(m_highlightManager);
m_rootItem->appendChild(m_nonSettingsTree); m_rootItem->appendChild(m_nonSettingsTree);
m_rootItem->setHighlightManager(m_highlightManager);
connect(m_settingsTree, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(m_settingsTree, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
connect(m_nonSettingsTree, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(m_nonSettingsTree, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
@ -96,6 +100,7 @@ void UAVObjectTreeModel::addDataObject(UAVDataObject *obj)
addInstance(obj, root->child(index)); addInstance(obj, root->child(index));
} else { } else {
DataObjectTreeItem *data = new DataObjectTreeItem(obj->getName() + " (" + QString::number(obj->getNumBytes()) + " bytes)"); DataObjectTreeItem *data = new DataObjectTreeItem(obj->getName() + " (" + QString::number(obj->getNumBytes()) + " bytes)");
data->setHighlightManager(m_highlightManager);
connect(data, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(data, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
int index = root->nameIndex(obj->getName()); int index = root->nameIndex(obj->getName());
root->insert(index, data); root->insert(index, data);
@ -110,6 +115,7 @@ void UAVObjectTreeModel::addMetaObject(UAVMetaObject *obj, TreeItem *parent)
{ {
connect(obj, SIGNAL(objectUpdated(UAVObject*)), this, SLOT(highlightUpdatedObject(UAVObject*))); connect(obj, SIGNAL(objectUpdated(UAVObject*)), this, SLOT(highlightUpdatedObject(UAVObject*)));
MetaObjectTreeItem *meta = new MetaObjectTreeItem(obj, tr("Meta Data")); MetaObjectTreeItem *meta = new MetaObjectTreeItem(obj, tr("Meta Data"));
meta->setHighlightManager(m_highlightManager);
connect(meta, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(meta, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
foreach (UAVObjectField *field, obj->getFields()) { foreach (UAVObjectField *field, obj->getFields()) {
if (field->getNumElements() > 1) { if (field->getNumElements() > 1) {
@ -132,6 +138,7 @@ void UAVObjectTreeModel::addInstance(UAVObject *obj, TreeItem *parent)
} else { } else {
QString name = tr("Instance") + " " + QString::number(obj->getInstID()); QString name = tr("Instance") + " " + QString::number(obj->getInstID());
item = new InstanceTreeItem(obj, name); item = new InstanceTreeItem(obj, name);
item->setHighlightManager(m_highlightManager);
connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
parent->appendChild(item); parent->appendChild(item);
} }
@ -148,6 +155,7 @@ void UAVObjectTreeModel::addInstance(UAVObject *obj, TreeItem *parent)
void UAVObjectTreeModel::addArrayField(UAVObjectField *field, TreeItem *parent) void UAVObjectTreeModel::addArrayField(UAVObjectField *field, TreeItem *parent)
{ {
TreeItem *item = new ArrayFieldTreeItem(field->getName()); TreeItem *item = new ArrayFieldTreeItem(field->getName());
item->setHighlightManager(m_highlightManager);
connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
for (uint i = 0; i < field->getNumElements(); ++i) { for (uint i = 0; i < field->getNumElements(); ++i) {
addSingleField(i, field, item); addSingleField(i, field, item);
@ -192,6 +200,7 @@ void UAVObjectTreeModel::addSingleField(int index, UAVObjectField *field, TreeIt
default: default:
Q_ASSERT(false); Q_ASSERT(false);
} }
item->setHighlightManager(m_highlightManager);
connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*))); connect(item, SIGNAL(updateHighlight(TreeItem*)), this, SLOT(updateHighlight(TreeItem*)));
parent->appendChild(item); parent->appendChild(item);
} }
@ -352,7 +361,6 @@ void UAVObjectTreeModel::highlightUpdatedObject(UAVObject *obj)
Q_ASSERT(obj); Q_ASSERT(obj);
ObjectTreeItem *item = findObjectTreeItem(obj); ObjectTreeItem *item = findObjectTreeItem(obj);
Q_ASSERT(item); Q_ASSERT(item);
item->setHighlight(true);
item->update(); item->update();
QModelIndex itemIndex = index(item); QModelIndex itemIndex = index(item);
Q_ASSERT(itemIndex != QModelIndex()); Q_ASSERT(itemIndex != QModelIndex());

View File

@ -97,6 +97,7 @@ private:
int m_recentlyUpdatedTimeout; int m_recentlyUpdatedTimeout;
QColor m_recentlyUpdatedColor; QColor m_recentlyUpdatedColor;
QColor m_manuallyChangedColor; QColor m_manuallyChangedColor;
HighLightManager *m_highlightManager;
}; };
#endif // UAVOBJECTTREEMODEL_H #endif // UAVOBJECTTREEMODEL_H