forked from qt-creator/qt-creator
		
	We should restrict ourselves to calling all qml debugging support observer, instead of mixing qml debugger / inspector / observer interchangeably.
		
			
				
	
	
		
			131 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "qmlapplicationviewer.h"
 | 
						|
 | 
						|
#include <QtCore/QCoreApplication>
 | 
						|
#include <QtCore/QDir>
 | 
						|
#include <QtCore/QFileInfo>
 | 
						|
#include <QtDeclarative/QDeclarativeComponent>
 | 
						|
#include <QtDeclarative/QDeclarativeEngine>
 | 
						|
#include <QtDeclarative/QDeclarativeContext>
 | 
						|
 | 
						|
#if defined(QMLOBSERVER)
 | 
						|
#include <qdeclarativeviewobserver.h>
 | 
						|
#endif
 | 
						|
 | 
						|
#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 QmlApplicationViewerPrivate
 | 
						|
{
 | 
						|
    QString mainQmlFile;
 | 
						|
    friend class QmlApplicationViewer;
 | 
						|
    static QString adjustPath(const QString &path);
 | 
						|
};
 | 
						|
 | 
						|
QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
 | 
						|
{
 | 
						|
#ifdef Q_OS_UNIX
 | 
						|
#ifdef Q_OS_MAC
 | 
						|
    if (!QDir::isAbsolutePath(path))
 | 
						|
        return QCoreApplication::applicationDirPath()
 | 
						|
                + QLatin1String("/../Resources/") + path;
 | 
						|
#else
 | 
						|
    const QString pathInShareDir = QCoreApplication::applicationDirPath()
 | 
						|
        + QLatin1String("/../share/")
 | 
						|
        + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
 | 
						|
        + QLatin1Char('/') + path;
 | 
						|
    if (QFileInfo(pathInShareDir).exists())
 | 
						|
        return pathInShareDir;
 | 
						|
#endif
 | 
						|
#endif
 | 
						|
    return path;
 | 
						|
}
 | 
						|
 | 
						|
QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
 | 
						|
    QDeclarativeView(parent),
 | 
						|
    m_d(new QmlApplicationViewerPrivate)
 | 
						|
{
 | 
						|
    connect(engine(), SIGNAL(quit()), SLOT(close()));
 | 
						|
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
 | 
						|
#ifdef QMLOBSERVER
 | 
						|
    new QmlObserver::QDeclarativeViewObserver(this, parent);
 | 
						|
#endif
 | 
						|
}
 | 
						|
 | 
						|
QmlApplicationViewer::~QmlApplicationViewer()
 | 
						|
{
 | 
						|
    delete m_d;
 | 
						|
}
 | 
						|
 | 
						|
void QmlApplicationViewer::setMainQmlFile(const QString &file)
 | 
						|
{
 | 
						|
    m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
 | 
						|
    setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
 | 
						|
}
 | 
						|
 | 
						|
void QmlApplicationViewer::addImportPath(const QString &path)
 | 
						|
{
 | 
						|
    engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
 | 
						|
}
 | 
						|
 | 
						|
void QmlApplicationViewer::setOrientation(Orientation orientation)
 | 
						|
{
 | 
						|
#ifdef Q_OS_SYMBIAN
 | 
						|
    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
 | 
						|
    }
 | 
						|
#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
 | 
						|
}
 | 
						|
 | 
						|
void QmlApplicationViewer::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);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |