refs #239, added a helper class for timer based pulls

* changed user componet to use new component
* all new components (aircraft, ATC stations) will use this timer
* it is implemented as "has a" timer based component, as inheriting from 2 QObjects would be problematic
This commit is contained in:
Klaus Basan
2014-05-14 23:38:09 +02:00
parent e2815cbdc3
commit 52b88ab24e
4 changed files with 77 additions and 20 deletions

View File

@@ -0,0 +1,29 @@
#include "timerbasedcomponent.h"
namespace BlackGui
{
CTimerBasedComponent::CTimerBasedComponent(const char *slot, QObject *parent) :
QObject(parent), m_timer(nullptr)
{
this->m_timer = new QTimer(this);
this->connect(this->m_timer, SIGNAL(timeout()), parent, slot);
}
CTimerBasedComponent::~CTimerBasedComponent()
{
this->m_timer->stop();
this->disconnect(this->parent());
}
void CTimerBasedComponent::setUpdateInterval(int milliSeconds)
{
if (milliSeconds < 100)
this->m_timer->stop();
else
{
this->m_timer->setInterval(milliSeconds);
if (!this->m_timer->isActive()) this->m_timer->start();
}
}
} // guard

View File

@@ -0,0 +1,34 @@
#ifndef BLACKGUI_TIMERBASEDCOMPONENT_H
#define BLACKGUI_TIMERBASEDCOMPONENT_H
#include <QTimer>
namespace BlackGui
{
//! Timer based componenet
class CTimerBasedComponent: public QObject
{
public:
//! Constructor
CTimerBasedComponent(const char *slot, QObject *parent);
//! Destructor
~CTimerBasedComponent();
public slots:
//! Update time, time < 100 stops updates
void setUpdateInterval(int milliSeconds);
//! Update time
void setUpdateIntervalSeconds(int seconds) { this->setUpdateInterval(1000 * seconds); }
//! Stop timer
void stopTimer() { this->setUpdateInterval(-1); }
private:
QTimer *m_timer;
};
}
#endif // guard

View File

@@ -3,13 +3,11 @@
namespace BlackGui
{
CUserComponent::CUserComponent(QWidget *parent) :
QTabWidget(parent), CRuntimeBasedComponent(nullptr, false), ui(new Ui::CUserComponent), m_timer(nullptr)
QTabWidget(parent), CRuntimeBasedComponent(nullptr, false), ui(new Ui::CUserComponent), m_timerComponent(nullptr)
{
ui->setupUi(this);
this->m_timer = new QTimer(this);
this->connect(this->m_timer, &QTimer::timeout, this, &CUserComponent::update);
this->m_timerComponent = new CTimerBasedComponent(SLOT(update()), this);
}
CUserComponent::~CUserComponent()
@@ -29,15 +27,4 @@ namespace BlackGui
this->ui->tvp_AllUsers->update(this->getIContextNetwork()->getUsers());
}
}
void CUserComponent::setUpdateInterval(int milliSeconds)
{
if (milliSeconds < 100)
this->m_timer->stop();
else
{
this->m_timer->setInterval(milliSeconds);
if (!this->m_timer->isActive()) this->m_timer->start();
}
}
} // guard

View File

@@ -2,6 +2,7 @@
#define BLACKGUI_USERCOMPONENT_H
#include "blackgui/runtimebasedcomponent.h"
#include "blackgui/timerbasedcomponent.h"
#include "blackmisc/nwuserlist.h"
#include <QTabWidget>
@@ -24,19 +25,25 @@ namespace BlackGui
//! Destructor
~CUserComponent();
//! Timer for updating
CTimerBasedComponent *getTimerComponent() { return this->m_timerComponent; }
public slots:
//! Update users
void update();
//! Update time, time < 100 stops updates
void setUpdateInterval(int milliSeconds);
//! \copydoc CTimerBasedComponent::setUpdateIntervalSeconds
void setUpdateIntervalSeconds(int seconds) { Q_ASSERT(this->m_timerComponent); this->m_timerComponent->setUpdateIntervalSeconds(seconds); }
//! Update time
void setUpdateIntervalSeconds(int seconds) { this->setUpdateInterval(1000 * seconds); }
//! \copydoc CTimerBasedComponent::setUpdateInterval
void setUpdateInterval(int milliSeconds) { Q_ASSERT(this->m_timerComponent); this->m_timerComponent->setUpdateInterval(milliSeconds); }
//! \copydoc CTimerBasedComponent::stopTimer
void stopTimer() { Q_ASSERT(this->m_timerComponent); this->m_timerComponent->stopTimer(); }
private:
Ui::CUserComponent *ui;
QTimer *m_timer;
CTimerBasedComponent *m_timerComponent;
};
}