qskinny/examples/iot-dashboard/PieChartPainted.cpp

128 lines
3.8 KiB
C++
Raw Normal View History

#include "PieChartPainted.h"
2020-09-30 15:38:51 +00:00
#include <QskAnimator.h>
2020-09-23 15:45:06 +00:00
#include <QskBox.h>
2020-09-30 15:38:51 +00:00
#include <QskRgbValue.h>
2020-09-23 15:45:06 +00:00
#include <QskSetup.h>
#include <QskSkin.h>
#include <QskTextLabel.h>
#include <QFontMetricsF>
2020-09-30 15:38:51 +00:00
#include <QGuiApplication>
#include <QQuickPaintedItem>
2020-09-30 15:38:51 +00:00
#include <QQuickWindow>
2020-09-23 15:45:06 +00:00
QSK_SUBCONTROL( PieChartPainted, Panel )
2020-09-30 15:38:51 +00:00
namespace
{
QColor invertedColor( const QColor& c )
{
QColor ret = { 255 - c.red(), 255 - c.green(), 255 - c.blue()};
return ret;
}
}
// ### There must be an easier way to do this
class ProgressBarAnimator : public QskAnimator
{
public:
ProgressBarAnimator( PieChartPainted* pieChart, CircularProgressBar* progressBar )
: m_pieChart( pieChart )
, m_progressBar( progressBar )
{
QQuickWindow* w = static_cast<QQuickWindow*>( qGuiApp->allWindows().at( 0 ) );
setWindow( w );
setDuration( 500 );
setEasingCurve( QEasingCurve::Linear );
setAutoRepeat( false );
}
void setup() override
{
m_backgroundColor = m_pieChart->color( PieChartPainted::Panel );
m_ringGradient = m_progressBar->ringGradient();
}
void advance( qreal value ) override
{
const QColor c = m_backgroundColor;
const QColor c2 = invertedColor( c );
const QColor newColor = QskRgb::interpolated( c2, c, value );
m_progressBar->setBackgroundColor( newColor );
QRadialGradient gradient = m_ringGradient;
QRadialGradient newGradient = gradient;
for( const QGradientStop& stop : gradient.stops() )
{
QColor c = stop.second;
QColor c2 = invertedColor( c );
const QColor newColor = QskRgb::interpolated( c, c2, value );
newGradient.setColorAt( stop.first, newColor );
}
m_progressBar->setRingGradient( newGradient );
m_progressBar->update();
}
private:
QColor m_backgroundColor;
QRadialGradient m_ringGradient;
PieChartPainted* m_pieChart;
CircularProgressBar* m_progressBar;
};
2020-09-23 15:45:06 +00:00
PieChartPainted::PieChartPainted( const QColor& color, const QGradient& gradient, int progress, int value, QQuickItem* parent )
: QskControl( parent )
, m_color( color )
, m_gradient( gradient )
, m_progressBar( new CircularProgressBar( gradient, progress, this ) )
, m_progressLabel( new QskTextLabel( this ) )
2020-09-30 15:38:51 +00:00
, m_animator( new ProgressBarAnimator( this, m_progressBar ) )
{
2020-09-23 15:45:06 +00:00
setAutoLayoutChildren( true );
auto progressText = QString::number( progress ) + " %";
m_progressLabel->setText( progressText );
m_progressLabel->setFontRole( QskSkin::SmallFont );
m_progressLabel->setTextColor( color );
const QColor c = this->color( Panel );
m_progressBar->setBackgroundColor( c );
connect( qskSetup, &QskSetup::skinChanged, [this]()
{
2020-09-30 15:38:51 +00:00
m_animator->start();
2020-09-23 15:45:06 +00:00
} );
}
QskAspect::Subcontrol PieChartPainted::effectiveSubcontrol( QskAspect::Subcontrol subControl ) const
{
if( subControl == QskBox::Panel )
{
return PieChartPainted::Panel;
}
2020-09-23 15:45:06 +00:00
return subControl;
}
2020-09-23 15:45:06 +00:00
QSizeF PieChartPainted::contentsSizeHint( Qt::SizeHint /*sizeHint*/, const QSizeF& /*size*/ ) const
{
return {57, 57};
}
void PieChartPainted::updateLayout()
{
2020-09-23 15:45:06 +00:00
m_progressBar->setContentsSize( size().toSize() );
m_progressBar->update();
auto rect = contentsRect();
2020-09-23 15:45:06 +00:00
QFontMetricsF progressMetrics( m_progressLabel->effectiveFont( QskTextLabel::Text ) );
auto textWidth = progressMetrics.width( m_progressLabel->text() );
auto posX = rect.width() / 2 - textWidth / 2;
auto posY = rect.height() / 2 - progressMetrics.height() / 2;
2020-09-23 15:45:06 +00:00
m_progressLabel->setPosition( {posX, posY} );
m_progressLabel->setFixedWidth( textWidth );
}