on xenial help sign in
Changed shellImportPath to return a list of paths.

Added prependImportPaths and changed appendImportPaths to check for duplicates. Approved by PS Jenkins bot, Michael Zanetti.

commita6fea5cae712551446b8cd904059e4dac71a448c authorNick Dedekind <nicholas.dedekind@gmail.com> dateThu 27 Jun 2013, 08:29 (13 years ago) parentfca0dc5e6ddac9
2 files changed+36−9
main.cpp+26−6 paths.h.in+10−3
main.cpp+26−6
@@ -31,13 +31,33 @@
3131#include "MouseTouchAdaptor.h"
3232
3333namespace {
34+
35+void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
36+{
37+ QStringList importPathList = engine->importPathList();
38+ for (int i = paths.count() -1; i >= 0; i--) {
39+ // don't duplicate
40+ const QString& path = paths[i];
41+ QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
42+ if (iter == importPathList.end())
43+ importPathList.prepend(path);
44+ }
45+ engine->setImportPathList(importPathList);
46+}
47+
3448/* When you append and import path to the list of import paths it will be the *last*
3549 place where Qt will search for QML modules.
3650 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
37−void appendImportPath(QQmlEngine *engine, const QString &path)
51+void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
3852{
3953 QStringList importPathList = engine->importPathList();
40− importPathList.append(path);
54+ Q_FOREACH(const QString& path, paths) {
55+ // don't duplicate
56+ QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
57+ if (iter == importPathList.end())
58+ importPathList.append(path);
59+ }
60+ importPathList.append(paths);
4161 engine->setImportPathList(importPathList);
4262}
4363
@@ -88,7 +108,7 @@ int main(int argc, char** argv)
88108 QQuickView* view = new QQuickView();
89109 view->setResizeMode(QQuickView::SizeRootObjectToView);
90110 view->setTitle("Qml Phone Shell");
91− view->engine()->setBaseUrl(QUrl::fromLocalFile(shellAppDirectory()));
111+ view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
92112 if (args.contains(QLatin1String("-frameless"))) {
93113 view->setFlags(Qt::FramelessWindowHint);
94114 }
@@ -114,9 +134,9 @@ int main(int argc, char** argv)
114134 QObject::connect(view->engine(), SIGNAL(quit()), qApp, SLOT(quit()));
115135
116136 QUrl source("Shell.qml");
117− view->engine()->addImportPath(shellAppDirectory());
118− view->engine()->addImportPath(shellImportPath());
119− appendImportPath(view->engine(), fakePluginsImportPath());
137+ prependImportPaths(view->engine(), QStringList() << ::shellAppDirectory());
138+ prependImportPaths(view->engine(), ::shellImportPaths());
139+ appendImportPaths(view->engine(), QStringList() << ::fakePluginsImportPath());
120140 view->setSource(source);
121141 view->setColor("transparent");
122142
paths.h.in+10−3
@@ -17,6 +17,9 @@
1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20+#ifndef PATHS_H
21+#define PATHS_H
22+
2023// Qt
2124#include <QtCore/QCoreApplication>
2225#include <QtCore/QDir>
@@ -43,12 +46,14 @@ inline QString shellAppDirectory() {
4346 }
4447}
4548
46−inline QString shellImportPath() {
49+inline QStringList shellImportPaths() {
50+ QStringList paths;
4751 if (isRunningInstalled()) {
48− return QString("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/plugins");
52+ paths << QString("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/plugins");
4953 } else {
50− return QString("@CMAKE_BINARY_DIR@/plugins/");
54+ paths << QString("@CMAKE_BINARY_DIR@/plugins");
5155 }
56+ return paths;
5257}
5358
5459inline QString fakePluginsImportPath() {
@@ -62,3 +67,5 @@ inline QString fakePluginsImportPath() {
6267inline QString sourceDirectory() {
6368 return QString("@CMAKE_SOURCE_DIR@/");
6469}
70+
71+#endif