Main Page   Compound List   File List   Compound 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 #include <iostream.h>
00029 //==============================================
00030 PhotoViewWidget::PhotoViewWidget(QWidget *parent, 
00031                                                    const char* name ) : 
00032                                                    QWidget(parent,name)
00033 {
00034   photo = NULL;
00035   setPaletteBackgroundColor( QColor(255, 255, 255) );
00036   setMinimumSize(600, 400);
00037   
00038   //set default selection off screen
00039   corner1 = new QPoint(-1, -1);
00040   corner2 = new QPoint(-1, -1);
00041   
00042   //by default not in drag mode
00043   currentlyDragging = false;
00044   
00045   //no photo set yet
00046   photoObject = NULL;
00047   
00048   QAccel *keyAccel = new QAccel( this );
00049   keyAccel->connectItem( keyAccel->insertItem( CTRL + Key_A),
00050                                       this, SLOT(selectAll()) );
00051 }
00052 //==============================================
00053 PhotoViewWidget::~PhotoViewWidget()
00054 {
00055   delete photo;
00056   photo = NULL;
00057 }
00058 //==============================================
00059 void PhotoViewWidget::setPhoto(Photo* phto)
00060 {
00061   //delete pixmap
00062   delete photo;
00063   
00064   //construct new pixmap
00065   photo = new QPixmap(phto->getSlideshowFilename());  
00066   
00067   //save photo object handle
00068   photoObject = phto;
00069   
00070   //reset selection area to nothing
00071   corner1->setX(-1); 
00072   corner1->setY(-1);
00073   corner2->setX(-1); 
00074   corner2->setY(-1);
00075   
00076   //repaint
00077   repaint(false);
00078 }
00079 //==============================================
00080 void PhotoViewWidget::paintEvent(QPaintEvent *e)
00081 {  
00082     //if no photo just paint white
00083     if(photo == NULL)
00084     {
00085       QPainter p;
00086       p.begin(this);
00087       p.fillRect(e->rect(), Qt::white);
00088       p.end();
00089       return;
00090     }
00091     
00092     //create buffer to draw in
00093     QRect rct = rect();
00094     rct.moveBy(-x(), -y());
00095     QPixmap buffer( size() );
00096     
00097     //create a painter pointing to the buffer
00098     QPainter bufferPainter( &buffer );
00099 
00100     //first flood entire region with white
00101     buffer.fill( white );
00102     
00103     //next paint the image
00104     QRect pr(0, 0, photo->width(), photo->height());
00105     bufferPainter.drawPixmap(pr, *photo);
00106     
00107     //next draw selection rectangle
00108     int xmin, xmax, ymin, ymax;
00109     
00110     if(corner1->x() < corner2->x())
00111       xmin = corner1->x();
00112     else
00113       xmin = corner2->x();
00114     if(corner1->x() > corner2->x())
00115       xmax = corner1->x();
00116     else
00117       xmax = corner2->x();
00118     if(corner1->y() < corner2->y())
00119       ymin = corner1->y();
00120     else
00121       ymin = corner2->y();
00122     if(corner1->y() > corner2->y())
00123       ymax = corner1->y();
00124     else
00125       ymax = corner2->y();
00126       
00127     QRect selection( xmin, ymin, xmax-xmin+1, ymax-ymin+1 );
00128     
00129     //construct a pen for selection drawing purposes
00130     QPen selectionPen;
00131 
00132     //draw selection in white    
00133     selectionPen.setStyle( Qt::SolidLine );
00134     selectionPen.setColor( white );
00135     bufferPainter.setPen( selectionPen);
00136     bufferPainter.drawRect(selection); 
00137     
00138     //draw selection a second time with dashed black, thus 
00139     //producing an alternating black-white selection rectangle
00140     selectionPen.setStyle( Qt::DotLine );
00141     selectionPen.setColor( black );
00142     bufferPainter.setPen( selectionPen);
00143     bufferPainter.drawRect(selection); 
00144     
00145     bufferPainter.end();
00146     
00147     //paint buffer to widget
00148     bitBlt( this, e->rect().topLeft(), &buffer, e->rect() );
00149 }
00150 //==============================================
00151 void PhotoViewWidget::keyReleaseEvent( QKeyEvent *e)
00152 {
00153   cout << "Key Pressed: " << e->ascii() << endl;
00154 }
00155 //==============================================
00156 void PhotoViewWidget::mousePressEvent(QMouseEvent *e)
00157 {
00158   if(photo == NULL)
00159     return;
00160     
00161   currentlyDragging = true;
00162   QPoint p = cropSelectedPoint(e->pos());
00163   corner1->setX( p.x() );
00164   corner1->setY( p.y() );
00165   corner2->setX( corner1->x() );
00166   corner2->setY( corner1->y() );
00167   repaint(false);
00168 }
00169 //==============================================
00170 void PhotoViewWidget::mouseMoveEvent(QMouseEvent *e)
00171 {
00172   if(!currentlyDragging)
00173     return;
00174     
00175   QPoint p = cropSelectedPoint(e->pos());
00176   corner2->setX( p.x() );
00177   corner2->setY( p.y() );
00178   
00179   repaint(false);
00180 }
00181 //==============================================
00182 void PhotoViewWidget::mouseReleaseEvent(QMouseEvent *)
00183 {
00184   currentlyDragging = false;
00185 }
00186 //==============================================
00187 QPoint PhotoViewWidget::cropSelectedPoint(QPoint p)
00188 {
00189   int newX = p.x();
00190   int newY = p.y();
00191   
00192   int minXAllow, maxXAllow, minYAllow, maxYAllow;
00193   int xdiff, ydiff;
00194     
00195   xdiff = width() - photoObject->actualSlideshowWidth();
00196   ydiff = height() - photoObject->actualSlideshowHeight();
00197 
00198   minXAllow = xdiff/2;
00199   minYAllow = ydiff/2;
00200   
00201   maxXAllow = minXAllow + photoObject->actualSlideshowWidth() - 1;
00202   maxYAllow = minYAllow + photoObject->actualSlideshowHeight() - 1;
00203 
00204   if(newX < minXAllow)
00205     newX = minXAllow;
00206   if(newX > maxXAllow)
00207     newX = maxXAllow;
00208   if(newY < minYAllow)
00209     newY = minYAllow;
00210   if(newY > maxYAllow)
00211     newY = maxYAllow;
00212   
00213   return QPoint(newX, newY);
00214 }
00215 //==============================================
00216 void PhotoViewWidget::selectAll()
00217 {
00218   int minXAllow, maxXAllow, minYAllow, maxYAllow;
00219   int xdiff, ydiff;
00220     
00221   xdiff = width() - photoObject->actualSlideshowWidth();
00222   ydiff = height() - photoObject->actualSlideshowHeight();
00223 
00224   minXAllow = xdiff/2;
00225   minYAllow = ydiff/2;  
00226   maxXAllow = minXAllow + photoObject->actualSlideshowWidth() - 1;
00227   maxYAllow = minYAllow + photoObject->actualSlideshowHeight() - 1;
00228 
00229   corner1->setX( minXAllow );
00230   corner1->setY( minYAllow );
00231   corner2->setX( maxXAllow );
00232   corner2->setY( maxYAllow );
00233 
00234   repaint(false);
00235 }
00236 //==============================================
00237 void PhotoViewWidget::getSelection(QPoint &topLeft, QPoint &bottomRight)
00238 {
00239   //if none selected just return that
00240   if(corner1->x() == -1)
00241   {
00242     topLeft.setX(corner1->x());
00243     topLeft.setY(corner1->y());
00244     bottomRight.setX(corner2->x());
00245     bottomRight.setY(corner2->y());
00246     return;
00247   }
00248 
00249   //--
00250   //set coordinates based on raw selection
00251   //--
00252   if(corner1->x() < corner2->x())
00253   {
00254     topLeft.setX( corner1->x() );
00255     bottomRight.setX( corner2->x() );
00256   }
00257   else
00258   {
00259     topLeft.setX( corner2->x() );
00260     bottomRight.setX( corner1->x() );
00261   }
00262   //--
00263   if(corner1->y() < corner2->y())
00264   {
00265     topLeft.setY( corner1->y() );
00266     bottomRight.setY( corner2->y() );
00267   }
00268   else
00269   {
00270     topLeft.setY( corner2->y() );
00271     bottomRight.setY( corner1->y() );
00272   }
00273   //--
00274   //subtract offsets from coordinates
00275   int xbuff = (width() - photoObject->actualSlideshowWidth()) / 2;
00276   int ybuff = (height() - photoObject->actualSlideshowHeight()) / 2;
00277   topLeft.setX( topLeft.x() - xbuff );
00278   topLeft.setY( topLeft.y() - ybuff );
00279   bottomRight.setX( bottomRight.x() - xbuff );
00280   bottomRight.setY( bottomRight.y() - ybuff );
00281   //--
00282 }
00283 //==============================================

Generated on Tue Jun 10 23:41:21 2003 for AlbumShaper by doxygen 1.3.1