qskinny/examples/iot-dashboard/MyDevices.cpp

80 lines
2.6 KiB
C++
Raw Normal View History

2020-08-17 15:19:05 +00:00
#include "MyDevices.h"
2021-03-31 08:08:30 +00:00
#include "Skin.h"
2020-08-17 15:19:05 +00:00
#include "RoundedIcon.h"
2020-08-26 12:45:57 +00:00
#include <QskGraphic.h>
#include <QskGraphicLabel.h>
2020-08-17 15:19:05 +00:00
#include <QskGridBox.h>
#include <QskTextLabel.h>
2020-08-26 12:45:57 +00:00
#include <QImage>
2020-09-08 15:16:30 +00:00
namespace
{
2020-08-17 15:19:05 +00:00
class Device : public QskLinearBox
{
2020-09-08 15:16:30 +00:00
public:
2021-03-31 16:39:19 +00:00
Device( const QString& name, bool isBright, QQuickItem* parent )
2020-09-08 15:16:30 +00:00
: QskLinearBox( Qt::Vertical, parent )
, m_name( name )
{
setDefaultAlignment( Qt::AlignCenter );
setAutoAddChildren( false );
2021-04-07 07:44:21 +00:00
m_icon = new RoundedIcon( QString(), isBright, true, this );
m_icon->setSkinState( m_icon->skinState() | RoundedIcon::Small );
2020-09-08 15:16:30 +00:00
m_icon->setOpacity( 0.15 );
addItem( m_icon );
auto* textLabel = new QskTextLabel( name, this );
textLabel->setFontRole( QskSkin::TinyFont );
textLabel->setAlignment( Qt::AlignHCenter );
addItem( textLabel );
auto fileName = name.toLower();
fileName.replace( ' ', '-' );
fileName = ":/images/" + fileName + ".png";
QImage image( fileName );
auto graphic = QskGraphic::fromImage( image );
m_graphicLabel = new QskGraphicLabel( graphic, this );
}
protected:
void updateLayout() override
{
QskLinearBox::updateLayout();
2021-04-07 07:44:21 +00:00
// We cannot use the icon from RoundedIcon here because
// it would inherit the transparency
const qreal size = metric( RoundedIcon::Icon | QskAspect::Size );
m_graphicLabel->setSize( {size, size} );
2020-09-08 15:16:30 +00:00
m_graphicLabel->setPosition( { m_icon->position().x() + ( m_icon->width() - m_graphicLabel->width() ) / 2,
( m_icon->position().y() + m_icon->height() - m_graphicLabel->height() ) / 2 } );
}
private:
QString m_name;
RoundedIcon* m_icon;
QskGraphicLabel* m_graphicLabel;
2020-08-17 15:19:05 +00:00
};
}
2020-09-17 14:47:34 +00:00
MyDevices::MyDevices( QQuickItem* parent )
: Box( "My devices", parent )
2020-08-17 15:19:05 +00:00
{
2021-04-07 07:44:21 +00:00
auto* gridBox = new QskGridBox( this );
gridBox->setSpacing( 15 );
2020-08-17 15:19:05 +00:00
2021-04-07 07:44:21 +00:00
auto* lamps = new Device( "Lamps", true, gridBox );
gridBox->addItem( lamps, 0, 0 );
2020-08-17 15:19:05 +00:00
2021-04-07 07:44:21 +00:00
auto* musicSystem = new Device( "Music System", false, gridBox );
gridBox->addItem( musicSystem, 0, 1 );
2020-08-17 15:19:05 +00:00
2021-04-07 07:44:21 +00:00
auto* ac = new Device( "AC", false, gridBox );
gridBox->addItem( ac, 1, 0 );
2020-08-17 15:19:05 +00:00
2021-04-07 07:44:21 +00:00
auto* router = new Device( "Router", true, gridBox );
gridBox->addItem( router, 1, 1 );
2020-08-17 15:19:05 +00:00
}