cleanup
[lichviet] / qmlapplicationviewer / qmlapplicationviewer.cpp
1 // checksum 0x17fa version 0x3000a
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/QCoreApplication>
14 #include <QtCore/QDir>
15 #include <QtCore/QFileInfo>
16 #include <QtDeclarative/QDeclarativeComponent>
17 #include <QtDeclarative/QDeclarativeEngine>
18 #include <QtDeclarative/QDeclarativeContext>
19
20 #include <QtDBus/QDBusConnection>
21 #include <QtDBus/QDBusMessage>
22 #include <QtDBus/QDBusInterface>
23
24 #include <QDir>
25 #include <QFileDialog>
26
27 #if defined(QMLJSDEBUGGER)
28 #include <qt_private/qdeclarativedebughelper_p.h>
29 #endif
30
31 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
32 #include <jsdebuggeragent.h>
33 #endif
34 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
35 #include <qdeclarativeviewobserver.h>
36 #endif
37
38 #if defined(QMLJSDEBUGGER)
39
40 // Enable debugging before any QDeclarativeEngine is created
41 struct QmlJsDebuggingEnabler
42 {
43     QmlJsDebuggingEnabler()
44     {
45         QDeclarativeDebugHelper::enableDebugging();
46     }
47 };
48
49 // Execute code in constructor before first QDeclarativeEngine is instantiated
50 static QmlJsDebuggingEnabler enableDebuggingHelper;
51
52 #endif // QMLJSDEBUGGER
53
54 class QmlApplicationViewerPrivate
55 {
56     QString mainQmlFile;
57     friend class QmlApplicationViewer;
58     static 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     const QString pathInShareDir = QCoreApplication::applicationDirPath()
70         + QLatin1String("/../share/")
71         + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
72         + QLatin1Char('/') + path;
73     if (QFileInfo(pathInShareDir).exists())
74         return pathInShareDir;
75 #endif
76 #endif
77     return path;
78 }
79
80 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
81     QDeclarativeView(parent),
82     m_d(new QmlApplicationViewerPrivate)
83 {
84     connect(engine(), SIGNAL(quit()), SLOT(close()));
85     setResizeMode(QDeclarativeView::SizeRootObjectToView);
86 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
87     new QmlJSDebugger::JSDebuggerAgent(engine());
88 #endif
89 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
90     new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
91 #endif
92 }
93
94 QmlApplicationViewer::~QmlApplicationViewer()
95 {
96     delete m_d;
97 }
98
99 void QmlApplicationViewer::setMainQmlFile(const QString &file)
100 {
101     m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
102     setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
103 }
104
105 void QmlApplicationViewer::addImportPath(const QString &path)
106 {
107     engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
108 }
109
110 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
111 {
112 #if defined(Q_OS_SYMBIAN)
113     // If the version of Qt on the device is < 4.7.2, that attribute won't work
114     if (orientation != ScreenOrientationAuto) {
115         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
116         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
117             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
118             return;
119         }
120     }
121 #endif // Q_OS_SYMBIAN
122
123     Qt::WidgetAttribute attribute;
124     switch (orientation) {
125 #if QT_VERSION < 0x040702
126     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
127     case ScreenOrientationLockPortrait:
128         attribute = static_cast<Qt::WidgetAttribute>(128);
129         break;
130     case ScreenOrientationLockLandscape:
131         attribute = static_cast<Qt::WidgetAttribute>(129);
132         break;
133     default:
134     case ScreenOrientationAuto:
135         attribute = static_cast<Qt::WidgetAttribute>(130);
136         break;
137 #else // QT_VERSION < 0x040702
138     case ScreenOrientationLockPortrait:
139         attribute = Qt::WA_LockPortraitOrientation;
140         break;
141     case ScreenOrientationLockLandscape:
142         attribute = Qt::WA_LockLandscapeOrientation;
143         break;
144     default:
145     case ScreenOrientationAuto:
146         attribute = Qt::WA_AutoOrientation;
147         break;
148 #endif // QT_VERSION < 0x040702
149     };
150     setAttribute(attribute, true);
151 }
152
153 void QmlApplicationViewer::showExpanded()
154 {
155 #ifdef Q_OS_SYMBIAN
156     showFullScreen();
157 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
158     showMaximized();
159 #else
160     show();
161 #endif
162 }
163
164 void QmlApplicationViewer::initScreenOrientation(int OO){
165     if (OO == 1)
166             this->setOrientation(this->ScreenOrientationAuto);
167     else
168         this->setOrientation(this->ScreenOrientationLockLandscape);
169 }
170
171 void QmlApplicationViewer::minimize(){
172     QDBusConnection c = QDBusConnection::sessionBus();
173     QDBusMessage m = QDBusMessage::createSignal("/", "com.nokia.hildon_desktop", "exit_app_view");
174     c.send(m);
175 }