qmlapplicationviewer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // checksum 0x7f25 version 0x6000f
  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/QCoreApplication>
  12. #include <QtCore/QDir>
  13. #include <QtCore/QFileInfo>
  14. #include <QtDeclarative/QDeclarativeComponent>
  15. #include <QtDeclarative/QDeclarativeEngine>
  16. #include <QtDeclarative/QDeclarativeContext>
  17. #include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
  18. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  19. #include <qt_private/qdeclarativedebughelper_p.h>
  20. #if !defined(NO_JSDEBUGGER)
  21. #include <jsdebuggeragent.h>
  22. #endif
  23. #if !defined(NO_QMLOBSERVER)
  24. #include <qdeclarativeviewobserver.h>
  25. #endif
  26. // Enable debugging before any QDeclarativeEngine is created
  27. struct QmlJsDebuggingEnabler
  28. {
  29. QmlJsDebuggingEnabler()
  30. {
  31. QDeclarativeDebugHelper::enableDebugging();
  32. }
  33. };
  34. // Execute code in constructor before first QDeclarativeEngine is instantiated
  35. static QmlJsDebuggingEnabler enableDebuggingHelper;
  36. #endif // QMLJSDEBUGGER
  37. class QmlApplicationViewerPrivate
  38. {
  39. QString mainQmlFile;
  40. friend class QmlApplicationViewer;
  41. static QString adjustPath(const QString &path);
  42. };
  43. QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
  44. {
  45. #ifdef Q_OS_UNIX
  46. #ifdef Q_OS_MAC
  47. if (!QDir::isAbsolutePath(path))
  48. return QCoreApplication::applicationDirPath()
  49. + QLatin1String("/../Resources/") + path;
  50. #else
  51. const QString pathInInstallDir = QCoreApplication::applicationDirPath()
  52. + QLatin1String("/../") + path;
  53. if (pathInInstallDir.contains(QLatin1String("opt"))
  54. && pathInInstallDir.contains(QLatin1String("bin"))
  55. && QFileInfo(pathInInstallDir).exists()) {
  56. return pathInInstallDir;
  57. }
  58. #endif
  59. #endif
  60. return path;
  61. }
  62. QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
  63. QDeclarativeView(parent),
  64. m_d(new QmlApplicationViewerPrivate)
  65. {
  66. connect(engine(), SIGNAL(quit()), SLOT(close()));
  67. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  68. // Qt versions prior to 4.8.0 don't have QML/JS debugging services built in
  69. #if defined(QMLJSDEBUGGER) && QT_VERSION < 0x040800
  70. #if !defined(NO_JSDEBUGGER)
  71. new QmlJSDebugger::JSDebuggerAgent(engine());
  72. #endif
  73. #if !defined(NO_QMLOBSERVER)
  74. new QmlJSDebugger::QDeclarativeViewObserver(this, this);
  75. #endif
  76. #endif
  77. }
  78. QmlApplicationViewer::~QmlApplicationViewer()
  79. {
  80. delete m_d;
  81. }
  82. void QmlApplicationViewer::setMainQmlFile(const QString &file)
  83. {
  84. m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
  85. setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
  86. }
  87. void QmlApplicationViewer::addImportPath(const QString &path)
  88. {
  89. engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
  90. }
  91. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  92. {
  93. #if defined(Q_OS_SYMBIAN)
  94. // If the version of Qt on the device is < 4.7.2, that attribute won't work
  95. if (orientation != ScreenOrientationAuto) {
  96. const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
  97. if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
  98. qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
  99. return;
  100. }
  101. }
  102. #endif // Q_OS_SYMBIAN
  103. Qt::WidgetAttribute attribute;
  104. switch (orientation) {
  105. #if QT_VERSION < 0x040702
  106. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  107. case ScreenOrientationLockPortrait:
  108. attribute = static_cast<Qt::WidgetAttribute>(128);
  109. break;
  110. case ScreenOrientationLockLandscape:
  111. attribute = static_cast<Qt::WidgetAttribute>(129);
  112. break;
  113. default:
  114. case ScreenOrientationAuto:
  115. attribute = static_cast<Qt::WidgetAttribute>(130);
  116. break;
  117. #else // QT_VERSION < 0x040702
  118. case ScreenOrientationLockPortrait:
  119. attribute = Qt::WA_LockPortraitOrientation;
  120. break;
  121. case ScreenOrientationLockLandscape:
  122. attribute = Qt::WA_LockLandscapeOrientation;
  123. break;
  124. default:
  125. case ScreenOrientationAuto:
  126. attribute = Qt::WA_AutoOrientation;
  127. break;
  128. #endif // QT_VERSION < 0x040702
  129. };
  130. setAttribute(attribute, true);
  131. }
  132. void QmlApplicationViewer::showExpanded()
  133. {
  134. #if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_SIMULATOR)
  135. showFullScreen();
  136. #elif defined(Q_WS_MAEMO_5)
  137. showMaximized();
  138. #else
  139. show();
  140. #endif
  141. }