qmlapplicationviewer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // checksum 0x78c version 0x60010
  2. /*
  3. This file was generated by the Qt Quick Application wizard of Qt Creator.
  4. QmlApplicationViewer is a convenience class containing mobile device specific
  5. code such as screen orientation handling. Also QML paths and debugging are
  6. handled here.
  7. It is recommended not to modify this file, since newer versions of Qt Creator
  8. may offer an updated version of it.
  9. */
  10. #include "qmlapplicationviewer.h"
  11. #include <QtCore/QDir>
  12. #include <QtCore/QFileInfo>
  13. #include <QtDeclarative/QDeclarativeComponent>
  14. #include <QtDeclarative/QDeclarativeEngine>
  15. #include <QtDeclarative/QDeclarativeContext>
  16. #include <QtGui/QApplication>
  17. #include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
  18. #ifdef HARMATTAN_BOOSTER
  19. #include <MDeclarativeCache>
  20. #endif
  21. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  22. #include <qt_private/qdeclarativedebughelper_p.h>
  23. #if !defined(NO_JSDEBUGGER)
  24. #include <jsdebuggeragent.h>
  25. #endif
  26. #if !defined(NO_QMLOBSERVER)
  27. #include <qdeclarativeviewobserver.h>
  28. #endif
  29. // Enable debugging before any QDeclarativeEngine is created
  30. struct QmlJsDebuggingEnabler
  31. {
  32. QmlJsDebuggingEnabler()
  33. {
  34. QDeclarativeDebugHelper::enableDebugging();
  35. }
  36. };
  37. // Execute code in constructor before first QDeclarativeEngine is instantiated
  38. static QmlJsDebuggingEnabler enableDebuggingHelper;
  39. #endif // QMLJSDEBUGGER
  40. class QmlApplicationViewerPrivate
  41. {
  42. QmlApplicationViewerPrivate(QDeclarativeView *view_) : view(view_) {}
  43. QString mainQmlFile;
  44. QDeclarativeView *view;
  45. friend class QmlApplicationViewer;
  46. QString adjustPath(const QString &path);
  47. };
  48. QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
  49. {
  50. #ifdef Q_OS_UNIX
  51. #ifdef Q_OS_MAC
  52. if (!QDir::isAbsolutePath(path))
  53. return QCoreApplication::applicationDirPath()
  54. + QLatin1String("/../Resources/") + path;
  55. #else
  56. QString pathInInstallDir;
  57. const QString applicationDirPath = QCoreApplication::applicationDirPath();
  58. pathInInstallDir = QString::fromAscii("%1/../%2").arg(applicationDirPath, path);
  59. if (QFileInfo(pathInInstallDir).exists())
  60. return pathInInstallDir;
  61. #endif
  62. #endif
  63. return path;
  64. }
  65. QmlApplicationViewer::QmlApplicationViewer(QWidget *parent)
  66. : QDeclarativeView(parent)
  67. , d(new QmlApplicationViewerPrivate(this))
  68. {
  69. connect(engine(), SIGNAL(quit()), SLOT(close()));
  70. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  71. // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
  72. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  73. #if !defined(NO_JSDEBUGGER)
  74. new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
  75. #endif
  76. #if !defined(NO_QMLOBSERVER)
  77. new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
  78. #endif
  79. #endif
  80. }
  81. QmlApplicationViewer::QmlApplicationViewer(QDeclarativeView *view, QWidget *parent)
  82. : QDeclarativeView(parent)
  83. , d(new QmlApplicationViewerPrivate(view))
  84. {
  85. connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
  86. view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
  87. // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
  88. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  89. #if !defined(NO_JSDEBUGGER)
  90. new QmlJSDebugger::JSDebuggerAgent(d->view->engine());
  91. #endif
  92. #if !defined(NO_QMLOBSERVER)
  93. new QmlJSDebugger::QDeclarativeViewObserver(d->view, d->view);
  94. #endif
  95. #endif
  96. }
  97. QmlApplicationViewer::~QmlApplicationViewer()
  98. {
  99. delete d;
  100. }
  101. QmlApplicationViewer *QmlApplicationViewer::create()
  102. {
  103. #ifdef HARMATTAN_BOOSTER
  104. return new QmlApplicationViewer(MDeclarativeCache::qDeclarativeView(), 0);
  105. #else
  106. return new QmlApplicationViewer();
  107. #endif
  108. }
  109. void QmlApplicationViewer::setMainQmlFile(const QString &file)
  110. {
  111. d->mainQmlFile = d->adjustPath(file);
  112. d->view->setSource(QUrl::fromLocalFile(d->mainQmlFile));
  113. }
  114. void QmlApplicationViewer::addImportPath(const QString &path)
  115. {
  116. d->view->engine()->addImportPath(d->adjustPath(path));
  117. }
  118. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  119. {
  120. #if defined(Q_OS_SYMBIAN)
  121. // If the version of Qt on the device is < 4.7.2, that attribute won't work
  122. if (orientation != ScreenOrientationAuto) {
  123. const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
  124. if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
  125. qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
  126. return;
  127. }
  128. }
  129. #endif // Q_OS_SYMBIAN
  130. Qt::WidgetAttribute attribute;
  131. switch (orientation) {
  132. #if QT_VERSION < 0x040702
  133. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  134. case ScreenOrientationLockPortrait:
  135. attribute = static_cast<Qt::WidgetAttribute>(128);
  136. break;
  137. case ScreenOrientationLockLandscape:
  138. attribute = static_cast<Qt::WidgetAttribute>(129);
  139. break;
  140. default:
  141. case ScreenOrientationAuto:
  142. attribute = static_cast<Qt::WidgetAttribute>(130);
  143. break;
  144. #else // QT_VERSION < 0x040702
  145. case ScreenOrientationLockPortrait:
  146. attribute = Qt::WA_LockPortraitOrientation;
  147. break;
  148. case ScreenOrientationLockLandscape:
  149. attribute = Qt::WA_LockLandscapeOrientation;
  150. break;
  151. default:
  152. case ScreenOrientationAuto:
  153. attribute = Qt::WA_AutoOrientation;
  154. break;
  155. #endif // QT_VERSION < 0x040702
  156. };
  157. setAttribute(attribute, true);
  158. }
  159. void QmlApplicationViewer::showExpanded()
  160. {
  161. #if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
  162. d->view->showFullScreen();
  163. #elif defined(Q_WS_MAEMO_5)
  164. d->view->showMaximized();
  165. #else
  166. d->view->show();
  167. #endif
  168. }
  169. QApplication *createApplication(int &argc, char **argv)
  170. {
  171. #ifdef HARMATTAN_BOOSTER
  172. return MDeclarativeCache::qApplication(argc, argv);
  173. #else
  174. return new QApplication(argc, argv);
  175. #endif
  176. }