improved desktop UI
[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 #include <QObject>
39 #include "qmlappviewer.h"
40
41 QString selectQmlMain(QStringList inportPaths,QString cmdParam,QString activeUI,uiListModel &uiList);
42
43
44
45
46 // MacOSX sdl hack so that SDL won't hijack main
47 #undef main
48
49 #ifdef QT_IOS
50 int Qt_main(int argc, char *argv[])
51 #else
52 int main(int argc, char *argv[])
53 #endif
54 {
55     qmlRegisterType<DroneControl>("Drone", 1, 0, "DroneControl");
56     qmlRegisterType<DroneVideo>("Drone", 1, 0, "DroneVideo");
57     qmlRegisterType<GaugeTape>("Gauges", 1, 0, "GaugeTape");
58     qmlRegisterType<GaugeLabel>("Gauges", 1, 0, "GaugeLabel");
59     qmlRegisterType<GaugeHorizon>("Gauges", 1, 0, "GaugeHorizon");
60     qmlRegisterType<GaugeArc>("Gauges", 1, 0, "GaugeArc");
61     qmlRegisterType<GaugeHSI>("Gauges", 1, 0, "GaugeHSI");
62     qmlRegisterType<JoyStick>("JoyStick", 1, 0, "JoyStick");
63     qmlRegisterType<QDeclarativeTouchArea>("TouchArea", 1, 0, "TouchArea");
64
65     QApplication app(argc, argv);
66     QUrl *url;
67     uiListModel uiList;
68     bool setDefault=false;
69
70     QSettings *mardroneSettings=new QSettings("katix.org","mardrone");
71 #ifdef QT_IOS
72     QString activeUi=mardroneSettings->value("activeUI","ardrone_plainqml.qml").toString();
73 #else
74     QString activeUi=mardroneSettings->value("activeUI","ardrone_harmattan.qml").toString();
75 //    QString activeUi=mardroneSettings->value("activeUI","ardrone_desktop.qml").toString();
76 #endif
77     qDebug() << "activeUI=" << activeUi;
78     QString qmlmainfile=app.arguments().size()>=2 ? app.arguments()[1]:"";
79     if(app.arguments().size()>=3 && qmlmainfile=="set") {
80         qmlmainfile=app.arguments()[2];
81         setDefault=true;
82     };
83
84  //   QDeclarativeView window;
85     QmlAppViewer window;
86     window.rootContext()->setContextProperty("MArdrone",window.window());
87     QObject::connect((QObject*)window.engine(), SIGNAL(quit()), &app, SLOT(quit()));
88
89     // Check available UI's
90     window.engine()->addImportPath(":/gauges/");
91     window.engine()->addImportPath("qrc:");
92     window.engine()->addImportPath(":");
93     window.engine()->addImportPath("gauges");
94     window.engine()->addImportPath(".");
95 #ifdef Q_OS_ANDROID
96     window.engine()->addImportPath("/");
97     window.engine()->addImportPath("/imports");
98     window.engine()->addImportPath("/assets");
99     window.engine()->addPluginPath(QDir::homePath()+"/../lib");
100 #endif
101     qDebug() << "ImportPathList" << window.engine()->importPathList() ;
102     QStringList importPath=window.engine()->importPathList();
103     window.rootContext()->setContextProperty("uiList",&uiList);
104     QString qmlFile=selectQmlMain(importPath,qmlmainfile,activeUi,uiList);
105     if(setDefault) mardroneSettings->setValue("activeUI",qmlFile);
106 #ifndef QT_IOS
107     qmlFile=QString("qrc:/")+qmlFile;
108 #endif
109
110     qDebug() << "using:" << qmlFile;
111
112     if(qmlFile.contains("desktop")) { // We have desktop UI
113         QDeclarativeComponent component(window.engine(),QUrl(qmlFile));
114         if(component.isError()) qDebug() << component.errors(); else
115             QObject *myObject = component.create();
116     } else { // We have Mobile UI
117         window.setSource(QUrl(qmlFile));
118         QRect screenGeometry=QApplication::desktop()->screenGeometry();
119         window.setAttribute(Qt::WA_LockLandscapeOrientation, true);
120         if((screenGeometry.height()<=800) && (screenGeometry.width()<=1024)) window.window()->showFullScreen();
121         else window.window()->show();
122     }
123     QObject::connect(&window, SIGNAL(backKey()), window.rootObject(), SLOT(backKey()));
124     return app.exec();
125 }
126
127 QString selectQmlMain(QStringList importPaths,QString cmdParam,QString activeUI,uiListModel &uiList)
128 {
129   bool haveHarmattan=FALSE;
130   bool haveSymbian=FALSE;
131   bool haveDesktop=FALSE;
132   bool haveAndroid=FALSE;
133   QStringList appQmlFilesFilter;
134   QString uiName;
135   appQmlFilesFilter << "ardrone*.qml";
136   QStringList appQmlFiles;
137   qDebug() << "selectQmlMain importPath=" << importPaths << " ActiveUi=" << activeUI;
138   if(cmdParam.contains(".qml")) return cmdParam;
139   if(cmdParam=="plain") activeUI="ardrone_plainqml.qml";
140   if(cmdParam=="harmattan") activeUI="ardrone_harmattan.qml";
141   if(cmdParam=="symbian") activeUI="ardrone_symbian.qml";
142   if(cmdParam=="desktop") activeUI="ardrone_desktop.qml";
143   // Scan supported components
144   for(int i=0;i<importPaths.size();i++) {
145     QString importPath=importPaths[i];
146     QDir searchDir(importPath);
147     QStringList qmlFiles=searchDir.entryList();
148     qDebug() << importPath  << qmlFiles;
149     haveHarmattan|=QFile(importPath+"/com/nokia/meego/qmldir").exists();
150     haveSymbian|=QFile(importPath+"/com/nokia/symbian.1.1/qmldir").exists();
151     haveAndroid|=QFile(importPath+"/com/nokia/android.1.1/qmldir").exists();
152     haveDesktop|=QFile(importPath+"/QtDesktop/qmldir").exists();
153     qDebug() << importPath << haveHarmattan << haveSymbian << haveDesktop << haveAndroid ;
154   }
155   // Scan UI .qml files
156   for(int i=0;i<importPaths.size();i++) {
157     QString importPath=importPaths[i];
158     QDir searchDir(importPath);
159     QStringList qmlFiles=searchDir.entryList(appQmlFilesFilter);
160     qDebug() << qmlFiles;
161     appQmlFiles+=qmlFiles;
162     for(int j=0;j<qmlFiles.size();j++) {
163         if((qmlFiles[j].contains("harmattan") && haveHarmattan)||
164            (qmlFiles[j].contains("symbian") && haveSymbian) ||
165            (qmlFiles[j].contains("android") && haveAndroid) ||
166            (qmlFiles[j].contains("desktop") && haveDesktop) ||
167            (qmlFiles[j].contains("plainqml")))
168         {
169             QFile qmlFile(importPath+"/"+qmlFiles[j]);
170             if(qmlFile.open(QFile::ReadOnly))
171             {
172                 QString firstLine=QString(qmlFile.readLine(100));
173                 if(firstLine.contains("// uiName:")) { uiName=firstLine.remove(0,10); }
174                 else uiName=qmlFiles[j];
175                 if(uiName.endsWith("\n")) uiName.chop(1);
176                 qDebug() << importPath+"/"+qmlFiles[j] << uiName;
177                 uiList.addUI(new uiListElement(uiName,qmlFiles[j]));
178             }
179         }
180       }
181     }
182     // Check validity of activeUi
183     bool validUi=false;
184     for(int j=0;j<uiList.count();j++) validUi|=(((uiListElement*)uiList.get(j))->file())==activeUI;
185     if(!validUi) activeUI=((uiListElement*)uiList.get(0))->file();
186   return(activeUI);
187 }
188