onscreenapp: Initial clean-up in code
[apps/onscreenapp.git] / 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 <QGuiApplication>
18 #include <QCommandLineParser>
19 #include <QtGui/QGuiApplication>
20 #include <QtQml/QQmlApplicationEngine>
21 #include <QtQml/QQmlContext>
22 #include <QtQml/qqml.h>
23 #include <QQuickWindow>
24 #include <QtQuickControls2/QQuickStyle>
25
26 #include "eventhandler.h"
27 #include "onscreenmodel.h"
28
29
30 int main(int argc, char *argv[])
31 {
32     QGuiApplication app(argc, argv);
33
34 #if 0
35     QCoreApplication::setOrganizationDomain("LinuxFoundation");
36     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
37     QCoreApplication::setApplicationName("Onscreenapp");
38     QCoreApplication::setApplicationVersion("0.1.0");
39 #endif
40
41     // this is necessary to identify app, setApplicationName is only for the
42     // title
43     app.setDesktopFileName(APP_ID);
44
45     //QQuickStyle::setStyle("AGL");
46
47     QCommandLineParser parser;
48     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
49     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
50     parser.addHelpOption();
51     parser.addVersionOption();
52     parser.process(app);
53     QStringList positionalArguments = parser.positionalArguments();
54
55     int port = 1700;
56     QString token = "wm";
57
58     if (positionalArguments.length() == 2) {
59         port = positionalArguments.takeFirst().toInt();
60         token = positionalArguments.takeFirst();
61     }
62
63     HMI_DEBUG(APP_ID, "port = %d, token = %s", port, token.toStdString().c_str());
64
65     QQmlApplicationEngine engine;
66     EventHandler *eventHandler = new EventHandler();
67     eventHandler->init(port, token.toStdString().c_str());
68     engine.rootContext()->setContextProperty("eventHandler", eventHandler);
69     qmlRegisterType<OnScreenModel>("OnScreenModel", 1, 0, "OnScreenModel");
70     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
71     if (engine.rootObjects().isEmpty()) {
72         HMI_DEBUG(APP_ID, "Fatal Error, rootObject is empty!");
73         return -1;
74     }
75
76     QObject *root = engine.rootObjects().first();
77     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
78
79     QObject::connect(eventHandler, SIGNAL(updateModel(QVariant)), window, SLOT(setOnScreenModel(QVariant)));
80     QObject::connect(eventHandler, SIGNAL(showOnScreen()), window, SLOT(showOnScreen()));
81     QObject::connect(eventHandler, SIGNAL(hideOnScreen()), window, SLOT(hideOnScreen()));
82
83     HMI_DEBUG(APP_ID, "onscreenapp started!");
84     return app.exec();
85 }
86