Add onscreenapp
[apps/onscreenapp.git] / sample / app / main.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <QQmlContext>
18 #include <QtCore/QCommandLineParser>
19 #include <QtGui/QGuiApplication>
20 #include <QtQml/QQmlContext>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQuickControls2/QQuickStyle>
23 #include <QtQuick/QQuickWindow>
24
25 #include "eventhandler.h"
26
27
28 int main(int argc, char *argv[])
29 {
30     QGuiApplication app(argc, argv);
31     app.setApplicationName("onstestapp");
32     app.setApplicationVersion(QStringLiteral("3.99.3"));
33     app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
34     app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
35
36     QQuickStyle::setStyle("AGL");
37
38     QCommandLineParser parser;
39     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
40     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
41     parser.addHelpOption();
42     parser.addVersionOption();
43     parser.process(app);
44     QStringList positionalArguments = parser.positionalArguments();
45
46     QQmlApplicationEngine engine;
47     int port = 0;
48     QString secret;
49     if (positionalArguments.length() == 2) {
50         port = positionalArguments.takeFirst().toInt();
51         secret = positionalArguments.takeFirst();
52     }
53
54     EventHandler *eventHandler = new EventHandler();
55     eventHandler->init(port, secret.toStdString().c_str());
56     engine.rootContext()->setContextProperty("eventHandler", eventHandler);
57
58     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
59     if (engine.rootObjects().isEmpty()) {
60         HMI_DEBUG(APP_ID, "Fatal Error, rootObject is empty!");
61         return -1;
62     }
63
64     QObject *root = engine.rootObjects().first();
65     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
66     QObject::connect(eventHandler, SIGNAL(signalOnReplyShowWindow(QVariant)), window, SLOT(qmlOnReplyShowWindow(QVariant)));
67     eventHandler->setQuickWindow(window);
68
69     return app.exec();
70 }