diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-10-06 00:11:25 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-25 07:20:59 +0000 |
commit | 32810acaa191ba00be5aac5d771c23b87628292c (patch) | |
tree | 0c45b6ab2da561612032aa66a542cb89b9128b5a /src/quickcontrols2/qquickstyle.cpp | |
parent | 6ba40b132562593781ceffc13f642d366d6ad672 (diff) |
Let specifying the fallback style for custom styles
For example, you can call QQuickStyle::setStyle(":/mycontrols") and
QQuickStyle::setFallbackStyle("Material") to select a custom style so
that the missing files will fallback to the Material style.
Notice that the Material and Universal styles do not contain all files.
For example, the non-visual Control.qml, Container.qml are not duplicated.
For these, we must fallback to the Default style that is guaranteed to
contain them all.
[ChangeLog][Controls] Added support for specifying the fallback style for
custom styles via :/qtquickcontrols2.conf, QT_QUICK_CONTROLS_FALLBACK_STYLE
or QQuickStyle::setFallbackStyle().
Change-Id: I00be1c8c6aaca875ef851c90d018e9b5e2f501b7
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quickcontrols2/qquickstyle.cpp')
-rw-r--r-- | src/quickcontrols2/qquickstyle.cpp | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/src/quickcontrols2/qquickstyle.cpp b/src/quickcontrols2/qquickstyle.cpp index 489eee15..7bd183a0 100644 --- a/src/quickcontrols2/qquickstyle.cpp +++ b/src/quickcontrols2/qquickstyle.cpp @@ -85,7 +85,7 @@ QT_BEGIN_NAMESPACE struct QQuickStyleSpec { - QQuickStyleSpec() : resolved(false) { } + QQuickStyleSpec() : custom(false), resolved(false) { } QString name() { @@ -111,6 +111,12 @@ struct QQuickStyleSpec resolve(); } + void setFallbackStyle(const QString &fallback, const QByteArray &method) + { + fallbackStyle = fallback; + fallbackMethod = method; + } + static QString findStyle(const QString &path, const QString &name) { QDir dir(path); @@ -135,11 +141,18 @@ struct QQuickStyleSpec style = QGuiApplicationPrivate::styleOverride; if (style.isEmpty()) style = QString::fromLatin1(qgetenv("QT_QUICK_CONTROLS_STYLE")); - if (style.isEmpty()) { + if (fallbackStyle.isEmpty()) + setFallbackStyle(QString::fromLatin1(qgetenv("QT_QUICK_CONTROLS_FALLBACK_STYLE")), "QT_QUICK_CONTROLS_FALLBACK_STYLE"); + if (style.isEmpty() || fallbackStyle.isEmpty()) { QSharedPointer<QSettings> settings = QQuickStyleAttached::settings(QStringLiteral("Controls")); - if (settings) - style = settings->value(QStringLiteral("Style")).toString(); + if (settings) { + if (style.isEmpty()) + style = settings->value(QStringLiteral("Style")).toString(); + if (fallbackStyle.isEmpty()) + setFallbackStyle(settings->value(QStringLiteral("FallbackStyle")).toString(), ":/qtquickcontrols2.conf"); + } } + custom = style.contains(QLatin1Char('/')); if (baseUrl.isValid()) { QString path = QQmlFile::urlToLocalFileOrQrc(baseUrl); @@ -151,7 +164,7 @@ struct QQuickStyleSpec } if (QGuiApplication::instance()) { - if (!style.contains(QLatin1Char('/'))) { + if (!custom) { const QString targetPath = QStringLiteral("QtQuick/Controls.2"); const QStringList importPaths = QQmlEngine().importPathList(); @@ -168,15 +181,54 @@ struct QQuickStyleSpec } } + void reset() + { + custom = false; + resolved = false; + style.clear(); + fallbackStyle.clear(); + fallbackMethod.clear(); + } + + bool custom; bool resolved; QString style; + QString fallbackStyle; + QByteArray fallbackMethod; }; Q_GLOBAL_STATIC(QQuickStyleSpec, styleSpec) +QString QQuickStylePrivate::fallbackStyle() +{ + return styleSpec()->fallbackStyle; +} + +bool QQuickStylePrivate::isCustomStyle() +{ + return styleSpec()->custom; +} + void QQuickStylePrivate::init(const QUrl &baseUrl) { - styleSpec()->resolve(baseUrl); + QQuickStyleSpec *spec = styleSpec(); + spec->resolve(baseUrl); + + if (!spec->fallbackStyle.isEmpty()) { + QString fallbackStyle = spec->findStyle(baseUrl.toLocalFile(), spec->fallbackStyle); + if (fallbackStyle.isEmpty()) { + if (spec->fallbackStyle.compare(QStringLiteral("Default")) != 0) { + qWarning() << "ERROR: unable to locate fallback style" << spec->fallbackStyle; + qInfo().nospace().noquote() << spec->fallbackMethod << ": the fallback style must be the name of one of the built-in Qt Quick Controls 2 styles."; + } + spec->fallbackStyle.clear(); + } + } +} + +void QQuickStylePrivate::reset() +{ + styleSpec()->reset(); } /*! @@ -220,4 +272,23 @@ void QQuickStyle::setStyle(const QString &style) styleSpec()->setStyle(style); } +/*! + \since 5.9 + Sets the application fallback style to \a style. + + \note The fallback style must be the name of one of the built-in Qt Quick Controls 2 styles, e.g. "Material". + + \note The style must be configured \b before loading QML that imports Qt Quick Controls 2. + It is not possible to change the style after the QML types have been registered. +*/ +void QQuickStyle::setFallbackStyle(const QString &style) +{ + if (QQmlMetaType::isModule(QStringLiteral("QtQuick.Controls"), 2, 0)) { + qWarning() << "ERROR: QQuickStyle::setFallbackStyle() must be called before loading QML that imports Qt Quick Controls 2."; + return; + } + + styleSpec()->setFallbackStyle(style, "QQuickStyle::setFallbackStyle()"); +} + QT_END_NAMESPACE |