Go to the documentation of this file.00001 #include "Camera.h"
00002
00003 #include <QtGui>
00004
00005 const int LINE_POS = 70;
00006
00007 CameraWidget::CameraWidget(QWidget *parent)
00008 : QGraphicsView(parent), timerId(0)
00009 {
00010
00011 QGraphicsScene *scene = new QGraphicsScene(this);
00012 scene->setItemIndexMethod(QGraphicsScene::NoIndex);
00013 scene->setSceneRect(0, 00, 440,380);
00014
00015
00016 setScene(scene);
00017 setCacheMode(CacheBackground);
00018 setViewportUpdateMode(BoundingRectViewportUpdate);
00019 setRenderHint(QPainter::Antialiasing);
00020 setTransformationAnchor(AnchorUnderMouse);
00021 scale(qreal(1), qreal(1));
00022 setMinimumSize(210, 110);
00023 setWindowTitle(tr("Camera"));
00024
00025
00026 QGraphicsLineItem *line = new QGraphicsLineItem(this->sceneRect().right()-LINE_POS ,this->sceneRect().top(),this->sceneRect().right()-LINE_POS ,this->sceneRect().bottom());
00027 scene->addItem(line);
00028
00029
00030 timerId = startTimer(1000 / 25);
00031 }
00032
00033
00034 void CameraWidget::keyPressEvent(QKeyEvent *event)
00035 {
00036 switch (event->key()) {
00037
00038 case Qt::Key_Plus:
00039 scaleView(qreal(1.2));
00040 break;
00041 case Qt::Key_Minus:
00042 scaleView(1 / qreal(1.2));
00043 break;
00044 default:
00045 QGraphicsView::keyPressEvent(event);
00046 }
00047 }
00048 void CameraWidget::timerEvent(QTimerEvent *event)
00049 {
00050 Q_UNUSED(event);
00051
00052 QList<QGraphicsPixmapItem *> pictures;
00053 foreach (QGraphicsItem *item, scene()->items()) {
00054 if (QGraphicsPixmapItem *p = qgraphicsitem_cast<QGraphicsPixmapItem *>(item))
00055 pictures << p;
00056 }
00057
00058
00059 foreach (QGraphicsPixmapItem *p, pictures){
00060
00061 if(p->scale()>0.1){
00062 if((p->pixmap().size().width()*0.5+p->pos().x()) > (this->sceneRect().right() - LINE_POS)){
00063 p->setPos(this->sceneRect().right()-70,p->pos().y());
00064 p->setScale(0.1);
00065
00066 }
00067 }
00068 else{
00069 if((p->pixmap().size().width()*0.1+p->pos().x()) < (this->sceneRect().right() - LINE_POS)){
00070 p->setScale(0.5);
00071 p->setPos(p->pos().x()+(p->pixmap().size().width()*0.1)-p->pixmap().size().width()*0.5,p->pos().y());
00072 }
00073 }
00074
00075 }
00076 }
00077
00078
00079
00080 void CameraWidget::wheelEvent(QWheelEvent *event)
00081 {
00082 scaleView(pow((double)2, -event->delta() / 240.0));
00083 }
00084
00085 void CameraWidget::drawBackground(QPainter *painter, const QRectF &rect)
00086 {
00087 Q_UNUSED(rect);
00088
00089
00090 QRectF sceneRect = this->sceneRect();
00091 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
00092 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
00093 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
00094 painter->fillRect(rightShadow, Qt::darkGray);
00095 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
00096 painter->fillRect(bottomShadow, Qt::darkGray);
00097
00098
00099
00100 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
00101 gradient.setColorAt(0, Qt::white);
00102 gradient.setColorAt(1, Qt::lightGray);
00103 painter->fillRect(rect.intersect(sceneRect), gradient);
00104 painter->setBrush(Qt::NoBrush);
00105 painter->drawRect(sceneRect);
00106
00107
00108
00109 }
00110
00111 void CameraWidget::scaleView(qreal scaleFactor)
00112 {
00113 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
00114 if (factor < 0.07 || factor > 100)
00115 return;
00116
00117 scale(scaleFactor, scaleFactor);
00118 }
00119
00120