Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickimaginestyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#if QT_CONFIG(settings)
8#include <QtCore/qsettings.h>
9#endif
10#include <QtQuickControls2/private/qquickstyle_p.h>
11
13
14Q_GLOBAL_STATIC_WITH_ARGS(QString, GlobalPath, (QLatin1String("qrc:/qt-project.org/imports/QtQuick/Controls/Imagine/images/")))
15
16static QString ensureSlash(const QString &path)
17{
18 const QChar slash = QLatin1Char('/');
19 return path.endsWith(slash) ? path : path + slash;
20}
21
22QQuickImagineStyle::QQuickImagineStyle(QObject *parent)
23 : QQuickAttachedPropertyPropagator(parent),
24 m_path(*GlobalPath())
25{
26 init();
27}
28
29QQuickImagineStyle *QQuickImagineStyle::qmlAttachedProperties(QObject *object)
30{
31 return new QQuickImagineStyle(object);
32}
33
34QString QQuickImagineStyle::path() const
35{
36 return m_path;
37}
38
39void QQuickImagineStyle::setPath(const QString &path)
40{
41 m_explicitPath = true;
42 if (m_path == path)
43 return;
44
45 m_path = path;
46 propagatePath();
47
48 emit pathChanged();
49}
50
51void QQuickImagineStyle::inheritPath(const QString &path)
52{
53 if (m_explicitPath || m_path == path)
54 return;
55
56 m_path = path;
57 propagatePath();
58 emit pathChanged();
59}
60
61void QQuickImagineStyle::propagatePath()
62{
63 const auto styles = attachedChildren();
64 for (QQuickAttachedPropertyPropagator *child : styles) {
65 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(child);
66 if (imagine)
67 imagine->inheritPath(m_path);
68 }
69}
70
71void QQuickImagineStyle::resetPath()
72{
73 if (!m_explicitPath)
74 return;
75
76 m_explicitPath = false;
77 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(attachedParent());
78 inheritPath(imagine ? imagine->path() : *GlobalPath());
79}
80
81QUrl QQuickImagineStyle::url() const
82{
83 // Using ApplicationWindow as an example, its NinePatchImage url
84 // was previously assigned like this:
85 //
86 // soruce: Imagine.path + "applicationwindow-background"
87 //
88 // If Imagine.path is set to ":/images" by the user, then the final URL would be:
89 //
90 // QUrl("file:///home/user/qt/qtbase/qml/QtQuick/Controls/Imagine/:/images/applicationwindow-background")
91 //
92 // To ensure that the correct URL is constructed, we do it ourselves here,
93 // and then the control QML files use the "url" property instead.
94 const QString path = ensureSlash(m_path);
95 if (path.startsWith(QLatin1String("qrc")))
96 return QUrl(path);
97
98 if (path.startsWith(QLatin1String(":/")))
99 return QUrl(QLatin1String("qrc") + path);
100
101 return QUrl::fromLocalFile(path);
102}
103
104void QQuickImagineStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
105{
106 Q_UNUSED(oldParent);
107 QQuickImagineStyle *imagine = qobject_cast<QQuickImagineStyle *>(newParent);
108 if (imagine)
109 inheritPath(imagine->path());
110}
111
112static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name)
113{
114 QByteArray value = qgetenv(env);
115#if QT_CONFIG(settings)
116 if (value.isNull() && !settings.isNull())
117 value = settings->value(name).toByteArray();
118#endif
119 return value;
120}
121
122void QQuickImagineStyle::init()
123{
124 static bool globalsInitialized = false;
125 if (!globalsInitialized) {
126 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(QStringLiteral("Imagine"));
127
128 QString path = QString::fromUtf8(resolveSetting("QT_QUICK_CONTROLS_IMAGINE_PATH", settings, QStringLiteral("Path")));
129 if (!path.isEmpty())
130 *GlobalPath() = m_path = ensureSlash(path);
131
132 globalsInitialized = true;
133 }
134
135 QQuickAttachedPropertyPropagator::initialize(); // TODO: lazy init?
136}
137
138QT_END_NAMESPACE
139
140#include "moc_qquickimaginestyle_p.cpp"
static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer< QSettings > &settings, const QString &name)