qmlapplicationviewer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // checksum 0x28c7 version 0x2000a
  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(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
  27. #include <eikenv.h>
  28. #include <eikappui.h>
  29. #include <aknenv.h>
  30. #include <aknappui.h>
  31. #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
  32. #if defined(QMLJSDEBUGGER)
  33. // Enable debugging before any QDeclarativeEngine is created
  34. struct QmlJsDebuggingEnabler
  35. {
  36. QmlJsDebuggingEnabler()
  37. {
  38. QDeclarativeDebugHelper::enableDebugging();
  39. }
  40. };
  41. // Execute code in constructor before first QDeclarativeEngine is instantiated
  42. static QmlJsDebuggingEnabler enableDebuggingHelper;
  43. #endif // QMLJSDEBUGGER
  44. class QmlApplicationViewerPrivate
  45. {
  46. QString mainQmlFile;
  47. friend class QmlApplicationViewer;
  48. static QString adjustPath(const QString &path);
  49. };
  50. QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
  51. {
  52. #ifdef Q_OS_UNIX
  53. #ifdef Q_OS_MAC
  54. if (!QDir::isAbsolutePath(path))
  55. return QCoreApplication::applicationDirPath()
  56. + QLatin1String("/../Resources/") + path;
  57. #else
  58. const QString pathInShareDir = QCoreApplication::applicationDirPath()
  59. + QLatin1String("/../share/")
  60. + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
  61. + QLatin1Char('/') + path;
  62. if (QFileInfo(pathInShareDir).exists())
  63. return pathInShareDir;
  64. #endif
  65. #endif
  66. return path;
  67. }
  68. QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
  69. QDeclarativeView(parent),
  70. m_d(new QmlApplicationViewerPrivate)
  71. {
  72. connect(engine(), SIGNAL(quit()), SLOT(close()));
  73. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  74. #if defined(QMLJSDEBUGGER) && !defined(NO_JSDEBUGGER)
  75. new QmlJSDebugger::JSDebuggerAgent(engine());
  76. #endif
  77. #if defined(QMLJSDEBUGGER) && !defined(NO_QMLOBSERVER)
  78. new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
  79. #endif
  80. }
  81. QmlApplicationViewer::~QmlApplicationViewer()
  82. {
  83. delete m_d;
  84. }
  85. void QmlApplicationViewer::setMainQmlFile(const QString &file)
  86. {
  87. m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
  88. setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
  89. }
  90. void QmlApplicationViewer::addImportPath(const QString &path)
  91. {
  92. engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
  93. }
  94. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  95. {
  96. #ifdef Q_OS_SYMBIAN
  97. if (orientation != ScreenOrientationAuto) {
  98. #if defined(ORIENTATIONLOCK)
  99. const CAknAppUiBase::TAppUiOrientation uiOrientation =
  100. (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
  101. : CAknAppUi::EAppUiOrientationLandscape;
  102. CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
  103. TRAPD(error,
  104. if (appUi)
  105. appUi->SetOrientationL(uiOrientation);
  106. );
  107. Q_UNUSED(error)
  108. #else // ORIENTATIONLOCK
  109. qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
  110. #endif // ORIENTATIONLOCK
  111. }
  112. #elif defined(Q_WS_MAEMO_5)
  113. Qt::WidgetAttribute attribute;
  114. switch (orientation) {
  115. case ScreenOrientationLockPortrait:
  116. attribute = Qt::WA_Maemo5PortraitOrientation;
  117. break;
  118. case ScreenOrientationLockLandscape:
  119. attribute = Qt::WA_Maemo5LandscapeOrientation;
  120. break;
  121. case ScreenOrientationAuto:
  122. default:
  123. attribute = Qt::WA_Maemo5AutoOrientation;
  124. break;
  125. }
  126. setAttribute(attribute, true);
  127. #else // Q_OS_SYMBIAN
  128. Q_UNUSED(orientation);
  129. #endif // Q_OS_SYMBIAN
  130. }
  131. void QmlApplicationViewer::showExpanded()
  132. {
  133. #ifdef Q_OS_SYMBIAN
  134. showFullScreen();
  135. #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
  136. showMaximized();
  137. #else
  138. show();
  139. #endif
  140. }