ImageView.qt.C

Go to the documentation of this file.
00001 /*!@file Qt4/ImageView.qt.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Pezhman Firoozfam (pezhman.firoozfam@usc.edu)
00034 // $HeadURL$ svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt4/ImageView.qt.C $
00035 //
00036 
00037 #include <QtGui/QtGui>
00038 #include "Qt4/ImageView.qt.H"
00039 #include "QtUtil/ImageConvert4.H"
00040 
00041 // ######################################################################
00042 ImageView::ImageView(QWidget *parent, const char* name) :
00043   QGraphicsView(parent),
00044   itsZoomStatus(Unknown)
00045 {
00046   setAttribute(Qt::WA_DeleteOnClose);
00047 
00048   //Create a new graphics scene which will handle all of our objects
00049   itsScene = new QGraphicsScene;
00050   setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
00051 
00052   //Create a new display widget to show our movie images
00053   itsImageDisplay = new ImageGraphicsItem;
00054   itsScene->addItem(itsImageDisplay);
00055   itsImageDisplay->setZValue(1);
00056 
00057   QGraphicsTextItem *text = new QGraphicsTextItem(QString(name));
00058   itsScene->addItem(text);
00059   text->setZValue(-1);
00060 
00061   //Set itsScene as the main scene for this viewer
00062   setScene(itsScene);
00063 }
00064 
00065 // ######################################################################
00066 ImageView::~ImageView()
00067 {
00068 }
00069 
00070 // ######################################################################
00071 void ImageView::setImage(Image< PixRGB<byte> > img)
00072 {
00073   itsImageDisplay->setImage(convertToQImage4(img));
00074 
00075   if (itsZoomStatus == Unknown)
00076   {
00077     fitInView();
00078   }
00079   else
00080   {
00081     itsImageDisplay->update();
00082     update();
00083   }
00084 }
00085 
00086 // ######################################################################
00087 void ImageView::fitInView()
00088 {
00089   QRectF rect = itsImageDisplay->getRect();
00090 
00091   if (rect.height() <= 1e-3 || rect.width() <= 1e-3) return;
00092 
00093   // set the image viewer to fit the image in the view
00094   itsZoomStatus = Fit;
00095   setSceneRect(itsImageDisplay->getRect());
00096   QGraphicsView::fitInView(itsImageDisplay, Qt::KeepAspectRatio);
00097   itsImageDisplay->update();
00098   update();
00099 }
00100 
00101 // ######################################################################
00102 void ImageView::wheelEvent(QWheelEvent *event)
00103 {
00104   // call the base class to perform default behaviour
00105   QGraphicsView::wheelEvent(event);
00106 
00107   // ignore the event if the zoom state is unknown
00108   if (itsZoomStatus == Unknown) return;
00109 
00110   // ignore if already zoomed too much
00111   double currentZoom = matrix().determinant();
00112   currentZoom = sqrt(currentZoom > 0 ? currentZoom : -currentZoom);
00113 
00114   if (event->delta() < 0 && currentZoom < 0.1) return;
00115   if (event->delta() > 0 && currentZoom > 4.0) return;
00116 
00117   // let's zoom the image accordingly
00118   const double delta = static_cast<double>(event->delta()) / 120.0;
00119   const double sc = (delta > 0) ? (delta * 1.25) : (-delta / 1.25);
00120   scale(sc, sc);
00121   itsZoomStatus = Zoomed;
00122   itsImageDisplay->update();
00123   update();
00124   event->accept();
00125 }
00126 
00127 // ######################################################################
00128 void ImageView::mousePressEvent(QMouseEvent *event)
00129 {
00130   // call the base class to perform default behaviour
00131   QGraphicsView::mousePressEvent(event);
00132 
00133   // ignore the event if the zoom state is unknown
00134   if (itsZoomStatus == Unknown) return;
00135 
00136   if ((event->buttons() & Qt::MidButton) != 0)
00137   {
00138     fitInView();
00139     event->accept();
00140     return;
00141   }
00142 
00143   // TODO: Add easy panning of the image by mouse dragging
00144 /*if ((event->buttons() & Qt::LeftButton) != 0 && itsZoomStatus == Zoomed)
00145   {
00146     // scroll the image
00147     event->accept();
00148     return;
00149   }*/
00150 }
00151 
00152 // ######################################################################
00153 /* So things look consistent in everyone's emacs... */
00154 /* Local Variables: */
00155 /* mode: c++ */
00156 /* indent-tabs-mode: nil */
00157 /* End: */
00158 
Generated on Sun May 8 08:41:16 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3