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