00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "qmlapplicationviewer.h"
00012
00013 #include <QtCore/QCoreApplication>
00014 #include <QtCore/QDir>
00015 #include <QtCore/QFileInfo>
00016 #include <QtDeclarative/QDeclarativeComponent>
00017 #include <QtDeclarative/QDeclarativeEngine>
00018 #include <QtDeclarative/QDeclarativeContext>
00019
00020 #if defined(QMLJSDEBUGGER)
00021 #include <qt_private/qdeclarativedebughelper_p.h>
00022 #endif
00023
00024 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00025 #include <jsdebuggeragent.h>
00026 #endif
00027 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00028 #include <qdeclarativeviewobserver.h>
00029 #endif
00030
00031 #if defined(QMLJSDEBUGGER)
00032
00033
00034 struct QmlJsDebuggingEnabler
00035 {
00036 QmlJsDebuggingEnabler()
00037 {
00038 QDeclarativeDebugHelper::enableDebugging();
00039 }
00040 };
00041
00042
00043 static QmlJsDebuggingEnabler enableDebuggingHelper;
00044
00045 #endif // QMLJSDEBUGGER
00046
00047 class QmlApplicationViewerPrivate
00048 {
00049 QString mainQmlFile;
00050 friend class QmlApplicationViewer;
00051 static QString adjustPath(const QString &path);
00052 };
00053
00054 QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
00055 {
00056 #ifdef Q_OS_UNIX
00057 #ifdef Q_OS_MAC
00058 if (!QDir::isAbsolutePath(path))
00059 return QCoreApplication::applicationDirPath()
00060 + QLatin1String("/../Resources/") + path;
00061 #else
00062 const QString pathInShareDir = QCoreApplication::applicationDirPath()
00063 + QLatin1String("/../share/")
00064 + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
00065 + QLatin1Char('/') + path;
00066 if (QFileInfo(pathInShareDir).exists())
00067 return pathInShareDir;
00068 #endif
00069 #endif
00070 return path;
00071 }
00072
00073 QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
00074 QDeclarativeView(parent),
00075 m_d(new QmlApplicationViewerPrivate)
00076 {
00077 connect(engine(), SIGNAL(quit()), SLOT(close()));
00078 setResizeMode(QDeclarativeView::SizeRootObjectToView);
00079 #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
00080 new QmlJSDebugger::JSDebuggerAgent(engine());
00081 #endif
00082 #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
00083 new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
00084 #endif
00085 }
00086
00087 QmlApplicationViewer::~QmlApplicationViewer()
00088 {
00089 delete m_d;
00090 }
00091
00092 void QmlApplicationViewer::setMainQmlFile(const QString &file)
00093 {
00094 m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
00095 setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
00096 }
00097
00098 void QmlApplicationViewer::addImportPath(const QString &path)
00099 {
00100 engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
00101 }
00102
00103 void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
00104 {
00105 #if defined(Q_OS_SYMBIAN)
00106
00107 if (orientation != ScreenOrientationAuto) {
00108 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
00109 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
00110 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
00111 return;
00112 }
00113 }
00114 #endif // Q_OS_SYMBIAN
00115
00116 Qt::WidgetAttribute attribute;
00117 switch (orientation) {
00118 #if QT_VERSION < 0x040702
00119
00120 case ScreenOrientationLockPortrait:
00121 attribute = static_cast<Qt::WidgetAttribute>(128);
00122 break;
00123 case ScreenOrientationLockLandscape:
00124 attribute = static_cast<Qt::WidgetAttribute>(129);
00125 break;
00126 default:
00127 case ScreenOrientationAuto:
00128 attribute = static_cast<Qt::WidgetAttribute>(130);
00129 break;
00130 #else // QT_VERSION < 0x040702
00131 case ScreenOrientationLockPortrait:
00132 attribute = Qt::WA_LockPortraitOrientation;
00133 break;
00134 case ScreenOrientationLockLandscape:
00135 attribute = Qt::WA_LockLandscapeOrientation;
00136 break;
00137 default:
00138 case ScreenOrientationAuto:
00139 attribute = Qt::WA_AutoOrientation;
00140 break;
00141 #endif // QT_VERSION < 0x040702
00142 };
00143 setAttribute(attribute, true);
00144 }
00145
00146 void QmlApplicationViewer::showExpanded()
00147 {
00148 #ifdef Q_OS_SYMBIAN
00149 showFullScreen();
00150 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
00151 showMaximized();
00152 #else
00153 show();
00154 #endif
00155 }