qmlapplicationviewer.cpp 4.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
  This file was generated by the Qt Quick Application wizard of Qt Creator.
  QmlApplicationViewer is a convenience class containing mobile device specific
  code such as screen orientation handling. Also QML paths and debugging are
  handled here.
  It is recommended not to modify this file, since newer versions of Qt Creator
  may offer an updated version of it.
*/

10
#include "qmlapplicationviewer.h"
11

12
#include <QtCore/QCoreApplication>
13
14
15
16
17
18
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>

19
20
#if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800

21
22
#include <qt_private/qdeclarativedebughelper_p.h>

23
#if !defined(NO_JSDEBUGGER)
24
25
#include <jsdebuggeragent.h>
#endif
26
#if !defined(NO_QMLOBSERVER)
27
28
29
#include <qdeclarativeviewobserver.h>
#endif

30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Enable debugging before any QDeclarativeEngine is created
struct QmlJsDebuggingEnabler
{
    QmlJsDebuggingEnabler()
    {
        QDeclarativeDebugHelper::enableDebugging();
    }
};

// Execute code in constructor before first QDeclarativeEngine is instantiated
static QmlJsDebuggingEnabler enableDebuggingHelper;

#endif // QMLJSDEBUGGER

44
class QmlApplicationViewerPrivate
45
46
{
    QString mainQmlFile;
47
    friend class QmlApplicationViewer;
48
    static QString adjustPath(const QString &path);
49
50
};

51
QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
52
{
53
54
#ifdef Q_OS_UNIX
#ifdef Q_OS_MAC
Alessandro Portale's avatar
Alessandro Portale committed
55
    if (!QDir::isAbsolutePath(path))
56
57
        return QCoreApplication::applicationDirPath()
                + QLatin1String("/../Resources/") + path;
58
#else
59
60
61
62
63
64
65
    const QString pathInInstallDir = QCoreApplication::applicationDirPath()
        + QLatin1String("/../") + path;
    if (pathInInstallDir.contains(QLatin1String("opt"))
            && pathInInstallDir.contains(QLatin1String("bin"))
            && QFileInfo(pathInInstallDir).exists()) {
        return pathInInstallDir;
    }
66
#endif
67
68
69
70
#endif
    return path;
}

71
QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
72
73
    QDeclarativeView(parent),
    m_d(new QmlApplicationViewerPrivate)
74
75
76
{
    connect(engine(), SIGNAL(quit()), SLOT(close()));
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
77
78
79
    // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
#if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
#if !defined(NO_JSDEBUGGER)
80
81
    new QmlJSDebugger::JSDebuggerAgent(engine());
#endif
82
#if !defined(NO_QMLOBSERVER)
83
    new QmlJSDebugger::QDeclarativeViewObserver(this, this);
84
#endif
85
#endif
86
87
}

88
QmlApplicationViewer::~QmlApplicationViewer()
89
90
91
92
{
    delete m_d;
}

93
void QmlApplicationViewer::setMainQmlFile(const QString &file)
94
{
95
    m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
96
97
98
    setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
}

99
void QmlApplicationViewer::addImportPath(const QString &path)
100
{
101
    engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
102
103
}

104
void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
105
{
106
107
#if defined(Q_OS_SYMBIAN)
    // If the version of Qt on the device is < 4.7.2, that attribute won't work
108
    if (orientation != ScreenOrientationAuto) {
109
110
111
112
113
        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
            return;
        }
114
    }
115
116
#endif // Q_OS_SYMBIAN

117
118
    Qt::WidgetAttribute attribute;
    switch (orientation) {
119
120
#if QT_VERSION < 0x040702
    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
121
    case ScreenOrientationLockPortrait:
122
        attribute = static_cast<Qt::WidgetAttribute>(128);
123
        break;
124
    case ScreenOrientationLockLandscape:
125
        attribute = static_cast<Qt::WidgetAttribute>(129);
126
        break;
127
    default:
128
    case ScreenOrientationAuto:
129
130
131
132
133
134
135
136
137
        attribute = static_cast<Qt::WidgetAttribute>(130);
        break;
#else // QT_VERSION < 0x040702
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_LockPortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_LockLandscapeOrientation;
        break;
138
    default:
139
140
    case ScreenOrientationAuto:
        attribute = Qt::WA_AutoOrientation;
141
        break;
142
143
#endif // QT_VERSION < 0x040702
    };
144
    setAttribute(attribute, true);
145
146
}

147
void QmlApplicationViewer::showExpanded()
148
{
149
#if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
150
    showFullScreen();
151
#elif defined(Q_WS_MAEMO_5)
152
153
    showMaximized();
#else
154
    show();
155
156
#endif
}