qmlapplicationview.cpp 2.81 KB
Newer Older
1
2
#include "qmlapplicationview.h"

3
#include <QtCore/QCoreApplication>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>

#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK

class QmlApplicationViewPrivate
{
    QString mainQmlFile;
    friend class QmlApplicationView;
};

QmlApplicationView::QmlApplicationView(const QString &mainQmlFile, QWidget *parent)
    : QDeclarativeView(parent)
    , m_d(new QmlApplicationViewPrivate)
{
27
28
29
30
#ifdef Q_OS_MAC
    m_d->mainQmlFile = QCoreApplication::applicationDirPath()
            + QLatin1String("/../../Resources/") + mainQmlFile;
#else
31
    m_d->mainQmlFile = mainQmlFile;
32
#endif
33
34
35
    setSource(QUrl(m_d->mainQmlFile));
    connect(engine(), SIGNAL(quit()), SLOT(close()));
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
36
    setMinimumSize(10, 10); // Prevent size of 0 if Qml document does not specify width/height
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

QmlApplicationView::~QmlApplicationView()
{
    delete m_d;
}

void QmlApplicationView::setImportPathList(const QStringList &importPaths)
{
    engine()->setImportPathList(importPaths);
}

void QmlApplicationView::setOrientation(Orientation orientation)
{
    if (orientation != Auto) {
#if defined(Q_OS_SYMBIAN)
#if defined(ORIENTATIONLOCK)
        const CAknAppUiBase::TAppUiOrientation uiOrientation =
                (orientation == LockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
                    : CAknAppUi::EAppUiOrientationLandscape;
        CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
        TRAPD(error,
            if (appUi)
                appUi->SetOrientationL(uiOrientation);
        );
#else // ORIENTATIONLOCK
        qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
#endif // ORIENTATIONLOCK
#endif // Q_OS_SYMBIAN
    }
}

void QmlApplicationView::setLoadDummyData(bool loadDummyData)
{
    if (loadDummyData) {
        const QFileInfo mainQmlFileInfo(m_d->mainQmlFile);
        const QDir dir(mainQmlFileInfo.absolutePath() + QLatin1String("/dummydata"),
                 QLatin1String("*.qml"));
        foreach (const QFileInfo &qmlFile, dir.entryInfoList()) {
            QFile f(qmlFile.absoluteFilePath());
            if (f.open(QIODevice::ReadOnly)) {
                QDeclarativeComponent comp(engine());
                comp.setData(f.readAll(), QUrl());
                QObject *dummyData = comp.create();
                if (dummyData) {
                    rootContext()->setContextProperty(qmlFile.baseName(), dummyData);
                    dummyData->setParent(this);
                }
            }
        }
    }
}