mainwindow.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // checksum 0xfd0b version 0x20001
  2. /*
  3. * Copyright (c) 2011 Nokia Corporation.
  4. */
  5. #include "mainwindow.h"
  6. #include "ui_mainwindow.h"
  7. #include <QtCore/QCoreApplication>
  8. #include <QGraphicsRectItem>
  9. #include <QLinearGradient>
  10. #include <QDebug>
  11. #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
  12. #include <eikenv.h>
  13. #include <eikappui.h>
  14. #include <aknenv.h>
  15. #include <aknappui.h>
  16. #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
  17. void View::setOrientation(ScreenOrientation orientation)
  18. {
  19. #if defined(Q_OS_SYMBIAN)
  20. // If the version of Qt on the device is < 4.7.2, that attribute won't work
  21. if (orientation != ScreenOrientationAuto) {
  22. const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
  23. if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
  24. qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
  25. return;
  26. }
  27. }
  28. #endif // Q_OS_SYMBIAN
  29. Qt::WidgetAttribute attribute;
  30. switch (orientation) {
  31. #if QT_VERSION < 0x040702
  32. // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
  33. case ScreenOrientationLockPortrait:
  34. attribute = static_cast<Qt::WidgetAttribute>(128);
  35. break;
  36. case ScreenOrientationLockLandscape:
  37. attribute = static_cast<Qt::WidgetAttribute>(129);
  38. break;
  39. default:
  40. case ScreenOrientationAuto:
  41. attribute = static_cast<Qt::WidgetAttribute>(130);
  42. break;
  43. #else // QT_VERSION < 0x040702
  44. case ScreenOrientationLockPortrait:
  45. attribute = Qt::WA_LockPortraitOrientation;
  46. break;
  47. case ScreenOrientationLockLandscape:
  48. attribute = Qt::WA_LockLandscapeOrientation;
  49. break;
  50. default:
  51. case ScreenOrientationAuto:
  52. attribute = Qt::WA_AutoOrientation;
  53. break;
  54. #endif // QT_VERSION < 0x040702
  55. };
  56. setAttribute(attribute, true);
  57. }
  58. void View::showExpanded()
  59. {
  60. #ifdef Q_OS_SYMBIAN
  61. showFullScreen();
  62. #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
  63. showMaximized();
  64. #else
  65. show();
  66. #endif
  67. }
  68. View::View( QWidget * parent) : QDeclarativeView ( parent )
  69. {
  70. init();
  71. }
  72. View::View( const QUrl & source, QWidget * parent ) : QDeclarativeView ( source, parent )
  73. {
  74. init();
  75. }
  76. void View::init()
  77. {
  78. /***********************************************************************************************
  79. *Initialize the view, set the frame count and fps to 0. *
  80. *Set up the view to cache its background. *
  81. ***********************************************************************************************/
  82. m_frames = 0;
  83. m_fps = 0.0;
  84. setCacheMode(QGraphicsView::CacheBackground);
  85. setBackgroundBrush(Qt::black);
  86. /***********************************************************************************************
  87. *Prevent the viewport form accepting any focus. *
  88. *Set up the view to accept click and tab focus. *
  89. *Set up the scene to have sticky focus. *
  90. ***********************************************************************************************/
  91. viewport()->setFocusPolicy(Qt::NoFocus);
  92. setFocusPolicy(Qt::StrongFocus);
  93. scene()->setStickyFocus(true);
  94. }
  95. void View::resizeEvent(QResizeEvent *event)
  96. {
  97. /***********************************************************************************************
  98. *Call the base classes resizeEvent and scale the views contents so that it fits in the view. *
  99. ***********************************************************************************************/
  100. QDeclarativeView::resizeEvent(event);
  101. fittInView( QRect(0,0,1600,1000));
  102. viewport()->update();
  103. }
  104. void View::fittInView(const QRectF &rect)
  105. {
  106. QDeclarativeView::fitInView(rect, Qt::KeepAspectRatio);
  107. }
  108. void View::scrollContentsBy ( int, int)
  109. {
  110. /***********************************************************************************************
  111. *Prevent scrolling. *
  112. ***********************************************************************************************/
  113. }
  114. void View::paintEvent ( QPaintEvent * event )
  115. {
  116. QDeclarativeView::paintEvent(event);
  117. /***********************************************************************************************
  118. *Calculate fps. *
  119. ***********************************************************************************************/
  120. if (!(m_frames % 25)) {
  121. m_fps = m_frames /(m_time.elapsed() / 1000.0);
  122. /*******************************************************************************************
  123. *Notify that there is a new fps reading. *
  124. *******************************************************************************************/
  125. emit fpsChanged();
  126. m_time.start();
  127. m_frames = 0;
  128. }
  129. ++m_frames;
  130. }