qskinny/examples/iot-dashboard/PieChartPainted.cpp

79 lines
2.4 KiB
C++
Raw Normal View History

#include "PieChartPainted.h"
2020-09-23 15:45:06 +00:00
#include <QskBox.h>
#include <QskSetup.h>
#include <QskSkin.h>
#include <QskTextLabel.h>
#include <QFontMetricsF>
#include <QQuickPaintedItem>
2020-09-23 15:45:06 +00:00
QSK_SUBCONTROL( PieChartPainted, Panel )
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 ) )
// , m_numberLabel(new QskTextLabel(QString::number(value), this))
// , m_unitLabel(new QskTextLabel("kwH", this))
{
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]()
{
const QColor c = this->color( Panel );
m_progressBar->setBackgroundColor( c );
2020-09-30 14:25:42 +00:00
QRadialGradient gradient = m_progressBar->ringGradient();
QRadialGradient newGradient = gradient;
for( const QGradientStop& stop : gradient.stops() )
{
QColor s = stop.second;
QColor newColor = { 255 - s.red(), 255 - s.green(), 255 - s.blue()};
newGradient.setColorAt( stop.first, newColor );
}
m_progressBar->setRingGradient( newGradient );
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 );
}