411a04c13919ad347058d321ab3b72739e57d963
[profile/ivi/qtdeclarative.git] / examples / declarative / modelviews / listview / sections / qmlapplicationviewer / qmlapplicationviewer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 // checksum 0x28c7 version 0x2000a
42 /*
43   This file was generated by the Qt Quick Application wizard of Qt Creator.
44   QmlApplicationViewer is a convenience class containing mobile device specific
45   code such as screen orientation handling. Also QML paths and debugging are
46   handled here.
47   It is recommended not to modify this file, since newer versions of Qt Creator
48   may offer an updated version of it.
49 */
50
51 #include "qmlapplicationviewer.h"
52
53 #include <QtCore/QCoreApplication>
54 #include <QtCore/QDir>
55 #include <QtCore/QFileInfo>
56 #include <QtDeclarative/QDeclarativeComponent>
57 #include <QtDeclarative/QDeclarativeEngine>
58 #include <QtDeclarative/QDeclarativeContext>
59
60 #if defined(QMLJSDEBUGGER)
61 #include <qt_private/qdeclarativedebughelper_p.h>
62 #endif
63
64 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
65 #include <jsdebuggeragent.h>
66 #endif
67 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
68 #include <qdeclarativeviewobserver.h>
69 #endif
70
71 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
72 #include <eikenv.h>
73 #include <eikappui.h>
74 #include <aknenv.h>
75 #include <aknappui.h>
76 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
77
78 #if defined(QMLJSDEBUGGER)
79
80 // Enable debugging before any QDeclarativeEngine is created
81 struct QmlJsDebuggingEnabler
82 {
83     QmlJsDebuggingEnabler()
84     {
85         QDeclarativeDebugHelper::enableDebugging();
86     }
87 };
88
89 // Execute code in constructor before first QDeclarativeEngine is instantiated
90 static QmlJsDebuggingEnabler enableDebuggingHelper;
91
92 #endif // QMLJSDEBUGGER
93
94 class QmlApplicationViewerPrivate
95 {
96     QString mainQmlFile;
97     friend class QmlApplicationViewer;
98     static QString adjustPath(const QString &path);
99 };
100
101 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
102 {
103 #ifdef Q_OS_UNIX
104 #ifdef Q_OS_MAC
105     if (!QDir::isAbsolutePath(path))
106         return QCoreApplication::applicationDirPath()
107                 + QLatin1String("/../Resources/") + path;
108 #else
109     const QString pathInShareDir = QCoreApplication::applicationDirPath()
110         + QLatin1String("/../share/")
111         + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
112         + QLatin1Char('/') + path;
113     if (QFileInfo(pathInShareDir).exists())
114         return pathInShareDir;
115 #endif
116 #endif
117     return path;
118 }
119
120 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
121     QDeclarativeView(parent),
122     m_d(new QmlApplicationViewerPrivate)
123 {
124     connect(engine(), SIGNAL(quit()), SLOT(close()));
125     setResizeMode(QDeclarativeView::SizeRootObjectToView);
126 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
127     new QmlJSDebugger::JSDebuggerAgent(engine());
128 #endif
129 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
130     new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
131 #endif
132 }
133
134 QmlApplicationViewer::~QmlApplicationViewer()
135 {
136     delete m_d;
137 }
138
139 void QmlApplicationViewer::setMainQmlFile(const QString &file)
140 {
141     m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
142     setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
143 }
144
145 void QmlApplicationViewer::addImportPath(const QString &path)
146 {
147     engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
148 }
149
150 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
151 {
152 #ifdef Q_OS_SYMBIAN
153     if (orientation != ScreenOrientationAuto) {
154 #if defined(ORIENTATIONLOCK)
155         const CAknAppUiBase::TAppUiOrientation uiOrientation =
156                 (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
157                     : CAknAppUi::EAppUiOrientationLandscape;
158         CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
159         TRAPD(error,
160             if (appUi)
161                 appUi->SetOrientationL(uiOrientation);
162         );
163         Q_UNUSED(error)
164 #else // ORIENTATIONLOCK
165         qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
166 #endif // ORIENTATIONLOCK
167     }
168 #elif defined(Q_WS_MAEMO_5)
169     Qt::WidgetAttribute attribute;
170     switch (orientation) {
171     case ScreenOrientationLockPortrait:
172         attribute = Qt::WA_Maemo5PortraitOrientation;
173         break;
174     case ScreenOrientationLockLandscape:
175         attribute = Qt::WA_Maemo5LandscapeOrientation;
176         break;
177     case ScreenOrientationAuto:
178     default:
179         attribute = Qt::WA_Maemo5AutoOrientation;
180         break;
181     }
182     setAttribute(attribute, true);
183 #else // Q_OS_SYMBIAN
184     Q_UNUSED(orientation);
185 #endif // Q_OS_SYMBIAN
186 }
187
188 void QmlApplicationViewer::showExpanded()
189 {
190 #ifdef Q_OS_SYMBIAN
191     showFullScreen();
192 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
193     showMaximized();
194 #else
195     show();
196 #endif
197 }