qskinny/examples/iotdashboard/StorageMeter.cpp

69 lines
1.7 KiB
C++
Raw Normal View History

2023-01-05 16:08:50 +00:00
/******************************************************************************
* Copyright (C) 2022 Edelhirsch Software GmbH
2023-04-06 08:15:03 +00:00
* SPDX-License-Identifier: BSD-3-Clause
2023-01-05 16:08:50 +00:00
*****************************************************************************/
2023-01-04 16:17:17 +00:00
#include "StorageMeter.h"
2024-02-19 14:51:55 +00:00
#include <QskFontRole.h>
2023-01-04 16:17:17 +00:00
#include <QskTextLabel.h>
2023-01-05 16:08:50 +00:00
QSK_SUBCONTROL( StorageMeter, Status )
2023-01-04 16:17:17 +00:00
2023-01-05 16:08:50 +00:00
StorageMeter::StorageMeter( QQuickItem* parent ) noexcept
: QskProgressRing( parent )
2023-01-04 16:17:17 +00:00
{
2023-01-05 16:08:50 +00:00
setAutoLayoutChildren( true );
initSizePolicy( QskSizePolicy::MinimumExpanding, QskSizePolicy::Constrained );
2023-01-05 16:08:50 +00:00
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() );
2023-01-04 16:17:17 +00:00
}
void StorageMeter::updateMeter( const qreal value )
2023-01-04 16:17:17 +00:00
{
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 );
2023-01-05 16:08:50 +00:00
}
2023-01-04 16:17:17 +00:00
QSizeF StorageMeter::contentsSizeHint(
Qt::SizeHint which, const QSizeF& constraint ) const
2023-01-05 16:08:50 +00:00
{
if ( which != Qt::PreferredSize )
return QSizeF();
2023-01-04 16:17:17 +00:00
2023-01-05 16:08:50 +00:00
qreal size;
2023-01-04 16:17:17 +00:00
2023-01-05 16:08:50 +00:00
if ( constraint.width() > 0 )
{
size = constraint.width();
}
else if ( constraint.height() > 0 )
{
size = constraint.height();
}
else
{
size = 57;
}
2023-01-04 16:17:17 +00:00
2023-01-05 16:08:50 +00:00
return QSizeF( size, size );
2023-01-04 16:17:17 +00:00
}