KDeclarative

main.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include <QApplication>
8 
9 #include <KLocalizedString>
10 #include <QCommandLineParser>
11 #include <QCommandLineOption>
12 #include <QQuickItem>
13 
14 #include <kpackage/package.h>
15 #include <kpackage/packageloader.h>
16 #include <QQmlContext>
17 #include <QQmlEngine>
18 #include <QQmlExpression>
19 #include <QQmlProperty>
20 #include <QQuickWindow>
21 #include <QStandardPaths>
22 #include <kdeclarative/qmlobject.h>
23 #include <KAboutData>
24 
25 int main(int argc, char **argv)
26 {
27  QCommandLineParser parser;
28  QApplication app(argc, argv);
29 
30  const QString description = i18n("KPackage QML application shell");
31 
32  app.setApplicationVersion(QStringLiteral("0.1"));
33  parser.addVersionOption();
34  parser.addHelpOption();
35  parser.setApplicationDescription(description);
36 
37  QCommandLineOption appPluginOption(QCommandLineOption(QStringList() << QStringLiteral("a") << QStringLiteral("app"), i18n("The unique name of the application (mandatory)"), QStringLiteral("app")));
38 
39  parser.addOption(appPluginOption);
40 
41  parser.process(app);
42 
43  QString packagePath;
44  if (parser.isSet(appPluginOption)) {
45  packagePath = parser.value(appPluginOption);
46  } else {
47  parser.showHelp(1);
48  }
49 
50  //usually we have an ApplicationWindow here, so we do not need to create a window by ourselves
52  obj.setTranslationDomain(packagePath);
53  obj.setInitializationDelayed(true);
54  obj.loadPackage(packagePath);
55  if (!obj.package().isValid()) {
56  qWarning() << "The specified Application package is not valid.";
57  return 1;
58  }
59  obj.engine()->rootContext()->setContextProperty(QStringLiteral("commandlineArguments"), parser.positionalArguments());
60  QVariantMap paths;
61  paths[QStringLiteral("desktop")] = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
62  paths[QStringLiteral("documents")] = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
63  paths[QStringLiteral("music")] = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
64  paths[QStringLiteral("movies")] = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
65  paths[QStringLiteral("pictures")] = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
66  paths[QStringLiteral("home")] = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
67  obj.engine()->rootContext()->setContextProperty(QStringLiteral("userPaths"), paths);
69 
70  if (!obj.package().metadata().isValid()) {
71  return -1;
72  }
73 
74  KPluginMetaData data = obj.package().metadata();
75  // About data
76  KAboutData aboutData(data.pluginId(), data.name(), data.version(), data.description(), KAboutLicense::byKeyword(data.license()).key());
77 
78  const auto authors = data.authors();
79  for (auto& author : authors) {
80  aboutData.addAuthor(author.name(), author.task(), author.emailAddress(), author.webAddress(), author.ocsUsername());
81  }
82  // We assume that desktop file in applications dir is named similar as
83  // the pluginId, this is required to get icons working on wayland for example
84  aboutData.setDesktopFileName(data.pluginId());
85 
87 
88  //The root is not a window?
89  //have to use a normal QQuickWindow since the root item is already created
90  QQuickItem *item = qobject_cast<QQuickItem *>(obj.rootObject());
91  QWindow *window = qobject_cast<QWindow *>(obj.rootObject());
92  if (item) {
93  QQuickWindow view;
94  item->setParentItem(view.contentItem());
95  if (item->implicitWidth() > 0 && item->implicitHeight() > 0) {
96  view.resize(item->implicitWidth(), item->implicitHeight());
97  } else {
98  view.resize(item->width(), item->height());
99  }
100 
101  //set anchors
102  QQmlExpression expr(obj.engine()->rootContext(), obj.rootObject(), QStringLiteral("parent"));
103  QQmlProperty prop(obj.rootObject(), QStringLiteral("anchors.fill"));
104  prop.write(expr.evaluate());
105  view.setTitle(obj.package().metadata().name());
107 
108  // Forward Qt.quit() in QML code to QApplication::quit()
110 
111  view.show();
112  return app.exec();
113  } else if (window) {
114  window->setTitle(obj.package().metadata().name());
115  window->setIcon(QIcon::fromTheme(obj.package().metadata().iconName()));
116  } else {
117  qWarning() << "The root Qml Item should be either a kind of window or a QQuickItem";
118  return 1;
119  }
120 
121  return app.exec();
122 }
123 
void loadPackage(const QString &packageName)
Load the package called packageName, then loads the mainscript file for that package.
Definition: qmlobject.cpp:212
QString description() const
QStringList positionalArguments() const const
QList< KAboutPerson > authors() const
void setParentItem(QQuickItem *parent)
QString writableLocation(QStandardPaths::StandardLocation type)
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
QString name() const
KPackage::Package package() const
Definition: qmlobject.cpp:225
An object that instantiates an entire QML context, with its own declarative engine.
Definition: qmlobject.h:38
QCommandLineOption addVersionOption()
QQmlEngine * engine()
Definition: qmlobject.cpp:240
void show()
QString iconName() const
QCommandLineOption addHelpOption()
void setApplicationDescription(const QString &description)
void setContextProperty(const QString &name, QObject *value)
QQmlContext * rootContext() const const
bool isSet(const QString &name) const const
void setTitle(const QString &)
static void setApplicationData(const KAboutData &aboutData)
bool write(const QVariant &value) const const
void setInitializationDelayed(const bool delay)
Sets whether the execution of the QML file has to be delayed later in the event loop.
Definition: qmlobject.cpp:230
void process(const QStringList &arguments)
QString license() const
QString i18n(const char *text, const TYPE &arg...)
KPluginMetaData metadata() const
void showHelp(int exitCode)
static KAboutLicense byKeyword(const QString &keyword)
void resize(const QSize &newSize)
bool isValid() const
bool addOption(const QCommandLineOption &option)
QIcon fromTheme(const QString &name)
void setTranslationDomain(const QString &translationDomain)
Call this method before calling setupBindings to install a translation domain for all i18n global fun...
Definition: qmlobject.cpp:191
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void completeInitialization(const QVariantHash &initialProperties=QVariantHash())
Finishes the process of initialization.
Definition: qmlobject.cpp:291
QString value(const QString &optionName) const const
bool isValid() const
QString pluginId() const
void quit()
QString version() const
void setIcon(const QIcon &icon)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.