1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

Added comments to the code.

This commit is contained in:
Fredrik Arvidsson 2012-06-17 12:46:45 +02:00
parent 0e4ffa7f6d
commit c8b4abc71f
4 changed files with 75 additions and 0 deletions

View File

@ -27,15 +27,24 @@
#include "treeitem.h"
/* Constructor */
HighLightManager::HighLightManager(long checkingInterval)
{
// Start the timer and connect it to the callback
m_expirationTimer.start(checkingInterval);
connect(&m_expirationTimer, SIGNAL(timeout()), this, SLOT(checkItemsExpired()));
}
/*
* Called to add item to list. Item is only added if absent.
* Returns true if item was added, otherwise false.
*/
bool HighLightManager::add(TreeItem *itemToAdd)
{
// Lock to ensure thread safety
QMutexLocker locker(&m_listMutex);
// Check so that the item isn't already in the list
if(!m_itemsList.contains(itemToAdd))
{
m_itemsList.append(itemToAdd);
@ -44,23 +53,46 @@ bool HighLightManager::add(TreeItem *itemToAdd)
return false;
}
/*
* Called to remove item from list.
* Returns true if item was removed, otherwise false.
*/
bool HighLightManager::remove(TreeItem *itemToRemove)
{
// Lock to ensure thread safety
QMutexLocker locker(&m_listMutex);
// Remove item and return result
return m_itemsList.removeOne(itemToRemove);
}
/*
* Callback called periodically by the timer.
* This method checks for expired highlights and
* removes them if they are expired.
* Expired highlights are restored.
*/
void HighLightManager::checkItemsExpired()
{
// Lock to ensure thread safety
QMutexLocker locker(&m_listMutex);
// Get a mutable iterator for the list
QMutableLinkedListIterator<TreeItem*> iter(m_itemsList);
// This is the timestamp to compare with
QTime now = QTime::currentTime();
// Loop over all items, check if they expired.
while(iter.hasNext())
{
TreeItem* item = iter.next();
if(item->getHiglightExpires() < now)
{
// If expired, call removeHighlight
item->removeHighlight();
// Remove from list since it is restored.
iter.remove();
}
}
@ -145,20 +177,32 @@ void TreeItem::apply() {
child->apply();
}
/*
* Called after a value has changed to trigger highlightning of tree item.
*/
void TreeItem::setHighlight(bool highlight) {
m_highlight = highlight;
m_changed = false;
if (highlight) {
// Update the expires timestamp
m_highlightExpires = QTime::currentTime().addMSecs(m_highlightTimeMs);
// Add to highlightmanager
if(m_highlightManager->add(this))
{
// Only emit signal if it was added
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.
// This will ensure that the root of a leaf that is changed also is highlighted.
// Only updates that really changes values will trigger highlight of parents.
if(m_parent)
{
m_parent->setHighlight(highlight);

View File

@ -41,20 +41,47 @@
class TreeItem;
/*
* Small utility class that handles the higlighting of
* tree grid items.
* Basicly it maintains all items due to be restored to
* non highlighted state in a linked list.
* A timer traverses this list periodically to find out
* if any of the items should be restored. All items are
* updated withan expiration timestamp when they expires.
* An item that is beeing restored is removed from the
* list and its removeHighlight() method is called. Items
* that are not expired are left in the list til next time.
* Items that are updated during the expiration time are
* left untouched in the list. This reduces unwanted emits
* of signals to the repaint/update function.
*/
class HighLightManager : public QObject
{
Q_OBJECT
public:
// Constructor taking the checking interval in ms.
HighLightManager(long checkingInterval);
// This is called when an item has been set to
// highlighted = true.
bool add(TreeItem* itemToAdd);
//This is called when an item is set to highlighted = false;
bool remove(TreeItem* itemToRemove);
private slots:
// Timer callback method.
void checkItemsExpired();
private:
// The timer checking highlight expiration.
QTimer m_expirationTimer;
// The list holding all items due to be updated.
QLinkedList<TreeItem*> m_itemsList;
//Mutex to lock when accessing list.
QMutex m_listMutex;
};

View File

@ -46,6 +46,8 @@ UAVObjectTreeModel::UAVObjectTreeModel(QObject *parent) :
{
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
// Create highlight manager, let it run every 300 ms.
m_highlightManager = new HighLightManager(300);
connect(objManager, SIGNAL(newObject(UAVObject*)), this, SLOT(newObject(UAVObject*)));
connect(objManager, SIGNAL(newInstance(UAVObject*)), this, SLOT(newObject(UAVObject*)));

View File

@ -97,6 +97,8 @@ private:
int m_recentlyUpdatedTimeout;
QColor m_recentlyUpdatedColor;
QColor m_manuallyChangedColor;
// Highlight manager to handle highlighting of tree items.
HighLightManager *m_highlightManager;
};