1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-04-10 02:02:21 +02:00

LP-567 uavobjectbrowser: improve highlight handling

keep track of when to check highlight and schedule timer accordingly
old way would do that at a fixed periodicity leading to aliasing
This commit is contained in:
Philippe Renon 2017-12-15 21:15:17 +01:00
parent 1ab7956c1a
commit ca1ebc7cba
3 changed files with 57 additions and 26 deletions

View File

@ -28,11 +28,12 @@
#include "treeitem.h" #include "treeitem.h"
/* Constructor */ /* Constructor */
HighLightManager::HighLightManager(long checkingInterval) HighLightManager::HighLightManager()
{ {
// Start the timer and connect it to the callback // Initialize the timer and connect it to the callback
m_expirationTimer.start(checkingInterval); m_expirationTimer.setTimerType(Qt::PreciseTimer);
connect(&m_expirationTimer, SIGNAL(timeout()), this, SLOT(checkItemsExpired())); m_expirationTimer.setSingleShot(true);
connect(&m_expirationTimer, &QTimer::timeout, this, &HighLightManager::checkItemsExpired);
} }
/* /*
@ -52,6 +53,20 @@ bool HighLightManager::add(TreeItem *itemToAdd)
return false; return false;
} }
bool HighLightManager::startTimer(QTime expirationTime)
{
// Lock to ensure thread safety
QMutexLocker locker(&m_mutex);
if (!m_expirationTimer.isActive()) {
int msec = QTime::currentTime().msecsTo(expirationTime);
// qDebug() << "start" << msec;
m_expirationTimer.start((msec < 10) ? 10 : msec);
return true;
}
return false;
}
/* /*
* Called to remove item from list. * Called to remove item from list.
* Returns true if item was removed, otherwise false. * Returns true if item was removed, otherwise false.
@ -81,35 +96,48 @@ void HighLightManager::checkItemsExpired()
// This is the timestamp to compare with // This is the timestamp to compare with
QTime now = QTime::currentTime(); QTime now = QTime::currentTime();
QTime next;
// Loop over all items, check if they expired. // Loop over all items, check if they expired.
while (iter.hasNext()) { while (iter.hasNext()) {
TreeItem *item = iter.next(); TreeItem *item = iter.next();
if (item->getHiglightExpires() < now) { if (item->getHiglightExpires() <= now) {
// If expired, call removeHighlight // expired, call removeHighlight
item->removeHighlight(); item->removeHighlight();
// Remove from list since it is restored. // Remove from list since it is restored.
iter.remove(); iter.remove();
} else {
// not expired, check if next to expire
if (!next.isValid() || (next > item->getHiglightExpires())) {
next = item->getHiglightExpires();
}
} }
} }
if (next.isValid()) {
int msec = QTime::currentTime().msecsTo(next);
// qDebug() << "restart" << msec;
m_expirationTimer.start((msec < 10) ? 10 : msec);
}
} }
int TreeItem::m_highlightTimeMs = 500; int TreeItem::m_highlightTimeMs = 300;
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) : TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) :
QObject(0), QObject(0),
m_data(data), m_data(data),
m_parent(parent), m_parent(parent),
m_highlight(false), m_highlight(false),
m_changed(false) m_changed(false),
m_highlightManager(0)
{} {}
TreeItem::TreeItem(const QVariant &data, TreeItem *parent) : TreeItem::TreeItem(const QVariant &data, TreeItem *parent) :
QObject(0), QObject(0),
m_parent(parent), m_parent(parent),
m_highlight(false), m_highlight(false),
m_changed(false) m_changed(false),
m_highlightManager(0)
{ {
m_data << data << "" << ""; m_data << data << "" << "";
} }
@ -180,24 +208,27 @@ void TreeItem::apply()
} }
/* /*
* Called after a value has changed to trigger highlightning of tree item. * Called after a value has changed to trigger highlighting of tree item.
*/ */
void TreeItem::setHighlight(bool highlight) void TreeItem::setHighlight(bool highlight)
{ {
m_highlight = highlight;
m_changed = false; m_changed = false;
if (highlight) { if (m_highlight != highlight) {
// Update the expires timestamp m_highlight = highlight;
m_highlightExpires = QTime::currentTime().addMSecs(m_highlightTimeMs); if (highlight) {
// Add to highlight manager
// Add to highlightmanager if (m_highlightManager->add(this)) {
if (m_highlightManager->add(this)) { // Only emit signal if it was added
// Only emit signal if it was added emit updateHighlight(this);
}
// Update expiration timeout
m_highlightExpires = QTime::currentTime().addMSecs(m_highlightTimeMs);
// start expiration timer if necessary
m_highlightManager->startTimer(m_highlightExpires);
} else if (m_highlightManager->remove(this)) {
// Only emit signal if it was removed
emit updateHighlight(this); emit updateHighlight(this);
} }
} else if (m_highlightManager->remove(this)) {
// Only emit signal if it was removed
emit updateHighlight(this);
} }
// If we have a parent, call recursively to update highlight status of parents. // If we have a parent, call recursively to update highlight status of parents.

View File

@ -49,7 +49,7 @@ class TreeItem;
* non highlighted state in a linked list. * non highlighted state in a linked list.
* A timer traverses this list periodically to find out * A timer traverses this list periodically to find out
* if any of the items should be restored. All items are * if any of the items should be restored. All items are
* updated withan expiration timestamp when they expires. * updated with an expiration timestamp when they expires.
* An item that is beeing restored is removed from the * An item that is beeing restored is removed from the
* list and its removeHighlight() method is called. Items * list and its removeHighlight() method is called. Items
* that are not expired are left in the list til next time. * that are not expired are left in the list til next time.
@ -60,8 +60,7 @@ class TreeItem;
class HighLightManager : public QObject { class HighLightManager : public QObject {
Q_OBJECT Q_OBJECT
public: public:
// Constructor taking the checking interval in ms. HighLightManager();
HighLightManager(long checkingInterval);
// This is called when an item has been set to // This is called when an item has been set to
// highlighted = true. // highlighted = true.
@ -70,6 +69,8 @@ public:
// This is called when an item is set to highlighted = false; // This is called when an item is set to highlighted = false;
bool remove(TreeItem *itemToRemove); bool remove(TreeItem *itemToRemove);
bool startTimer(QTime time);
private slots: private slots:
// Timer callback method. // Timer callback method.
void checkItemsExpired(); void checkItemsExpired();

View File

@ -52,8 +52,7 @@ UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent, bool categorize, bool sh
Q_ASSERT(objManager); Q_ASSERT(objManager);
// Create highlight manager, let it run every 300 ms. m_highlightManager = new HighLightManager();
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 *)));