qmlapplicationviewer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // checksum 0x7895 version 0x4000b
  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 pathInInstallDir = QCoreApplication::applicationDirPath()
  53. + QLatin1String("/../") + path;
  54. if (pathInInstallDir.contains(QLatin1String("opt"))
  55. && pathInInstallDir.contains(QLatin1String("bin"))
  56. && QFileInfo(pathInInstallDir).exists()) {
  57. return pathInInstallDir;
  58. }
  59. #endif
  60. #endif
  61. return path;
  62. }
  63. QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
  64. QDeclarativeView(parent),
  65. m_d(new QmlApplicationViewerPrivate)
  66. {
  67. connect(engine(), SIGNAL(quit()), SLOT(close()));
  68. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  69. #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
  70. new QmlJSDebugger::JSDebuggerAgent(engine());
  71. #endif
  72. #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
  73. new QmlJSDebugger::QDeclarativeViewObserver(this, this);
  74. #endif
  75. }
  76. QmlApplicationViewer::~QmlApplicationViewer()
  77. {
  78. delete m_d;
  79. }
  80. void QmlApplicationViewer::setMainQmlFile(const QString &file)
  81. {
  82. m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
  83. setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
  84. }
  85. void QmlApplicationViewer::addImportPath(const QString &path)
  86. {
  87. engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
  88. }
  89. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  90. {
  91. #if defined(Q_OS_SYMBIAN)
  92. // If the version of Qt on the device is < 4.7.2, that attribute won't work
  93. if (orientation != ScreenOrientationAuto) {
  94. const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
  95. if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
  96. qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
  97. return;
  98. }
  99. }
  100. #endif // Q_OS_SYMBIAN
  101. Qt::WidgetAttribute attribute;
  102. switch (orientation) {
  103. #if QT_VERSION < 0x040702
  104. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  105. case ScreenOrientationLockPortrait:
  106. attribute = static_cast<Qt::WidgetAttribute>(128);
  107. break;
  108. case ScreenOrientationLockLandscape:
  109. attribute = static_cast<Qt::WidgetAttribute>(129);
  110. break;
  111. default:
  112. case ScreenOrientationAuto:
  113. attribute = static_cast<Qt::WidgetAttribute>(130);
  114. break;
  115. #else // QT_VERSION < 0x040702
  116. case ScreenOrientationLockPortrait:
  117. attribute = Qt::WA_LockPortraitOrientation;
  118. break;
  119. case ScreenOrientationLockLandscape:
  120. attribute = Qt::WA_LockLandscapeOrientation;
  121. break;
  122. default:
  123. case ScreenOrientationAuto:
  124. attribute = Qt::WA_AutoOrientation;
  125. break;
  126. #endif // QT_VERSION < 0x040702
  127. };
  128. setAttribute(attribute, true);
  129. }
  130. void QmlApplicationViewer::showExpanded()
  131. {
  132. #ifdef Q_OS_SYMBIAN
  133. showFullScreen();
  134. #elif defined(Q_WS_MAEMO_5)
  135. showMaximized();
  136. #else
  137. show();
  138. #endif
  139. }