PageRenderTime 35ms CodeModel.GetById 7ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 24#include <QDebug>
 25
 26CameraView::CameraView(QGraphicsView& v,
 27                       QObject *parent) :
 28    QObject(parent),
 29    m_view(v),
 30    m_object(NULL),
 31    m_position(QPointF())
 32{
 33}
 34
 35void CameraView::centerOn(QGraphicsItem *object)
 36{
 37//    qDebug() << "CameraView::centerOn(QGraphicsItem *object)";
 38    m_object = object;
 39    m_position = QPointF();
 40    m_view.centerOn(object);
 41}
 42
 43void CameraView::centerOn(QPointF position)
 44{
 45//    qDebug() << "CameraView::centerOn(QPointF position)";
 46    m_position = position;
 47    m_object = NULL;
 48    m_view.centerOn(position);
 49}
 50
 51QPointF CameraView::topLeft() const
 52{
 53    return m_view.mapToScene(m_view.rect().topLeft());
 54}
 55QPointF CameraView::bottomRight() const
 56{
 57    return m_view.mapToScene(m_view.rect().bottomRight());
 58}
 59
 60QPointF CameraView::center() const
 61{
 62    return m_view.mapToScene(m_view.rect().center() );
 63}
 64
 65void CameraView::update()
 66{
 67    if (m_object) {
 68//        qDebug() << "CameraView::update - object";
 69        m_view.centerOn(m_object);
 70    } else  {
 71//        qDebug() << "CameraView::update - pos";
 72        m_view.centerOn(m_position);
 73    }
 74
 75    //    if (m_cameraView->centeredItem() == m_ball) {
 76    //        m_cameraView->centerOn(m_ball);
 77    //    }
 78    //    m_screenGraphicsFrameProxy->setPos(m_cameraView->topLeft());
 79
 80    QPointF point;
 81    foreach(PositionedProxyWidget item, m_widgets) {
 82        switch (item.viewPos) {
 83        case TopLeft:
 84            point = topLeft();
 85            break;
 86        case Center:
 87            point = center();
 88            point = QPointF(point.x() - item.widget->rect().width() / 2,point.y());
 89            break;
 90        case BottomRight:
 91            {
 92            QPointF tmp = QPointF(bottomRight());
 93            point = tmp - QPointF(item.widget->size().width(),item.widget->size().height());
 94            }
 95            break;
 96        default:
 97            point = topLeft();
 98            break;
 99        }
100
101        item.widget->setPos(point);
102    }
103}
104
105void CameraView::appendProxyWidget(QGraphicsProxyWidget *item, ViewPosition viewPos )
106{
107      PositionedProxyWidget pw;
108      pw.viewPos = viewPos;
109      pw.widget = item;
110
111      m_widgets.append(pw);
112}
113