Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

photoViewWidget.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it 
00005 //  and/or modify it under the terms of the GNU General 
00006 //  Public License as published by the Free Software 
00007 //  Foundation; either version 2 of the License, or  
00008 //  (at your option) any later version.         
00009 //
00010 //  As a special exception, Will Stokes gives permission to 
00011 //  link this program with Qt non-commercial edition, and 
00012 //  distribute the resulting executable, without including the 
00013 //  source code for the Qt non-commercial edition in the 
00014 //  source distribution. 
00015 //==============================================
00016 
00017 //Systemwide includes
00018 #include <qpixmap.h>
00019 #include <qpainter.h>
00020 #include <qevent.h>
00021 #include <qpoint.h>
00022 #include <qaccel.h>
00023 
00024 //Projectwide includes
00025 #include "photoViewWidget.h"
00026 #include "../backend/photo.h"
00027 //==============================================
00028 PhotoViewWidget::PhotoViewWidget(QWidget *parent, 
00029                                                    const char* name ) : 
00030                                                    QWidget(parent,name)
00031 {
00032   photo = NULL;
00033   setPaletteBackgroundColor( QColor(255, 255, 255) );
00034   setMinimumSize(600, 400);
00035   
00036   //set default selection off screen
00037   corner1 = new QPoint(-1, -1);
00038   corner2 = new QPoint(-1, -1);
00039   
00040   //by default not in drag mode
00041   currentlyDragging = false;
00042   
00043   //no photo set yet
00044   photoObject = NULL;
00045   
00046   QAccel *keyAccel = new QAccel( this );
00047   keyAccel->connectItem( keyAccel->insertItem( CTRL + Key_A),
00048                                       this, SLOT(selectAll()) );
00049 }
00050 //==============================================
00051 PhotoViewWidget::~PhotoViewWidget()
00052 {
00053   delete photo;
00054   photo = NULL;
00055 }
00056 //==============================================
00057 void PhotoViewWidget::setPhoto(Photo* phto)
00058 {
00059   //delete pixmap
00060   delete photo;
00061   
00062   //construct new pixmap
00063   photo = new QPixmap(phto->getSlideshowFilename());  
00064   
00065   //save photo object handle
00066   photoObject = phto;
00067   
00068   //reset selection area to nothing
00069   corner1->setX(-1); 
00070   corner1->setY(-1);
00071   corner2->setX(-1); 
00072   corner2->setY(-1);
00073   
00074   //repaint
00075   repaint(false);
00076 }
00077 //==============================================
00078 void PhotoViewWidget::paintEvent(QPaintEvent *e)
00079 {  
00080     //if no photo just paint white
00081     if(photo == NULL)
00082     {
00083       QPainter p;
00084       p.begin(this);
00085       p.fillRect(e->rect(), Qt::white);
00086       p.end();
00087       return;
00088     }
00089     
00090     //create buffer to draw in
00091     QRect rct = rect();
00092     rct.moveBy(-x(), -y());
00093     QPixmap buffer( size() );
00094     
00095     //create a painter pointing to the buffer
00096     QPainter bufferPainter( &buffer );
00097 
00098     //first flood entire region with white
00099     buffer.fill( white );
00100     
00101     //next paint the image
00102     QRect pr(0, 0, photo->width(), photo->height());
00103     bufferPainter.drawPixmap(pr, *photo);
00104     
00105     //next draw selection rectangle
00106     int xmin, xmax, ymin, ymax;
00107     
00108     if(corner1->x() < corner2->x())
00109       xmin = corner1->x();
00110     else
00111       xmin = corner2->x();
00112     if(corner1->x() > corner2->x())
00113       xmax = corner1->x();
00114     else
00115       xmax = corner2->x();
00116     if(corner1->y() < corner2->y())
00117       ymin = corner1->y();
00118     else
00119       ymin = corner2->y();
00120     if(corner1->y() > corner2->y())
00121       ymax = corner1->y();
00122     else
00123       ymax = corner2->y();
00124       
00125     QRect selection( xmin, ymin, xmax-xmin+1, ymax-ymin+1 );
00126     
00127     //construct a pen for selection drawing purposes
00128     QPen selectionPen;
00129 
00130     //draw selection in white    
00131     selectionPen.setStyle( Qt::SolidLine );
00132     selectionPen.setColor( white );
00133     bufferPainter.setPen( selectionPen);
00134     bufferPainter.drawRect(selection); 
00135     
00136     //draw selection a second time with dashed black, thus 
00137     //producing an alternating black-white selection rectangle
00138     selectionPen.setStyle( Qt::DotLine );
00139     selectionPen.setColor( black );
00140     bufferPainter.setPen( selectionPen);
00141     bufferPainter.drawRect(selection); 
00142     
00143     bufferPainter.end();
00144     
00145     //paint buffer to widget
00146     bitBlt( this, e->rect().topLeft(), &buffer, e->rect() );
00147 }
00148 //==============================================
00149 void PhotoViewWidget::mousePressEvent(QMouseEvent *e)
00150 {
00151   if(photo == NULL)
00152     return;
00153     
00154   currentlyDragging = true;
00155   QPoint p = cropSelectedPoint(e->pos());
00156   corner1->setX( p.x() );
00157   corner1->setY( p.y() );
00158   corner2->setX( corner1->x() );
00159   corner2->setY( corner1->y() );
00160   repaint(false);
00161 }
00162 //==============================================
00163 void PhotoViewWidget::mouseMoveEvent(QMouseEvent *e)
00164 {
00165   if(!currentlyDragging)
00166     return;
00167     
00168   QPoint p = cropSelectedPoint(e->pos());
00169   corner2->setX( p.x() );
00170   corner2->setY( p.y() );
00171   
00172   repaint(false);
00173 }
00174 //==============================================
00175 void PhotoViewWidget::mouseReleaseEvent(QMouseEvent *)
00176 {
00177   currentlyDragging = false;
00178 }
00179 //==============================================
00180 QPoint PhotoViewWidget::cropSelectedPoint(QPoint p)
00181 {
00182   int newX = p.x();
00183   int newY = p.y();
00184   
00185   int minXAllow, maxXAllow, minYAllow, maxYAllow;
00186   int xdiff, ydiff;
00187     
00188   xdiff = width() - photoObject->actualSlideshowWidth();
00189   ydiff = height() - photoObject->actualSlideshowHeight();
00190 
00191   minXAllow = xdiff/2;
00192   minYAllow = ydiff/2;
00193   
00194   maxXAllow = minXAllow + photoObject->actualSlideshowWidth() - 1;
00195   maxYAllow = minYAllow + photoObject->actualSlideshowHeight() - 1;
00196 
00197   if(newX < minXAllow)
00198     newX = minXAllow;
00199   if(newX > maxXAllow)
00200     newX = maxXAllow;
00201   if(newY < minYAllow)
00202     newY = minYAllow;
00203   if(newY > maxYAllow)
00204     newY = maxYAllow;
00205   
00206   return QPoint(newX, newY);
00207 }
00208 //==============================================
00209 void PhotoViewWidget::selectAll()
00210 {
00211   int minXAllow, maxXAllow, minYAllow, maxYAllow;
00212   int xdiff, ydiff;
00213     
00214   xdiff = width() - photoObject->actualSlideshowWidth();
00215   ydiff = height() - photoObject->actualSlideshowHeight();
00216 
00217   minXAllow = xdiff/2;
00218   minYAllow = ydiff/2;  
00219   maxXAllow = minXAllow + photoObject->actualSlideshowWidth() - 1;
00220   maxYAllow = minYAllow + photoObject->actualSlideshowHeight() - 1;
00221 
00222   corner1->setX( minXAllow );
00223   corner1->setY( minYAllow );
00224   corner2->setX( maxXAllow );
00225   corner2->setY( maxYAllow );
00226 
00227   repaint(false);
00228 }
00229 //==============================================
00230 void PhotoViewWidget::getSelection(QPoint &topLeft, QPoint &bottomRight)
00231 {
00232   //if none selected just return that
00233   if(corner1->x() == -1)
00234   {
00235     topLeft.setX(corner1->x());
00236     topLeft.setY(corner1->y());
00237     bottomRight.setX(corner2->x());
00238     bottomRight.setY(corner2->y());
00239     return;
00240   }
00241 
00242   //--
00243   //set coordinates based on raw selection
00244   //--
00245   if(corner1->x() < corner2->x())
00246   {
00247     topLeft.setX( corner1->x() );
00248     bottomRight.setX( corner2->x() );
00249   }
00250   else
00251   {
00252     topLeft.setX( corner2->x() );
00253     bottomRight.setX( corner1->x() );
00254   }
00255   //--
00256   if(corner1->y() < corner2->y())
00257   {
00258     topLeft.setY( corner1->y() );
00259     bottomRight.setY( corner2->y() );
00260   }
00261   else
00262   {
00263     topLeft.setY( corner2->y() );
00264     bottomRight.setY( corner1->y() );
00265   }
00266   //--
00267   //subtract offsets from coordinates
00268   int xbuff = (width() - photoObject->actualSlideshowWidth()) / 2;
00269   int ybuff = (height() - photoObject->actualSlideshowHeight()) / 2;
00270   topLeft.setX( topLeft.x() - xbuff );
00271   topLeft.setY( topLeft.y() - ybuff );
00272   bottomRight.setX( bottomRight.x() - xbuff );
00273   bottomRight.setY( bottomRight.y() - ybuff );
00274   //--
00275 }
00276 //==============================================

Generated on Thu Nov 13 00:10:54 2003 for AlbumShaper by doxygen 1.3.4