qmlapplicationviewer/qmlapplicationviewer.cpp

changeset 0
ff1b43f833a9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/qmlapplicationviewer/qmlapplicationviewer.cpp	Fri Jul 08 21:05:20 2011 +0200
     1.3 @@ -0,0 +1,156 @@
     1.4 +// checksum 0x7895 version 0x4000b
     1.5 +/*
     1.6 +  This file was generated by the Qt Quick Application wizard of Qt Creator.
     1.7 +  QmlApplicationViewer is a convenience class containing mobile device specific
     1.8 +  code such as screen orientation handling. Also QML paths and debugging are
     1.9 +  handled here.
    1.10 +  It is recommended not to modify this file, since newer versions of Qt Creator
    1.11 +  may offer an updated version of it.
    1.12 +*/
    1.13 +
    1.14 +#include "qmlapplicationviewer.h"
    1.15 +
    1.16 +#include <QtCore/QCoreApplication>
    1.17 +#include <QtCore/QDir>
    1.18 +#include <QtCore/QFileInfo>
    1.19 +#include <QtDeclarative/QDeclarativeComponent>
    1.20 +#include <QtDeclarative/QDeclarativeEngine>
    1.21 +#include <QtDeclarative/QDeclarativeContext>
    1.22 +
    1.23 +#if defined(QMLJSDEBUGGER)
    1.24 +#include <qt_private/qdeclarativedebughelper_p.h>
    1.25 +#endif
    1.26 +
    1.27 +#if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
    1.28 +#include <jsdebuggeragent.h>
    1.29 +#endif
    1.30 +#if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
    1.31 +#include <qdeclarativeviewobserver.h>
    1.32 +#endif
    1.33 +
    1.34 +#if defined(QMLJSDEBUGGER)
    1.35 +
    1.36 +// Enable debugging before any QDeclarativeEngine is created
    1.37 +struct QmlJsDebuggingEnabler
    1.38 +{
    1.39 +    QmlJsDebuggingEnabler()
    1.40 +    {
    1.41 +        QDeclarativeDebugHelper::enableDebugging();
    1.42 +    }
    1.43 +};
    1.44 +
    1.45 +// Execute code in constructor before first QDeclarativeEngine is instantiated
    1.46 +static QmlJsDebuggingEnabler enableDebuggingHelper;
    1.47 +
    1.48 +#endif // QMLJSDEBUGGER
    1.49 +
    1.50 +class QmlApplicationViewerPrivate
    1.51 +{
    1.52 +    QString mainQmlFile;
    1.53 +    friend class QmlApplicationViewer;
    1.54 +    static QString adjustPath(const QString &path);
    1.55 +};
    1.56 +
    1.57 +QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
    1.58 +{
    1.59 +#ifdef Q_OS_UNIX
    1.60 +#ifdef Q_OS_MAC
    1.61 +    if (!QDir::isAbsolutePath(path))
    1.62 +        return QCoreApplication::applicationDirPath()
    1.63 +                + QLatin1String("/../Resources/") + path;
    1.64 +#else
    1.65 +    const QString pathInInstallDir = QCoreApplication::applicationDirPath()
    1.66 +        + QLatin1String("/../") + path;
    1.67 +    if (pathInInstallDir.contains(QLatin1String("opt"))
    1.68 +            && pathInInstallDir.contains(QLatin1String("bin"))
    1.69 +            && QFileInfo(pathInInstallDir).exists()) {
    1.70 +        return pathInInstallDir;
    1.71 +    }
    1.72 +#endif
    1.73 +#endif
    1.74 +    return path;
    1.75 +}
    1.76 +
    1.77 +QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
    1.78 +    QDeclarativeView(parent),
    1.79 +    m_d(new QmlApplicationViewerPrivate)
    1.80 +{
    1.81 +    connect(engine(), SIGNAL(quit()), SLOT(close()));
    1.82 +    setResizeMode(QDeclarativeView::SizeRootObjectToView);
    1.83 +#if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
    1.84 +    new QmlJSDebugger::JSDebuggerAgent(engine());
    1.85 +#endif
    1.86 +#if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
    1.87 +    new QmlJSDebugger::QDeclarativeViewObserver(this, this);
    1.88 +#endif
    1.89 +}
    1.90 +
    1.91 +QmlApplicationViewer::~QmlApplicationViewer()
    1.92 +{
    1.93 +    delete m_d;
    1.94 +}
    1.95 +
    1.96 +void QmlApplicationViewer::setMainQmlFile(const QString &file)
    1.97 +{
    1.98 +    m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
    1.99 +    setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
   1.100 +}
   1.101 +
   1.102 +void QmlApplicationViewer::addImportPath(const QString &path)
   1.103 +{
   1.104 +    engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
   1.105 +}
   1.106 +
   1.107 +void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
   1.108 +{
   1.109 +#if defined(Q_OS_SYMBIAN)
   1.110 +    // If the version of Qt on the device is < 4.7.2, that attribute won't work
   1.111 +    if (orientation != ScreenOrientationAuto) {
   1.112 +        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
   1.113 +        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
   1.114 +            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
   1.115 +            return;
   1.116 +        }
   1.117 +    }
   1.118 +#endif // Q_OS_SYMBIAN
   1.119 +
   1.120 +    Qt::WidgetAttribute attribute;
   1.121 +    switch (orientation) {
   1.122 +#if QT_VERSION < 0x040702
   1.123 +    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
   1.124 +    case ScreenOrientationLockPortrait:
   1.125 +        attribute = static_cast<Qt::WidgetAttribute>(128);
   1.126 +        break;
   1.127 +    case ScreenOrientationLockLandscape:
   1.128 +        attribute = static_cast<Qt::WidgetAttribute>(129);
   1.129 +        break;
   1.130 +    default:
   1.131 +    case ScreenOrientationAuto:
   1.132 +        attribute = static_cast<Qt::WidgetAttribute>(130);
   1.133 +        break;
   1.134 +#else // QT_VERSION < 0x040702
   1.135 +    case ScreenOrientationLockPortrait:
   1.136 +        attribute = Qt::WA_LockPortraitOrientation;
   1.137 +        break;
   1.138 +    case ScreenOrientationLockLandscape:
   1.139 +        attribute = Qt::WA_LockLandscapeOrientation;
   1.140 +        break;
   1.141 +    default:
   1.142 +    case ScreenOrientationAuto:
   1.143 +        attribute = Qt::WA_AutoOrientation;
   1.144 +        break;
   1.145 +#endif // QT_VERSION < 0x040702
   1.146 +    };
   1.147 +    setAttribute(attribute, true);
   1.148 +}
   1.149 +
   1.150 +void QmlApplicationViewer::showExpanded()
   1.151 +{
   1.152 +#ifdef Q_OS_SYMBIAN
   1.153 +    showFullScreen();
   1.154 +#elif defined(Q_WS_MAEMO_5)
   1.155 +    showMaximized();
   1.156 +#else
   1.157 +    show();
   1.158 +#endif
   1.159 +}

mercurial