Kirigami2

styleselector.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "styleselector.h"
8
9#include <QDir>
10#include <QFile>
11#include <QQuickStyle>
12
13namespace Kirigami
14{
15namespace Platform
16{
17
18QString StyleSelector::style()
19{
20 if (qEnvironmentVariableIntValue("KIRIGAMI_FORCE_STYLE") == 1) {
21 return QQuickStyle::name();
22 } else {
23 return styleChain().first();
24 }
25}
26
27QStringList StyleSelector::styleChain()
28{
29 if (qEnvironmentVariableIntValue("KIRIGAMI_FORCE_STYLE") == 1) {
30 return {QQuickStyle::name()};
31 }
32
33 if (!s_styleChain.isEmpty()) {
34 return s_styleChain;
35 }
36
37 auto style = QQuickStyle::name();
38
39#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
40 // org.kde.desktop.plasma is a couple of files that fall back to desktop by purpose
41 if (style.isEmpty() || style == QStringLiteral("org.kde.desktop.plasma")) {
42 auto path = resolveFilePath(QStringLiteral("/styles/org.kde.desktop"));
43 if (QFile::exists(path)) {
44 s_styleChain.prepend(QStringLiteral("org.kde.desktop"));
45 }
46 }
47#elif defined(Q_OS_ANDROID)
48 s_styleChain.prepend(QStringLiteral("Material"));
49#else // do we have an iOS specific style?
50 s_styleChain.prepend(QStringLiteral("Material"));
51#endif
52
53 auto stylePath = resolveFilePath(QStringLiteral("/styles/") + style);
54 if (!style.isEmpty() && QFile::exists(stylePath) && !s_styleChain.contains(style)) {
55 s_styleChain.prepend(style);
56 // if we have plasma deps installed, use them for extra integration
57 auto plasmaPath = resolveFilePath(QStringLiteral("/styles/org.kde.desktop.plasma"));
58 if (style == QStringLiteral("org.kde.desktop") && QFile::exists(plasmaPath)) {
59 s_styleChain.prepend(QStringLiteral("org.kde.desktop.plasma"));
60 }
61 } else {
62#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
63 s_styleChain.prepend(QStringLiteral("org.kde.desktop"));
64#endif
65 }
66
67 return s_styleChain;
68}
69
70QUrl StyleSelector::componentUrl(const QString &fileName)
71{
72 const auto chain = styleChain();
73 for (const QString &style : chain) {
74 const QString candidate = QStringLiteral("styles/") + style + QLatin1Char('/') + fileName;
75 if (QFile::exists(resolveFilePath(candidate))) {
76 return QUrl(resolveFileUrl(candidate));
77 }
78 }
79
80 return QUrl(resolveFileUrl(fileName));
81}
82
83void StyleSelector::setBaseUrl(const QUrl &baseUrl)
84{
85 s_baseUrl = baseUrl;
86}
87
88QString StyleSelector::resolveFilePath(const QString &path)
89{
90#if defined(KIRIGAMI_BUILD_TYPE_STATIC) || defined(Q_OS_ANDROID)
91 return QStringLiteral(":/qt/qml/org/kde/kirigami/") + path;
92#else
93 if (s_baseUrl.isValid()) {
94 return s_baseUrl.toLocalFile() + QLatin1Char('/') + path;
95 } else {
96 return QDir::currentPath() + QLatin1Char('/') + path;
97 }
98#endif
99}
100
101QString StyleSelector::resolveFileUrl(const QString &path)
102{
103#if defined(KIRIGAMI_BUILD_TYPE_STATIC) || defined(Q_OS_ANDROID)
104 return QStringLiteral("qrc:/qt/qml/org/kde/kirigami/") + path;
105#else
106 return s_baseUrl.toString() + QLatin1Char('/') + path;
107#endif
108}
109
110}
111}
QString path(const QString &relativePath)
QString currentPath()
bool exists() const const
T & first()
bool isEmpty() const const
void prepend(parameter_type value)
QString name()
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
bool isValid() const const
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:46 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.