KDeclarative

kdeclarative.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "kdeclarative.h"
8 #include "private/kdeclarative_p.h"
9 #include "private/kiconprovider_p.h"
10 #include "private/kioaccessmanagerfactory_p.h"
11 #include "qmlobject.h"
12 
13 #include <QCoreApplication>
14 #include <QQmlDebuggingEnabler>
15 #include <QQmlContext>
16 
17 #include <KConfigGroup>
18 #include <KSharedConfig>
19 
20 namespace KDeclarative {
21 
22 QStringList KDeclarativePrivate::s_runtimePlatform;
23 
24 KDeclarativePrivate::KDeclarativePrivate()
25  : contextObj(nullptr)
26 {
27 }
28 
30  : d(new KDeclarativePrivate)
31 {
32 }
33 
34 KDeclarative::~KDeclarative()
35 {
36  delete d;
37 }
38 
39 void KDeclarative::setDeclarativeEngine(QQmlEngine *engine)
40 {
41  if (d->declarativeEngine.data() == engine) {
42  return;
43  }
44  d->declarativeEngine = engine;
45 }
46 
47 QQmlEngine *KDeclarative::declarativeEngine() const
48 {
49  return d->declarativeEngine.data();
50 }
51 
52 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 0)
53 void KDeclarative::initialize()
54 {
55 }
56 #endif
57 
58 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 45)
59 void KDeclarative::setupBindings()
60 {
61  setupContext();
62  setupEngine(d->declarativeEngine.data());
63 }
64 #endif
65 
66 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
67 void KDeclarative::setupContext()
68 {
69  /*Create a context object for the root qml context.
70  in this way we can register global functions, in this case the i18n() family*/
71  if (!d->contextObj) {
72  d->contextObj = new KLocalizedContext(d->declarativeEngine.data());
73  }
74 
75  //If the engine is in a qmlObject take the qmlObject rootContext instead of the engine one.
76  if (d->qmlObj) {
77  d->qmlObj->rootContext()->setContextObject(d->contextObj);
78  } else {
79  d->declarativeEngine.data()->rootContext()->setContextObject(d->contextObj);
80  }
81 
82  if (!d->translationDomain.isNull()) {
83  d->contextObj->setTranslationDomain(d->translationDomain);
84  }
85 }
86 #endif
87 
88 void KDeclarative::setupEngine(QQmlEngine *engine)
89 {
90  //get rid of stock network access manager factory
92  engine->setNetworkAccessManagerFactory(nullptr);
93  delete factory;
94  engine->setNetworkAccessManagerFactory(new KIOAccessManagerFactory());
95 
96  /* Tell the engine to search for platform-specific imports first
97  (so it will "win" in import name resolution).
98  addImportPath adds the path at the beginning, so to honour user's
99  paths we need to traverse the list in reverse order */
100  const QStringList pluginPathList = engine->importPathList();
101 
102  const QString target = componentsTarget();
103  if (target != defaultComponentsTarget()) {
104  const QStringList paths = pluginPathList;
105  QStringListIterator it(paths);
106  it.toBack();
107  while (it.hasPrevious()) {
108  QString path = it.previous();
109  path = path.left(path.lastIndexOf(QLatin1Char('/')));
110  engine->addImportPath(path + QStringLiteral("/platformqml/") + target);
111  }
112  }
113 
114  // setup ImageProvider for KDE icons
115  engine->addImageProvider(QStringLiteral("icon"), new KIconProvider);
116 }
117 
118 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
119 void KDeclarative::setTranslationDomain(const QString &translationDomain)
120 {
121  d->translationDomain = translationDomain;
122  if (d->contextObj) {
123  d->contextObj->setTranslationDomain(d->translationDomain);
124  }
125 }
126 #endif
127 
128 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
129 QString KDeclarative::translationDomain() const
130 {
131  return d->translationDomain;
132 }
133 #endif
134 
135 void KDeclarative::setupQmlJsDebugger()
136 {
137  if (QCoreApplication::arguments().contains(QLatin1String("-qmljsdebugger"))) {
138  QQmlDebuggingEnabler enabler;
139  }
140 }
141 
142 QString KDeclarative::defaultComponentsTarget()
143 {
144  return QLatin1String("desktop");
145 }
146 
147 QString KDeclarative::componentsTarget()
148 {
149  const QStringList platform = runtimePlatform();
150  if (platform.isEmpty()) {
151  return defaultComponentsTarget();
152  }
153 
154  return platform.last();
155 }
156 
157 QStringList KDeclarative::runtimePlatform()
158 {
159  if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
160  const QString env = QString::fromLocal8Bit(getenv("PLASMA_PLATFORM"));
161  KDeclarativePrivate::s_runtimePlatform = QStringList(env.split(QLatin1Char(':'), Qt::SkipEmptyParts));
162  if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
163  KConfigGroup cg(KSharedConfig::openConfig(), "General");
164  KDeclarativePrivate::s_runtimePlatform = cg.readEntry(QStringLiteral("runtimePlatform"), KDeclarativePrivate::s_runtimePlatform);
165  }
166  }
167 
168  return KDeclarativePrivate::s_runtimePlatform;
169 }
170 
171 void KDeclarative::setRuntimePlatform(const QStringList &platform)
172 {
173  KDeclarativePrivate::s_runtimePlatform = platform;
174 }
175 
176 }
void addImportPath(const QString &path)
The KDeclarative class is used to manipulate the QQmlEngine instance used by the application and to g...
Definition: kdeclarative.h:56
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString fromLocal8Bit(const char *str, int size)
bool isEmpty() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QQmlNetworkAccessManagerFactory * networkAccessManagerFactory() const const
void addImageProvider(const QString &providerId, QQmlImageProviderBase *provider)
T * data() const const
SkipEmptyParts
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
bool isNull() const const
T & last()
QString left(int n) const const
QStringList importPathList() const const
QStringList arguments()
void setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
T readEntry(const QString &key, const T &aDefault) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 22:41:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.