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

photoPreviewWidget.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 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 
00011 //Systemwide includes
00012 #include <qpixmap.h>
00013 #include <qstring.h>
00014 #include <qpainter.h>
00015 #include <qfontmetrics.h>
00016 #include <qapplication.h>
00017 #include <qrect.h>
00018 #include <qbitmap.h>
00019 
00020 //Projectwide includes
00021 #include "photoPreviewWidget.h"
00022 #include "photosIconView.h"
00023 #include "window.h"
00024 #include "../config.h"
00025 #include "../backend/photo.h"
00026 #include "../backend/tools/guiTools.h"
00027 
00028 //==============================================
00029 PhotoPreviewWidget::PhotoPreviewWidget( QIconView *parent, Photo* phto ) :
00030                                    QIconViewItem( parent, QString(""), QPixmap(phto->getThumbnailFilename()) )
00031 {
00032   //initially item not moused over, set photo pointer
00033   mousedOver = false;
00034   this->phto = phto;
00035 
00036   //calibrated text width is icon width minus margin + info button size (info button is sized to be a square of height
00037   //equal to text height, aka fm.height)
00038   QFontMetrics fm( qApp->font() );
00039   calibratedWidth = THUMBNAIL_WIDTH - PHOTO_TEXT_MARGIN - fm.height();
00040   
00041   //actually set the items text by clipping it using the calibration width we just computed
00042   setText( phto->getDescription() );
00043 
00044   //update the items rectange which is a function of the text width, icon rect,
00045   //and margins for displaying selection and mouse over ovals
00046   initializeItemRect();
00047 }
00048 //==============================================
00049 Photo* PhotoPreviewWidget::getPhoto()
00050 {
00051   return phto;
00052 }
00053 //==============================================
00054 void PhotoPreviewWidget::updateImage()
00055 {
00056   setPixmap( QPixmap(phto->getThumbnailFilename()), false);
00057 }
00058 //==============================================
00059 void PhotoPreviewWidget::setPixmap(const QPixmap& p, bool redraw )
00060 {
00061   pixmapXOffset = (THUMBNAIL_WIDTH - p.width() ) / 2;  
00062   pixmapYOffset = (THUMBNAIL_HEIGHT - p.height() ) / 2;  
00063   QIconViewItem::setPixmap( p, redraw );
00064 }
00065 //==============================================
00066 void PhotoPreviewWidget::updateDescription()
00067 {
00068   setText( phto->getDescription() );
00069 }
00070 //==============================================
00071 void PhotoPreviewWidget::setText ( const QString & text )
00072 {
00073   QIconViewItem::setText( clipText(text, 1, calibratedWidth), false );
00074 }
00075 //==============================================
00076 void PhotoPreviewWidget::paint( QPainter *p )
00077 {
00078   //create colors
00079   QColor offWhite( 255, 255, 255 );
00080   QColor darkBlue(35, 75, 139);
00081   QColor paperColor;
00082 
00083   //draw offwhite or selected color depending on if photo is selected
00084   QRect paperRect( x(), y(),
00085                               2*PHOTO_MARGIN + pixmapRect().width(),
00086                               2*PHOTO_MARGIN + pixmapRect().height() + PHOTO_TEXT_MARGIN + textRect().height() );
00087   if(isSelected())
00088     paperColor = darkBlue;
00089   else
00090     paperColor = offWhite;
00091   p->fillRect(  paperRect, QBrush( paperColor ) );
00092 
00093   //paint pixmap
00094   p->drawPixmap( x() + pixmapRect().x() + pixmapXOffset + 1,
00095                                  y() + pixmapRect().y() + pixmapYOffset + 1, 
00096                                  *pixmap());
00097   
00098   //paint text
00099   int align = AlignLeft | AlignTop | BreakAnywhere;
00100   if(isSelected())
00101     p->setPen( white );
00102   else
00103     p->setPen( black );
00104   p->drawText( x() + textRect().x() + 1, y() + textRect().y() + 1, 
00105                            textRect().width(), textRect().height(),
00106                            align, text() );
00107 }
00108 //==============================================
00109 void PhotoPreviewWidget::paintItem( QPainter* p, const QColorGroup&)
00110 {
00111   //resize old static buffer to new needed size, fill with widget background color
00112   static QPixmap buffer;
00113   QRect r = rect();
00114   QSize newSize = r.size().expandedTo(buffer.size() );
00115   buffer.resize(newSize);
00116   buffer.fill( white );
00117 
00118   //construct painter for buffer
00119   QPainter bufferPainter(&buffer, this);
00120   bufferPainter.translate( -r.x(), -r.y() );
00121  
00122    //paint item
00123   paint(&bufferPainter);
00124 
00125   //paint edit button
00126   if(mousedOver)
00127   {
00128     QRect photoInfoRect = getPhotoInfoRect();
00129     bufferPainter.drawPixmap( photoInfoRect, * (((Window*) qApp->mainWidget())->photoInfo) );
00130   }
00131 
00132   //paint shadows
00133   QPixmap* shadowBL, *shadowB, *shadowBR, *shadowR, *shadowTR;
00134   Window* window = (Window*) qApp->mainWidget();
00135   shadowBL = window->shadowBL;
00136   shadowB = window->shadowB;
00137   shadowBR = window->shadowBR;
00138   shadowR = window->shadowR;
00139   shadowTR = window->shadowTR;
00140 
00141   QRect shadowRect;
00142   shadowRect.setLeft( x() + PHOTO_SHADOW_END_OFFSET );
00143   shadowRect.setRight( shadowRect.left() + PHOTO_SHADOW );
00144   shadowRect.setTop( y() + rect().height() - PHOTO_SHADOW );
00145   shadowRect.setBottom( shadowRect.top() + PHOTO_SHADOW );
00146   bufferPainter.drawPixmap( shadowRect, *shadowBL );
00147 
00148   shadowRect.setLeft( shadowRect.right() + 1 );
00149   shadowRect.setRight( x() +  rect().width() - PHOTO_SHADOW - 1 );
00150   bufferPainter.drawPixmap( shadowRect, *shadowB );
00151 
00152   shadowRect.setLeft( shadowRect.right() + 1 );
00153   shadowRect.setRight( shadowRect.left() + PHOTO_SHADOW );
00154   bufferPainter.drawPixmap( shadowRect, *shadowBR );
00155 
00156   shadowRect.setBottom( shadowRect.top() - 1 );
00157   shadowRect.setTop( y() +PHOTO_SHADOW_END_OFFSET + PHOTO_SHADOW );
00158   bufferPainter.drawPixmap( shadowRect, *shadowR );
00159 
00160   shadowRect.setBottom( shadowRect.top() - 1 );
00161   shadowRect.setTop( y() +PHOTO_SHADOW_END_OFFSET );
00162   bufferPainter.drawPixmap( shadowRect, *shadowTR );
00163   
00164   //draw buffer to screen
00165   p->drawPixmap( x(), y(), buffer );
00166 }
00167 //==============================================
00168 void PhotoPreviewWidget::paintFocus( QPainter*, const QColorGroup& ) { }
00169 //==============================================
00170 bool PhotoPreviewWidget::acceptDrop( const QMimeSource *) const
00171 {
00172   return true;
00173 }
00174 //==============================================
00175 int PhotoPreviewWidget::compare ( QIconViewItem * i ) const
00176 {
00177   if( pos().y() > (i->pos().y() + height()) ||
00178       (
00179         pos().y() >= i->pos().y() &&
00180         pos().x() >= i->pos().x()
00181       ))
00182   { return 1; }
00183   else
00184   { return -1; }
00185 }
00186 //==============================================
00187 void PhotoPreviewWidget::initializeItemRect()
00188 {
00189   //set pixmap rect to be offset slightly from top left corner (by photo margin)
00190   QRect pr = pixmapRect();
00191   int itemLeft = x();
00192   int itemTop = y();
00193   
00194   pixmapXOffset = (THUMBNAIL_WIDTH - pixmap()->width() ) / 2;  
00195   pixmapYOffset = (THUMBNAIL_HEIGHT - pixmap()->height() ) / 2;  
00196   
00197   pr.setLeft( x() + PHOTO_MARGIN );
00198   pr.setRight( pr.left() + THUMBNAIL_WIDTH );
00199   pr.setTop( y() + PHOTO_MARGIN );
00200   pr.setBottom( pr.top() + THUMBNAIL_HEIGHT );
00201   setPixmapRect( pr );
00202 
00203   //move text rect to be below new pixmap region.
00204   //reset height to allow for up to 3 lines of text.
00205   QFontMetrics fm( qApp->font() );
00206   QRect tr = QRect();
00207   tr.setLeft( x() + PHOTO_MARGIN );
00208   tr.setRight( tr.left() +THUMBNAIL_WIDTH );
00209   tr.setTop( y() + PHOTO_MARGIN + THUMBNAIL_HEIGHT + PHOTO_TEXT_MARGIN );
00210   tr.setBottom( tr.top() + 0*fm.leading() + 1*fm.height() );
00211   setTextRect( tr );
00212 
00213   //set overall item rect
00214   int itemW = THUMBNAIL_WIDTH + 2*PHOTO_MARGIN + PHOTO_SHADOW;
00215   int itemH = THUMBNAIL_HEIGHT + PHOTO_TEXT_MARGIN + textRect().height() + 2*PHOTO_MARGIN + PHOTO_SHADOW;
00216   setItemRect( QRect( itemLeft, itemTop, itemW, itemH ) );
00217 }
00218 //==============================================
00219 void PhotoPreviewWidget::setMousedOver(bool val)
00220 { 
00221   mousedOver = val;   
00222 }
00223 //==============================================
00224 QRect PhotoPreviewWidget::getPhotoInfoRect()
00225 {
00226   QRect photoInfoRect;
00227   QFontMetrics fm( qApp->font() );
00228   photoInfoRect.setLeft( x() + rect().width() - fm.height() - PHOTO_MARGIN - PHOTO_SHADOW - 1 );
00229   photoInfoRect.setRight( photoInfoRect.left() + fm.height() );
00230   photoInfoRect.setTop( y() + rect().height() - fm.height() - PHOTO_MARGIN - PHOTO_SHADOW - 1 );
00231   photoInfoRect.setBottom( photoInfoRect.top() + fm.height() );
00232   return photoInfoRect;
00233 }
00234 //==============================================
00235 QPoint PhotoPreviewWidget::getPhotoPos()
00236 {
00237   //get widget coordiantes of item
00238   int xpos,ypos;
00239   xpos = x() + pixmapRect().x() + pixmapXOffset + 1;
00240   ypos = y() + pixmapRect().y() + pixmapYOffset + 1;
00241   
00242   //shift by scrolled amount
00243   xpos-= iconView()->contentsX();
00244   ypos-= iconView()->contentsY();
00245 
00246   //offset by viewport top left
00247   //(why not iconview topleft? item actually placed in viewport which is placed in iconview. this
00248   //viewport can be offset (and when I wrote this code it was) from the iconview depending on Trolltech's
00249   //scrollview code which can using spacing between the viewport and scrolls widgets. since the viewport
00250   //is a full blown widget, we can figure out it's reall screen coordinates and need not consult the iconview object at all.
00251   QPoint viewportTL = iconView()->viewport()->mapToGlobal( QPoint(0,0) );
00252   xpos+= viewportTL.x();
00253   ypos+= viewportTL.y();
00254 
00255   return QPoint(xpos,ypos);
00256 }
00257 //==============================================

Generated on Sat Apr 2 05:44:04 2005 for AlbumShaper by  doxygen 1.3.9.1