diff options
author | J-P Nurmi <jpnurmi@digia.com> | 2013-06-17 18:26:38 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-14 00:31:50 +0200 |
commit | 74a66d452488c249db025c7400432ec993482c59 (patch) | |
tree | c5e3fddd384db0606e81c405765f8507aa4dd2c7 | |
parent | fb74539d7d14c2444950474ad3f45a80443bfc73 (diff) |
Say hello to Qt.labs.settings
Change-Id: Id4970555b2cbbc2df893dd6269fb8b884ce06e45
Reviewed-by: Alan Alpert (Personal) <416365416c@gmail.com>
-rw-r--r-- | src/imports/imports.pro | 3 | ||||
-rw-r--r-- | src/imports/settings/plugin.cpp | 64 | ||||
-rw-r--r-- | src/imports/settings/plugins.qmltypes | 16 | ||||
-rw-r--r-- | src/imports/settings/qmldir | 3 | ||||
-rw-r--r-- | src/imports/settings/qqmlsettings.cpp | 415 | ||||
-rw-r--r-- | src/imports/settings/qqmlsettings_p.h | 84 | ||||
-rw-r--r-- | src/imports/settings/settings.pro | 15 | ||||
-rw-r--r-- | tests/auto/qml/qml.pro | 3 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/aliases.qml | 85 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/basic.qml | 45 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/categories.qml | 46 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/cpp-aliases.qml | 67 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/siblings.qml | 61 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/data/types.qml | 111 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/qqmlsettings.pro | 12 | ||||
-rw-r--r-- | tests/auto/qml/qqmlsettings/tst_qqmlsettings.cpp | 584 |
16 files changed, 1612 insertions, 2 deletions
diff --git a/src/imports/imports.pro b/src/imports/imports.pro index 7a922a832e..5bc5d61eb7 100644 --- a/src/imports/imports.pro +++ b/src/imports/imports.pro @@ -3,7 +3,8 @@ TEMPLATE = subdirs SUBDIRS += \ folderlistmodel \ localstorage \ - models + models \ + settings qtHaveModule(quick) { SUBDIRS += \ diff --git a/src/imports/settings/plugin.cpp b/src/imports/settings/plugin.cpp new file mode 100644 index 0000000000..ce955a9e36 --- /dev/null +++ b/src/imports/settings/plugin.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtQml/qqmlextensionplugin.h> +#include <QtQml/qqml.h> + +#include "qqmlsettings_p.h" + +QT_BEGIN_NAMESPACE + +class QmlSettingsPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + virtual void registerTypes(const char *uri) + { + Q_ASSERT(QByteArray(uri) == QByteArray("Qt.labs.settings")); + qmlRegisterType<QQmlSettings>(uri, 1, 0, "Settings"); + } +}; + +QT_END_NAMESPACE + +#include "plugin.moc" diff --git a/src/imports/settings/plugins.qmltypes b/src/imports/settings/plugins.qmltypes new file mode 100644 index 0000000000..290c4c5570 --- /dev/null +++ b/src/imports/settings/plugins.qmltypes @@ -0,0 +1,16 @@ +import QtQuick.tooling 1.1 + +// This file describes the plugin-supplied types contained in the library. +// It is used for QML tooling purposes only. +// +// This file was auto-generated with the command 'qmlplugindump -notrelocatable Qt.labs.settings 1.0'. + +Module { + Component { + name: "QQmlSettings" + prototype: "QObject" + exports: ["Qt.labs.settings/Settings 1.0"] + exportMetaObjectRevisions: [0] + Property { name: "category"; type: "string" } + } +} diff --git a/src/imports/settings/qmldir b/src/imports/settings/qmldir new file mode 100644 index 0000000000..0d68a0742c --- /dev/null +++ b/src/imports/settings/qmldir @@ -0,0 +1,3 @@ +module Qt.labs.settings +plugin qmlsettingsplugin +typeinfo plugins.qmltypes diff --git a/src/imports/settings/qqmlsettings.cpp b/src/imports/settings/qqmlsettings.cpp new file mode 100644 index 0000000000..f73ab08cf3 --- /dev/null +++ b/src/imports/settings/qqmlsettings.cpp @@ -0,0 +1,415 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qqmlsettings_p.h" +#include <qcoreevent.h> +#include <qsettings.h> +#include <qpointer.h> +#include <qdebug.h> +#include <qhash.h> + +QT_BEGIN_NAMESPACE + +/*! + \qmlmodule Qt.labs.settings 1.0 + \title Qt Labs Settings QML Types + \ingroup qmlmodules + \brief Provides persistent platform-independent application settings. + + To use this module, import the module with the following line: + + \code + import Qt.labs.settings 1.0 + \endcode +*/ + +/*! + \qmltype Settings + \instantiates QQmlSettings + \inqmlmodule Qt.labs.settings 1.0 + \ingroup settings + \brief Provides persistent platform-independent application settings. + + The Settings type provides persistent platform-independent application settings. + + \note This type is made available by importing the \b Qt.labs.settings module. + \e {Types in the Qt.labs module are not guaranteed to remain compatible + in future versions.} + + Users normally expect an application to remember its settings (window sizes + and positions, options, etc.) across sessions. The Settings type enables you + to save and restore such application settings with the minimum of effort. + + Individual setting values are specified by declaring properties within a + Settings element. All \l {QML Basic Types}{basic type} properties are + supported. The recommended approach is to use property aliases in order + to get automatic property updates both ways. The following example shows + how to use Settings to store and restore the geometry of a window. + + \qml + import QtQuick.Window 2.1 + import Qt.labs.settings 1.0 + + Window { + id: window + + width: 800 + height: 600 + + Settings { + property alias x: window.x + property alias y: window.y + property alias width: window.width + property alias height: window.height + } + } + \endqml + + At first application startup, the window gets default dimensions specified + as 800x600. Notice that no default position is specified - we let the window + manager handle that. Later when the window geometry changes, new values will + be automatically stored to the persistent settings. The second application + run will get initial values from the persistent settings, bringing the window + back to the previous position and size. + + A fully declarative syntax, achieved by using property aliases, comes at the + cost of storing persistent settings whenever the values of aliased properties + change. Normal properties can be used to gain more fine-grained control over + storing the persistent settings. The following example illustrates how to save + a setting on component destruction. + + \qml + import QtQuick 2.1 + import Qt.labs.settings 1.0 + + Item { + id: page + + state: settings.state + + states: [ + State { + name: "active" + // ... + }, + State { + name: "inactive" + // ... + } + ] + + Settings { + id: settings + property string state: "active" + } + + Component.onDestruction: { + settings.state = page.state + } + } + \endqml + + Notice how the default value is now specified in the persistent setting property, + and the actual property is bound to the setting in order to get the initial value + from the persistent settings. + + \section1 Application Identifiers + + Application specific settings are identified by providing application + \l {QCoreApplication::applicationName}{name}, + \l {QCoreApplication::organizationName}{organization} and + \l {QCoreApplication::organizationDomain}{domain}. + + \code + #include <QGuiApplication> + #include <QQmlApplicationEngine> + + int main(int argc, char *argv[]) + { + QGuiApplication app(argc, argv); + app.setOrganizationName("Some Company"); + app.setOrganizationDomain("somecompany.com"); + app.setApplicationName("Amazing Application"); + + QQmlApplicationEngine engine("main.qml"); + return app.exec(); + } + \endcode + + These are typically specified in C++ in the beginning of \c main(), + but can also be controlled in QML via the following properties: + \list + \li \l {Qt::application}{Qt.application.name}, + \li \l {Qt::application}{Qt.application.organization} and + \li \l {Qt::application}{Qt.application.domain}. + \endlist + + \section1 Categories + + Application settings may be divided into logical categories by specifying + a category name via the \l category property. Using logical categories not + only provides a cleaner settings structure, but also prevents possible + conflicts between setting keys. + + \qml + Item { + id: panel + + visible: true + + Settings { + category: "OutputPanel" + property alias visible: panel.visible + // ... + } + } + \endqml + + Instead of ensuring that all settings in the application have unique names, + the settings can be divided into unique categories that may then contain + settings using the same names that are used in other categories - without + a conflict. + + \section1 Notes + + The current implementation is based on \l QSettings. This imposes certain + limitations, such as missing change notifications. Writing a setting value + using one instance of Settings does not update the value in another Settings + instance, even if they are referring to the same setting in the same category. + + The information is stored in the system registry on Windows, and in XML + preferences files on OS X. On other Unix systems, in the absence of a + standard, INI text files are used. See \l QSettings documentation for + more details. + + \sa QSettings +*/ + +// #define SETTINGS_DEBUG + +static const int settingsWriteDelay = 500; + +class QQmlSettingsPrivate +{ + Q_DECLARE_PUBLIC(QQmlSettings) + +public: + QQmlSettingsPrivate(); + + QSettings *instance() const; + + void init(); + void reset(); + + void load(); + void store(); + + void _q_propertyChanged(); + + QQmlSettings *q_ptr; + int timerId; + bool initialized; + QString category; + mutable QPointer<QSettings> settings; + QHash<const char *, QVariant> changedProperties; +}; + +QQmlSettingsPrivate::QQmlSettingsPrivate() + : q_ptr(0), timerId(0), initialized(false) +{ +} + +QSettings *QQmlSettingsPrivate::instance() const +{ + if (!settings) { + QQmlSettings *q = const_cast<QQmlSettings*>(q_func()); + settings = new QSettings(q); + if (!category.isEmpty()) + settings->beginGroup(category); + if (initialized) + q->d_func()->load(); + } + return settings; +} + +void QQmlSettingsPrivate::init() +{ + if (!initialized) { + load(); + initialized = true; + } +} + +void QQmlSettingsPrivate::reset() +{ + if (initialized && settings && !changedProperties.isEmpty()) + store(); + delete settings; +} + +void QQmlSettingsPrivate::load() +{ + Q_Q(QQmlSettings); + const QMetaObject *mo = q->metaObject(); + const int offset = mo->propertyOffset(); + const int count = mo->propertyCount(); + for (int i = offset; i < count; ++i) { + QMetaProperty property = mo->property(i); + + const QVariant previousValue = property.read(q); + const QVariant currentValue = instance()->value(property.name(), previousValue); + + if (!currentValue.isNull() + && currentValue.canConvert(previousValue.type()) + && previousValue != currentValue) { + property.write(q, currentValue); +#ifdef SETTINGS_DEBUG + qDebug() << "QQmlSettings: load" << property.name() << "setting:" << currentValue << "default:" << previousValue; +#endif + } + + // ensure that a non-existent setting gets written + // even if the property wouldn't change later + if (!instance()->contains(property.name())) + _q_propertyChanged(); + + // setup change notifications on first load + if (!initialized && property.hasNotifySignal()) { + static const int propertyChangedIndex = mo->indexOfSlot("_q_propertyChanged()"); + QMetaObject::connect(q, property.notifySignalIndex(), q, propertyChangedIndex); + } + } +} + +void QQmlSettingsPrivate::store() +{ + QHash<const char *, QVariant>::iterator it = changedProperties.begin(); + while (it != changedProperties.end()) { + instance()->setValue(it.key(), it.value()); +#ifdef SETTINGS_DEBUG + qDebug() << "QQmlSettings: store" << it.key() << ":" << it.value(); +#endif + it = changedProperties.erase(it); + } +} + +void QQmlSettingsPrivate::_q_propertyChanged() +{ + Q_Q(QQmlSettings); + const QMetaObject *mo = q->metaObject(); + const int offset = mo->propertyOffset(); + const int count = mo->propertyCount(); + for (int i = offset; i < count; ++i) { + const QMetaProperty &property = mo->property(i); + changedProperties.insert(property.name(), property.read(q)); + #ifdef SETTINGS_DEBUG + qDebug() << "QQmlSettings: cache" << property.name() << ":" << property.read(q); + #endif + } + if (timerId != 0) + q->killTimer(timerId); + timerId = q->startTimer(settingsWriteDelay); +} + +QQmlSettings::QQmlSettings(QObject *parent) + : QObject(parent), d_ptr(new QQmlSettingsPrivate) +{ + Q_D(QQmlSettings); + d->q_ptr = this; +} + +QQmlSettings::~QQmlSettings() +{ + Q_D(QQmlSettings); + d->reset(); // flush pending changes +} + +/*! + \qmlproperty string Settings::category + + This property holds the name of the settings category. + + Categories can be used to group related settings together. +*/ +QString QQmlSettings::category() const +{ + Q_D(const QQmlSettings); + return d->category; +} + +void QQmlSettings::setCategory(const QString &category) +{ + Q_D(QQmlSettings); + if (d->category != category) { + d->reset(); + d->category = category; + if (d->initialized) + d->load(); + } +} + +void QQmlSettings::classBegin() +{ +} + +void QQmlSettings::componentComplete() +{ + Q_D(QQmlSettings); + d->init(); +} + +void QQmlSettings::timerEvent(QTimerEvent *event) +{ + Q_D(QQmlSettings); + if (event->timerId() == d->timerId) { + if (d->changedProperties.isEmpty()) { + killTimer(d->timerId); + d->timerId = 0; + } else { + d->store(); + } + } + QObject::timerEvent(event); +} + +QT_END_NAMESPACE + +#include "moc_qqmlsettings_p.cpp" diff --git a/src/imports/settings/qqmlsettings_p.h b/src/imports/settings/qqmlsettings_p.h new file mode 100644 index 0000000000..b5e25469dc --- /dev/null +++ b/src/imports/settings/qqmlsettings_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQMLSETTINGS_P_H +#define QQMLSETTINGS_P_H + +#include <QtQml/qqml.h> +#include <QtCore/qobject.h> +#include <QtCore/qscopedpointer.h> +#include <QtQml/qqmlparserstatus.h> + +QT_BEGIN_NAMESPACE + +class QQmlSettingsPrivate; + +class QQmlSettings : public QObject, public QQmlParserStatus +{ + Q_OBJECT + Q_INTERFACES(QQmlParserStatus) + Q_PROPERTY(QString category READ category WRITE setCategory FINAL) + +public: + explicit QQmlSettings(QObject *parent = 0); + ~QQmlSettings(); + + QString category() const; + void setCategory(const QString &category); + +protected: + void timerEvent(QTimerEvent *event); + + void classBegin(); + void componentComplete(); + +private: + Q_DISABLE_COPY(QQmlSettings) + Q_DECLARE_PRIVATE(QQmlSettings) + QScopedPointer<QQmlSettingsPrivate> d_ptr; + Q_PRIVATE_SLOT(d_func(), void _q_propertyChanged()) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQmlSettings) + +#endif // QQMLSETTINGS_P_H diff --git a/src/imports/settings/settings.pro b/src/imports/settings/settings.pro new file mode 100644 index 0000000000..29229f59cb --- /dev/null +++ b/src/imports/settings/settings.pro @@ -0,0 +1,15 @@ +CXX_MODULE = qml +TARGET = qmlsettingsplugin +TARGETPATH = Qt/labs/settings +IMPORT_VERSION = 1.0 + +QT = core qml + +HEADERS += \ + qqmlsettings_p.h + +SOURCES += \ + plugin.cpp \ + qqmlsettings.cpp + +load(qml_plugin) diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro index 18065f1a24..58132786a0 100644 --- a/tests/auto/qml/qml.pro +++ b/tests/auto/qml/qml.pro @@ -25,7 +25,8 @@ PUBLICTESTS += \ qqmlxmlhttprequest \ qtqmlmodules \ qquickfolderlistmodel \ - qqmlapplicationengine + qqmlapplicationengine \ + qqmlsettings PRIVATETESTS += \ animation \ diff --git a/tests/auto/qml/qqmlsettings/data/aliases.qml b/tests/auto/qml/qqmlsettings/data/aliases.qml new file mode 100644 index 0000000000..ac746a16a2 --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/aliases.qml @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQml 2.1 +import QtQuick 2.2 +import Qt.labs.settings 1.0 + +QtObject { + id: root + + property int intProperty: 123 + property bool boolProperty: true + property real realProperty: 1.23 + property double doubleProperty: 3.45 + property string stringProperty: "foo" + property url urlProperty: "http://www.qt-project.org" + property var intListProperty: [1, 2, 3] + property var stringListProperty: ["a", "b", "c"] + property date dateProperty: "2000-01-02" + // QTBUG-32295: Expected property type + //property time timeProperty: "12:34:56" + property size sizeProperty: Qt.size(12, 34) + property point pointProperty: Qt.point(12, 34) + property rect rectProperty: Qt.rect(1, 2, 3, 4) + property color colorProperty: "red" + property font fontProperty + + property Settings settings: Settings { + id: settings + + property alias intProperty: root.intProperty + property alias boolProperty: root.boolProperty + property alias realProperty: root.realProperty + property alias doubleProperty: root.doubleProperty + property alias stringProperty: root.stringProperty + property alias urlProperty: root.urlProperty + property alias intListProperty: root.intListProperty + property alias stringListProperty: root.stringListProperty + property alias dateProperty: root.dateProperty + // QTBUG-32295: Expected property type + //property alias timeProperty: root.timeProperty + property alias sizeProperty: root.sizeProperty + property alias pointProperty: root.pointProperty + property alias rectProperty: root.rectProperty + property alias colorProperty: root.colorProperty + property alias fontProperty: root.fontProperty + } +} diff --git a/tests/auto/qml/qqmlsettings/data/basic.qml b/tests/auto/qml/qqmlsettings/data/basic.qml new file mode 100644 index 0000000000..1ac377036e --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/basic.qml @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import Qt.labs.settings 1.0 + +Settings { + property bool success: true +} diff --git a/tests/auto/qml/qqmlsettings/data/categories.qml b/tests/auto/qml/qqmlsettings/data/categories.qml new file mode 100644 index 0000000000..77110c0b05 --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/categories.qml @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import Qt.labs.settings 1.0 + +Settings { + category: "initialCategory" + property string value: "initialValue" +} diff --git a/tests/auto/qml/qqmlsettings/data/cpp-aliases.qml b/tests/auto/qml/qqmlsettings/data/cpp-aliases.qml new file mode 100644 index 0000000000..41c2f58da7 --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/cpp-aliases.qml @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQml 2.1 +import QtQuick 2.2 +import Qt.labs.settings 1.0 +import Qt.test 1.0 + +CppObject { + id: obj + + property Settings settings: Settings { + property alias intProperty: obj.intProperty + property alias boolProperty: obj.boolProperty + property alias realProperty: obj.realProperty + property alias doubleProperty: obj.doubleProperty + property alias stringProperty: obj.stringProperty + property alias urlProperty: obj.urlProperty + property alias intListProperty: obj.intListProperty + property alias stringListProperty: obj.stringListProperty + property alias dateProperty: obj.dateProperty + // QTBUG-32295: Expected property type + //property alias timeProperty: obj.timeProperty + property alias sizeProperty: obj.sizeProperty + property alias pointProperty: obj.pointProperty + property alias rectProperty: obj.rectProperty + property alias colorProperty: obj.colorProperty + property alias fontProperty: obj.fontProperty + } +} diff --git a/tests/auto/qml/qqmlsettings/data/siblings.qml b/tests/auto/qml/qqmlsettings/data/siblings.qml new file mode 100644 index 0000000000..4e388b4747 --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/siblings.qml @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQuick 2.2 +import Qt.labs.settings 1.0 + +Item { + id: root + + Item { + id: sibling1 + property string prop1: "value1" + } + + Settings { + property alias alias1: sibling1.prop1 + property alias alias2: sibling2.prop2 + } + + Item { + id: sibling2 + property string prop2: "value2" + } +} diff --git a/tests/auto/qml/qqmlsettings/data/types.qml b/tests/auto/qml/qqmlsettings/data/types.qml new file mode 100644 index 0000000000..7adcc5fc1d --- /dev/null +++ b/tests/auto/qml/qqmlsettings/data/types.qml @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQml 2.1 +import QtQuick 2.2 +import Qt.labs.settings 1.0 + +QtObject { + id: root + + property int intProperty + property bool boolProperty + property real realProperty + property double doubleProperty + property string stringProperty + property url urlProperty + property var intListProperty + property var stringListProperty + property date dateProperty + // QTBUG-32295: Expected property type + // property time timeProperty + property size sizeProperty + property point pointProperty + property rect rectProperty + property color colorProperty + property font fontProperty + + function readSettings() { + __setProperties(settings, root) + } + + function writeSettings() { + __setProperties(root, settings) + } + + function __setProperties(from, to) { + to.intProperty = from.intProperty + to.boolProperty = from.boolProperty + to.realProperty = from.realProperty + to.doubleProperty = from.doubleProperty + to.stringProperty = from.stringProperty + to.urlProperty = from.urlProperty + to.intListProperty = from.intListProperty + to.stringListProperty = from.stringListProperty + to.dateProperty = from.dateProperty + //to.timeProperty = from.timeProperty + to.sizeProperty = from.sizeProperty + to.pointProperty = from.pointProperty + to.rectProperty = from.rectProperty + to.colorProperty = from.colorProperty + to.fontProperty = from.fontProperty + } + + property Settings settings: Settings { + id: settings + + property int intProperty: 123 + property bool boolProperty: true + property real realProperty: 1.23 + property double doubleProperty: 3.45 + property string stringProperty: "foo" + property url urlProperty: "http://www.qt-project.org" + property var intListProperty: [1, 2, 3] + property var stringListProperty: ["a", "b", "c"] + property date dateProperty: "2000-01-02" + // QTBUG-32295: Expected property type + //property time timeProperty: "12:34:56" + property size sizeProperty: Qt.size(12, 34) + property point pointProperty: Qt.point(12, 34) + property rect rectProperty: Qt.rect(1, 2, 3, 4) + property color colorProperty: "red" + property font fontProperty + } +} diff --git a/tests/auto/qml/qqmlsettings/qqmlsettings.pro b/tests/auto/qml/qqmlsettings/qqmlsettings.pro new file mode 100644 index 0000000000..efcef27f7e --- /dev/null +++ b/tests/auto/qml/qqmlsettings/qqmlsettings.pro @@ -0,0 +1,12 @@ +CONFIG += testcase +TARGET = tst_qqmlsettings +macx:CONFIG -= app_bundle + +SOURCES += tst_qqmlsettings.cpp + +include (../../shared/util.pri) + +TESTDATA = data/* + +QT += qml testlib +CONFIG += parallel_test diff --git a/tests/auto/qml/qqmlsettings/tst_qqmlsettings.cpp b/tests/auto/qml/qqmlsettings/tst_qqmlsettings.cpp new file mode 100644 index 0000000000..897450823c --- /dev/null +++ b/tests/auto/qml/qqmlsettings/tst_qqmlsettings.cpp @@ -0,0 +1,584 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtTest/QtTest> +#include <QtCore/QCoreApplication> +#include <QtCore/QScopedPointer> +#include <QtCore/QSettings> +#include <QtCore/QVariant> +#include <QtGui/QColor> +#include <QtGui/QFont> +#include <QtQml/QQmlEngine> +#include <QtQml/QQmlComponent> +#include "../../shared/util.h" + +class tst_QQmlSettings : public QQmlDataTest +{ + Q_OBJECT + +private slots: + void initTestCase(); + + void init(); + void cleanup(); + + void basic(); + void types(); + void aliases_data(); + void aliases(); + void categories(); + void siblings(); +}; + +class CppObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged) + Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty NOTIFY boolPropertyChanged) + Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty NOTIFY realPropertyChanged) + Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty NOTIFY doublePropertyChanged) + Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringPropertyChanged) + Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlPropertyChanged) + Q_PROPERTY(QVariant varProperty READ varProperty WRITE setVarProperty NOTIFY varPropertyChanged) + Q_PROPERTY(QVariantList intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged) + Q_PROPERTY(QVariantList stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged) + Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty NOTIFY datePropertyChanged) + // QTBUG-32295: Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty NOTIFY timePropertyChanged) + Q_PROPERTY(QSizeF sizeProperty READ sizeProperty WRITE setSizeProperty NOTIFY sizePropertyChanged) + Q_PROPERTY(QPointF pointProperty READ pointProperty WRITE setPointProperty NOTIFY pointPropertyChanged) + Q_PROPERTY(QRectF rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged) + Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty NOTIFY colorPropertyChanged) + Q_PROPERTY(QFont fontProperty READ fontProperty WRITE setFontProperty NOTIFY fontPropertyChanged) + +public: + CppObject(QObject *parent = 0) : QObject(parent), + m_intProperty(123), + m_boolProperty(true), + m_realProperty(1.23), + m_doubleProperty(3.45), + m_stringProperty("foo"), + m_urlProperty("http://www.qt-project.org"), + m_intListProperty(QVariantList() << 1 << 2 << 3), + m_stringListProperty(QVariantList() << "a" << "b" << "c"), + m_dateProperty(2000, 1, 2), + // QTBUG-32295: m_timeProperty(12, 34, 56), + m_sizeProperty(12, 34), + m_pointProperty(12, 34), + m_rectProperty(1, 2, 3, 4), + m_colorProperty("red") + { + } + + int intProperty() const { return m_intProperty; } + bool boolProperty() const { return m_boolProperty; } + qreal realProperty() const { return m_realProperty; } + double doubleProperty() const { return m_doubleProperty; } + QString stringProperty() const { return m_stringProperty; } + QUrl urlProperty() const { return m_urlProperty; } + QVariant varProperty() const { return m_varProperty; } + QVariantList intListProperty() const { return m_intListProperty; } + QVariantList stringListProperty() const { return m_stringListProperty; } + QDate dateProperty() const { return m_dateProperty; } + QSizeF sizeProperty() const { return m_sizeProperty; } + QPointF pointProperty() const { return m_pointProperty; } + QRectF rectProperty() const { return m_rectProperty; } + QColor colorProperty() const { return m_colorProperty; } + QFont fontProperty() const { return m_fontProperty; } + +public slots: + void setIntProperty(int arg) + { + if (m_intProperty != arg) { + m_intProperty = arg; + emit intPropertyChanged(arg); + } + } + + void setBoolProperty(bool arg) + { + if (m_boolProperty != arg) { + m_boolProperty = arg; + emit boolPropertyChanged(arg); + } + } + + void setRealProperty(qreal arg) + { + if (m_realProperty != arg) { + m_realProperty = arg; + emit realPropertyChanged(arg); + } + } + + void setDoubleProperty(double arg) + { + if (m_doubleProperty != arg) { + m_doubleProperty = arg; + emit doublePropertyChanged(arg); + } + } + + void setStringProperty(const QString &arg) + { + if (m_stringProperty != arg) { + m_stringProperty = arg; + emit stringPropertyChanged(arg); + } + } + + void setUrlProperty(const QUrl &arg) + { + if (m_urlProperty != arg) { + m_urlProperty = arg; + emit urlPropertyChanged(arg); + } + } + + void setVarProperty(const QVariant &arg) + { + if (m_varProperty != arg) { + m_varProperty = arg; + emit varPropertyChanged(arg); + } + } + + void setIntListProperty(const QVariantList &arg) + { + if (m_intListProperty != arg) { + m_intListProperty = arg; + emit intListPropertyChanged(arg); + } + } + + void setStringListProperty(const QVariantList &arg) + { + if (m_stringListProperty != arg) { + m_stringListProperty = arg; + emit stringListPropertyChanged(arg); + } + } + + void setDateProperty(const QDate &arg) + { + if (m_dateProperty != arg) { + m_dateProperty = arg; + emit datePropertyChanged(arg); + } + } + + void setSizeProperty(const QSizeF &arg) + { + if (m_sizeProperty != arg) { + m_sizeProperty = arg; + emit sizePropertyChanged(arg); + } + } + + void setPointProperty(const QPointF &arg) + { + if (m_pointProperty != arg) { + m_pointProperty = arg; + emit pointPropertyChanged(arg); + } + } + + void setRectProperty(const QRectF &arg) + { + if (m_rectProperty != arg) { + m_rectProperty = arg; + emit rectPropertyChanged(arg); + } + } + + void setColorProperty(const QColor &arg) + { + if (m_colorProperty != arg) { + m_colorProperty = arg; + emit colorPropertyChanged(arg); + } + } + + void setFontProperty(const QFont &arg) + { + if (m_fontProperty != arg) { + m_fontProperty = arg; + emit fontPropertyChanged(arg); + } + } + +signals: + void intPropertyChanged(int arg); + void boolPropertyChanged(bool arg); + void realPropertyChanged(qreal arg); + void doublePropertyChanged(double arg); + void stringPropertyChanged(const QString &arg); + void urlPropertyChanged(const QUrl &arg); + void varPropertyChanged(const QVariant &arg); + void intListPropertyChanged(const QVariantList &arg); + void stringListPropertyChanged(const QVariantList &arg); + void datePropertyChanged(const QDate &arg); + void sizePropertyChanged(const QSizeF &arg); + void pointPropertyChanged(const QPointF &arg); + void rectPropertyChanged(const QRectF &arg); + void colorPropertyChanged(const QColor &arg); + void fontPropertyChanged(const QFont &arg); + +private: + int m_intProperty; + bool m_boolProperty; + qreal m_realProperty; + double m_doubleProperty; + QString m_stringProperty; + QUrl m_urlProperty; + QVariant m_varProperty; + QVariantList m_intListProperty; + QVariantList m_stringListProperty; + QDate m_dateProperty; + QSizeF m_sizeProperty; + QPointF m_pointProperty; + QRectF m_rectProperty; + QColor m_colorProperty; + QFont m_fontProperty; +}; + +void tst_QQmlSettings::initTestCase() +{ + QQmlDataTest::initTestCase(); + + QCoreApplication::setApplicationName("tst_QQmlSettings"); + QCoreApplication::setOrganizationName("QtProject"); + QCoreApplication::setOrganizationDomain("qt-project.org"); + + qmlRegisterType<CppObject>("Qt.test", 1, 0, "CppObject"); +} + +void tst_QQmlSettings::init() +{ + QSettings settings; + settings.clear(); +} + +void tst_QQmlSettings::cleanup() +{ + QSettings settings; + settings.clear(); +} + +void tst_QQmlSettings::basic() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("basic.qml")); + QScopedPointer<QObject> root(component.create()); + QVERIFY(root.data()); + QVERIFY(root->property("success").toBool()); + QSettings settings; + QTRY_VERIFY(settings.value("success").toBool()); +} + +void tst_QQmlSettings::types() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("types.qml")); + QScopedPointer<QObject> root(component.create()); + QVERIFY(root.data()); + QObject *settings = root->property("settings").value<QObject *>(); + QVERIFY(settings); + + // default property values + QCOMPARE(root->property("intProperty").toInt(), 0); + QCOMPARE(root->property("boolProperty").toBool(), false); + QCOMPARE(root->property("realProperty").toReal(), static_cast<qreal>(0.0)); + QCOMPARE(root->property("doubleProperty").toDouble(), static_cast<double>(0.0)); + QCOMPARE(root->property("stringProperty").toString(), QString()); + QCOMPARE(root->property("urlProperty").toUrl(), QUrl()); + QCOMPARE(root->property("intListProperty").toList(), QVariantList()); + QCOMPARE(root->property("stringListProperty").toList(), QVariantList()); + QCOMPARE(root->property("dateProperty").toDate(), QDate()); + // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime()); + QCOMPARE(root->property("sizeProperty").toSizeF(), QSizeF()); + QCOMPARE(root->property("pointProperty").toPointF(), QPointF()); + QCOMPARE(root->property("rectProperty").toRectF(), QRectF()); + QCOMPARE(root->property("colorProperty").value<QColor>(), QColor()); + QCOMPARE(root->property("fontProperty").value<QFont>(), QFont()); + + // default settings values + QCOMPARE(settings->property("intProperty").toInt(), 123); + QCOMPARE(settings->property("boolProperty").toBool(), true); + QCOMPARE(settings->property("realProperty").toReal(), static_cast<qreal>(1.23)); + QCOMPARE(settings->property("doubleProperty").toDouble(), static_cast<double>(3.45)); + QCOMPARE(settings->property("stringProperty").toString(), QStringLiteral("foo")); + QCOMPARE(settings->property("urlProperty").toUrl(), QUrl("http://www.qt-project.org")); + QCOMPARE(settings->property("intListProperty").toList(), QVariantList() << 1 << 2 << 3); + QCOMPARE(settings->property("stringListProperty").toList(), QVariantList() << QStringLiteral("a") << QStringLiteral("b") << QStringLiteral("c")); + QCOMPARE(settings->property("dateProperty").toDate(), QDate(2000, 01, 02)); + // QTBUG-32295: QCOMPARE(settings->property("timeProperty").toDate(), QTime(12, 34, 56)); + QCOMPARE(settings->property("sizeProperty").toSizeF(), QSizeF(12, 34)); + QCOMPARE(settings->property("pointProperty").toPointF(), QPointF(12, 34)); + QCOMPARE(settings->property("rectProperty").toRectF(), QRectF(1, 2, 3, 4)); + QCOMPARE(settings->property("colorProperty").value<QColor>(), QColor(Qt::red)); + QCOMPARE(settings->property("fontProperty").value<QFont>(), QFont()); + + // read settings + QVERIFY(QMetaObject::invokeMethod(root.data(), "readSettings")); + QCOMPARE(root->property("intProperty").toInt(), 123); + QCOMPARE(root->property("boolProperty").toBool(), true); + QCOMPARE(root->property("realProperty").toReal(), static_cast<qreal>(1.23)); + QCOMPARE(root->property("doubleProperty").toDouble(), static_cast<double>(3.45)); + QCOMPARE(root->property("stringProperty").toString(), QStringLiteral("foo")); + QCOMPARE(root->property("urlProperty").toUrl(), QUrl("http://www.qt-project.org")); + QCOMPARE(root->property("intListProperty").toList(), QVariantList() << 1 << 2 << 3); + QCOMPARE(root->property("stringListProperty").toList(), QVariantList() << QStringLiteral("a") << QStringLiteral("b") << QStringLiteral("c")); + QCOMPARE(root->property("dateProperty").toDate(), QDate(2000, 01, 02)); + // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime(12, 34, 56)); + QCOMPARE(root->property("sizeProperty").toSizeF(), QSizeF(12, 34)); + QCOMPARE(root->property("pointProperty").toPointF(), QPointF(12, 34)); + QCOMPARE(root->property("rectProperty").toRectF(), QRectF(1, 2, 3, 4)); + QCOMPARE(root->property("colorProperty").value<QColor>(), QColor(Qt::red)); + QCOMPARE(root->property("fontProperty").value<QFont>(), QFont()); + + // change properties + QVERIFY(root->setProperty("intProperty", 456)); + QVERIFY(root->setProperty("boolProperty", false)); + QVERIFY(root->setProperty("realProperty", static_cast<qreal>(4.56))); + QVERIFY(root->setProperty("doubleProperty", static_cast<double>(6.78))); + QVERIFY(root->setProperty("stringProperty", QStringLiteral("bar"))); + QVERIFY(root->setProperty("urlProperty", QUrl("https://codereview.qt-project.org"))); + QVERIFY(root->setProperty("intListProperty", QVariantList() << 4 << 5 << 6)); + QVERIFY(root->setProperty("stringListProperty", QVariantList() << QStringLiteral("d") << QStringLiteral("e") << QStringLiteral("f"))); + QVERIFY(root->setProperty("dateProperty", QDate(2010, 02, 01))); + // QTBUG-32295: QVERIFY(root->setProperty("timeProperty", QTime(6, 56, 34))); + QVERIFY(root->setProperty("sizeProperty", QSizeF(56, 78))); + QVERIFY(root->setProperty("pointProperty", QPointF(56, 78))); + QVERIFY(root->setProperty("rectProperty", QRectF(5, 6, 7, 8))); + QVERIFY(root->setProperty("colorProperty", QColor(Qt::blue))); + QFont boldFont; boldFont.setBold(true); + QVERIFY(root->setProperty("fontProperty", boldFont)); + + // write settings + QVERIFY(QMetaObject::invokeMethod(root.data(), "writeSettings")); + QTRY_COMPARE(settings->property("intProperty").toInt(), 456); + QTRY_COMPARE(settings->property("boolProperty").toBool(), false); + QTRY_COMPARE(settings->property("realProperty").toReal(), static_cast<qreal>(4.56)); + QTRY_COMPARE(settings->property("doubleProperty").toDouble(), static_cast<double>(6.78)); + QTRY_COMPARE(settings->property("stringProperty").toString(), QStringLiteral("bar")); + QTRY_COMPARE(settings->property("urlProperty").toUrl(), QUrl("https://codereview.qt-project.org")); + QTRY_COMPARE(settings->property("intListProperty").toList(), QVariantList() << 4 << 5 << 6); + QTRY_COMPARE(settings->property("stringListProperty").toList(), QVariantList() << QStringLiteral("d") << QStringLiteral("e") << QStringLiteral("f")); + QTRY_COMPARE(settings->property("dateProperty").toDate(), QDate(2010, 02, 01)); + // QTBUG-32295: QTRY_COMPARE(settings->property("timeProperty").toDate(), QTime(6, 56, 34)); + QTRY_COMPARE(settings->property("sizeProperty").toSizeF(), QSizeF(56, 78)); + QTRY_COMPARE(settings->property("pointProperty").toPointF(), QPointF(56, 78)); + QTRY_COMPARE(settings->property("rectProperty").toRectF(), QRectF(5, 6, 7, 8)); + QTRY_COMPARE(settings->property("colorProperty").value<QColor>(), QColor(Qt::blue)); + QTRY_COMPARE(settings->property("fontProperty").value<QFont>(), boldFont); + + QSettings qs; + QTRY_COMPARE(qs.value("intProperty").toInt(), 456); + QTRY_COMPARE(qs.value("boolProperty").toBool(), false); + QTRY_COMPARE(qs.value("realProperty").toReal(), static_cast<qreal>(4.56)); + QTRY_COMPARE(qs.value("doubleProperty").toDouble(), static_cast<double>(6.78)); + QTRY_COMPARE(qs.value("stringProperty").toString(), QStringLiteral("bar")); + QTRY_COMPARE(qs.value("urlProperty").toUrl(), QUrl("https://codereview.qt-project.org")); + QTRY_COMPARE(qs.value("intListProperty").toList(), QVariantList() << 4 << 5 << 6); + QTRY_COMPARE(qs.value("stringListProperty").toList(), QVariantList() << QStringLiteral("d") << QStringLiteral("e") << QStringLiteral("f")); + QTRY_COMPARE(qs.value("dateProperty").toDate(), QDate(2010, 02, 01)); + // QTBUG-32295: QTRY_COMPARE(qs.value("timeProperty").toDate(), QTime(6, 56, 34)); + QTRY_COMPARE(qs.value("sizeProperty").toSizeF(), QSizeF(56, 78)); + QTRY_COMPARE(qs.value("pointProperty").toPointF(), QPointF(56, 78)); + QTRY_COMPARE(qs.value("rectProperty").toRectF(), QRectF(5, 6, 7, 8)); + QTRY_COMPARE(qs.value("colorProperty").value<QColor>(), QColor(Qt::blue)); + QTRY_COMPARE(qs.value("fontProperty").value<QFont>(), boldFont); +} + +void tst_QQmlSettings::aliases_data() +{ + QTest::addColumn<QString>("testFile"); + QTest::newRow("qml") << "aliases.qml"; + QTest::newRow("cpp") << "cpp-aliases.qml"; +} + +void tst_QQmlSettings::aliases() +{ + QFETCH(QString, testFile); + + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl(testFile)); + QScopedPointer<QObject> root(component.create()); + QVERIFY(root.data()); + QObject *settings = root->property("settings").value<QObject *>(); + QVERIFY(settings); + + // default property values + QCOMPARE(root->property("intProperty").toInt(), 123); + QCOMPARE(root->property("boolProperty").toBool(), true); + QCOMPARE(root->property("realProperty").toReal(), static_cast<qreal>(1.23)); + QCOMPARE(root->property("doubleProperty").toDouble(), static_cast<double>(3.45)); + QCOMPARE(root->property("stringProperty").toString(), QStringLiteral("foo")); + QCOMPARE(root->property("urlProperty").toUrl(), QUrl("http://www.qt-project.org")); + QCOMPARE(root->property("intListProperty").toList(), QVariantList() << 1 << 2 << 3); + QCOMPARE(root->property("stringListProperty").toList(), QVariantList() << QStringLiteral("a") << QStringLiteral("b") << QStringLiteral("c")); + QCOMPARE(root->property("dateProperty").toDate(), QDate(2000, 01, 02)); + // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime(12, 34, 56)); + QCOMPARE(root->property("sizeProperty").toSizeF(), QSizeF(12, 34)); + QCOMPARE(root->property("pointProperty").toPointF(), QPointF(12, 34)); + QCOMPARE(root->property("rectProperty").toRectF(), QRectF(1, 2, 3, 4)); + QCOMPARE(root->property("colorProperty").value<QColor>(), QColor(Qt::red)); + QCOMPARE(root->property("fontProperty").value<QFont>(), QFont()); + + // default settings values + QCOMPARE(settings->property("intProperty").toInt(), 123); + QCOMPARE(settings->property("boolProperty").toBool(), true); + QCOMPARE(settings->property("realProperty").toReal(), static_cast<qreal>(1.23)); + QCOMPARE(settings->property("doubleProperty").toDouble(), static_cast<double>(3.45)); + QCOMPARE(settings->property("stringProperty").toString(), QStringLiteral("foo")); + QCOMPARE(settings->property("urlProperty").toUrl(), QUrl("http://www.qt-project.org")); + QCOMPARE(settings->property("intListProperty").toList(), QVariantList() << 1 << 2 << 3); + QCOMPARE(settings->property("stringListProperty").toList(), QVariantList() << QStringLiteral("a") << QStringLiteral("b") << QStringLiteral("c")); + QCOMPARE(settings->property("dateProperty").toDate(), QDate(2000, 01, 02)); + // QTBUG-32295: QCOMPARE(settings->property("timeProperty").toDate(), QTime(12, 34, 56)); + QCOMPARE(settings->property("sizeProperty").toSizeF(), QSizeF(12, 34)); + QCOMPARE(settings->property("pointProperty").toPointF(), QPointF(12, 34)); + QCOMPARE(settings->property("rectProperty").toRectF(), QRectF(1, 2, 3, 4)); + QCOMPARE(settings->property("colorProperty").value<QColor>(), QColor(Qt::red)); + QCOMPARE(settings->property("fontProperty").value<QFont>(), QFont()); + + // change settings + QVERIFY(settings->setProperty("intProperty", 456)); + QVERIFY(settings->setProperty("boolProperty", false)); + QVERIFY(settings->setProperty("realProperty", static_cast<qreal>(4.56))); + QVERIFY(settings->setProperty("doubleProperty", static_cast<double>(6.78))); + QVERIFY(settings->setProperty("stringProperty", QStringLiteral("bar"))); + QVERIFY(settings->setProperty("urlProperty", QUrl("https://codereview.qt-project.org"))); + QVERIFY(settings->setProperty("intListProperty", QVariantList() << 4 << 5 << 6)); + QVERIFY(settings->setProperty("stringListProperty", QVariantList() << QStringLiteral("d") << QStringLiteral("e") << QStringLiteral("f"))); + QVERIFY(settings->setProperty("dateProperty", QDate(2010, 02, 01))); + // QTBUG-32295: QVERIFY(settings->setProperty("timeProperty", QTime(6, 56, 34))); + QVERIFY(settings->setProperty("sizeProperty", QSizeF(56, 78))); + QVERIFY(settings->setProperty("pointProperty", QPointF(56, 78))); + QVERIFY(settings->setProperty("rectProperty", QRectF(5, 6, 7, 8))); + QVERIFY(settings->setProperty("colorProperty", QColor(Qt::blue))); + QFont boldFont; boldFont.setBold(true); + QVERIFY(settings->setProperty("fontProperty", boldFont)); + + QSettings qs; + QTRY_COMPARE(qs.value("intProperty").toInt(), 456); + QTRY_COMPARE(qs.value("boolProperty").toBool(), false); + QTRY_COMPARE(qs.value("realProperty").toReal(), static_cast<qreal>(4.56)); + QTRY_COMPARE(qs.value("doubleProperty").toDouble(), static_cast<double>(6.78)); + QTRY_COMPARE(qs.value("stringProperty").toString(), QStringLiteral("bar")); + QTRY_COMPARE(qs.value("urlProperty").toUrl(), QUrl("https://codereview.qt-project.org")); + QTRY_COMPARE(qs.value("intListProperty").toList(), QVariantList() << 4 << 5 << 6); + QTRY_COMPARE(qs.value("stringListProperty").toList(), QVariantList() << QStringLiteral("d") << QStringLiteral("e") << QStringLiteral("f")); + QTRY_COMPARE(qs.value("dateProperty").toDate(), QDate(2010, 02, 01)); + // QTBUG-32295: QTRY_COMPARE(qs.value("timeProperty").toDate(), QTime(6, 56, 34)); + QTRY_COMPARE(qs.value("sizeProperty").toSizeF(), QSizeF(56, 78)); + QTRY_COMPARE(qs.value("pointProperty").toPointF(), QPointF(56, 78)); + QTRY_COMPARE(qs.value("rectProperty").toRectF(), QRectF(5, 6, 7, 8)); + QTRY_COMPARE(qs.value("colorProperty").value<QColor>(), QColor(Qt::blue)); + QTRY_COMPARE(qs.value("fontProperty").value<QFont>(), boldFont); +} + +void tst_QQmlSettings::categories() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("categories.qml")); + + { + // initial state + QSettings qs; + QVERIFY(!qs.childGroups().contains("initialCategory")); + QVERIFY(!qs.childGroups().contains("changedCategory")); + QVERIFY(!qs.childKeys().contains("value")); + } + + { + // load & write default values + QScopedPointer<QObject> settings(component.create()); + QVERIFY(settings.data()); + QCOMPARE(settings->property("category").toString(), QStringLiteral("initialCategory")); + QCOMPARE(settings->property("value").toString(), QStringLiteral("initialValue")); + } + + { + // verify written settings & change the value + QSettings qs; + QVERIFY(qs.childGroups().contains("initialCategory")); + qs.beginGroup("initialCategory"); + QVERIFY(qs.childKeys().contains("value")); + QCOMPARE(qs.value("value").toString(), QStringLiteral("initialValue")); + qs.setValue("value", QStringLiteral("changedValue")); + } + + { + // load changed value & change the category + QScopedPointer<QObject> settings(component.create()); + QVERIFY(settings.data()); + QCOMPARE(settings->property("category").toString(), QStringLiteral("initialCategory")); + QCOMPARE(settings->property("value").toString(), QStringLiteral("changedValue")); + QVERIFY(settings->setProperty("category", QStringLiteral("changedCategory"))); + } + + { + // verify written settings + QSettings qs; + QVERIFY(qs.childGroups().contains("changedCategory")); + qs.beginGroup("changedCategory"); + QVERIFY(qs.childKeys().contains("value")); + QCOMPARE(qs.value("value").toString(), QStringLiteral("changedValue")); + qs.setValue("value", QStringLiteral("changedValue")); + qs.endGroup(); + } +} + +void tst_QQmlSettings::siblings() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("siblings.qml")); + delete component.create(); + + // verify setting aliases to destructed siblings + QSettings settings; + QCOMPARE(settings.value("alias1").toString(), QStringLiteral("value1")); + QCOMPARE(settings.value("alias2").toString(), QStringLiteral("value2")); +} + +QTEST_MAIN(tst_QQmlSettings) + +#include "tst_qqmlsettings.moc" |