From ef4dc978ea7a8a66405d7a9d9e2d21519f2d3565 Mon Sep 17 00:00:00 2001 From: Znurre Date: Thu, 29 Mar 2018 18:29:00 +0200 Subject: [PATCH] Rework Pixmap/Icon into singleton loaders --- src/declarativeicon.cpp | 126 ------------------ src/declarativeiconloader.cpp | 82 ++++++++++++ ...tiveicon_p.h => declarativeiconloader_p.h} | 69 +++------- src/declarativepixmap.cpp | 102 -------------- src/declarativepixmap_p.h | 83 ------------ src/declarativepixmaploader.cpp | 47 +++++++ src/declarativepixmaploader_p.h | 52 ++++++++ src/declarativewidgets_plugin.cpp | 10 +- src/src.pro | 12 +- 9 files changed, 214 insertions(+), 369 deletions(-) delete mode 100644 src/declarativeicon.cpp create mode 100644 src/declarativeiconloader.cpp rename src/{declarativeicon_p.h => declarativeiconloader_p.h} (52%) delete mode 100644 src/declarativepixmap.cpp delete mode 100644 src/declarativepixmap_p.h create mode 100644 src/declarativepixmaploader.cpp create mode 100644 src/declarativepixmaploader_p.h diff --git a/src/declarativeicon.cpp b/src/declarativeicon.cpp deleted file mode 100644 index ac0fcdb..0000000 --- a/src/declarativeicon.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - declarativeicon.cpp - - This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. - - Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com - Author: Kevin Krammer - - Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in - accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. - - Contact info@kdab.com if any conditions of this licensing are not clear to you. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "declarativeicon_p.h" - -DeclarativeIconAttached::DeclarativeIconAttached(QObject *parent) - : QObject(parent) -{ -} - -QString DeclarativeIconAttached::themeName() const -{ - return QIcon::themeName(); -} - -void DeclarativeIconAttached::setThemeName(const QString &name) -{ - if (name == QIcon::themeName()) - return; - - QIcon::setThemeName(name); - emit themeNameChanged(name); -} - -QStringList DeclarativeIconAttached::themeSearchPaths() const -{ - return QIcon::themeSearchPaths(); -} - -void DeclarativeIconAttached::setThemeSearchPaths(const QStringList &paths) -{ - if (paths == QIcon::themeSearchPaths()) - return; - - QIcon::setThemeSearchPaths(paths); - emit themeSearchPathsChanged(paths); -} - -QIcon DeclarativeIconAttached::fromTheme(const QString &name) -{ - return QIcon::fromTheme(name); -} - -QIcon DeclarativeIconAttached::fromFileName(const QString &fileName) -{ - return QIcon(fileName); -} - -class DeclarativeIcon::Private -{ - public: - QIcon icon; -}; - -DeclarativeIcon::DeclarativeIcon(QObject *parent) - : QObject(parent) - , d(new Private) -{ -} - -DeclarativeIcon::~DeclarativeIcon() -{ - delete d; -} - -QIcon DeclarativeIcon::icon() const -{ - return d->icon; -} - -void DeclarativeIcon::setIcon(const QIcon &icon) -{ - if (icon.cacheKey() == d->icon.cacheKey()) - return; - - const QString oldName = d->icon.name(); - const bool oldIsNull = d->icon.isNull(); - - d->icon = icon; - emit iconChanged(icon); - - if (oldName != d->icon.name()) - emit nameChanged(d->icon.name()); - - if (oldIsNull == d->icon.isNull()) - emit isNullChanged(d->icon.isNull()); -} - -QString DeclarativeIcon::name() const -{ - return d->icon.name(); -} - -bool DeclarativeIcon::isNull() const -{ - return d->icon.isNull(); -} - -DeclarativeIconAttached *DeclarativeIcon::qmlAttachedProperties(QObject *parent) -{ - return new DeclarativeIconAttached(parent); -} diff --git a/src/declarativeiconloader.cpp b/src/declarativeiconloader.cpp new file mode 100644 index 0000000..d18cbfa --- /dev/null +++ b/src/declarativeiconloader.cpp @@ -0,0 +1,82 @@ +/* + declarativeiconloader.cpp + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Kevin Krammer + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "declarativeiconloader_p.h" + +DeclarativeIconLoader::DeclarativeIconLoader(QObject *parent) + : QObject(parent) +{ +} + +QString DeclarativeIconLoader::themeName() const +{ + return QIcon::themeName(); +} + +void DeclarativeIconLoader::setThemeName(const QString &name) +{ + if (name == QIcon::themeName()) + return; + + QIcon::setThemeName(name); + + emit themeNameChanged(name); +} + +QStringList DeclarativeIconLoader::themeSearchPaths() const +{ + return QIcon::themeSearchPaths(); +} + +void DeclarativeIconLoader::setThemeSearchPaths(const QStringList &paths) +{ + if (paths == QIcon::themeSearchPaths()) + return; + + QIcon::setThemeSearchPaths(paths); + + emit themeSearchPathsChanged(paths); +} + +QIcon DeclarativeIconLoader::fromTheme(const QString &name) +{ + return QIcon::fromTheme(name); +} + +QIcon DeclarativeIconLoader::fromFileName(const QString &fileName) +{ + return QIcon(fileName); +} + +QObject *DeclarativeIconLoader::instance(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + + return new DeclarativeIconLoader(); +} diff --git a/src/declarativeicon_p.h b/src/declarativeiconloader_p.h similarity index 52% rename from src/declarativeicon_p.h rename to src/declarativeiconloader_p.h index 225be1d..648759e 100644 --- a/src/declarativeicon_p.h +++ b/src/declarativeiconloader_p.h @@ -1,10 +1,11 @@ /* - declarativeicon_p.h + declarativeiconloader_p.h This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. - Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com Author: Kevin Krammer + Author: Lova Widmark Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. @@ -25,24 +26,26 @@ along with this program. If not, see . */ -#ifndef DECLARATIVEICON_P_H -#define DECLARATIVEICON_P_H - -#include +#ifndef DECLARATIVEICONLOADER_P_H +#define DECLARATIVEICONLOADER_P_H #include #include -#include -#include -class DeclarativeIconAttached : public QObject +QT_BEGIN_NAMESPACE +class QQmlEngine; +class QJSEngine; +QT_END_NAMESPACE + +class DeclarativeIconLoader : public QObject { - Q_OBJECT - Q_PROPERTY(QString themeName READ themeName WRITE setThemeName NOTIFY themeNameChanged) - Q_PROPERTY(QStringList themeSearchPaths READ themeSearchPaths WRITE setThemeSearchPaths NOTIFY themeSearchPathsChanged) + Q_OBJECT - public: - explicit DeclarativeIconAttached(QObject *parent = 0); + Q_PROPERTY(QString themeName READ themeName WRITE setThemeName NOTIFY themeNameChanged) + Q_PROPERTY(QStringList themeSearchPaths READ themeSearchPaths WRITE setThemeSearchPaths NOTIFY themeSearchPathsChanged) + +public: + explicit DeclarativeIconLoader(QObject *parent = Q_NULLPTR); QString themeName() const; void setThemeName(const QString &name); @@ -53,41 +56,11 @@ class DeclarativeIconAttached : public QObject Q_INVOKABLE QIcon fromTheme(const QString &name); Q_INVOKABLE QIcon fromFileName(const QString &fileName); - Q_SIGNALS: + static QObject *instance(QQmlEngine *engine, QJSEngine *scriptEngine); + +Q_SIGNALS: void themeNameChanged(const QString &themeName); void themeSearchPathsChanged(const QStringList &themeSearchPaths); }; -class DeclarativeIcon : public QObject -{ - Q_OBJECT - Q_PROPERTY(QIcon icon READ icon WRITE setIcon NOTIFY iconChanged) - Q_PROPERTY(QString name READ name NOTIFY nameChanged) - Q_PROPERTY(bool isNull READ isNull NOTIFY isNullChanged) - - public: - explicit DeclarativeIcon(QObject *parent = 0); - ~DeclarativeIcon(); - - QIcon icon() const; - void setIcon(const QIcon &icon); - - QString name() const; - - bool isNull() const; - - static DeclarativeIconAttached *qmlAttachedProperties(QObject *parent); - - Q_SIGNALS: - void iconChanged(const QIcon &icon); - void nameChanged(const QString &name); - void isNullChanged(bool isNull); - - private: - class Private; - Private *const d; -}; - -QML_DECLARE_TYPEINFO(DeclarativeIcon, QML_HAS_ATTACHED_PROPERTIES) - -#endif // DECLARATIVEICON_P_H +#endif // DECLARATIVEICONLOADER_P_H diff --git a/src/declarativepixmap.cpp b/src/declarativepixmap.cpp deleted file mode 100644 index 2df1ec2..0000000 --- a/src/declarativepixmap.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - declarativepixmap.cpp - - This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. - - Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com - Author: Kevin Krammer - - Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in - accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. - - Contact info@kdab.com if any conditions of this licensing are not clear to you. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "declarativepixmap_p.h" - -DeclarativePixmapAttached::DeclarativePixmapAttached(QObject *parent) - : QObject(parent) -{ -} - -QPixmap DeclarativePixmapAttached::fromFileName(const QString &fileName) -{ - return QPixmap(fileName); -} - -class DeclarativePixmap::Private -{ - public: - QPixmap pixmap; -}; - -DeclarativePixmap::DeclarativePixmap(QObject *parent) - : QObject(parent) - , d(new Private) -{ -} - -DeclarativePixmap::~DeclarativePixmap() -{ - delete d; -} - -QPixmap DeclarativePixmap::pixmap() const -{ - return d->pixmap; -} - -void DeclarativePixmap::setPixmap(const QPixmap &pixmap) -{ - if (pixmap.handle() == d->pixmap.handle()) - return; - - const int oldHeight = d->pixmap.height(); - const int oldWidth = d->pixmap.width(); - const bool oldIsNull = d->pixmap.isNull(); - - d->pixmap = pixmap; - emit pixmapChanged(pixmap); - - if (oldHeight != d->pixmap.height()) - emit heightChanged(d->pixmap.height()); - - if (oldWidth != d->pixmap.width()) - emit widthChanged(d->pixmap.width()); - - if (oldIsNull == d->pixmap.isNull()) - emit isNullChanged(d->pixmap.isNull()); -} - -int DeclarativePixmap::height() const -{ - return d->pixmap.height(); -} - -int DeclarativePixmap::width() const -{ - return d->pixmap.width(); -} - -bool DeclarativePixmap::isNull() const -{ - return d->pixmap.isNull(); -} - -DeclarativePixmapAttached *DeclarativePixmap::qmlAttachedProperties(QObject *parent) -{ - return new DeclarativePixmapAttached(parent); -} diff --git a/src/declarativepixmap_p.h b/src/declarativepixmap_p.h deleted file mode 100644 index 5769fdc..0000000 --- a/src/declarativepixmap_p.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - declarativepixmap_p.h - - This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. - - Copyright (C) 2013-2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com - Author: Kevin Krammer - - Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in - accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. - - Contact info@kdab.com if any conditions of this licensing are not clear to you. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef DECLARATIVEPIXMAP_P_H -#define DECLARATIVEPIXMAP_P_H - -#include - -#include -#include -#include -#include - -class DeclarativePixmapAttached : public QObject -{ - Q_OBJECT - - public: - explicit DeclarativePixmapAttached(QObject *parent = 0); - - Q_INVOKABLE QPixmap fromFileName(const QString &fileName); -}; - -class DeclarativePixmap : public QObject -{ - Q_OBJECT - Q_PROPERTY(QPixmap Pixmap READ pixmap WRITE setPixmap NOTIFY pixmapChanged) - Q_PROPERTY(int width READ width NOTIFY widthChanged) - Q_PROPERTY(int height READ height NOTIFY heightChanged) - Q_PROPERTY(bool isNull READ isNull NOTIFY isNullChanged) - - public: - explicit DeclarativePixmap(QObject *parent = 0); - ~DeclarativePixmap(); - - QPixmap pixmap() const; - void setPixmap(const QPixmap &pixmap); - - int height() const; - int width() const; - - bool isNull() const; - - static DeclarativePixmapAttached *qmlAttachedProperties(QObject *parent); - - Q_SIGNALS: - void pixmapChanged(const QPixmap &Pixmap); - void heightChanged(int height); - void widthChanged(int width); - void isNullChanged(bool isNull); - - private: - class Private; - Private *const d; -}; - -QML_DECLARE_TYPEINFO(DeclarativePixmap, QML_HAS_ATTACHED_PROPERTIES) - -#endif // DECLARATIVEPIXMAP_P_H diff --git a/src/declarativepixmaploader.cpp b/src/declarativepixmaploader.cpp new file mode 100644 index 0000000..0de525f --- /dev/null +++ b/src/declarativepixmaploader.cpp @@ -0,0 +1,47 @@ +/* + declarativepixmaploader.cpp + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Kevin Krammer + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "declarativepixmaploader_p.h" + +DeclarativePixmapLoader::DeclarativePixmapLoader(QObject *parent) + : QObject(parent) +{ +} + +QPixmap DeclarativePixmapLoader::fromFileName(const QString &fileName) const +{ + return QPixmap(fileName); +} + +QObject *DeclarativePixmapLoader::instance(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + + return new DeclarativePixmapLoader(); +} diff --git a/src/declarativepixmaploader_p.h b/src/declarativepixmaploader_p.h new file mode 100644 index 0000000..7ef4619 --- /dev/null +++ b/src/declarativepixmaploader_p.h @@ -0,0 +1,52 @@ +/* + declarativepixmaploader_p.h + + This file is part of DeclarativeWidgets, library and tools for creating QtWidget UIs with QML. + + Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Kevin Krammer + Author: Lova Widmark + + Licensees holding valid commercial KDAB DeclarativeWidgets licenses may use this file in + accordance with DeclarativeWidgets Commercial License Agreement provided with the Software. + + Contact info@kdab.com if any conditions of this licensing are not clear to you. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef DECLARATIVEPIXMAPLOADER_P_H +#define DECLARATIVEPIXMAPLOADER_P_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QQmlEngine; +class QJSEngine; +QT_END_NAMESPACE + +class DeclarativePixmapLoader : public QObject +{ + Q_OBJECT + +public: + explicit DeclarativePixmapLoader(QObject *parent = Q_NULLPTR); + + Q_INVOKABLE QPixmap fromFileName(const QString &fileName) const; + + static QObject *instance(QQmlEngine *engine, QJSEngine *scriptEngine); +}; + +#endif // DECLARATIVEPIXMAPLOADER_P_H diff --git a/src/declarativewidgets_plugin.cpp b/src/declarativewidgets_plugin.cpp index 5c0267c..d458d7d 100644 --- a/src/declarativewidgets_plugin.cpp +++ b/src/declarativewidgets_plugin.cpp @@ -40,7 +40,7 @@ #include "declarativeformlayout_p.h" #include "declarativegridlayout_p.h" #include "declarativehboxlayout_p.h" -#include "declarativeicon_p.h" +#include "declarativeiconloader_p.h" #include "declarativeinputdialog_p.h" #include "declarativeitemviewextension_p.h" #include "declarativelabelextension_p.h" @@ -48,7 +48,7 @@ #include "declarativeline_p.h" #include "declarativeloaderwidget_p.h" #include "declarativemessagebox_p.h" -#include "declarativepixmap_p.h" +#include "declarativepixmaploader_p.h" #include "declarativeqmlcontext_p.h" #include "declarativequickwidgetextension_p.h" #include "declarativeseparator_p.h" @@ -142,10 +142,12 @@ void ExtensionpluginPlugin::registerTypes(const char *uri) qmlRegisterType(uri, 1, 0, "QmlContextProperty"); qmlRegisterType(uri, 1, 0, "QmlContext"); qmlRegisterExtendedType(uri, 1, 0, "FileSystemModel"); - qmlRegisterType(uri, 1, 0, "Icon"); qmlRegisterExtendedType(uri, 1, 0, "Separator"); qmlRegisterType("QtWidgets", 1, 0, "TabStops"); - qmlRegisterType(uri, 1, 0, "Pixmap"); + + // singletons + qmlRegisterSingletonType(uri, 1, 0, "Icon", &DeclarativeIconLoader::instance); + qmlRegisterSingletonType(uri, 1, 0, "Pixmap", &DeclarativePixmapLoader::instance); // layouts qmlRegisterExtendedType(uri, 1, 0, "FormLayout"); diff --git a/src/src.pro b/src/src.pro index 6d5919b..1ce9fdb 100644 --- a/src/src.pro +++ b/src/src.pro @@ -47,7 +47,6 @@ HEADERS = \ declarativeformlayout_p.h \ declarativegridlayout_p.h \ declarativehboxlayout_p.h \ - declarativeicon_p.h \ declarativeinputdialog_p.h \ declarativeitemviewextension_p.h \ declarativelabelextension_p.h \ @@ -57,7 +56,6 @@ HEADERS = \ declarativemessagebox_p.h \ declarativeobjectextension.h \ declarativeobjectproxy_p.h \ - declarativepixmap_p.h \ declarativeqmlcontext_p.h \ declarativequickwidgetextension_p.h \ declarativeseparator_p.h \ @@ -86,7 +84,9 @@ HEADERS = \ staticdialogmethodattached_p.h \ toolbarwidgetcontainer_p.h \ widgetcontainerinterface_p.h \ - declarativesizepolicy_p.h + declarativesizepolicy_p.h \ + declarativepixmaploader_p.h \ + declarativeiconloader_p.h SOURCES = \ abstractdeclarativeobject.cpp \ @@ -102,7 +102,6 @@ SOURCES = \ declarativeformlayout.cpp \ declarativegridlayout.cpp \ declarativehboxlayout.cpp \ - declarativeicon.cpp \ declarativeinputdialog.cpp \ declarativeitemviewextension.cpp \ declarativelabelextension.cpp \ @@ -111,7 +110,6 @@ SOURCES = \ declarativeloaderwidget.cpp \ declarativemessagebox.cpp \ declarativeobjectextension.cpp \ - declarativepixmap.cpp \ declarativeqmlcontext.cpp \ declarativequickwidgetextension.cpp \ declarativeseparator.cpp \ @@ -137,4 +135,6 @@ SOURCES = \ stackedwidgetwidgetcontainer.cpp \ staticdialogmethodattached.cpp \ toolbarwidgetcontainer.cpp \ - declarativesizepolicy.cpp + declarativesizepolicy.cpp \ + declarativepixmaploader.cpp \ + declarativeiconloader.cpp