From 5efd4b6a61498f0668d7367d620275367894140e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 28 Nov 2016 16:00:04 +0100 Subject: Allow specifying a relative style path in qtquickcontrols2.conf The style path is resolved relative to the location of config file. Even though qtquickcontrols2.conf is normally located in the root of resources, Quick Designer uses QT_QUICK_CONTROLS_CONF to set a custom location for the preview, where the standard config location is not feasible. This patch extends the config and style lookup further by allowing to specify a relative path to a custom style for the Quick Designer preview. Change-Id: Iad9c459421eefbf7a5bcbedfea59f5f16a1d3a39 Reviewed-by: Thomas Hartmann --- src/quickcontrols2/qquickstyle.cpp | 29 +++++++++++++++++++++++++++++ src/quickcontrols2/qquickstyle_p.h | 1 + src/quickcontrols2/qquickstyleattached.cpp | 15 ++------------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/quickcontrols2/qquickstyle.cpp b/src/quickcontrols2/qquickstyle.cpp index d7143be7..3ad7c074 100644 --- a/src/quickcontrols2/qquickstyle.cpp +++ b/src/quickcontrols2/qquickstyle.cpp @@ -183,6 +183,15 @@ struct QQuickStyleSpec setFallbackStyle(settings->value(QStringLiteral("FallbackStyle")).toString(), ":/qtquickcontrols2.conf"); } } + + // resolve a path relative to the config + QString configPath = QFileInfo(resolveConfigFilePath()).path(); + QString stylePath = findStyle(configPath, style); + if (!stylePath.isEmpty()) { + style = stylePath; + resolved = true; + } + custom = style.contains(QLatin1Char('/')); if (baseUrl.isValid()) { @@ -221,11 +230,26 @@ struct QQuickStyleSpec fallbackMethod.clear(); } + QString resolveConfigFilePath() + { + if (configFilePath.isEmpty()) { + configFilePath = QFile::decodeName(qgetenv("QT_QUICK_CONTROLS_CONF")); + if (!QFile::exists(configFilePath)) { + if (!configFilePath.isEmpty()) + qWarning("QT_QUICK_CONTROLS_CONF=%s: No such file", qPrintable(configFilePath)); + + configFilePath = QStringLiteral(":/qtquickcontrols2.conf"); + } + } + return configFilePath; + } + bool custom; bool resolved; QString style; QString fallbackStyle; QByteArray fallbackMethod; + QString configFilePath; }; Q_GLOBAL_STATIC(QQuickStyleSpec, styleSpec) @@ -262,6 +286,11 @@ void QQuickStylePrivate::reset() styleSpec()->reset(); } +QString QQuickStylePrivate::configFilePath() +{ + return styleSpec()->resolveConfigFilePath(); +} + /*! Returns the name of the application style. diff --git a/src/quickcontrols2/qquickstyle_p.h b/src/quickcontrols2/qquickstyle_p.h index cfe87fbb..65f48d95 100644 --- a/src/quickcontrols2/qquickstyle_p.h +++ b/src/quickcontrols2/qquickstyle_p.h @@ -60,6 +60,7 @@ public: static bool isCustomStyle(); static void init(const QUrl &baseUrl); static void reset(); + static QString configFilePath(); }; QT_END_NAMESPACE diff --git a/src/quickcontrols2/qquickstyleattached.cpp b/src/quickcontrols2/qquickstyleattached.cpp index 2ef07fd6..d1f27ff3 100644 --- a/src/quickcontrols2/qquickstyleattached.cpp +++ b/src/quickcontrols2/qquickstyleattached.cpp @@ -35,6 +35,7 @@ ****************************************************************************/ #include "qquickstyleattached_p.h" +#include "qquickstyle_p.h" #include #include @@ -167,18 +168,6 @@ static QList findChildStyles(const QMetaObject *type, QOb return children; } -static QString resolveConfigFile() -{ - QString filePath = QFile::decodeName(qgetenv("QT_QUICK_CONTROLS_CONF")); - if (!QFile::exists(filePath)) { - if (!filePath.isEmpty()) - qWarning("QT_QUICK_CONTROLS_CONF=%s: No such file", qPrintable(filePath)); - - filePath = QStringLiteral(":/qtquickcontrols2.conf"); - } - return filePath; -} - QQuickStyleAttached::QQuickStyleAttached(QObject *parent) : QObject(parent) { QQuickItem *item = qobject_cast(parent); @@ -208,7 +197,7 @@ QQuickStyleAttached::~QQuickStyleAttached() QSharedPointer QQuickStyleAttached::settings(const QString &group) { #ifndef QT_NO_SETTINGS - static const QString filePath = resolveConfigFile(); + const QString filePath = QQuickStylePrivate::configFilePath(); if (QFile::exists(filePath)) { QFileSelector selector; QSettings *settings = new QSettings(selector.select(filePath), QSettings::IniFormat); -- cgit v1.2.3