qmlapplicationviewer/qmlapplicationviewer.cpp

Fri, 08 Jul 2011 22:40:26 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 08 Jul 2011 22:40:26 +0200
changeset 1
8e26151e6bd3
permissions
-rw-r--r--

Added tag SCHUR_GENESIS for changeset ff1b43f833a9

michael@0 1 // checksum 0x7895 version 0x4000b
michael@0 2 /*
michael@0 3 This file was generated by the Qt Quick Application wizard of Qt Creator.
michael@0 4 QmlApplicationViewer is a convenience class containing mobile device specific
michael@0 5 code such as screen orientation handling. Also QML paths and debugging are
michael@0 6 handled here.
michael@0 7 It is recommended not to modify this file, since newer versions of Qt Creator
michael@0 8 may offer an updated version of it.
michael@0 9 */
michael@0 10
michael@0 11 #include "qmlapplicationviewer.h"
michael@0 12
michael@0 13 #include <QtCore/QCoreApplication>
michael@0 14 #include <QtCore/QDir>
michael@0 15 #include <QtCore/QFileInfo>
michael@0 16 #include <QtDeclarative/QDeclarativeComponent>
michael@0 17 #include <QtDeclarative/QDeclarativeEngine>
michael@0 18 #include <QtDeclarative/QDeclarativeContext>
michael@0 19
michael@0 20 #if defined(QMLJSDEBUGGER)
michael@0 21 #include <qt_private/qdeclarativedebughelper_p.h>
michael@0 22 #endif
michael@0 23
michael@0 24 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
michael@0 25 #include <jsdebuggeragent.h>
michael@0 26 #endif
michael@0 27 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
michael@0 28 #include <qdeclarativeviewobserver.h>
michael@0 29 #endif
michael@0 30
michael@0 31 #if defined(QMLJSDEBUGGER)
michael@0 32
michael@0 33 // Enable debugging before any QDeclarativeEngine is created
michael@0 34 struct QmlJsDebuggingEnabler
michael@0 35 {
michael@0 36 QmlJsDebuggingEnabler()
michael@0 37 {
michael@0 38 QDeclarativeDebugHelper::enableDebugging();
michael@0 39 }
michael@0 40 };
michael@0 41
michael@0 42 // Execute code in constructor before first QDeclarativeEngine is instantiated
michael@0 43 static QmlJsDebuggingEnabler enableDebuggingHelper;
michael@0 44
michael@0 45 #endif // QMLJSDEBUGGER
michael@0 46
michael@0 47 class QmlApplicationViewerPrivate
michael@0 48 {
michael@0 49 QString mainQmlFile;
michael@0 50 friend class QmlApplicationViewer;
michael@0 51 static QString adjustPath(const QString &path);
michael@0 52 };
michael@0 53
michael@0 54 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
michael@0 55 {
michael@0 56 #ifdef Q_OS_UNIX
michael@0 57 #ifdef Q_OS_MAC
michael@0 58 if (!QDir::isAbsolutePath(path))
michael@0 59 return QCoreApplication::applicationDirPath()
michael@0 60 + QLatin1String("/../Resources/") + path;
michael@0 61 #else
michael@0 62 const QString pathInInstallDir = QCoreApplication::applicationDirPath()
michael@0 63 + QLatin1String("/../") + path;
michael@0 64 if (pathInInstallDir.contains(QLatin1String("opt"))
michael@0 65 && pathInInstallDir.contains(QLatin1String("bin"))
michael@0 66 && QFileInfo(pathInInstallDir).exists()) {
michael@0 67 return pathInInstallDir;
michael@0 68 }
michael@0 69 #endif
michael@0 70 #endif
michael@0 71 return path;
michael@0 72 }
michael@0 73
michael@0 74 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
michael@0 75 QDeclarativeView(parent),
michael@0 76 m_d(new QmlApplicationViewerPrivate)
michael@0 77 {
michael@0 78 connect(engine(), SIGNAL(quit()), SLOT(close()));
michael@0 79 setResizeMode(QDeclarativeView::SizeRootObjectToView);
michael@0 80 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
michael@0 81 new QmlJSDebugger::JSDebuggerAgent(engine());
michael@0 82 #endif
michael@0 83 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
michael@0 84 new QmlJSDebugger::QDeclarativeViewObserver(this, this);
michael@0 85 #endif
michael@0 86 }
michael@0 87
michael@0 88 QmlApplicationViewer::~QmlApplicationViewer()
michael@0 89 {
michael@0 90 delete m_d;
michael@0 91 }
michael@0 92
michael@0 93 void QmlApplicationViewer::setMainQmlFile(const QString &file)
michael@0 94 {
michael@0 95 m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
michael@0 96 setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
michael@0 97 }
michael@0 98
michael@0 99 void QmlApplicationViewer::addImportPath(const QString &path)
michael@0 100 {
michael@0 101 engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
michael@0 102 }
michael@0 103
michael@0 104 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
michael@0 105 {
michael@0 106 #if defined(Q_OS_SYMBIAN)
michael@0 107 // If the version of Qt on the device is < 4.7.2, that attribute won't work
michael@0 108 if (orientation != ScreenOrientationAuto) {
michael@0 109 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
michael@0 110 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
michael@0 111 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
michael@0 112 return;
michael@0 113 }
michael@0 114 }
michael@0 115 #endif // Q_OS_SYMBIAN
michael@0 116
michael@0 117 Qt::WidgetAttribute attribute;
michael@0 118 switch (orientation) {
michael@0 119 #if QT_VERSION < 0x040702
michael@0 120 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
michael@0 121 case ScreenOrientationLockPortrait:
michael@0 122 attribute = static_cast<Qt::WidgetAttribute>(128);
michael@0 123 break;
michael@0 124 case ScreenOrientationLockLandscape:
michael@0 125 attribute = static_cast<Qt::WidgetAttribute>(129);
michael@0 126 break;
michael@0 127 default:
michael@0 128 case ScreenOrientationAuto:
michael@0 129 attribute = static_cast<Qt::WidgetAttribute>(130);
michael@0 130 break;
michael@0 131 #else // QT_VERSION < 0x040702
michael@0 132 case ScreenOrientationLockPortrait:
michael@0 133 attribute = Qt::WA_LockPortraitOrientation;
michael@0 134 break;
michael@0 135 case ScreenOrientationLockLandscape:
michael@0 136 attribute = Qt::WA_LockLandscapeOrientation;
michael@0 137 break;
michael@0 138 default:
michael@0 139 case ScreenOrientationAuto:
michael@0 140 attribute = Qt::WA_AutoOrientation;
michael@0 141 break;
michael@0 142 #endif // QT_VERSION < 0x040702
michael@0 143 };
michael@0 144 setAttribute(attribute, true);
michael@0 145 }
michael@0 146
michael@0 147 void QmlApplicationViewer::showExpanded()
michael@0 148 {
michael@0 149 #ifdef Q_OS_SYMBIAN
michael@0 150 showFullScreen();
michael@0 151 #elif defined(Q_WS_MAEMO_5)
michael@0 152 showMaximized();
michael@0 153 #else
michael@0 154 show();
michael@0 155 #endif
michael@0 156 }

mercurial