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