From 24af0f044faa8f2afb72bc35114fd85942f375b9 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 22 Mar 2017 18:31:31 +0100 Subject: [PATCH] refs #911, utility class to easily enable class for load indicator and used it with CDbLoadOverviewComponent --- .../components/dbloadoverviewcomponent.cpp | 32 +----------- .../components/dbloadoverviewcomponent.h | 16 +----- src/blackgui/loadindicator.cpp | 50 +++++++++++++++++++ src/blackgui/loadindicator.h | 39 +++++++++++++++ 4 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/blackgui/components/dbloadoverviewcomponent.cpp b/src/blackgui/components/dbloadoverviewcomponent.cpp index fad36a8fd..8cfc3934c 100644 --- a/src/blackgui/components/dbloadoverviewcomponent.cpp +++ b/src/blackgui/components/dbloadoverviewcomponent.cpp @@ -24,7 +24,7 @@ namespace BlackGui namespace Components { CDbLoadOverviewComponent::CDbLoadOverviewComponent(QWidget *parent) : - QFrame(parent), + QFrame(parent), CLoadIndicatorEnabled(this), ui(new Ui::CDbLoadOverviewComponent) { Q_ASSERT_X(sGui, Q_FUNC_INFO, "missing sGui"); @@ -105,16 +105,6 @@ namespace BlackGui QFrame::resizeEvent(event); } - bool CDbLoadOverviewComponent::isShowingLoadIndicator() const - { - return m_loadIndicator && this->isVisible() && m_loadIndicator->isAnimated(); - } - - bool CDbLoadOverviewComponent::isLoadInProgress() const - { - return m_loadInProgress; - } - void CDbLoadOverviewComponent::showVisibleLoadAllButtons(bool shared, bool db) { const bool widget = shared || db; @@ -133,13 +123,6 @@ namespace BlackGui this->triggerLoadingFromSharedFiles(CEntityFlags::AllDbEntitiesNoInfoObjects); } - void CDbLoadOverviewComponent::centerLoadIndicator() - { - if (!m_loadIndicator) { return; } - const QPoint middle = this->visibleRegion().boundingRect().center(); - this->m_loadIndicator->centerLoadIndicator(middle); - } - void CDbLoadOverviewComponent::ps_setValues() { if (!sGui) { return; } @@ -212,18 +195,7 @@ namespace BlackGui ui->lbl_SharedUrls->setMinimumHeight(10 + (18 * sharedUrls.size())); // Indicator - if (this->m_loadIndicator) { this->m_loadIndicator->stopAnimation(); } - } - - void CDbLoadOverviewComponent::showLoading() - { - if (!this->m_loadIndicator) - { - this->m_loadIndicator = new CLoadIndicator(64, 64, this); - } - - this->centerLoadIndicator(); - this->m_loadIndicator->startAnimation(true); + this->hideLoading(); } bool CDbLoadOverviewComponent::isInitialized() const diff --git a/src/blackgui/components/dbloadoverviewcomponent.h b/src/blackgui/components/dbloadoverviewcomponent.h index 19cd8a53c..2d402f6d1 100644 --- a/src/blackgui/components/dbloadoverviewcomponent.h +++ b/src/blackgui/components/dbloadoverviewcomponent.h @@ -27,7 +27,8 @@ namespace BlackGui /*! * Overview about load state of DB data */ - class BLACKGUI_EXPORT CDbLoadOverviewComponent : public QFrame + class BLACKGUI_EXPORT CDbLoadOverviewComponent : + public QFrame, public BlackGui::CLoadIndicatorEnabled { Q_OBJECT @@ -50,12 +51,6 @@ namespace BlackGui //! Show load all buttons void showVisibleLoadAllButtons(bool shared, bool db); - //! Showing load indicator? - bool isShowingLoadIndicator() const; - - //! Loading in progress? - bool isLoadInProgress() const; - //! Load all from DB void loadAllFromDb(); @@ -71,12 +66,8 @@ namespace BlackGui //! \copydoc QWidget::resizeEvent virtual void resizeEvent(QResizeEvent *event) override; - //! Center load indicator - void centerLoadIndicator(); - private: QScopedPointer ui; - BlackGui::CLoadIndicator *m_loadIndicator = nullptr; //!< load indicator if needed BlackMisc::CDigestSignal m_dsTriggerGuiUpdate { this, &CDbLoadOverviewComponent::ps_triggerDigestGuiUpdate, &CDbLoadOverviewComponent::ps_setValues, 750, 4 }; bool m_loadInProgress = false; //!< data loading in progress @@ -86,9 +77,6 @@ namespace BlackGui //! Trigger loading from shared files void triggerLoadingFromSharedFiles(BlackMisc::Network::CEntityFlags::Entity entities); - //! Show loading - void showLoading(); - //! Values at least set once bool isInitialized() const; diff --git a/src/blackgui/loadindicator.cpp b/src/blackgui/loadindicator.cpp index 70309af42..771e4630f 100644 --- a/src/blackgui/loadindicator.cpp +++ b/src/blackgui/loadindicator.cpp @@ -64,6 +64,7 @@ namespace BlackGui { // only timeout myself id this->stopAnimation(stopId); + emit this->timedOut(); }); } this->m_pendingIds.push_back(stopId); @@ -170,4 +171,53 @@ namespace BlackGui const int y = middle.y() - h / 2; this->setGeometry(x, y, w, h); } + + CLoadIndicatorEnabled::CLoadIndicatorEnabled(QWidget *usingWidget) : + m_usingWidget(usingWidget) + { + Q_ASSERT_X(usingWidget, Q_FUNC_INFO, "need widget"); + } + + bool CLoadIndicatorEnabled::isShowingLoadIndicator() const + { + return m_loadIndicator && m_usingWidget->isVisible() && m_loadIndicator->isAnimated(); + } + + bool CLoadIndicatorEnabled::isLoadInProgress() const + { + return m_loadInProgress; + } + + void CLoadIndicatorEnabled::showLoading(int timeoutMs, bool processEvents) + { + if (!this->m_loadIndicator) + { + this->m_loadIndicator = new CLoadIndicator(64, 64, m_usingWidget); + QObject::connect(this->m_loadIndicator, &CLoadIndicator::timedOut, + [this] { this->indicatorTimedOut(); }); + } + + this->centerLoadIndicator(); + m_indicatorId = this->m_loadIndicator->startAnimation(timeoutMs, processEvents); + } + + void CLoadIndicatorEnabled::hideLoading() + { + if (m_loadIndicator) + { + m_loadIndicator->stopAnimation(); + } + } + + void CLoadIndicatorEnabled::centerLoadIndicator() + { + if (!m_loadIndicator) { return; } + const QPoint middle = this->m_usingWidget->visibleRegion().boundingRect().center(); + this->m_loadIndicator->centerLoadIndicator(middle); + } + + void CLoadIndicatorEnabled::indicatorTimedOut() + { + // to be overridden + } } // ns diff --git a/src/blackgui/loadindicator.h b/src/blackgui/loadindicator.h index dca73ca64..7c8f99fa6 100644 --- a/src/blackgui/loadindicator.h +++ b/src/blackgui/loadindicator.h @@ -73,6 +73,10 @@ namespace BlackGui //! Center this load indicator void centerLoadIndicator(const QPoint &middle); + signals: + //! Timed out + void timedOut(); + public slots: //! Starts the spin animation. //! \sa stopAnimation isAnimated @@ -116,6 +120,41 @@ namespace BlackGui bool m_displayedWhenStopped = false; QColor m_color = Qt::blue; }; + + /** + * Enable widgte class for load indicator + */ + class BLACKGUI_EXPORT CLoadIndicatorEnabled + { + public: + //! Ctor + CLoadIndicatorEnabled(QWidget *usingWidget); + + //! Showing load indicator? + bool isShowingLoadIndicator() const; + + //! Show load indicator + void showLoading(int timeoutMs = -1, bool processEvents = true); + + //! Hide load indicator + void hideLoading(); + + //! Loading in progress? + bool isLoadInProgress() const; + + protected: + //! Center load indicator + void centerLoadIndicator(); + + //! Indicator timed out + //! \remark override for usage + void virtual indicatorTimedOut(); + + QWidget *m_usingWidget = nullptr; //!< widget which uses load indicator + CLoadIndicator *m_loadIndicator = nullptr; //!< indicator itself + bool m_loadInProgress = false; //!< flag indicating loading + int m_indicatorId = -1; //!< last indicator id returned + }; } // ns #endif