/cameraview.cpp

http://cutefootball.googlecode.com/ · C++ · 113 lines · 73 code · 13 blank · 27 comment · 3 complexity · 3444317186e876dbe4171aa71e7513cd MD5 · raw file

  1. /*
  2. * Copyright 2010,2011 Timothy Rochford
  3. *
  4. * This file is part of CuteFootball.
  5. *
  6. * CuteFootball is free software: you can redistribute it and/or modify
  7. * it under the terms of the Lesser GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * CuteFootball is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * Lesser GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the Lesser GNU General Public License
  17. * along with CuteFootball. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "cameraview.h"
  21. #include <QGraphicsItem>
  22. #include <QGraphicsProxyWidget>
  23. #include <QDebug>
  24. CameraView::CameraView(QGraphicsView& v,
  25. QObject *parent) :
  26. QObject(parent),
  27. m_view(v),
  28. m_object(NULL),
  29. m_position(QPointF())
  30. {
  31. }
  32. void CameraView::centerOn(QGraphicsItem *object)
  33. {
  34. // qDebug() << "CameraView::centerOn(QGraphicsItem *object)";
  35. m_object = object;
  36. m_position = QPointF();
  37. m_view.centerOn(object);
  38. }
  39. void CameraView::centerOn(QPointF position)
  40. {
  41. // qDebug() << "CameraView::centerOn(QPointF position)";
  42. m_position = position;
  43. m_object = NULL;
  44. m_view.centerOn(position);
  45. }
  46. QPointF CameraView::topLeft() const
  47. {
  48. return m_view.mapToScene(m_view.rect().topLeft());
  49. }
  50. QPointF CameraView::bottomRight() const
  51. {
  52. return m_view.mapToScene(m_view.rect().bottomRight());
  53. }
  54. QPointF CameraView::center() const
  55. {
  56. return m_view.mapToScene(m_view.rect().center() );
  57. }
  58. void CameraView::update()
  59. {
  60. if (m_object) {
  61. // qDebug() << "CameraView::update - object";
  62. m_view.centerOn(m_object);
  63. } else {
  64. // qDebug() << "CameraView::update - pos";
  65. m_view.centerOn(m_position);
  66. }
  67. // if (m_cameraView->centeredItem() == m_ball) {
  68. // m_cameraView->centerOn(m_ball);
  69. // }
  70. // m_screenGraphicsFrameProxy->setPos(m_cameraView->topLeft());
  71. QPointF point;
  72. foreach(PositionedProxyWidget item, m_widgets) {
  73. switch (item.viewPos) {
  74. case TopLeft:
  75. point = topLeft();
  76. break;
  77. case Center:
  78. point = center();
  79. point = QPointF(point.x() - item.widget->rect().width() / 2,point.y());
  80. break;
  81. case BottomRight:
  82. {
  83. QPointF tmp = QPointF(bottomRight());
  84. point = tmp - QPointF(item.widget->size().width(),item.widget->size().height());
  85. }
  86. break;
  87. default:
  88. point = topLeft();
  89. break;
  90. }
  91. item.widget->setPos(point);
  92. }
  93. }
  94. void CameraView::appendProxyWidget(QGraphicsProxyWidget *item, ViewPosition viewPos )
  95. {
  96. PositionedProxyWidget pw;
  97. pw.viewPos = viewPos;
  98. pw.widget = item;
  99. m_widgets.append(pw);
  100. }