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