Camera.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, CoroWare
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Stanford U. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "Camera.h"
00031 
00032 #include <QtGui>
00033 
00034 const int LINE_POS = 70;
00035 
00036 // Widget capable not only of displaying the camera image but also to zoom in and out on the image. 
00037 // Of course the zoom is a software zoom, even using a camera with hardware zoom.
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); // Used to animate the zooming in and out to have this fluid animation
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 // Animate the zooming in and out to have this fluid animation
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  // Use the wheel to zoom in and out
00115  {
00116      scaleView(pow((double)2, -event->delta() / 240.0));
00117  }
00118 
00119  void CameraWidget::drawBackground(QPainter *painter, const QRectF &rect)
00120  // Draw the background of the view, seem when zoomed out.
00121  {
00122      Q_UNUSED(rect);
00123 
00124      // Shadow
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      // Fill
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  //method used to change the scale of the view
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 


corobot_teleop
Author(s):
autogenerated on Sun Oct 5 2014 23:18:16