From 52b88ab24e3d25d82cc7d8de5b636285cb861903 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 14 May 2014 23:38:09 +0200 Subject: [PATCH] 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 --- src/blackgui/timerbasedcomponent.cpp | 29 ++++++++++++++++++++++++ src/blackgui/timerbasedcomponent.h | 34 ++++++++++++++++++++++++++++ src/blackgui/usercomponent.cpp | 17 ++------------ src/blackgui/usercomponent.h | 17 ++++++++++---- 4 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/blackgui/timerbasedcomponent.cpp create mode 100644 src/blackgui/timerbasedcomponent.h diff --git a/src/blackgui/timerbasedcomponent.cpp b/src/blackgui/timerbasedcomponent.cpp new file mode 100644 index 000000000..82bc4bfc8 --- /dev/null +++ b/src/blackgui/timerbasedcomponent.cpp @@ -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 diff --git a/src/blackgui/timerbasedcomponent.h b/src/blackgui/timerbasedcomponent.h new file mode 100644 index 000000000..6eca257c2 --- /dev/null +++ b/src/blackgui/timerbasedcomponent.h @@ -0,0 +1,34 @@ +#ifndef BLACKGUI_TIMERBASEDCOMPONENT_H +#define BLACKGUI_TIMERBASEDCOMPONENT_H + +#include + +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 diff --git a/src/blackgui/usercomponent.cpp b/src/blackgui/usercomponent.cpp index 8448fffda..c50383b8f 100644 --- a/src/blackgui/usercomponent.cpp +++ b/src/blackgui/usercomponent.cpp @@ -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 diff --git a/src/blackgui/usercomponent.h b/src/blackgui/usercomponent.h index bfef027b7..28b0d03a5 100644 --- a/src/blackgui/usercomponent.h +++ b/src/blackgui/usercomponent.h @@ -2,6 +2,7 @@ #define BLACKGUI_USERCOMPONENT_H #include "blackgui/runtimebasedcomponent.h" +#include "blackgui/timerbasedcomponent.h" #include "blackmisc/nwuserlist.h" #include @@ -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; }; }