5e822f74c18285028ae4f2a591b9bd96039dde24
[apps/onscreenapp.git] / sample / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
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 <QUrlQuery> 
18 #include <QQmlContext>
19 #include <QtCore/QDebug>
20 #include <QtCore/QCommandLineParser>
21 #include <QtCore/QUrlQuery>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <QtQuickControls2/QQuickStyle>
26 #include <QtQuick/QQuickWindow>
27
28 #include "eventhandler.h"
29
30 static EventHandler* eventHandler;
31
32 int main(int argc, char *argv[])
33 {
34     qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
35     qputenv("QT_LOGGING_RULES", QByteArray("qt.virtualkeyboard=true"));
36     QGuiApplication app(argc, argv);
37     app.setApplicationName("onstestapp");
38     app.setApplicationVersion(QStringLiteral("3.99.3"));
39     app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
40     app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
41
42     //QQuickStyle::setStyle("AGL");
43
44     QCommandLineParser parser;
45     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
46     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
47     parser.addHelpOption();
48     parser.addVersionOption();
49     parser.process(app);
50     QStringList positionalArguments = parser.positionalArguments();
51
52     QQmlApplicationEngine engine;
53     QQmlContext *context = engine.rootContext();
54     QUrl bindingAddress;
55     int port = 0;
56     QString secret;
57     if (positionalArguments.length() == 2) {
58         port = positionalArguments.takeFirst().toInt();
59         secret = positionalArguments.takeFirst();
60         bindingAddress.setScheme(QStringLiteral("ws"));
61         bindingAddress.setHost(QStringLiteral("localhost"));
62         bindingAddress.setPort(port);
63         bindingAddress.setPath(QStringLiteral("/api"));
64         QUrlQuery query;
65         query.addQueryItem(QStringLiteral("token"), secret);
66         bindingAddress.setQuery(query);
67         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
68     } else {
69         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
70     }
71
72     eventHandler = new EventHandler();
73     eventHandler->init(port, secret.toStdString().c_str());
74     engine.rootContext()->setContextProperty("eventHandler", eventHandler);
75
76     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
77     QObject *root = engine.rootObjects().first();
78     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
79
80     QObject::connect(eventHandler, SIGNAL(signalOnReplyShowWindow(QVariant)), window, SLOT(qmlOnReplyShowWindow(QVariant)));
81     eventHandler->setQuickWindow(window);
82
83     return app.exec();
84 }