qskinny/examples/iot-dashboard/RoundedIcon.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

2020-08-14 10:48:15 +00:00
#include "RoundedIcon.h"
#include <QskBoxShapeMetrics.h>
#include <QskGraphic.h>
#include <QskGraphicLabel.h>
#include <QImage>
2021-03-31 15:50:57 +00:00
QSK_SUBCONTROL( RoundedIcon, Panel )
2021-04-07 07:44:21 +00:00
QSK_SUBCONTROL( RoundedIcon, Icon )
2021-03-31 16:02:34 +00:00
QSK_STATE( RoundedIcon, Bright, ( QskAspect::FirstUserState << 1 ) )
2021-04-07 07:44:21 +00:00
QSK_STATE( RoundedIcon, Small, ( QskAspect::FirstUserState << 2 ) )
2021-03-31 15:50:57 +00:00
2021-04-07 07:44:21 +00:00
RoundedIcon::RoundedIcon( const QString& iconName, bool isBright, bool isSmall, QQuickItem* parent )
2021-03-26 08:45:33 +00:00
: QskBox( parent )
, m_iconName( iconName )
2020-08-14 10:48:15 +00:00
{
2021-03-26 08:45:33 +00:00
setPanel( true );
2020-08-14 10:48:15 +00:00
setPolishOnResize( true );
2021-04-07 07:44:21 +00:00
if( isSmall )
2021-03-31 16:02:34 +00:00
{
2021-04-07 07:44:21 +00:00
setSkinState( skinState() | Small );
2021-03-31 16:02:34 +00:00
}
2021-03-26 08:45:33 +00:00
setSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Constrained );
2021-04-07 07:44:21 +00:00
const qreal size = metric( RoundedIcon::Panel | QskAspect::Size );
setFixedWidth( size );
if( isBright )
{
setSkinState( Bright );
}
2020-08-14 10:48:15 +00:00
QString fileName = ":/images/" + iconName + ".png";
2020-08-26 12:45:57 +00:00
2021-03-26 08:45:33 +00:00
if( QFile::exists( fileName ) )
2020-08-26 12:45:57 +00:00
{
QImage image( fileName );
auto graphic = QskGraphic::fromImage( image );
m_graphicLabel = new QskGraphicLabel( graphic, this );
}
2020-08-14 10:48:15 +00:00
}
void RoundedIcon::updateLayout()
{
2021-03-26 08:45:33 +00:00
if( m_graphicLabel )
2020-08-26 12:45:57 +00:00
{
2021-04-07 07:44:21 +00:00
const qreal size = metric( Icon | QskAspect::Size );
m_graphicLabel->setSize( {size, size} );
2020-08-26 12:45:57 +00:00
m_graphicLabel->setPosition( { ( width() - m_graphicLabel->width() ) / 2, ( height() - m_graphicLabel->height() ) / 2 } );
}
2020-08-14 10:48:15 +00:00
}
QSizeF RoundedIcon::contentsSizeHint( Qt::SizeHint /*which*/, const QSizeF& size ) const
{
QSizeF ret = size;
ret.setHeight( size.width() );
return ret;
}