examples/QtQuick/qmlcontactslist/qmlapplicationviewer/qmlapplicationviewer.cpp

00001 // checksum 0x7895 version 0x4000b
00002 /*
00003   This file was generated by the Qt Quick Application wizard of Qt Creator.
00004   QmlApplicationViewer is a convenience class containing mobile device specific
00005   code such as screen orientation handling. Also QML paths and debugging are
00006   handled here.
00007   It is recommended not to modify this file, since newer versions of Qt Creator
00008   may offer an updated version of it.
00009 */
00010 
00011 #include "qmlapplicationviewer.h"
00012 
00013 #include <QtCore/QCoreApplication>
00014 #include <QtCore/QDir>
00015 #include <QtCore/QFileInfo>
00016 #include <QtDeclarative/QDeclarativeComponent>
00017 #include <QtDeclarative/QDeclarativeEngine>
00018 #include <QtDeclarative/QDeclarativeContext>
00019 
00020 #if defined(QMLJSDEBUGGER)
00021 #include <qt_private/qdeclarativedebughelper_p.h>
00022 #endif
00023 
00024 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00025 #include <jsdebuggeragent.h>
00026 #endif
00027 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00028 #include <qdeclarativeviewobserver.h>
00029 #endif
00030 
00031 #if defined(QMLJSDEBUGGER)
00032 
00033 // Enable debugging before any QDeclarativeEngine is created
00034 struct QmlJsDebuggingEnabler
00035 {
00036     QmlJsDebuggingEnabler()
00037     {
00038         QDeclarativeDebugHelper::enableDebugging();
00039     }
00040 };
00041 
00042 // Execute code in constructor before first QDeclarativeEngine is instantiated
00043 static QmlJsDebuggingEnabler enableDebuggingHelper;
00044 
00045 #endif // QMLJSDEBUGGER
00046 
00047 class QmlApplicationViewerPrivate
00048 {
00049     QString mainQmlFile;
00050     friend class QmlApplicationViewer;
00051     static QString adjustPath(const QString &path);
00052 };
00053 
00054 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
00055 {
00056 #ifdef Q_OS_UNIX
00057 #ifdef Q_OS_MAC
00058     if (!QDir::isAbsolutePath(path))
00059         return QCoreApplication::applicationDirPath()
00060                 + QLatin1String("/../Resources/") + path;
00061 #else
00062     const QString pathInInstallDir = QCoreApplication::applicationDirPath()
00063         + QLatin1String("/../") + path;
00064     if (pathInInstallDir.contains(QLatin1String("opt"))
00065             && pathInInstallDir.contains(QLatin1String("bin"))
00066             && QFileInfo(pathInInstallDir).exists()) {
00067         return pathInInstallDir;
00068     }
00069 #endif
00070 #endif
00071     return path;
00072 }
00073 
00074 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
00075     QDeclarativeView(parent),
00076     m_d(new QmlApplicationViewerPrivate)
00077 {
00078     connect(engine(), SIGNAL(quit()), SLOT(close()));
00079     setResizeMode(QDeclarativeView::SizeRootObjectToView);
00080 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00081     new QmlJSDebugger::JSDebuggerAgent(engine());
00082 #endif
00083 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00084     new QmlJSDebugger::QDeclarativeViewObserver(this, this);
00085 #endif
00086 }
00087 
00088 QmlApplicationViewer::~QmlApplicationViewer()
00089 {
00090     delete m_d;
00091 }
00092 
00093 void QmlApplicationViewer::setMainQmlFile(const QString &file)
00094 {
00095     m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
00096     setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
00097 }
00098 
00099 void QmlApplicationViewer::addImportPath(const QString &path)
00100 {
00101     engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
00102 }
00103 
00104 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
00105 {
00106 #if defined(Q_OS_SYMBIAN)
00107     // If the version of Qt on the device is < 4.7.2, that attribute won't work
00108     if (orientation != ScreenOrientationAuto) {
00109         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
00110         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
00111             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
00112             return;
00113         }
00114     }
00115 #endif // Q_OS_SYMBIAN
00116 
00117     Qt::WidgetAttribute attribute;
00118     switch (orientation) {
00119 #if QT_VERSION < 0x040702
00120     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
00121     case ScreenOrientationLockPortrait:
00122         attribute = static_cast<Qt::WidgetAttribute>(128);
00123         break;
00124     case ScreenOrientationLockLandscape:
00125         attribute = static_cast<Qt::WidgetAttribute>(129);
00126         break;
00127     default:
00128     case ScreenOrientationAuto:
00129         attribute = static_cast<Qt::WidgetAttribute>(130);
00130         break;
00131 #else // QT_VERSION < 0x040702
00132     case ScreenOrientationLockPortrait:
00133         attribute = Qt::WA_LockPortraitOrientation;
00134         break;
00135     case ScreenOrientationLockLandscape:
00136         attribute = Qt::WA_LockLandscapeOrientation;
00137         break;
00138     default:
00139     case ScreenOrientationAuto:
00140         attribute = Qt::WA_AutoOrientation;
00141         break;
00142 #endif // QT_VERSION < 0x040702
00143     };
00144     setAttribute(attribute, true);
00145 }
00146 
00147 void QmlApplicationViewer::showExpanded()
00148 {
00149 #ifdef Q_OS_SYMBIAN
00150     showFullScreen();
00151 #elif defined(Q_WS_MAEMO_5)
00152     showMaximized();
00153 #else
00154     show();
00155 #endif
00156 }

Generated by  doxygen 1.6.2