d3901bd76bd8c54ebba7dd1ba97d9b3bcb39eb5d
[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 #if defined(QMLJSDEBUGGER)
25 #include <qt_private/qdeclarativedebughelper_p.h>
26 #endif
27
28 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
29 #include <jsdebuggeragent.h>
30 #endif
31 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
32 #include <qdeclarativeviewobserver.h>
33 #endif
34
35 #if defined(QMLJSDEBUGGER)
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     QString mainQmlFile;
54     friend class QmlApplicationViewer;
55     static QString adjustPath(const QString &path);
56 };
57
58 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
59 {
60 #ifdef Q_OS_UNIX
61 #ifdef Q_OS_MAC
62     if (!QDir::isAbsolutePath(path))
63         return QCoreApplication::applicationDirPath()
64                 + QLatin1String("/../Resources/") + path;
65 #else
66     const QString pathInShareDir = QCoreApplication::applicationDirPath()
67         + QLatin1String("/../share/")
68         + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
69         + QLatin1Char('/') + path;
70     if (QFileInfo(pathInShareDir).exists())
71         return pathInShareDir;
72 #endif
73 #endif
74     return path;
75 }
76
77 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
78     QDeclarativeView(parent),
79     m_d(new QmlApplicationViewerPrivate)
80 {
81     connect(engine(), SIGNAL(quit()), SLOT(close()));
82     setResizeMode(QDeclarativeView::SizeRootObjectToView);
83 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
84     new QmlJSDebugger::JSDebuggerAgent(engine());
85 #endif
86 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
87     new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
88 #endif
89 }
90
91 QmlApplicationViewer::~QmlApplicationViewer()
92 {
93     delete m_d;
94 }
95
96 void QmlApplicationViewer::setMainQmlFile(const QString &file)
97 {
98     m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
99     setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
100 }
101
102 void QmlApplicationViewer::addImportPath(const QString &path)
103 {
104     engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
105 }
106
107 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
108 {
109 #if defined(Q_OS_SYMBIAN)
110     // If the version of Qt on the device is < 4.7.2, that attribute won't work
111     if (orientation != ScreenOrientationAuto) {
112         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
113         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
114             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
115             return;
116         }
117     }
118 #endif // Q_OS_SYMBIAN
119
120     Qt::WidgetAttribute attribute;
121     switch (orientation) {
122 #if QT_VERSION < 0x040702
123     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
124     case ScreenOrientationLockPortrait:
125         attribute = static_cast<Qt::WidgetAttribute>(128);
126         break;
127     case ScreenOrientationLockLandscape:
128         attribute = static_cast<Qt::WidgetAttribute>(129);
129         break;
130     default:
131     case ScreenOrientationAuto:
132         attribute = static_cast<Qt::WidgetAttribute>(130);
133         break;
134 #else // QT_VERSION < 0x040702
135     case ScreenOrientationLockPortrait:
136         attribute = Qt::WA_LockPortraitOrientation;
137         break;
138     case ScreenOrientationLockLandscape:
139         attribute = Qt::WA_LockLandscapeOrientation;
140         break;
141     default:
142     case ScreenOrientationAuto:
143         attribute = Qt::WA_AutoOrientation;
144         break;
145 #endif // QT_VERSION < 0x040702
146     };
147     setAttribute(attribute, true);
148 }
149
150 void QmlApplicationViewer::showExpanded()
151 {
152 #ifdef Q_OS_SYMBIAN
153     showFullScreen();
154 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
155     showMaximized();
156 #else
157     show();
158 #endif
159 }
160
161 void QmlApplicationViewer::initScreenOrientation(int OO){
162     if (OO == 1)
163             this->setOrientation(this->ScreenOrientationAuto);
164     else
165         this->setOrientation(this->ScreenOrientationLockLandscape);
166 }
167
168 void QmlApplicationViewer::minimize(){
169     QDBusConnection c = QDBusConnection::sessionBus();
170     QDBusMessage m = QDBusMessage::createSignal("/", "com.nokia.hildon_desktop", "exit_app_view");
171     c.send(m);
172 }