diff --git a/examples/iotdashboard/BoxWithButtons.cpp b/examples/iotdashboard/BoxWithButtons.cpp index 1cdc57cc..0ea0e9c2 100644 --- a/examples/iotdashboard/BoxWithButtons.cpp +++ b/examples/iotdashboard/BoxWithButtons.cpp @@ -45,7 +45,7 @@ BoxWithButtons::BoxWithButtons( const QString& title, const QString& value, layout->setSpacing( 20 ); auto iconLabel = new RoundedIcon( isBright, layout ); - iconLabel->setIcon( title ); + iconLabel->setSource( title ); iconLabel->setFixedSize( 68, 68 ); auto titleAndValue = new QskLinearBox( Qt::Vertical, layout ); diff --git a/examples/iotdashboard/GraphicProvider.cpp b/examples/iotdashboard/GraphicProvider.cpp new file mode 100644 index 00000000..45e777de --- /dev/null +++ b/examples/iotdashboard/GraphicProvider.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (C) 2021 Edelhirsch Software GmbH + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "GraphicProvider.h" + +#include + +#include +#include +#include +#include + +const inline QString pathName( const QString& baseName, const QString& suffix ) +{ + QString fileName = baseName; + if ( !suffix.isEmpty() ) + fileName += suffix; + + return QFile( fileName ).exists() ? fileName : QString(); +} + +const QskGraphic* GraphicProvider::loadGraphic( const QString& id ) const +{ + static QString scope = QStringLiteral( ":/images/" ); + + QString baseName = scope; + baseName += id.toLower().replace( ' ', '-' ); + + auto path = pathName( baseName, QString() ); + + if ( path.isEmpty() ) + path = pathName( baseName, ".png" ); + + if ( path.isEmpty() ) + path = pathName( baseName, ".svg" ); + + QskGraphic graphic; + + if ( !path.isEmpty() ) + { + if ( path.endsWith( ".png" ) ) + { + graphic = QskGraphic::fromImage( QImage( path ) ); + } + else + { + QSvgRenderer renderer; + if ( renderer.load( path ) ) + { + QPainter painter( &graphic ); + renderer.render( &painter ); + painter.end(); + } + } + } + + return graphic.isNull() ? nullptr : new QskGraphic( graphic ); +} diff --git a/examples/iotdashboard/GraphicProvider.h b/examples/iotdashboard/GraphicProvider.h new file mode 100644 index 00000000..1fe89518 --- /dev/null +++ b/examples/iotdashboard/GraphicProvider.h @@ -0,0 +1,15 @@ +/****************************************************************************** + * Copyright (C) 2021 Edelhirsch Software GmbH + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#pragma once + +#include + +class GraphicProvider final : public QskGraphicProvider +{ + protected: + const QskGraphic* loadGraphic( const QString& id ) const override; +}; + diff --git a/examples/iotdashboard/MenuBar.cpp b/examples/iotdashboard/MenuBar.cpp index 54a8ada4..96592c5b 100644 --- a/examples/iotdashboard/MenuBar.cpp +++ b/examples/iotdashboard/MenuBar.cpp @@ -5,14 +5,6 @@ #include "MenuBar.h" -#include -#include -#include -#include -#include - -#include - QSK_SUBCONTROL( MenuBarTopLabel, Graphic ) QSK_SUBCONTROL( MenuBarGraphicLabel, Graphic ) QSK_SUBCONTROL( MenuBarLabel, Text ) @@ -32,11 +24,7 @@ MenuItem::MenuItem( const QString& name, QQuickItem* parent ) setPanel( true ); setSubcontrolProxy( QskBox::Panel, MenuItem::Panel ); - QString fileName = ":/images/" + name.toLower() + ".png"; - QImage image( fileName ); - auto graphic = QskGraphic::fromImage( image ); - - auto graphicLabel = new MenuBarGraphicLabel( graphic, this ); + auto graphicLabel = new MenuBarGraphicLabel( name, this ); graphicLabel->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); graphicLabel->setFixedWidth( metric( MenuBarGraphicLabel::Graphic | QskAspect::Size ) ); @@ -52,9 +40,7 @@ MenuBar::MenuBar( QQuickItem* parent ) initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Preferred ); setSpacing( 8 ); - auto graphic = QskGraphic::fromImage( QImage( ":/images/main-icon.png" ) ); - - auto graphicLabel = new MenuBarTopLabel( graphic, this ); + auto graphicLabel = new MenuBarTopLabel( "main-icon", this ); graphicLabel->setMargins( marginHint( MenuBarTopLabel::Graphic ) ); graphicLabel->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); diff --git a/examples/iotdashboard/MenuBar.h b/examples/iotdashboard/MenuBar.h index a0a6f5fe..b141f260 100644 --- a/examples/iotdashboard/MenuBar.h +++ b/examples/iotdashboard/MenuBar.h @@ -16,8 +16,8 @@ class MenuBarTopLabel final : public QskGraphicLabel public: QSK_SUBCONTROLS( Graphic ) - MenuBarTopLabel( const QskGraphic& graphic, QQuickItem* parent = nullptr ) - : QskGraphicLabel( graphic, parent ) + MenuBarTopLabel( const QString& icon, QQuickItem* parent = nullptr ) + : QskGraphicLabel( icon, parent ) { setSubcontrolProxy( QskGraphicLabel::Graphic, Graphic ); } @@ -30,8 +30,8 @@ class MenuBarGraphicLabel final : public QskGraphicLabel public: QSK_SUBCONTROLS( Graphic ) - MenuBarGraphicLabel( const QskGraphic& graphic, QQuickItem* parent = nullptr ) - : QskGraphicLabel( graphic, parent ) + MenuBarGraphicLabel( const QString& icon, QQuickItem* parent = nullptr ) + : QskGraphicLabel( icon, parent ) { setSubcontrolProxy( QskGraphicLabel::Graphic, Graphic ); } diff --git a/examples/iotdashboard/MyDevices.cpp b/examples/iotdashboard/MyDevices.cpp index 8db92b8c..d5fa4945 100644 --- a/examples/iotdashboard/MyDevices.cpp +++ b/examples/iotdashboard/MyDevices.cpp @@ -26,7 +26,7 @@ namespace auto icon = new RoundedIcon( isBright, this ); icon->setPale( true ); - icon->setIcon( name ); + icon->setSource( name ); icon->setFixedSize( 68, 68 ); auto textLabel = new QskTextLabel( name, this ); diff --git a/examples/iotdashboard/RoundButton.cpp b/examples/iotdashboard/RoundButton.cpp index b5c71173..fcce9380 100644 --- a/examples/iotdashboard/RoundButton.cpp +++ b/examples/iotdashboard/RoundButton.cpp @@ -20,20 +20,15 @@ RoundButton::RoundButton( QskAspect::Placement placement, QQuickItem* parent ) setSubcontrolProxy( QskPushButton::Panel, RoundButton::Panel ); setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Expanding ); - QskGraphic graphic; - QImage image; - if( placement == QskAspect::Top ) { setSkinStateFlag( Top ); - image.load( ":/images/up.svg" ); + setGraphicSource( "up" ); } else { - image.load( ":/images/down.svg" ); + setGraphicSource( "down" ); } - setGraphicSourceSize( image.size() ); - graphic = QskGraphic::fromImage( image ); - setGraphic( graphic ); + setGraphicSourceSize( graphic().defaultSize() * 1.2 ); } diff --git a/examples/iotdashboard/RoundedIcon.cpp b/examples/iotdashboard/RoundedIcon.cpp index 9f1f738c..1a65593e 100644 --- a/examples/iotdashboard/RoundedIcon.cpp +++ b/examples/iotdashboard/RoundedIcon.cpp @@ -5,9 +5,6 @@ #include "RoundedIcon.h" -#include -#include - QSK_SUBCONTROL( RoundedIcon, Panel ) QSK_SUBCONTROL( RoundedIcon, PalePanel ) @@ -31,16 +28,4 @@ void RoundedIcon::setPale( bool on ) setSubcontrolProxy( QskGraphicLabel::Panel, on ? PalePanel : Panel ); } -void RoundedIcon::setIcon( const QString& iconName ) -{ - // we should use a graphic provider, TODO ... - - QString fileName = ":/images/"; - fileName += iconName.toLower().replace( ' ', '-' ); - fileName += ".png"; - - const QImage image( fileName ); - setGraphic( QskGraphic::fromImage( image ) ); -} - #include "moc_RoundedIcon.cpp" diff --git a/examples/iotdashboard/RoundedIcon.h b/examples/iotdashboard/RoundedIcon.h index 2e8c8bd6..b232d6d1 100644 --- a/examples/iotdashboard/RoundedIcon.h +++ b/examples/iotdashboard/RoundedIcon.h @@ -19,6 +19,5 @@ class RoundedIcon : public QskGraphicLabel RoundedIcon( bool isBright, QQuickItem* parent = nullptr ); - void setIcon( const QString& ); void setPale( bool ); }; diff --git a/examples/iotdashboard/iotdashboard.pro b/examples/iotdashboard/iotdashboard.pro index 27e7513e..e2ee840e 100644 --- a/examples/iotdashboard/iotdashboard.pro +++ b/examples/iotdashboard/iotdashboard.pro @@ -1,5 +1,7 @@ CONFIG += qskexample +QT += svg + SOURCES += \ Box.cpp \ BoxWithButtons.cpp \ @@ -7,6 +9,7 @@ SOURCES += \ CircularProgressBarSkinlet.cpp \ Diagram.cpp \ DiagramSkinlet.cpp \ + GraphicProvider.cpp \ LightIntensity.cpp \ MainContent.cpp \ MenuBar.cpp \ @@ -34,6 +37,7 @@ HEADERS += \ CircularProgressBarSkinlet.h \ Diagram.h \ DiagramSkinlet.h \ + GraphicProvider.h \ LightIntensity.h \ MainContent.h \ MainWindow.h \ diff --git a/examples/iotdashboard/main.cpp b/examples/iotdashboard/main.cpp index 4eb2338c..80be72ba 100644 --- a/examples/iotdashboard/main.cpp +++ b/examples/iotdashboard/main.cpp @@ -4,6 +4,7 @@ *****************************************************************************/ #include "MainWindow.h" +#include "GraphicProvider.h" #include "Skin.h" #include @@ -62,6 +63,8 @@ int main( int argc, char* argv[] ) SkinnyFont::init( &app ); + Qsk::addGraphicProvider( QString(), new GraphicProvider() ); + // disable default skins qskSkinManager->setPluginPaths( QStringList() ); // no plugins qskSkinManager->unregisterFactory( "materialfactory" );