Cync
[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,uiListModel &uiList,QStringList &uiNames,QStringList &uiFiles);
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     QStringList uiNames,uiFiles;
65
66     QSettings *mardroneSettings=new QSettings("katix.org","mardrone");
67 #ifdef QT_IOS
68     QString activeUi=mardroneSettings->value("activeUI","ardrone_plainqml.qml").toString();
69 #else
70     QString activeUi=mardroneSettings->value("activeUI","ardrone_harmattan.qml").toString();
71 //    QString activeUi=mardroneSettings->value("activeUI","ardrone_desktop.qml").toString();
72 #endif
73     qDebug() << "activeUI=" << activeUi;
74     QString qmlmainfile=app.arguments().size()>=2 ? app.arguments()[1]:"";
75     if(app.arguments().size()>=3 && qmlmainfile=="set") {
76         qmlmainfile=app.arguments()[2];
77         setDefault=true;
78     };
79
80     QDeclarativeView window;
81     window.rootContext()->setContextProperty("MArdrone",window.window());
82     QObject::connect((QObject*)window.engine(), SIGNAL(quit()), &app, SLOT(quit()));
83     // Check available UI's
84     window.engine()->addImportPath("qrc:/gauges/");
85     window.engine()->addImportPath("qrc:");
86     window.engine()->addImportPath(":");
87     window.engine()->addImportPath("gauges");
88     window.engine()->addImportPath(".");
89 #ifdef Q_OS_ANDROID
90     window.engine()->addImportPath("/imports/");
91     window.engine()->addPluginPath(QDir::homePath()+"/../lib");
92 #endif
93     qDebug() << "ImportPathList" << window.engine()->importPathList() ;
94     QStringList importPath=window.engine()->importPathList();
95     window.rootContext()->setContextProperty("uiList",&uiList);
96     window.rootContext()->setContextProperty("uiNames",uiNames);
97     window.rootContext()->setContextProperty("uiFiles",uiFiles);
98     QString qmlFile=selectQmlMain(importPath,qmlmainfile,activeUi,uiList,uiNames,uiFiles);
99     if(setDefault) mardroneSettings->setValue("activeUI",qmlFile);
100 #ifndef QT_IOS
101     qmlFile=QString("qrc:/")+qmlFile;
102 #endif
103
104     qDebug() << "using:" << qmlFile;
105
106     if(qmlFile.contains("desktop")) { // We have desktop UI
107         QDeclarativeComponent component(window.engine(),QUrl(qmlFile));
108         if(component.isError()) qDebug() << component.errors(); else
109             QObject *myObject = component.create();
110     } else { // We have Mobile UI
111         window.setSource(QUrl(qmlFile));
112         QRect screenGeometry=QApplication::desktop()->screenGeometry();
113         window.setAttribute(Qt::WA_LockLandscapeOrientation, true);
114         if((screenGeometry.height()<=800) && (screenGeometry.width()<=1024)) window.window()->showFullScreen();
115         else window.window()->show();
116     }
117     return app.exec();
118 }
119
120 QString selectQmlMain(QStringList importPaths,QString cmdParam,QString activeUI,uiListModel &uiList,QStringList &uiNames,QStringList &uiFiles)
121 {
122   bool haveHarmattan=FALSE;
123   bool haveSymbian=FALSE;
124   bool haveDesktop=FALSE;
125   QStringList appQmlFilesFilter;
126   QString uiName;
127   appQmlFilesFilter << "ardrone*.qml";
128   QStringList appQmlFiles;
129   qDebug() << "selectQmlMain importPath=" << importPaths << " ActiveUi=" << activeUI;
130   if(cmdParam.contains(".qml")) return cmdParam;
131   if(cmdParam=="plain") activeUI="ardrone_plainqml.qml";
132   if(cmdParam=="harmattan") activeUI="ardrone_harmattan.qml";
133   if(cmdParam=="symbian") activeUI="ardrone_symbian.qml";
134   if(cmdParam=="desktop") activeUI="ardrone_desktop.qml";
135   // Scan supported components
136   for(int i=0;i<importPaths.size();i++) {
137     QString importPath=importPaths[i];
138     haveHarmattan|=QFile(importPath+"/com/nokia/meego").exists();
139     haveSymbian|=QFile(importPath+"/com/nokia/symbian.1.1").exists();
140     haveDesktop|=QFile(importPath+"/QtDesktop").exists();
141     qDebug() << importPath << haveHarmattan << haveSymbian << haveDesktop;
142   }
143   // Scan UI .qml files
144   for(int i=0;i<importPaths.size();i++) {
145     QString importPath=importPaths[i];
146     QDir searchDir(importPath);
147     QStringList qmlFiles=searchDir.entryList(appQmlFilesFilter);
148     qDebug() << qmlFiles;
149     appQmlFiles+=qmlFiles;
150     for(int j=0;j<qmlFiles.size();j++) {
151         if((qmlFiles[j].contains("harmattan") && haveHarmattan)||
152            (qmlFiles[j].contains("symbian") && haveSymbian) ||
153            (qmlFiles[j].contains("desktop") && haveDesktop) ||
154            (qmlFiles[j].contains("plainqml")))
155         {
156             QFile qmlFile(importPath+"/"+qmlFiles[j]);
157             if(qmlFile.open(QFile::ReadOnly))
158             {
159                 QString firstLine=QString(qmlFile.readLine(100));
160                 if(firstLine.contains("// uiName:")) { uiName=firstLine.remove(0,10); }
161                 else uiName=qmlFiles[j];
162                 if(uiName.endsWith("\n")) uiName.chop(1);
163                 qDebug() << importPath+"/"+qmlFiles[j] << uiName;
164                 uiList.addUI(new uiListElement(uiName,qmlFiles[j]));
165             }
166         }
167       }
168     }
169     // Check validity of activeUi
170     bool validUi=false;
171     for(int j=0;j<uiList.count();j++) validUi|=(((uiListElement*)uiList.get(j))->file())==activeUI;
172     if(!validUi) activeUI=((uiListElement*)uiList.get(0))->file();
173   return(activeUI);
174 }