myqmlapplicationviewer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "myqmlapplicationviewer.h"
  5. #include <QDeclarativeEngine>
  6. #include <QGraphicsObject>
  7. #include <QTimer>
  8. #include <QUrl>
  9. /*!
  10. \class MyQmlApplicationViewer
  11. \brief Contains simple methods for screen orientation and QML UI handling.
  12. */
  13. /*!
  14. Constructor.
  15. */
  16. MyQmlApplicationViewer::MyQmlApplicationViewer(QWidget *parent) :
  17. QDeclarativeView(parent)
  18. {
  19. }
  20. /*!
  21. Destructor.
  22. */
  23. MyQmlApplicationViewer::~MyQmlApplicationViewer()
  24. {
  25. }
  26. /*!
  27. Sets the main QML file as \a file.
  28. */
  29. void MyQmlApplicationViewer::setMainQmlFile(const QString &file)
  30. {
  31. m_mainQml = file;
  32. }
  33. /*!
  34. Launches the application with a splash screen defined by \a file.
  35. */
  36. void MyQmlApplicationViewer::launchWithSplashQmlFile(const QString &file)
  37. {
  38. setSource(QUrl(file));
  39. showExpanded();
  40. // To delay the loading of main QML file so that the splash screen
  41. // would show, we use single shot timer.
  42. QTimer::singleShot(0, this, SLOT(loadMainQml()));
  43. }
  44. /*!
  45. Sets the screen orientation as \a orientation.
  46. */
  47. void MyQmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  48. {
  49. #if defined(Q_OS_SYMBIAN)
  50. // If the version of Qt on the device is < 4.7.2, that attribute won't work
  51. if (orientation != ScreenOrientationAuto) {
  52. const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
  53. if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
  54. qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
  55. return;
  56. }
  57. }
  58. #endif // Q_OS_SYMBIAN
  59. Qt::WidgetAttribute attribute;
  60. switch (orientation) {
  61. #if QT_VERSION < 0x040702
  62. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  63. case ScreenOrientationLockPortrait:
  64. attribute = static_cast<Qt::WidgetAttribute>(128);
  65. break;
  66. case ScreenOrientationLockLandscape:
  67. attribute = static_cast<Qt::WidgetAttribute>(129);
  68. break;
  69. default:
  70. case ScreenOrientationAuto:
  71. attribute = static_cast<Qt::WidgetAttribute>(130);
  72. break;
  73. #else // QT_VERSION < 0x040702
  74. case ScreenOrientationLockPortrait:
  75. attribute = Qt::WA_LockPortraitOrientation;
  76. break;
  77. case ScreenOrientationLockLandscape:
  78. attribute = Qt::WA_LockLandscapeOrientation;
  79. break;
  80. default:
  81. case ScreenOrientationAuto:
  82. attribute = Qt::WA_AutoOrientation;
  83. break;
  84. #endif // QT_VERSION < 0x040702
  85. };
  86. setAttribute(attribute, true);
  87. }
  88. /*!
  89. Shows the UI expanded.
  90. */
  91. void MyQmlApplicationViewer::showExpanded()
  92. {
  93. #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR) || \
  94. defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
  95. showFullScreen();
  96. #else
  97. show();
  98. #endif
  99. }
  100. /*!
  101. Loads the set main QML file.
  102. */
  103. void MyQmlApplicationViewer::loadMainQml()
  104. {
  105. connect(engine(), SIGNAL(quit()), SLOT(close()));
  106. setSource(QUrl(m_mainQml));
  107. #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
  108. int initWidth(width());
  109. int initHeight(height());
  110. rootObject()->setProperty("width", initWidth);
  111. rootObject()->setProperty("height", initHeight);
  112. setGeometry(0, 0, initWidth, initHeight);
  113. #endif
  114. }