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