1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickimaginestyle_p.h"
38
39#include <QtCore/qsettings.h>
40#include <QtQuickControls2/private/qquickstyle_p.h>
41
42QT_BEGIN_NAMESPACE
43
44Q_GLOBAL_STATIC_WITH_ARGS(QString, GlobalPath, (QLatin1String("qrc:/qt-project.org/imports/QtQuick/Controls.2/Imagine/images/")))
45
46static QString ensureSlash(const QString &path)
47{
48 const QChar slash = QLatin1Char('/');
49 return path.endsWith(c: slash) ? path : path + slash;
50}
51
52QQuickImagineStyle::QQuickImagineStyle(QObject *parent)
53 : QQuickAttachedObject(parent),
54 m_path(*GlobalPath())
55{
56 init();
57}
58
59QQuickImagineStyle *QQuickImagineStyle::qmlAttachedProperties(QObject *object)
60{
61 return new QQuickImagineStyle(object);
62}
63
64QString QQuickImagineStyle::path() const
65{
66 return m_path;
67}
68
69void QQuickImagineStyle::setPath(const QString &path)
70{
71 m_explicitPath = true;
72 if (m_path == path)
73 return;
74
75 m_path = path;
76 propagatePath();
77
78 emit pathChanged();
79}
80
81void QQuickImagineStyle::inheritPath(const QString &path)
82{
83 if (m_explicitPath || m_path == path)
84 return;
85
86 m_path = path;
87 propagatePath();
88 emit pathChanged();
89}
90
91void QQuickImagineStyle::propagatePath()
92{
93 const auto styles = attachedChildren();
94 for (QQuickAttachedObject *child : styles) {
95 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(object: child);
96 if (imagine)
97 imagine->inheritPath(path: m_path);
98 }
99}
100
101void QQuickImagineStyle::resetPath()
102{
103 if (!m_explicitPath)
104 return;
105
106 m_explicitPath = false;
107 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(object: attachedParent());
108 inheritPath(path: imagine ? imagine->path() : *GlobalPath());
109}
110
111QUrl QQuickImagineStyle::url() const
112{
113 // Using ApplicationWindow as an example, its NinePatchImage url
114 // was previously assigned like this:
115 //
116 // soruce: Imagine.path + "applicationwindow-background"
117 //
118 // If Imagine.path is set to ":/images" by the user, then the final URL would be:
119 //
120 // QUrl("file:///home/user/qt/qtbase/qml/QtQuick/Controls.2/Imagine/:/images/applicationwindow-background")
121 //
122 // To ensure that the correct URL is constructed, we do it ourselves here,
123 // and then the control QML files use the "url" property instead.
124 const QString path = ensureSlash(path: m_path);
125 if (path.startsWith(s: QLatin1String("qrc")))
126 return QUrl(path);
127
128 if (path.startsWith(s: QLatin1String(":/")))
129 return QUrl(QLatin1String("qrc") + path);
130
131 return QUrl::fromLocalFile(localfile: path);
132}
133
134void QQuickImagineStyle::attachedParentChange(QQuickAttachedObject *newParent, QQuickAttachedObject *oldParent)
135{
136 Q_UNUSED(oldParent);
137 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(object: newParent);
138 if (imagine)
139 inheritPath(path: imagine->path());
140}
141
142static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name)
143{
144 QByteArray value = qgetenv(varName: env);
145#if QT_CONFIG(settings)
146 if (value.isNull() && !settings.isNull())
147 value = settings->value(key: name).toByteArray();
148#endif
149 return value;
150}
151
152void QQuickImagineStyle::init()
153{
154 static bool globalsInitialized = false;
155 if (!globalsInitialized) {
156 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(QStringLiteral("Imagine"));
157
158 QString path = QString::fromUtf8(str: resolveSetting(env: "QT_QUICK_CONTROLS_IMAGINE_PATH", settings, QStringLiteral("Path")));
159 if (!path.isEmpty())
160 *GlobalPath() = m_path = ensureSlash(path);
161
162 globalsInitialized = true;
163 }
164
165 QQuickAttachedObject::init(); // TODO: lazy init?
166}
167
168QT_END_NAMESPACE
169

source code of qtquickcontrols2/src/imports/controls/imagine/qquickimaginestyle.cpp