From d0a564ff501a1ab2b43cf4decf4cfa2f0f20f5f9 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Thu, 5 Sep 2024 09:15:25 +0200 Subject: [PATCH] using ValueMeter for StorageMeter/EnergyMeter --- examples/iotdashboard/CMakeLists.txt | 3 +- examples/iotdashboard/EnergyMeter.cpp | 71 ------------------- examples/iotdashboard/EnergyMeter.h | 18 ----- examples/iotdashboard/Skin.cpp | 12 ++-- examples/iotdashboard/StorageMeter.cpp | 68 ------------------ examples/iotdashboard/StoragePage.cpp | 61 ++++++++++++++-- examples/iotdashboard/StoragePage.h | 35 +-------- examples/iotdashboard/TopBar.cpp | 22 ++++-- examples/iotdashboard/ValueMeter.cpp | 42 +++++++++++ .../{StorageMeter.h => ValueMeter.h} | 11 +-- 10 files changed, 135 insertions(+), 208 deletions(-) delete mode 100644 examples/iotdashboard/EnergyMeter.cpp delete mode 100644 examples/iotdashboard/EnergyMeter.h delete mode 100644 examples/iotdashboard/StorageMeter.cpp create mode 100644 examples/iotdashboard/ValueMeter.cpp rename examples/iotdashboard/{StorageMeter.h => ValueMeter.h} (66%) diff --git a/examples/iotdashboard/CMakeLists.txt b/examples/iotdashboard/CMakeLists.txt index 8a85943e3..afeffdb9d 100644 --- a/examples/iotdashboard/CMakeLists.txt +++ b/examples/iotdashboard/CMakeLists.txt @@ -8,7 +8,6 @@ set(SOURCES BoxWithButtons.h BoxWithButtons.cpp Diagram.h Diagram.cpp DiagramSkinlet.h DiagramSkinlet.cpp - EnergyMeter.h EnergyMeter.cpp GraphicProvider.h GraphicProvider.cpp GridBox.h GridBox.cpp LightDisplaySkinlet.h LightDisplaySkinlet.cpp @@ -29,7 +28,7 @@ set(SOURCES UsageBox.h UsageBox.cpp UsageDiagram.h UsageDiagram.cpp StoragePage.h StoragePage.cpp - StorageMeter.h StorageMeter.cpp + ValueMeter.h ValueMeter.cpp StorageBar.h StorageBar.cpp StorageBarSkinlet.h StorageBarSkinlet.cpp nodes/DiagramDataNode.h nodes/DiagramDataNode.cpp diff --git a/examples/iotdashboard/EnergyMeter.cpp b/examples/iotdashboard/EnergyMeter.cpp deleted file mode 100644 index 9f00a0adb..000000000 --- a/examples/iotdashboard/EnergyMeter.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2021 Edelhirsch Software GmbH - * SPDX-License-Identifier: BSD-3-Clause - *****************************************************************************/ - -#include "EnergyMeter.h" - -#include -#include -#include - -namespace -{ - class ValueLabel : public QskTextLabel - { - public: - ValueLabel( QQuickItem* parent ) - : QskTextLabel( parent ) - { - initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); - setLayoutAlignmentHint( Qt::AlignCenter ); - setFontRole( QskFontRole::Caption ); - } - - void setValue( int value ) - { - setText( locale().toString( value ) + " " + locale().percent() ); - } - }; -} - -EnergyMeter::EnergyMeter( const QColor& textColor, - const QskGradient& gradient, int value, QQuickItem* parent ) - : QskControl( parent ) -{ - setAutoLayoutChildren( true ); - - auto valueBar = new QskProgressRing( this ); - valueBar->setSizePolicy( - QskSizePolicy::MinimumExpanding, QskSizePolicy::Constrained ); - valueBar->setFillGradient( gradient ); - valueBar->setValue( value ); - - auto valueLabel = new ValueLabel( this ); - valueLabel->setTextColor( textColor ); - valueLabel->setValue( value ); -} - -QSizeF EnergyMeter::contentsSizeHint( - Qt::SizeHint which, const QSizeF& constraint ) const -{ - if ( which != Qt::PreferredSize ) - return QSizeF(); - - qreal size; - - if ( constraint.width() > 0 ) - { - size = constraint.width(); - } - else if ( constraint.height() > 0 ) - { - size = constraint.height(); - } - else - { - size = 57; - } - - return QSizeF( size, size ); -} diff --git a/examples/iotdashboard/EnergyMeter.h b/examples/iotdashboard/EnergyMeter.h deleted file mode 100644 index 7780dd9e0..000000000 --- a/examples/iotdashboard/EnergyMeter.h +++ /dev/null @@ -1,18 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2021 Edelhirsch Software GmbH - * SPDX-License-Identifier: BSD-3-Clause - *****************************************************************************/ - -#pragma once - -#include - -class EnergyMeter : public QskControl -{ - public: - EnergyMeter( const QColor&, const QskGradient&, - int progress, QQuickItem* parent = nullptr ); - - protected: - QSizeF contentsSizeHint( Qt::SizeHint, const QSizeF& ) const override; -}; diff --git a/examples/iotdashboard/Skin.cpp b/examples/iotdashboard/Skin.cpp index 8f7a23f17..72abc78c6 100644 --- a/examples/iotdashboard/Skin.cpp +++ b/examples/iotdashboard/Skin.cpp @@ -19,7 +19,6 @@ #include "RoundedIcon.h" #include "StorageBar.h" #include "StorageBarSkinlet.h" -#include "StorageMeter.h" #include "StoragePage.h" #include "TopBar.h" #include "UsageBox.h" @@ -35,6 +34,7 @@ #include #include #include +#include #include #include @@ -298,9 +298,13 @@ void Skin::initHints() // storage meter { - ed.setGradient( StorageMeter::Status, - { { { 0.00, "#00ff00" }, { 0.33, "#00ff00" }, { 0.33, "#ffaf00" }, { 0.66, "#ffaf00" }, - { 0.66, "#ff0000" }, { 1.00, "#ff0000" } } } ); + ed.setGradient( StoragePage::Status, + { { + { 0.00, "#00ff00" }, { 0.33, "#00ff00" }, + { 0.33, "#ffaf00" }, { 0.66, "#ffaf00" }, + { 0.66, "#ff0000" }, { 1.00, "#ff0000" } + } } + ); } } diff --git a/examples/iotdashboard/StorageMeter.cpp b/examples/iotdashboard/StorageMeter.cpp deleted file mode 100644 index 38233cb7f..000000000 --- a/examples/iotdashboard/StorageMeter.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2022 Edelhirsch Software GmbH - * SPDX-License-Identifier: BSD-3-Clause - *****************************************************************************/ - -#include "StorageMeter.h" - -#include -#include - -QSK_SUBCONTROL( StorageMeter, Status ) - -StorageMeter::StorageMeter( QQuickItem* parent ) noexcept - : QskProgressRing( parent ) -{ - setAutoLayoutChildren( true ); - initSizePolicy( QskSizePolicy::MinimumExpanding, QskSizePolicy::Constrained ); - - m_label = new QskTextLabel( this ); - m_label->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); - m_label->setLayoutAlignmentHint( Qt::AlignCenter ); - m_label->setFontRole( QskFontRole::Caption ); - - connect( this, &QskProgressRing::valueChanged, - this, &StorageMeter::updateMeter ); - - updateMeter( value() ); -} - -void StorageMeter::updateMeter( const qreal value ) -{ - const auto color = qskInterpolatedColorAt( - gradientHint( Status ).stops(), value / 100.0 ); - - setFillGradient( { color, color.lighter() } ); - - m_label->setTextColor( color ); - - const auto locale = this->locale(); - const auto text = locale.toString( static_cast< int >( value ) ) - + " " + locale.percent(); - - m_label->setText( text ); -} - -QSizeF StorageMeter::contentsSizeHint( - Qt::SizeHint which, const QSizeF& constraint ) const -{ - if ( which != Qt::PreferredSize ) - return QSizeF(); - - qreal size; - - if ( constraint.width() > 0 ) - { - size = constraint.width(); - } - else if ( constraint.height() > 0 ) - { - size = constraint.height(); - } - else - { - size = 57; - } - - return QSizeF( size, size ); -} diff --git a/examples/iotdashboard/StoragePage.cpp b/examples/iotdashboard/StoragePage.cpp index f1ff5b3c0..038a6e39b 100644 --- a/examples/iotdashboard/StoragePage.cpp +++ b/examples/iotdashboard/StoragePage.cpp @@ -6,7 +6,7 @@ #include "StoragePage.h" #include "Box.h" #include "StorageBar.h" -#include "StorageMeter.h" +#include "ValueMeter.h" #include #include #include @@ -20,6 +20,60 @@ #include QSK_SUBCONTROL( StoragePage, Panel ) +QSK_SUBCONTROL( StoragePage, Status ) + +namespace +{ + struct Storage + { + struct Media + { + qreal pictures = 0; + qreal music = 0; + qreal videos = 0; + qreal documents = 0; + qreal others = 0; + + inline constexpr bool operator==( const Media& rhs ) const noexcept + { + return pictures == rhs.pictures && music == rhs.music && videos == rhs.videos && + documents == rhs.documents && others == rhs.others; + } + + inline constexpr qreal free() const noexcept + { + return 1.0 - pictures - music - videos - documents - others; + } + }; + + QString title; + QString description; + Media distribution; + }; + + class StorageMeter final : public ValueMeter + { + public: + StorageMeter( QQuickItem* parent = nullptr ) + : ValueMeter( parent ) + { + setFixedSize( 64, 64 ); + + connect( this, &ValueMeter::valueChanged, + this, &StorageMeter::setStatusColor ); + } + + private: + void setStatusColor( qreal value ) + { + const auto color = qskInterpolatedColorAt( + gradientHint( StoragePage::Status ).stops(), value / 100.0 ); + + setFillGradient( { color, color.lighter() } ); + setTextColor( color ); + } + }; +} struct StorageRowAnimator final : public QObject, public QskAnimator { @@ -85,8 +139,6 @@ void StoragePage::addRow( const QString& title, const QString& description, const auto percent = 100.0 * ( 1.0 - storage.distribution.free() ); auto* const meter = new StorageMeter( left ); meter->setValue( percent ); - meter->setMinimumSize( 64, 64 ); - meter->setMaximumSize( 64, 64 ); auto* const maintitle = new QskTextLabel( storage.title, center ); maintitle->setFontRole( QskFontRole::Headline ); @@ -136,5 +188,6 @@ void StoragePage::addRow( const QString& title, const QString& description, bar->setDocuments( media.documents * v ); bar->setOthers( media.others * v ); }; - connect( sync, &QskPushButton::clicked, animator, [ animator ]() { animator->start(); } ); + connect( sync, &QskPushButton::clicked, + animator, [ animator ]() { animator->start(); } ); } diff --git a/examples/iotdashboard/StoragePage.h b/examples/iotdashboard/StoragePage.h index efb09d412..7f1d01463 100644 --- a/examples/iotdashboard/StoragePage.h +++ b/examples/iotdashboard/StoragePage.h @@ -7,44 +7,15 @@ #include #include -#include - -class QQuickItem; class StoragePage final : public QskLinearBox { public: - QSK_SUBCONTROLS( Panel ) - explicit StoragePage( QQuickItem* parent = nullptr ); - - private: - struct Storage - { - struct Media - { - qreal pictures = 0; - qreal music = 0; - qreal videos = 0; - qreal documents = 0; - qreal others = 0; + QSK_SUBCONTROLS( Panel, Status ) - inline constexpr bool operator==( const Media& rhs ) const noexcept - { - return pictures == rhs.pictures && music == rhs.music && videos == rhs.videos && - documents == rhs.documents && others == rhs.others; - } - - inline constexpr qreal free() const noexcept - { - return 1.0 - pictures - music - videos - documents - others; - } - }; - - QString title; - QString description; - Media distribution; - }; + StoragePage( QQuickItem* parent = nullptr ); + private: void addRow( const QString& title, const QString& description, qreal pictures, qreal music, qreal videos, qreal documents, qreal others ); }; diff --git a/examples/iotdashboard/TopBar.cpp b/examples/iotdashboard/TopBar.cpp index 31b6d9329..0667f21e4 100644 --- a/examples/iotdashboard/TopBar.cpp +++ b/examples/iotdashboard/TopBar.cpp @@ -4,7 +4,7 @@ *****************************************************************************/ #include "TopBar.h" -#include "EnergyMeter.h" +#include "ValueMeter.h" #include #include @@ -41,6 +41,22 @@ namespace return TopBarItem::Item4; } } + + class EnergyMeter : public ValueMeter + { + public: + EnergyMeter( const QColor& textColor, + const QskGradient& gradient, int value, QQuickItem* parent ) + : ValueMeter( parent ) + { + setFillGradient( gradient ); + setValue( value ); + setTextColor( textColor ); + + setFixedSize( 57, 57 ); + } + }; + } TopBarItem::TopBarItem( @@ -62,9 +78,7 @@ TopBarItem::TopBarItem( const auto subcontrol = subcontrolForIndex( index ); const auto textColor = color( subcontrol | QskAspect::TextColor ); - auto meter = new EnergyMeter( - textColor, gradient, progress, pieChartAndDisplay ); - meter->setSizePolicy( Qt::Horizontal, QskSizePolicy::Constrained ); + (void) new EnergyMeter( textColor, gradient, progress, pieChartAndDisplay ); auto display = new QskLinearBox( Qt::Vertical, pieChartAndDisplay ); display->setSpacing( 0 ); diff --git a/examples/iotdashboard/ValueMeter.cpp b/examples/iotdashboard/ValueMeter.cpp new file mode 100644 index 000000000..43f34dd2a --- /dev/null +++ b/examples/iotdashboard/ValueMeter.cpp @@ -0,0 +1,42 @@ +/****************************************************************************** + * Copyright (C) 2022 Edelhirsch Software GmbH + * SPDX-License-Identifier: BSD-3-Clause + *****************************************************************************/ + +#include "ValueMeter.h" + +#include +#include + +ValueMeter::ValueMeter( QQuickItem* parent ) + : QskProgressRing( parent ) +{ + setAutoLayoutChildren( true ); + initSizePolicy( QskSizePolicy::MinimumExpanding, QskSizePolicy::Constrained ); + + m_label = new QskTextLabel( this ); + m_label->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); + m_label->setLayoutAlignmentHint( Qt::AlignCenter ); + m_label->setFontRole( QskFontRole::Caption ); + + connect( this, &QskProgressRing::valueChanged, + this, &ValueMeter::updateMeter ); + + updateMeter( value() ); +} + +void ValueMeter::updateMeter( const qreal value ) +{ + m_label->setText( text( value ) ); +} + +QString ValueMeter::text( qreal value ) const +{ + value = static_cast< int >( value ); + return locale().toString( value ) + ' ' + locale().percent(); +} + +void ValueMeter::setTextColor( const QColor& color ) +{ + m_label->setTextColor( color ); +} diff --git a/examples/iotdashboard/StorageMeter.h b/examples/iotdashboard/ValueMeter.h similarity index 66% rename from examples/iotdashboard/StorageMeter.h rename to examples/iotdashboard/ValueMeter.h index c6a6af42b..52d31dd31 100644 --- a/examples/iotdashboard/StorageMeter.h +++ b/examples/iotdashboard/ValueMeter.h @@ -9,16 +9,17 @@ class QskTextLabel; -class StorageMeter final : public QskProgressRing +class ValueMeter : public QskProgressRing { public: - QSK_SUBCONTROLS( Status ) + ValueMeter( QQuickItem* parent = nullptr ); - StorageMeter( QQuickItem* parent = nullptr ) noexcept; + void setTextColor( const QColor& ); + + protected: + virtual QString text( qreal ) const; private: void updateMeter( qreal value ); - QSizeF contentsSizeHint( Qt::SizeHint, const QSizeF& ) const override; - QskTextLabel* m_label = nullptr; };