diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-05-09 16:44:08 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-05-26 07:21:00 +0000 |
commit | bb49cde2decbee22f0d732d6e2f56a14c2970b24 (patch) | |
tree | c03bf34e153fc09046554ee1f35f13624a3e860b /src/imports/templates | |
parent | 828000334a6cf289689e3ce953e8f82a8f662cc9 (diff) |
Add QQuickPalette value type
Change-Id: Id4dfb4939d3ea720fe46a4a5171d3e068c8f1fd3
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/templates')
-rw-r--r-- | src/imports/templates/qquicktemplates2valuetypeprovider.cpp | 159 | ||||
-rw-r--r-- | src/imports/templates/qquicktemplates2valuetypeprovider_p.h | 68 | ||||
-rw-r--r-- | src/imports/templates/qtquicktemplates2plugin.cpp | 22 | ||||
-rw-r--r-- | src/imports/templates/templates.pro | 4 |
4 files changed, 253 insertions, 0 deletions
diff --git a/src/imports/templates/qquicktemplates2valuetypeprovider.cpp b/src/imports/templates/qquicktemplates2valuetypeprovider.cpp new file mode 100644 index 00000000..6debdbc4 --- /dev/null +++ b/src/imports/templates/qquicktemplates2valuetypeprovider.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquicktemplates2valuetypeprovider_p.h" + +#include <QtQml/private/qqmlvaluetype_p.h> +#include <QtQuickTemplates2/private/qquickpalette_p.h> + +QT_BEGIN_NAMESPACE + +#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) + #define ASSERT_VALID_SIZE(size, min) Q_UNUSED(size) +#else + #define ASSERT_VALID_SIZE(size, min) Q_ASSERT(size >= min) +#endif + +const QMetaObject *QQuickTemplates2ValueTypeProvider::getMetaObjectForMetaType(int type) +{ + switch (type) { + case QMetaType::QPalette: + return &QQuickPalette::staticMetaObject; + default: + break; + } + + return nullptr; +} + +bool QQuickTemplates2ValueTypeProvider::init(int type, QVariant& dst) +{ + switch (type) { + case QMetaType::QPalette: + dst.setValue<QPalette>(QPalette()); + return true; + default: break; + } + + return false; +} + +template<typename T> +bool typedEqual(const void *lhs, const QVariant& rhs) +{ + return (*(reinterpret_cast<const T *>(lhs)) == rhs.value<T>()); +} + +bool QQuickTemplates2ValueTypeProvider::equal(int type, const void *lhs, const QVariant &rhs) +{ + switch (type) { + case QMetaType::QPalette: + return typedEqual<QPalette>(lhs, rhs); + default: break; + } + + return false; +} + +template<typename T> +bool typedStore(const void *src, void *dst, size_t dstSize) +{ + ASSERT_VALID_SIZE(dstSize, sizeof(T)); + const T *srcT = reinterpret_cast<const T *>(src); + T *dstT = reinterpret_cast<T *>(dst); + new (dstT) T(*srcT); + return true; +} + +bool QQuickTemplates2ValueTypeProvider::store(int type, const void *src, void *dst, size_t dstSize) +{ + switch (type) { + case QMetaType::QPalette: + return typedStore<QPalette>(src, dst, dstSize); + default: break; + } + + return false; +} + +template<typename T> +bool typedRead(const QVariant& src, int dstType, void *dst) +{ + T *dstT = reinterpret_cast<T *>(dst); + if (src.type() == static_cast<uint>(dstType)) { + *dstT = src.value<T>(); + } else { + *dstT = T(); + } + return true; +} + +bool QQuickTemplates2ValueTypeProvider::read(const QVariant &src, void *dst, int dstType) +{ + switch (dstType) { + case QMetaType::QPalette: + return typedRead<QPalette>(src, dstType, dst); + default: break; + } + + return false; +} + +template<typename T> +bool typedWrite(const void *src, QVariant& dst) +{ + const T *srcT = reinterpret_cast<const T *>(src); + if (dst.value<T>() != *srcT) { + dst = *srcT; + return true; + } + return false; +} + +bool QQuickTemplates2ValueTypeProvider::write(int type, const void *src, QVariant& dst) +{ + switch (type) { + case QMetaType::QPalette: + return typedWrite<QPalette>(src, dst); + default: break; + } + + return false; +} + +#undef ASSERT_VALID_SIZE + +QT_END_NAMESPACE diff --git a/src/imports/templates/qquicktemplates2valuetypeprovider_p.h b/src/imports/templates/qquicktemplates2valuetypeprovider_p.h new file mode 100644 index 00000000..6b072029 --- /dev/null +++ b/src/imports/templates/qquicktemplates2valuetypeprovider_p.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKTEMPLATES2VALUETYPEPROVIDER_P_H +#define QQUICKTEMPLATES2VALUETYPEPROVIDER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQml/private/qqmlglobal_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickTemplates2ValueTypeProvider : public QQmlValueTypeProvider +{ +public: + const QMetaObject *getMetaObjectForMetaType(int type) override; + bool init(int type, QVariant& dst) override; + bool equal(int type, const void *lhs, const QVariant &rhs) override; + bool store(int type, const void *src, void *dst, size_t dstSize) override; + bool read(const QVariant &src, void *dst, int dstType) override; + bool write(int type, const void *src, QVariant& dst) override; +}; + +QT_END_NAMESPACE + +#endif // QQUICKTEMPLATES2VALUETYPEPROVIDER_P_H diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp index eea54894..e9ff17c8 100644 --- a/src/imports/templates/qtquicktemplates2plugin.cpp +++ b/src/imports/templates/qtquicktemplates2plugin.cpp @@ -35,6 +35,7 @@ ****************************************************************************/ #include <QtQml/qqmlextensionplugin.h> +#include <QtQml/private/qqmlglobal_p.h> #include <QtQuickTemplates2/private/qquickabstractbutton_p.h> #include <QtQuickTemplates2/private/qquickaction_p.h> @@ -95,6 +96,8 @@ #include <QtQuickTemplates2/private/qquicktumbler_p.h> #endif +#include "qquicktemplates2valuetypeprovider_p.h" + static inline void initResources() { #ifdef QT_STATIC @@ -111,6 +114,22 @@ extern void qt_quick_set_shortcut_context_matcher(ShortcutContextMatcher matcher QT_BEGIN_NAMESPACE +static QQmlValueTypeProvider *valueTypeProvider() +{ + static QQuickTemplates2ValueTypeProvider provider; + return &provider; +} + +static void initProviders() +{ + QQml_addValueTypeProvider(valueTypeProvider()); +} + +static void cleanupProviders() +{ + QQml_removeValueTypeProvider(valueTypeProvider()); +} + class QtQuickTemplates2Plugin: public QQmlExtensionPlugin { Q_OBJECT @@ -131,6 +150,7 @@ private: QtQuickTemplates2Plugin::QtQuickTemplates2Plugin(QObject *parent) : QQmlExtensionPlugin(parent) { initResources(); + initProviders(); #if QT_CONFIG(shortcut) originalContextMatcher = qt_quick_shortcut_context_matcher(); @@ -140,6 +160,8 @@ QtQuickTemplates2Plugin::QtQuickTemplates2Plugin(QObject *parent) : QQmlExtensio QtQuickTemplates2Plugin::~QtQuickTemplates2Plugin() { + cleanupProviders(); + #if QT_CONFIG(shortcut) qt_quick_set_shortcut_context_matcher(originalContextMatcher); #endif diff --git a/src/imports/templates/templates.pro b/src/imports/templates/templates.pro index 9a95812b..4ff0a76e 100644 --- a/src/imports/templates/templates.pro +++ b/src/imports/templates/templates.pro @@ -10,7 +10,11 @@ DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII OTHER_FILES += \ qmldir +HEADERS += \ + $$PWD/qquicktemplates2valuetypeprovider_p.h + SOURCES += \ + $$PWD/qquicktemplates2valuetypeprovider.cpp \ $$PWD/qtquicktemplates2plugin.cpp CONFIG += no_cxx_module |