qmlapplicationviewer.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // checksum 0xbd34 version 0x80016
  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 <QDir>
  12. #include <QFileInfo>
  13. #include <QApplication>
  14. #include <QDeclarativeComponent>
  15. #include <QDeclarativeEngine>
  16. #include <QDeclarativeContext>
  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. QString mainQmlFile;
  43. friend class QmlApplicationViewer;
  44. static QString adjustPath(const QString &path);
  45. };
  46. QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
  47. {
  48. #ifdef Q_OS_MAC
  49. if (!QDir::isAbsolutePath(path))
  50. return QString::fromLatin1("%1/../Resources/%2")
  51. .arg(QCoreApplication::applicationDirPath(), path);
  52. #elif defined(Q_OS_QNX)
  53. if (!QDir::isAbsolutePath(path))
  54. return QString::fromLatin1("app/native/%1").arg(path);
  55. #elif !defined(Q_OS_ANDROID)
  56. QString pathInInstallDir =
  57. QString::fromLatin1("%1/../%2").arg(QCoreApplication::applicationDirPath(), path);
  58. if (QFileInfo(pathInInstallDir).exists())
  59. return pathInInstallDir;
  60. pathInInstallDir =
  61. QString::fromLatin1("%1/%2").arg(QCoreApplication::applicationDirPath(), path);
  62. if (QFileInfo(pathInInstallDir).exists())
  63. return pathInInstallDir;
  64. #endif
  65. return path;
  66. }
  67. QmlApplicationViewer::QmlApplicationViewer(QWidget *parent)
  68. : QDeclarativeView(parent)
  69. , d(new QmlApplicationViewerPrivate())
  70. {
  71. connect(engine(), SIGNAL(quit()), SLOT(close()));
  72. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  73. // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
  74. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  75. #if !defined(NO_JSDEBUGGER)
  76. new QmlJSDebugger::JSDebuggerAgent(engine());
  77. #endif
  78. #if !defined(NO_QMLOBSERVER)
  79. new QmlJSDebugger::QDeclarativeViewObserver(this, this);
  80. #endif
  81. #endif
  82. }
  83. QmlApplicationViewer::~QmlApplicationViewer()
  84. {
  85. delete d;
  86. }
  87. QmlApplicationViewer *QmlApplicationViewer::create()
  88. {
  89. return new QmlApplicationViewer();
  90. }
  91. void QmlApplicationViewer::setMainQmlFile(const QString &file)
  92. {
  93. d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
  94. #ifdef Q_OS_ANDROID
  95. setSource(QUrl(QLatin1String("assets:/")+d->mainQmlFile));
  96. #else
  97. setSource(QUrl::fromLocalFile(d->mainQmlFile));
  98. #endif
  99. }
  100. void QmlApplicationViewer::addImportPath(const QString &path)
  101. {
  102. engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
  103. }
  104. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  105. {
  106. #if QT_VERSION < 0x050000
  107. Qt::WidgetAttribute attribute;
  108. switch (orientation) {
  109. #if QT_VERSION < 0x040702
  110. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  111. case ScreenOrientationLockPortrait:
  112. attribute = static_cast<Qt::WidgetAttribute>(128);
  113. break;
  114. case ScreenOrientationLockLandscape:
  115. attribute = static_cast<Qt::WidgetAttribute>(129);
  116. break;
  117. default:
  118. case ScreenOrientationAuto:
  119. attribute = static_cast<Qt::WidgetAttribute>(130);
  120. break;
  121. #else // QT_VERSION < 0x040702
  122. case ScreenOrientationLockPortrait:
  123. attribute = Qt::WA_LockPortraitOrientation;
  124. break;
  125. case ScreenOrientationLockLandscape:
  126. attribute = Qt::WA_LockLandscapeOrientation;
  127. break;
  128. default:
  129. case ScreenOrientationAuto:
  130. attribute = Qt::WA_AutoOrientation;
  131. break;
  132. #endif // QT_VERSION < 0x040702
  133. };
  134. setAttribute(attribute, true);
  135. #else // QT_VERSION < 0x050000
  136. Q_UNUSED(orientation)
  137. #endif // QT_VERSION < 0x050000
  138. }
  139. void QmlApplicationViewer::showExpanded()
  140. {
  141. #if defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
  142. showFullScreen();
  143. #elif defined(Q_WS_MAEMO_5) || defined(Q_OS_QNX)
  144. showMaximized();
  145. #else
  146. show();
  147. #endif
  148. }
  149. QApplication *createApplication(int &argc, char **argv)
  150. {
  151. #ifdef HARMATTAN_BOOSTER
  152. return MDeclarativeCache::qApplication(argc, argv);
  153. #else
  154. return new QApplication(argc, argv);
  155. #endif
  156. }