Version 0.7-0
[vicar] / src / vicar-config-qml / qmlapplicationviewer / qmlapplicationviewer.cpp
1 // checksum 0x78c version 0x60010
2 /*
3   This file was generated by the Qt Quick Application wizard of Qt Creator.
4   QmlApplicationViewer is a convenience class containing mobile device specific
5   code such as screen orientation handling. Also QML paths and debugging are
6   handled here.
7   It is recommended not to modify this file, since newer versions of Qt Creator
8   may offer an updated version of it.
9 */
10
11 #include "qmlapplicationviewer.h"
12
13 #include <QtCore/QDir>
14 #include <QtCore/QFileInfo>
15 #include <QtDeclarative/QDeclarativeComponent>
16 #include <QtDeclarative/QDeclarativeEngine>
17 #include <QtDeclarative/QDeclarativeContext>
18 #include <QtGui/QApplication>
19
20 #include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
21
22 #ifdef HARMATTAN_BOOSTER
23 #include <MDeclarativeCache>
24 #endif
25
26 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
27
28 #include <qt_private/qdeclarativedebughelper_p.h>
29
30 #if !defined(NO_JSDEBUGGER)
31 #include <jsdebuggeragent.h>
32 #endif
33 #if !defined(NO_QMLOBSERVER)
34 #include <qdeclarativeviewobserver.h>
35 #endif
36
37 // Enable debugging before any QDeclarativeEngine is created
38 struct QmlJsDebuggingEnabler
39 {
40     QmlJsDebuggingEnabler()
41     {
42         QDeclarativeDebugHelper::enableDebugging();
43     }
44 };
45
46 // Execute code in constructor before first QDeclarativeEngine is instantiated
47 static QmlJsDebuggingEnabler enableDebuggingHelper;
48
49 #endif // QMLJSDEBUGGER
50
51 class QmlApplicationViewerPrivate
52 {
53     QmlApplicationViewerPrivate(QDeclarativeView *view_) : view(view_) {}
54
55     QString mainQmlFile;
56     QDeclarativeView *view;
57     friend class QmlApplicationViewer;
58     QString adjustPath(const QString &path);
59 };
60
61 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
62 {
63 #ifdef Q_OS_UNIX
64 #ifdef Q_OS_MAC
65     if (!QDir::isAbsolutePath(path))
66         return QCoreApplication::applicationDirPath()
67                 + QLatin1String("/../Resources/") + path;
68 #else
69     QString pathInInstallDir;
70     const QString applicationDirPath = QCoreApplication::applicationDirPath();
71     pathInInstallDir = QString::fromAscii("%1/../%2").arg(applicationDirPath, path);
72
73     if (QFileInfo(pathInInstallDir).exists())
74         return pathInInstallDir;
75 #endif
76 #endif
77     return path;
78 }
79
80 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent)
81     : QDeclarativeView(parent)
82     , d(new QmlApplicationViewerPrivate(this))
83 {
84     connect(engine(), SIGNAL(quit()), SLOT(close()));
85     setResizeMode(QDeclarativeView::SizeRootObjectToView);
86     // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
87 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
88 #if !defined(NO_JSDEBUGGER)
89     new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
90 #endif
91 #if !defined(NO_QMLOBSERVER)
92     new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
93 #endif
94 #endif
95 }
96
97 QmlApplicationViewer::QmlApplicationViewer(QDeclarativeView *view, QWidget *parent)
98     : QDeclarativeView(parent)
99     , d(new QmlApplicationViewerPrivate(view))
100 {
101     connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
102     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
103     // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
104 #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
105 #if !defined(NO_JSDEBUGGER)
106     new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
107 #endif
108 #if !defined(NO_QMLOBSERVER)
109     new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
110 #endif
111 #endif
112 }
113
114 QmlApplicationViewer::~QmlApplicationViewer()
115 {
116     delete d;
117 }
118
119 QmlApplicationViewer *QmlApplicationViewer::create()
120 {
121 #ifdef HARMATTAN_BOOSTER
122     return new QmlApplicationViewer(MDeclarativeCache::qDeclarativeView(), 0);
123 #else
124     return new QmlApplicationViewer();
125 #endif
126 }
127
128 void QmlApplicationViewer::setMainQmlFile(const QString &file)
129 {
130     d->mainQmlFile = d->adjustPath(file);
131     d->view->setSource(QUrl::fromLocalFile(d->mainQmlFile));
132 }
133
134 void QmlApplicationViewer::addImportPath(const QString &path)
135 {
136     d->view->engine()->addImportPath(d->adjustPath(path));
137 }
138
139 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
140 {
141 #if defined(Q_OS_SYMBIAN)
142     // If the version of Qt on the device is < 4.7.2, that attribute won't work
143     if (orientation != ScreenOrientationAuto) {
144         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
145         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
146             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
147             return;
148         }
149     }
150 #endif // Q_OS_SYMBIAN
151
152     Qt::WidgetAttribute attribute;
153     switch (orientation) {
154 #if QT_VERSION < 0x040702
155     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
156     case ScreenOrientationLockPortrait:
157         attribute = static_cast<Qt::WidgetAttribute>(128);
158         break;
159     case ScreenOrientationLockLandscape:
160         attribute = static_cast<Qt::WidgetAttribute>(129);
161         break;
162     default:
163     case ScreenOrientationAuto:
164         attribute = static_cast<Qt::WidgetAttribute>(130);
165         break;
166 #else // QT_VERSION < 0x040702
167     case ScreenOrientationLockPortrait:
168         attribute = Qt::WA_LockPortraitOrientation;
169         break;
170     case ScreenOrientationLockLandscape:
171         attribute = Qt::WA_LockLandscapeOrientation;
172         break;
173     default:
174     case ScreenOrientationAuto:
175         attribute = Qt::WA_AutoOrientation;
176         break;
177 #endif // QT_VERSION < 0x040702
178     };
179     setAttribute(attribute, true);
180 }
181
182 void QmlApplicationViewer::showExpanded()
183 {
184 #if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
185     d->view->showFullScreen();
186 #elif defined(Q_WS_MAEMO_5)
187     d->view->showMaximized();
188 #else
189     d->view->show();
190 #endif
191 }
192
193 QApplication *createApplication(int &argc, char **argv)
194 {
195 #ifdef HARMATTAN_BOOSTER
196     return MDeclarativeCache::qApplication(argc, argv);
197 #else
198     return new QApplication(argc, argv);
199 #endif
200 }