From 11df0bb694a29324164e85e9372fd85ffb95af1e Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 4 Sep 2024 16:30:05 +0200 Subject: [PATCH] code duplication eliminated --- examples/iotdashboard/CircularProgressBar.cpp | 180 +----------------- examples/iotdashboard/CircularProgressBar.h | 48 +---- .../CircularProgressBarSkinlet.cpp | 2 +- examples/iotdashboard/EnergyMeter.cpp | 2 +- examples/iotdashboard/Skin.cpp | 2 +- examples/iotdashboard/StorageMeter.cpp | 2 +- 6 files changed, 9 insertions(+), 227 deletions(-) diff --git a/examples/iotdashboard/CircularProgressBar.cpp b/examples/iotdashboard/CircularProgressBar.cpp index 479c5668..8bb873ab 100644 --- a/examples/iotdashboard/CircularProgressBar.cpp +++ b/examples/iotdashboard/CircularProgressBar.cpp @@ -5,87 +5,10 @@ #include "CircularProgressBar.h" -#include -#include - -QSK_SUBCONTROL( CircularProgressBar, Groove ) -QSK_SUBCONTROL( CircularProgressBar, Bar ) - -namespace -{ - class PositionAnimator : public QskAnimator - { - public: - PositionAnimator( CircularProgressBar* progressBar ) - : m_progressBar( progressBar ) - { - setAutoRepeat( true ); - setDuration( 1300 ); - - setWindow( progressBar->window() ); - } - - void advance( qreal value ) override - { - const auto aspect = CircularProgressBar::Bar | QskAspect::Position; - - m_progressBar->setMetric( aspect, value ); - m_progressBar->update(); - } - - private: - CircularProgressBar* m_progressBar; - }; -} - -class CircularProgressBar::PrivateData -{ - public: - void updateIndeterminateAnimator( CircularProgressBar* progressBar ) - { - if ( !isIndeterminate ) - { - delete animator; - animator = nullptr; - - return; - } - - if ( progressBar->window() && progressBar->isVisible() ) - { - if ( animator == nullptr ) - animator = new PositionAnimator( progressBar ); - - animator->start(); - } - else - { - if ( animator ) - animator->stop(); - } - } - - PositionAnimator* animator = nullptr; - - qreal value = 0.0; - qreal origin = 0.0; - - bool hasOrigin = false; - bool isIndeterminate = false; -}; - -CircularProgressBar::~CircularProgressBar() = default; - CircularProgressBar::CircularProgressBar( qreal min, qreal max, QQuickItem* parent ) - : QskBoundedControl( min, max, parent ) - , m_data( new PrivateData ) + : Inherited( min, max, parent ) { - m_data->value = minimum(); - initSizePolicy( QskSizePolicy::MinimumExpanding, QskSizePolicy::MinimumExpanding ); - - connect( this, &QskBoundedControl::boundariesChanged, - this, &CircularProgressBar::adjustValue ); } CircularProgressBar::CircularProgressBar( QQuickItem* parent ) @@ -93,105 +16,4 @@ CircularProgressBar::CircularProgressBar( QQuickItem* parent ) { } -bool CircularProgressBar::isIndeterminate() const -{ - return m_data->isIndeterminate; -} - -void CircularProgressBar::setIndeterminate( bool on ) -{ - if ( on == m_data->isIndeterminate ) - return; - - m_data->isIndeterminate = on; - m_data->updateIndeterminateAnimator( this ); - - update(); - Q_EMIT indeterminateChanged( on ); -} - -void CircularProgressBar::resetOrigin() -{ - if ( m_data->hasOrigin ) - { - m_data->hasOrigin = false; - - update(); - Q_EMIT originChanged( origin() ); - } -} - -qreal CircularProgressBar::origin() const -{ - if ( m_data->hasOrigin ) - { - return boundedValue( m_data->origin ); - } - - return minimum(); -} - -qreal CircularProgressBar::value() const -{ - return m_data->value; -} - -qreal CircularProgressBar::valueAsRatio() const -{ - return QskBoundedControl::valueAsRatio( m_data->value ); -} - -void CircularProgressBar::setValue( qreal value ) -{ - if ( isComponentComplete() ) - value = boundedValue( value ); - - setValueInternal( value ); -} - -void CircularProgressBar::setValueAsRatio( qreal ratio ) -{ - ratio = qBound( 0.0, ratio, 1.0 ); - setValue( minimum() + ratio * boundaryLength() ); -} - -void CircularProgressBar::setOrigin( qreal origin ) -{ - if ( isComponentComplete() ) - origin = boundedValue( origin ); - - if( !m_data->hasOrigin || !qskFuzzyCompare( m_data->origin, origin ) ) - { - m_data->hasOrigin = true; - m_data->origin = origin; - - update(); - Q_EMIT originChanged( origin ); - } -} - -void CircularProgressBar::componentComplete() -{ - Inherited::componentComplete(); - adjustValue(); -} - -void CircularProgressBar::setValueInternal( qreal value ) -{ - if ( !qskFuzzyCompare( value, m_data->value ) ) - { - m_data->value = value; - - Q_EMIT valueChanged( value ); - - update(); - } -} - -void CircularProgressBar::adjustValue() -{ - if ( isComponentComplete() ) - setValueInternal( boundedValue( m_data->value ) ); -} - #include "moc_CircularProgressBar.cpp" diff --git a/examples/iotdashboard/CircularProgressBar.h b/examples/iotdashboard/CircularProgressBar.h index 9cd84898..67359722 100644 --- a/examples/iotdashboard/CircularProgressBar.h +++ b/examples/iotdashboard/CircularProgressBar.h @@ -5,57 +5,17 @@ #pragma once -#include +#include -class CircularProgressBar : public QskBoundedControl +class CircularProgressBar : public QskProgressIndicator { Q_OBJECT - Q_PROPERTY( bool indeterminate READ isIndeterminate - WRITE setIndeterminate NOTIFY indeterminateChanged ) - - Q_PROPERTY( qreal origin READ origin - WRITE setOrigin RESET resetOrigin NOTIFY originChanged ) - - Q_PROPERTY( qreal value READ value WRITE setValue NOTIFY valueChanged ) - Q_PROPERTY( qreal valueAsRatio READ valueAsRatio - WRITE setValueAsRatio NOTIFY valueChanged ) - - using Inherited = QskBoundedControl; + using Inherited = QskProgressIndicator; public: - QSK_SUBCONTROLS( Groove, Bar ) - CircularProgressBar( qreal min, qreal max, QQuickItem* parent = nullptr ); CircularProgressBar( QQuickItem* parent = nullptr ); - ~CircularProgressBar() override; - bool isIndeterminate() const; - void setIndeterminate( bool on = true ); - - void resetOrigin(); - qreal origin() const; - - qreal value() const; - qreal valueAsRatio() const; // [0.0, 1.0] - - public Q_SLOTS: - void setValue( qreal ); - void setValueAsRatio( qreal ); - void setOrigin( qreal ); - - Q_SIGNALS: - void indeterminateChanged( bool ); - void valueChanged( qreal ); - void originChanged( qreal ); - - protected: - void componentComplete() override; - - private: - void setValueInternal( qreal value ); - void adjustValue(); - - class PrivateData; - std::unique_ptr< PrivateData > m_data; + ~CircularProgressBar() override = default; }; diff --git a/examples/iotdashboard/CircularProgressBarSkinlet.cpp b/examples/iotdashboard/CircularProgressBarSkinlet.cpp index 0bd5f418..8b9f7a5c 100644 --- a/examples/iotdashboard/CircularProgressBarSkinlet.cpp +++ b/examples/iotdashboard/CircularProgressBarSkinlet.cpp @@ -39,7 +39,7 @@ QSGNode* CircularProgressBarSkinlet::updateSubNode( const qreal spanAngle = 360.0 * bar->valueAsRatio(); return updateArcNode( skinnable, node, startAngle, -spanAngle, - CircularProgressBar::Bar ); + CircularProgressBar::Fill ); } } diff --git a/examples/iotdashboard/EnergyMeter.cpp b/examples/iotdashboard/EnergyMeter.cpp index f9d20537..dae877ba 100644 --- a/examples/iotdashboard/EnergyMeter.cpp +++ b/examples/iotdashboard/EnergyMeter.cpp @@ -36,7 +36,7 @@ EnergyMeter::EnergyMeter( const QColor& textColor, setAutoLayoutChildren( true ); auto valueBar = new CircularProgressBar( this ); - valueBar->setGradientHint( CircularProgressBar::Bar, gradient ); + valueBar->setGradientHint( CircularProgressBar::Fill, gradient ); valueBar->setValue( value ); auto valueLabel = new ValueLabel( this ); diff --git a/examples/iotdashboard/Skin.cpp b/examples/iotdashboard/Skin.cpp index 5e7b56e3..2495d744 100644 --- a/examples/iotdashboard/Skin.cpp +++ b/examples/iotdashboard/Skin.cpp @@ -131,7 +131,7 @@ void Skin::initHints() ed.setArcMetrics( CircularProgressBar::Groove, 90, -360, 8.53 ); // the span angle will be set in the progress bar, we just give a dummy // value here: - ed.setArcMetrics( CircularProgressBar::Bar, 90, -180, 8.53 ); + ed.setArcMetrics( CircularProgressBar::Fill, 90, -180, 8.53 ); ed.setFontRole( TimeTitleLabel::Text, { QskFontRole::Caption, QskFontRole::High } ); diff --git a/examples/iotdashboard/StorageMeter.cpp b/examples/iotdashboard/StorageMeter.cpp index af9d528b..38b9c3fd 100644 --- a/examples/iotdashboard/StorageMeter.cpp +++ b/examples/iotdashboard/StorageMeter.cpp @@ -35,7 +35,7 @@ void StorageMeter::setValue( const qreal value ) { const auto gradient = gradientHint( StorageMeter::Status ); const auto color = gradient.extracted( value / 100.0, value / 100.0 ).startColor(); - setGradientHint( StorageMeter::Bar, { color, color.lighter() } ); + setGradientHint( StorageMeter::Fill, { color, color.lighter() } ); CircularProgressBar::setValue( value ); label->setTextColor( color ); label->setText( make_text( locale(), value ) );