Kirigami2

styleselector.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "styleselector_p.h"
8 
9 #include <QDir>
10 #include <QFile>
11 #include <QQuickStyle>
12 
13 namespace Kirigami
14 {
15 QUrl StyleSelector::s_baseUrl;
16 QStringList StyleSelector::s_styleChain;
17 
18 QString StyleSelector::style()
19 {
20  if (qEnvironmentVariableIntValue("KIRIGAMI_FORCE_STYLE") == 1) {
21  return QQuickStyle::name();
22  } else {
23  return styleChain().first();
24  }
25 }
26 
27 QStringList 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 
70 QUrl 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 
83 void StyleSelector::setBaseUrl(const QUrl &baseUrl)
84 {
85  s_baseUrl = baseUrl;
86 }
87 
88 QString StyleSelector::resolveFilePath(const QString &path)
89 {
90 #if defined(KIRIGAMI_BUILD_TYPE_STATIC)
91  return QStringLiteral(":/qt-project.org/imports/org/kde/kirigami.2/") + path;
92 #elif defined(Q_OS_ANDROID)
93  return QStringLiteral(":/android_rcc_bundle/qml/org/kde/kirigami.2/") + path;
94 #else
95  if (s_baseUrl.isValid()) {
96  return s_baseUrl.toLocalFile() + QLatin1Char('/') + path;
97  } else {
98  return QDir::currentPath() + QLatin1Char('/') + path;
99  }
100 #endif
101 }
102 
103 QString StyleSelector::resolveFileUrl(const QString &path)
104 {
105 #if defined(KIRIGAMI_BUILD_TYPE_STATIC)
106  return QStringLiteral("qrc:/qt-project.org/imports/org/kde/kirigami.2/") + path;
107 #elif defined(Q_OS_ANDROID)
108  return QStringLiteral("qrc:/android_rcc_bundle/qml/org/kde/kirigami.2/") + path;
109 #else
110  return s_baseUrl.toString() + QLatin1Char('/') + path;
111 #endif
112 }
113 
114 }
bool exists() const const
QString currentPath()
QString name()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.