qskinny/examples/iotdashboard/StorageMeter.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2023-01-05 17:08:50 +01:00
/******************************************************************************
* Copyright (C) 2022 Edelhirsch Software GmbH
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Claus
2023-01-05 17:08:50 +01:00
*****************************************************************************/
2023-01-04 17:17:17 +01:00
#include "StorageMeter.h"
#include "CircularProgressBar.h"
2023-01-05 17:08:50 +01:00
#include <QskSkin.h>
2023-01-04 17:17:17 +01:00
#include <QskTextLabel.h>
2023-01-05 17:08:50 +01:00
QSK_SUBCONTROL( StorageMeter, Status )
2023-01-04 17:17:17 +01:00
2023-01-05 17:08:50 +01:00
namespace
2023-01-04 17:17:17 +01:00
{
2023-01-05 17:08:50 +01:00
inline QString make_text( const QLocale& locale, const qreal value )
{
return locale.toString( static_cast< int >( value ) ) + " " + locale.percent();
}
2023-01-04 17:17:17 +01:00
}
2023-01-05 17:08:50 +01:00
StorageMeter::StorageMeter( QQuickItem* parent ) noexcept
: CircularProgressBar( parent )
, label( new QskTextLabel( this ) )
2023-01-04 17:17:17 +01:00
{
2023-01-05 17:08:50 +01:00
setAutoLayoutChildren( true );
setSizePolicy( QskSizePolicy::Preferred, QskSizePolicy::Constrained );
label->setText( make_text( locale(), value() ) );
label->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
label->setLayoutAlignmentHint( Qt::AlignCenter );
label->setFontRole( QskSkin::SmallFont );
2023-01-04 17:17:17 +01:00
}
2023-01-05 17:08:50 +01:00
void StorageMeter::setValue( const qreal value )
2023-01-04 17:17:17 +01:00
{
2023-01-05 17:08:50 +01:00
const auto gradient = gradientHint( StorageMeter::Status );
const auto color = gradient.extracted( value / 100.0, value / 100.0 ).startColor();
setGradientHint( StorageMeter::Bar, { color, color.lighter() } );
CircularProgressBar::setValue( value );
label->setTextColor( color );
label->setText( make_text( locale(), value ) );
}
2023-01-04 17:17:17 +01:00
2023-01-05 17:08:50 +01:00
QSizeF StorageMeter::contentsSizeHint( Qt::SizeHint which, const QSizeF& constraint ) const
{
if ( which != Qt::PreferredSize )
return QSizeF();
2023-01-04 17:17:17 +01:00
2023-01-05 17:08:50 +01:00
qreal size;
2023-01-04 17:17:17 +01:00
2023-01-05 17:08:50 +01:00
if ( constraint.width() > 0 )
{
size = constraint.width();
}
else if ( constraint.height() > 0 )
{
size = constraint.height();
}
else
{
size = 57;
}
2023-01-04 17:17:17 +01:00
2023-01-05 17:08:50 +01:00
return QSizeF( size, size );
2023-01-04 17:17:17 +01:00
}