qmlapplicationviewer.cpp 4.5 KB

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