UIname added
[mardrone] / mardrone / main.cpp
1 /*==================================================================
2   !
3   !  mardrone application AR-Drone for MeeGo
4
5   ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6   ! All rights reserved.
7   !
8   !  Author:Kate Alhola  kate.alhola@nokia.com
9   !
10   ! GNU Lesser General Public License Usage
11   ! This file may be used under the terms of the GNU Lesser
12   ! General Public License version 2.1 as published by the Free Software
13   ! Foundation and appearing in the file LICENSE.LGPL included in the
14   ! packaging of this file.  Please review the following information to
15   ! ensure the GNU Lesser General Public License version 2.1 requirements
16   ! will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17   !
18   !
19   !
20   *===================================================================*/
21 #include <QtGui/QApplication>
22 #include "dronecontrol.h"
23 #include "video.h"
24 #include "gaugehorizon.h"
25 #include "gaugetape.h"
26 #include "gaugelabel.h"
27 #include "uilistelement.h"
28 #include <QDeclarativeView>
29 #include <QDeclarativeContext>
30 #include <QDeclarativeEngine>
31 #include <QtDeclarative>
32 #include <gaugearc.h>
33 #include <gaugehsi.h>
34 #include <QSettings>
35 #include "joystick.h"
36 #include "qdeclarativetoucharea.h"
37 #include <QList>
38
39 QString selectQmlMain(QStringList inportPaths,QString cmdParam,QString activeUI);
40
41 // MacOSX sdl hack so that SDL won't hijack main
42 #undef main
43
44 #ifdef QT_IOS
45 int Qt_main(int argc, char *argv[])
46 #else
47 int main(int argc, char *argv[])
48 #endif
49 {
50     qmlRegisterType<DroneControl>("Drone", 1, 0, "DroneControl");
51     qmlRegisterType<DroneVideo>("Drone", 1, 0, "DroneVideo");
52     qmlRegisterType<GaugeTape>("Gauges", 1, 0, "GaugeTape");
53     qmlRegisterType<GaugeLabel>("Gauges", 1, 0, "GaugeLabel");
54     qmlRegisterType<GaugeHorizon>("Gauges", 1, 0, "GaugeHorizon");
55     qmlRegisterType<GaugeArc>("Gauges", 1, 0, "GaugeArc");
56     qmlRegisterType<GaugeHSI>("Gauges", 1, 0, "GaugeHSI");
57     qmlRegisterType<JoyStick>("JoyStick", 1, 0, "JoyStick");
58     qmlRegisterType<QDeclarativeTouchArea>("TouchArea", 1, 0, "TouchArea");
59
60     QApplication app(argc, argv);
61     QUrl *url;
62     uiListModel uiList;
63     bool setDefault=false;
64
65     uiList.addUI(new uiListElement("MeeGo","ardrone_harmattan.qml"));
66     uiList.addUI(new uiListElement("Symbian","ardrone_symbian.qml"));
67     uiList.addUI(new uiListElement("Desktop","ardrone_desktop.qml"));
68     uiList.addUI(new uiListElement("PlainQml","ardrone_plainqml.qml"));
69
70
71     QSettings *mardroneSettings=new QSettings("katix.org","mardrone");
72 #ifdef QT_IOS
73     QString activeUi=mardroneSettings->value("activeUI","ardrone_plainqml.qml").toString();
74 #else
75     QString activeUi=mardroneSettings->value("activeUI","ardrone_harmattan.qml").toString();
76 //    QString activeUi=mardroneSettings->value("activeUI","ardrone_desktop.qml").toString();
77 #endif
78     qDebug() << "activeUI=" << activeUi;
79     QString qmlmainfile=app.arguments().size()>=2 ? app.arguments()[1]:"";
80     if(app.arguments().size()>=3 && qmlmainfile=="set") {
81         qmlmainfile=app.arguments()[2];
82         setDefault=true;
83     };
84
85     QDeclarativeView window;
86     window.rootContext()->setContextProperty("MArdrone",window.window());
87     QObject::connect((QObject*)window.engine(), SIGNAL(quit()), &app, SLOT(quit()));
88     // Check available UI's
89     window.engine()->addImportPath("qrc:/gauges/");
90     window.engine()->addImportPath("qrc:");
91     window.engine()->addImportPath("gauges");
92     window.engine()->addImportPath(".");
93 #ifdef Q_OS_ANDROID
94     window.engine()->addImportPath("/imports/");
95     window.engine()->addPluginPath(QDir::homePath()+"/../lib");
96 #endif
97     qDebug() << "ImportPathList" << window.engine()->importPathList() ;
98     QStringList importPath=window.engine()->importPathList();
99     window.rootContext()->setContextProperty("uiList",&uiList);
100     QString qmlFile=selectQmlMain(importPath,qmlmainfile,activeUi);
101     if(setDefault) mardroneSettings->setValue("activeUI",qmlFile);
102 #ifndef QT_IOS
103     qmlFile=QString("qrc:/")+qmlFile;
104 #endif
105
106     qDebug() << "using:" << qmlFile;
107
108     if(qmlFile.contains("desktop")) { // We have desktop UI
109         QDeclarativeComponent component(window.engine(),QUrl(qmlFile));
110         if(component.isError()) qDebug() << component.errors(); else
111             QObject *myObject = component.create();
112     } else { // We have Mobile UI
113         window.setSource(QUrl(qmlFile));
114         QRect screenGeometry=QApplication::desktop()->screenGeometry();
115         window.setAttribute(Qt::WA_LockLandscapeOrientation, true);
116         if((screenGeometry.height()<=800) && (screenGeometry.width()<=1024)) window.window()->showFullScreen();
117         else window.window()->show();
118     }
119     return app.exec();
120 }
121
122 QString selectQmlMain(QStringList importPaths,QString cmdParam,QString activeUI)
123 {
124   QString importPath=importPaths.last();
125   if(cmdParam.contains(".qml")) return cmdParam;
126   if(cmdParam.isEmpty() && !activeUI.isEmpty()) return activeUI;
127   if(cmdParam=="harmattan" || (cmdParam=="" && QFile(importPath+"/com/nokia/meego").exists())) return QString("ardrone_harmattan.qml");
128   if(cmdParam=="symbian" || (cmdParam=="" && QFile(importPath+"/MeeGo/Components").exists())) return QString("ardrone_symbian.qml");
129   if(cmdParam=="desktop" || (cmdParam=="" && QFile(importPath+"/QtDesktop").exists())) return QString("ardrone_desktop.qml");
130    if(cmdParam=="plain") return QString("ardrone_plainqml.qml");
131   return(activeUI);
132 }