2010-08-25 19:07:50 +02:00
|
|
|
#include "qmlapplicationviewer.h"
|
2010-07-15 11:58:37 +02:00
|
|
|
|
2010-07-30 10:49:11 +02:00
|
|
|
#include <QtCore/QCoreApplication>
|
2010-07-15 11:58:37 +02:00
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtDeclarative/QDeclarativeComponent>
|
|
|
|
|
#include <QtDeclarative/QDeclarativeEngine>
|
|
|
|
|
#include <QtDeclarative/QDeclarativeContext>
|
|
|
|
|
|
2010-09-16 16:20:12 +02:00
|
|
|
#if defined(QMLOBSERVER)
|
2010-09-16 14:41:39 +02:00
|
|
|
#include <qdeclarativeviewobserver.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-07-15 11:58:37 +02:00
|
|
|
#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
|
|
|
|
|
#include <eikenv.h>
|
|
|
|
|
#include <eikappui.h>
|
|
|
|
|
#include <aknenv.h>
|
|
|
|
|
#include <aknappui.h>
|
|
|
|
|
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
class QmlApplicationViewerPrivate
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
|
|
|
|
QString mainQmlFile;
|
2010-08-25 19:07:50 +02:00
|
|
|
friend class QmlApplicationViewer;
|
2010-08-16 20:27:30 +02:00
|
|
|
static QString adjustPath(const QString &path);
|
2010-07-15 11:58:37 +02:00
|
|
|
};
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
|
2010-08-16 20:27:30 +02:00
|
|
|
{
|
2010-09-08 16:50:47 +02:00
|
|
|
#ifdef Q_OS_UNIX
|
2010-08-16 20:27:30 +02:00
|
|
|
#ifdef Q_OS_MAC
|
2010-08-16 22:02:20 +02:00
|
|
|
if (!QDir::isAbsolutePath(path))
|
2010-08-16 20:27:30 +02:00
|
|
|
return QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/../Resources/") + path;
|
2010-09-08 16:50:47 +02:00
|
|
|
#else
|
|
|
|
|
const QString pathInShareDir = QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/../share/")
|
|
|
|
|
+ QFileInfo(QCoreApplication::applicationFilePath()).fileName()
|
|
|
|
|
+ QLatin1Char('/') + path;
|
|
|
|
|
if (QFileInfo(pathInShareDir).exists())
|
|
|
|
|
return pathInShareDir;
|
|
|
|
|
#endif
|
2010-08-16 20:27:30 +02:00
|
|
|
#endif
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
|
2010-09-16 14:41:39 +02:00
|
|
|
QDeclarativeView(parent),
|
|
|
|
|
m_d(new QmlApplicationViewerPrivate)
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
|
|
|
|
connect(engine(), SIGNAL(quit()), SLOT(close()));
|
|
|
|
|
setResizeMode(QDeclarativeView::SizeRootObjectToView);
|
2010-09-16 16:20:12 +02:00
|
|
|
#ifdef QMLOBSERVER
|
2010-09-16 15:59:52 +02:00
|
|
|
new QmlObserver::QDeclarativeViewObserver(this, parent);
|
2010-09-16 14:41:39 +02:00
|
|
|
#endif
|
2010-07-15 11:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
QmlApplicationViewer::~QmlApplicationViewer()
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
|
|
|
|
delete m_d;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
void QmlApplicationViewer::setMainQmlFile(const QString &file)
|
2010-08-16 20:27:30 +02:00
|
|
|
{
|
2010-08-25 19:07:50 +02:00
|
|
|
m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
|
2010-08-16 20:27:30 +02:00
|
|
|
setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
void QmlApplicationViewer::addImportPath(const QString &path)
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
2010-08-25 19:07:50 +02:00
|
|
|
engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
|
2010-07-15 11:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
void QmlApplicationViewer::setOrientation(Orientation orientation)
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
2010-09-10 13:54:31 +02:00
|
|
|
#ifdef Q_OS_SYMBIAN
|
2010-07-15 11:58:37 +02:00
|
|
|
if (orientation != Auto) {
|
|
|
|
|
#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
|
|
|
|
|
}
|
2010-09-10 13:54:31 +02:00
|
|
|
#elif defined(Q_WS_MAEMO_5)
|
|
|
|
|
Qt::WidgetAttribute attribute;
|
|
|
|
|
switch (orientation) {
|
|
|
|
|
case LockPortrait:
|
|
|
|
|
attribute = Qt::WA_Maemo5PortraitOrientation;
|
|
|
|
|
break;
|
|
|
|
|
case LockLandscape:
|
|
|
|
|
attribute = Qt::WA_Maemo5LandscapeOrientation;
|
|
|
|
|
break;
|
|
|
|
|
case Auto:
|
|
|
|
|
default:
|
|
|
|
|
attribute = Qt::WA_Maemo5AutoOrientation;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
setAttribute(attribute, true);
|
|
|
|
|
#else // Q_OS_SYMBIAN
|
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
|
#endif // Q_OS_SYMBIAN
|
2010-07-15 11:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-25 19:07:50 +02:00
|
|
|
void QmlApplicationViewer::setLoadDummyData(bool loadDummyData)
|
2010-07-15 11:58:37 +02:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|