examples/QtQuick/contentlauncherexample/qmlapplicationviewer/qmlapplicationviewer.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation
00006 **
00007 **
00008 ** $QT_BEGIN_LICENSE:BSD$
00009 ** You may use this file under the terms of the BSD license as follows:
00010 **
00011 ** "Redistribution and use in source and binary forms, with or without
00012 ** modification, are permitted provided that the following conditions are
00013 ** met:
00014 **   * Redistributions of source code must retain the above copyright
00015 **     notice, this list of conditions and the following disclaimer.
00016 **   * Redistributions in binary form must reproduce the above copyright
00017 **     notice, this list of conditions and the following disclaimer in
00018 **     the documentation and/or other materials provided with the
00019 **     distribution.
00020 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00021 **     the names of its contributors may be used to endorse or promote
00022 **     products derived from this software without specific prior written
00023 **     permission.
00024 **
00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00036 ** $QT_END_LICENSE$
00037 ** Description:
00038 ****************************************************************************/
00039 
00040 #include "qmlapplicationviewer.h"
00041 #if defined(Q_OS_SYMBIAN)
00042 #include "mifimageprovider.h"
00043 #endif
00044 
00045 #include <QtCore/QCoreApplication>
00046 #include <QtCore/QDir>
00047 #include <QtCore/QFileInfo>
00048 #include <QtDeclarative/QDeclarativeComponent>
00049 #include <QtDeclarative/QDeclarativeEngine>
00050 #include <QtDeclarative/QDeclarativeContext>
00051 
00052 #if defined(QMLJSDEBUGGER)
00053 #include <qt_private/qdeclarativedebughelper_p.h>
00054 #endif
00055 
00056 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00057 #include <jsdebuggeragent.h>
00058 #endif
00059 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00060 #include <qdeclarativeviewobserver.h>
00061 #endif
00062 
00063 #if defined(QMLJSDEBUGGER)
00064 
00065 // Enable debugging before any QDeclarativeEngine is created
00066 struct QmlJsDebuggingEnabler
00067 {
00068     QmlJsDebuggingEnabler()
00069     {
00070         QDeclarativeDebugHelper::enableDebugging();
00071     }
00072 };
00073 
00074 // Execute code in constructor before first QDeclarativeEngine is instantiated
00075 static QmlJsDebuggingEnabler enableDebuggingHelper;
00076 
00077 #endif // QMLJSDEBUGGER
00078 
00079 class QmlApplicationViewerPrivate
00080 {
00081     QString mainQmlFile;
00082     friend class QmlApplicationViewer;
00083     static QString adjustPath(const QString &path);
00084 };
00085 
00086 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
00087 {
00088 #ifdef Q_OS_UNIX
00089 #ifdef Q_OS_MAC
00090     if (!QDir::isAbsolutePath(path))
00091         return QCoreApplication::applicationDirPath()
00092                 + QLatin1String("/../Resources/") + path;
00093 #else
00094     const QString pathInInstallDir = QCoreApplication::applicationDirPath()
00095         + QLatin1String("/../") + path;
00096     if (pathInInstallDir.contains(QLatin1String("opt"))
00097             && pathInInstallDir.contains(QLatin1String("bin"))
00098             && QFileInfo(pathInInstallDir).exists()) {
00099         return pathInInstallDir;
00100     }
00101 #endif
00102 #endif
00103     return path;
00104 }
00105 
00106 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
00107     QDeclarativeView(parent),
00108     m_d(new QmlApplicationViewerPrivate)
00109 {
00110     connect(engine(), SIGNAL(quit()), SLOT(close()));
00111     setResizeMode(QDeclarativeView::SizeRootObjectToView);
00112 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00113     new QmlJSDebugger::JSDebuggerAgent(engine());
00114 #endif
00115 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00116     new QmlJSDebugger::QDeclarativeViewObserver(this, this);
00117 #endif
00118 
00119 #if defined(Q_OS_SYMBIAN)
00120     engine()->addImageProvider(QLatin1String("mifprovider"), new MifImageProvider());
00121 #endif
00122 }
00123 
00124 QmlApplicationViewer::~QmlApplicationViewer()
00125 {
00126     delete m_d;
00127 }
00128 
00129 void QmlApplicationViewer::setMainQmlFile(const QString &file)
00130 {
00131     m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
00132     setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
00133 }
00134 
00135 void QmlApplicationViewer::addImportPath(const QString &path)
00136 {
00137     engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
00138 }
00139 
00140 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
00141 {
00142 #if defined(Q_OS_SYMBIAN)
00143     // If the version of Qt on the device is < 4.7.2, that attribute won't work
00144     if (orientation != ScreenOrientationAuto) {
00145         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
00146         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
00147             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
00148             return;
00149         }
00150     }
00151 #endif // Q_OS_SYMBIAN
00152 
00153     Qt::WidgetAttribute attribute;
00154     switch (orientation) {
00155 #if QT_VERSION < 0x040702
00156     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
00157     case ScreenOrientationLockPortrait:
00158         attribute = static_cast<Qt::WidgetAttribute>(128);
00159         break;
00160     case ScreenOrientationLockLandscape:
00161         attribute = static_cast<Qt::WidgetAttribute>(129);
00162         break;
00163     default:
00164     case ScreenOrientationAuto:
00165         attribute = static_cast<Qt::WidgetAttribute>(130);
00166         break;
00167 #else // QT_VERSION < 0x040702
00168     case ScreenOrientationLockPortrait:
00169         attribute = Qt::WA_LockPortraitOrientation;
00170         break;
00171     case ScreenOrientationLockLandscape:
00172         attribute = Qt::WA_LockLandscapeOrientation;
00173         break;
00174     default:
00175     case ScreenOrientationAuto:
00176         attribute = Qt::WA_AutoOrientation;
00177         break;
00178 #endif // QT_VERSION < 0x040702
00179     };
00180     setAttribute(attribute, true);
00181 }
00182 
00183 void QmlApplicationViewer::showExpanded()
00184 {
00185 #ifdef Q_OS_SYMBIAN
00186     showFullScreen();
00187 #elif defined(Q_WS_MAEMO_5)
00188     showMaximized();
00189 #else
00190     show();
00191 #endif
00192 }

Generated by  doxygen 1.6.2