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

subalbumPreviewWidget.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 <qiconview.h> 00016 #include <qfontmetrics.h> 00017 #include <qapplication.h> 00018 00019 //Projectwide includes 00020 #include "subalbumPreviewWidget.h" 00021 #include "subalbumsIconView.h" 00022 #include "../backend/subalbum.h" 00023 #include "../backend/photo.h" 00024 #include "../backend/tools/imageTools.h" 00025 #include "subalbumsWidget.h" 00026 #include "layoutWidget.h" 00027 #include "subalbumWidget.h" 00028 #include "photoPreviewWidget.h" 00029 #include "../backend/tools/guiTools.h" 00030 00031 #include "../backend/album.h" 00032 00033 //============================================== 00034 SubalbumPreviewWidget::SubalbumPreviewWidget( SubalbumsIconView* parent, Subalbum* salbum ) 00035 : QIconViewItem(parent, 00036 clipText( salbum->getName(), 2, parent->getTextWidth()), 00037 *salbum->getRepresentativeImage(MEDIUM) ) 00038 { 00039 mousedOver = false; 00040 this->parent = parent; 00041 subalbum = salbum; 00042 00043 //initialize item rectangle 00044 initializeItemRect(); 00045 } 00046 //============================================== 00047 Subalbum* SubalbumPreviewWidget::getSubalbum() 00048 { 00049 return subalbum; 00050 } 00051 //============================================== 00052 void SubalbumPreviewWidget::paintFocus( QPainter*, const QColorGroup& ) { } 00053 //============================================== 00054 void SubalbumPreviewWidget::paint( QPainter *p ) 00055 { 00056 paintItem( p, QColorGroup() ); 00057 } 00058 //============================================== 00059 void SubalbumPreviewWidget::paintItem( QPainter* p, const QColorGroup&) 00060 { 00061 QColor lightLightBlue( 152, 180, 226 ); 00062 QColor darkLightBlue(193, 210, 238); 00063 QColor darkBlue(35, 75, 139); 00064 QColor background = darkLightBlue; 00065 00066 //resize old static buffer to new needed size, fill with widget background color 00067 static QPixmap buffer; 00068 QRect r = rect(); 00069 QSize newSize = r.size().expandedTo(buffer.size() ); 00070 buffer.resize(newSize); 00071 buffer.fill( background ); 00072 00073 //construct painter for buffer 00074 QPainter bufferPainter(&buffer, this); 00075 bufferPainter.translate( -r.x(), -r.y() ); 00076 00077 //turn off clipping to make painting operations faster 00078 bufferPainter.setClipping(false); 00079 00080 //paint mouse over or actual selection color 00081 bool paintRect = false; 00082 QColor paintColor; 00083 if(isSelected()) { paintColor = darkBlue; paintRect = true; } 00084 else if(mousedOver) { paintColor = lightLightBlue; paintRect = true; } 00085 00086 if(paintRect) 00087 { 00088 //first paint alpha blended edges 00089 00090 //------------------------- 00091 //top and bottom edges 00092 QRect r2 = r; 00093 r2.setLeft( r.left() + 4); 00094 r2.setRight( r.right() - 4); 00095 r2.setTop( r.top() ); 00096 r2.setBottom( r.bottom() ); 00097 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.40 ) ); 00098 00099 r2.setLeft( r2.left() + 1); 00100 r2.setRight( r2.right() - 1); 00101 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.67 ) ); 00102 //------------------------- 00103 //inner top and bottom edges 00104 r2.setLeft( r2.left() - 3); 00105 r2.setRight( r2.right() + 3); 00106 r2.setTop( r2.top() + 1 ); 00107 r2.setBottom( r2.bottom() - 1); 00108 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.40 ) ); 00109 00110 r2.setLeft( r2.left() + 1); 00111 r2.setRight( r2.right() - 1); 00112 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.67 ) ); 00113 //------------------------- 00114 //left and right inner edges 00115 r2.setLeft( r2.left() - 2); 00116 r2.setRight( r2.right() + 2); 00117 r2.setTop( r2.top() + 1 ); 00118 r2.setBottom( r2.bottom() - 1); 00119 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.40) ); 00120 00121 r2.setTop( r2.top() + 1); 00122 r2.setBottom( r2.bottom() - 1); 00123 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.67) ); 00124 //------------------------- 00125 // middle region 00126 r2.setLeft( r2.left() - 1 ); 00127 r2.setRight( r2.right() + 1 ); 00128 r2.setTop( r2.top() + 1); 00129 r2.setBottom( r2.bottom() - 1); 00130 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.40) ); 00131 00132 r2.setTop( r2.top() + 1); 00133 r2.setBottom( r2.bottom() - 1); 00134 bufferPainter.fillRect( r2, blendColors( paintColor, background, 0.67) ); 00135 //------------------------- 00136 //second paint inner selection 00137 r2 = r; 00138 r2.setLeft( r.left() + 1); 00139 r2.setRight( r.right() - 1); 00140 r2.setTop( r.top() + 4 ); 00141 r2.setBottom( r.bottom() - 4); 00142 bufferPainter.fillRect( r2, paintColor ); 00143 00144 r2.setLeft( r2.left() + 1); 00145 r2.setRight( r2.right() - 1); 00146 r2.setTop( r2.top() - 2 ); 00147 r2.setBottom( r2.bottom() + 2 ); 00148 bufferPainter.fillRect( r2, paintColor ); 00149 00150 r2.setLeft( r2.left() + 2); 00151 r2.setRight( r2.right() - 2); 00152 r2.setTop( r2.top() - 1 ); 00153 r2.setBottom( r2.bottom() + 1 ); 00154 bufferPainter.fillRect( r2, paintColor ); 00155 } 00156 00157 //paint pixmap 00158 bufferPainter.drawPixmap( x()+4 , y() + 4, *pixmap()); 00159 00160 //paint text 00161 int align = AlignLeft | AlignTop | BreakAnywhere; 00162 if(isSelected()) 00163 bufferPainter.setPen( white ); 00164 else 00165 bufferPainter.setPen( black ); 00166 bufferPainter.drawText( x() + 4 + pixmapRect().width(), 00167 y() + 4, 00168 textRect().width(), textRect().height(), 00169 align, text() ); 00170 00171 //draw buffer to screen 00172 p->drawPixmap( x(), y(), buffer ); 00173 } 00174 //============================================== 00175 void SubalbumPreviewWidget::dropped( QDropEvent *e, const QValueList<QIconDragItem> & ) 00176 { 00177 //if source is not from the application then ignore 00178 if(e->source() == NULL) 00179 return; 00180 00181 //if source of drop event is from this widget when user is attempting to 00182 //rearrange subalbums, move currently selected item to 00183 //approximately where the cursor is before rearranging items 00184 if(e->source()->parentWidget() == parent) 00185 { 00186 if(e->pos().y() < (y() + (height()/2))) 00187 { 00188 parent->currentItem()->move(x(), y() - 1); 00189 } 00190 else 00191 { 00192 parent->currentItem()->move(x(), y() + (height()/2) + 1); 00193 } 00194 } 00195 //else check to see if user dropped photo(s) on subalbum 00196 else 00197 { 00198 //if the source of the items is the current subalbum icon view and 00199 //this is a different subalbum then 00200 //move photos from that subalbum to this one 00201 if( 00202 !isSelected() && 00203 ( 00204 e->source()->parentWidget() == 00205 ((LayoutWidget*)(parent->parentWidget()->parentWidget()))->getSubalbum()->getPhotos() 00206 ) 00207 ) 00208 { 00209 //iterate over all selected photos, inserting each 00210 //into this subalbum, removing from old subalbum, 00211 //and deleting old photo widgets 00212 SubalbumWidget* oldSubalbumWidget = ((LayoutWidget*)(parent->parentWidget()->parentWidget()))->getSubalbum(); 00213 Subalbum* oldSubalbum = oldSubalbumWidget->getSubalbum(); 00214 QIconViewItem* current = oldSubalbumWidget->getPhotos()->firstItem(); 00215 while(current != NULL) 00216 { 00217 //found a selected photo 00218 if(current->isSelected()) 00219 { 00220 //get pointer to photo 00221 Photo* photo = ((PhotoPreviewWidget*)current)->getPhoto(); 00222 00223 //remove photo from that subalbum 00224 oldSubalbum->photoMoved(photo); 00225 00226 //add photo to this subalbum 00227 subalbum->addPhoto(photo); 00228 00229 //delete photo widget and rearrange photos 00230 QIconViewItem* temp = current; 00231 current = current->nextItem(); 00232 delete temp; 00233 } 00234 else 00235 { 00236 current = current->nextItem(); 00237 } 00238 } //end while 00239 00240 //reannarge photos once all photos have been removed 00241 oldSubalbumWidget->getPhotos()->arrangeItemsInGrid(); 00242 } 00243 } 00244 } 00245 //============================================== 00246 bool SubalbumPreviewWidget::acceptDrop( const QMimeSource *) const 00247 { 00248 return true; 00249 } 00250 //============================================== 00251 int SubalbumPreviewWidget::compare ( QIconViewItem * i ) const 00252 { 00253 if(pos().y() >= i->pos().y()) 00254 { return 1; } 00255 else 00256 { return -1; } 00257 } 00258 //============================================== 00259 QPixmap* SubalbumPreviewWidget::createSubalbumPixmap( QString imageName ) 00260 { 00261 //load image 00262 QImage icon(imageName); 00263 00264 //if null then bail immediately 00265 if( icon.isNull() ) 00266 return NULL; 00267 00268 //---------------------------------------------- 00269 //resize image based on text properties. Find ideal hight 00270 QFontMetrics fm( qApp->font() ); 00271 00272 //ideal image height is two text lines, 1 pixel inbetween 00273 int idealImageHeight = fm.leading() + 2*fm.height(); 00274 00275 //ideal image width assuming 4:3 aspect ratio 00276 int idealImageWidth = (4 * idealImageHeight ) / 3; 00277 //---------------------------------------------- 00278 //resize image to fit within bounding rectangle, pad and center as necessary 00279 int actualImageWidth = 0; 00280 int actualImageHeight = 0; 00281 calcScaledImageDimensions( icon.width(), icon.height(), 00282 idealImageWidth, idealImageHeight, 00283 actualImageWidth, actualImageHeight ); 00284 00285 //if off by one pixel fudge it so icon perfectly cenetered 00286 if(actualImageHeight == idealImageHeight - 1) 00287 { 00288 actualImageHeight = idealImageHeight; 00289 } 00290 00291 QImage scaledIcon= icon.smoothScale( actualImageWidth, actualImageHeight ); 00292 QImage* paddedScaledIcon = new QImage(idealImageWidth, idealImageHeight, scaledIcon.depth()); 00293 paddedScaledIcon->setAlphaBuffer(true); 00294 00295 //make entire image transparent 00296 int x, y; 00297 for(x=0; x< idealImageWidth; x++) 00298 { 00299 for(y=0; y<idealImageHeight; y++) 00300 { 00301 paddedScaledIcon->setPixel(x,y, qRgba(255, 255, 255,0) ); 00302 } 00303 } 00304 00305 //paint image in center of padded region 00306 int xDiff = idealImageWidth - actualImageWidth; 00307 int yDiff = idealImageHeight - actualImageHeight; 00308 int x2 = 0; 00309 for(x= xDiff/2; x < (xDiff/2) + actualImageWidth; x++) 00310 { 00311 int y2 = 0; 00312 for(y= yDiff/2; y < (yDiff/2) + actualImageHeight; y++) 00313 { 00314 paddedScaledIcon->setPixel(x, y, scaledIcon.pixel(x2, y2)); 00315 y2++; 00316 } 00317 x2++; 00318 } 00319 00320 //clip corners if image takes up full width 00321 if(xDiff == 0) 00322 { 00323 paddedScaledIcon->setPixel(0, 0, qRgba(255, 0,0,0) ); 00324 paddedScaledIcon->setPixel(idealImageWidth-1, 0, qRgba(255, 0,0,0) ); 00325 paddedScaledIcon->setPixel(0, idealImageHeight-1, qRgba(255, 0,0,0) ); 00326 paddedScaledIcon->setPixel(idealImageWidth-1, idealImageHeight-1, qRgba(255, 0,0,0) ); 00327 } 00328 00329 QPixmap* padddedScaledPix = new QPixmap( paddedScaledIcon->width(), paddedScaledIcon->height() ); 00330 padddedScaledPix->convertFromImage( *paddedScaledIcon ); 00331 delete paddedScaledIcon; 00332 return padddedScaledPix; 00333 //---------------------------------------------- 00334 } 00335 //============================================== 00336 void SubalbumPreviewWidget::calcRect( const QString & text_ ) 00337 { 00338 //setup default dimensions 00339 QIconViewItem::calcRect( text_ ); 00340 00341 //update using init method 00342 initializeItemRect(); 00343 } 00344 //============================================== 00345 void SubalbumPreviewWidget::initializeItemRect() 00346 { 00347 //reset pixmap rect 00348 QRect pr = pixmapRect(); 00349 int prWidth = pr.width(); 00350 int prHeight = pr.height(); 00351 pr.setTopLeft( QPoint(3,3) ); 00352 pr.setBottomRight( QPoint(pr.left()+prWidth, pr.top()+prHeight) ); 00353 setPixmapRect( pr ); 00354 00355 //reset text rect 00356 int textWidth = parent->getTextWidth(); 00357 QRect tr = textRect(); 00358 tr.setTop( pixmapRect().top() ); 00359 tr.setBottom( pixmapRect().bottom() ); 00360 tr.setLeft( pixmapRect().right() + 2 ); 00361 tr.setRight( tr.left() + textWidth ); 00362 setTextRect( tr ); 00363 00364 //reset item rect using pixmap and text rect dimensions 00365 int itemW = 3 + pixmapRect().width() + (tr.left() - pr.right()) + textRect().width() + 3; 00366 int itemH = 3 + pixmapRect().height() + 3; 00367 setItemRect( QRect( pixmapRect().left() - 3, pixmapRect().top() - 3, itemW, itemH ) ); 00368 } 00369 //============================================== 00370 void SubalbumPreviewWidget::setText ( const QString & text ) 00371 { 00372 QIconViewItem::setText( clipText(text, 2, parent->getTextWidth()), false ); 00373 } 00374 //============================================== 00375 void SubalbumPreviewWidget::setMousedOver(bool val) 00376 { 00377 mousedOver = val; 00378 } 00379 //============================================== 00380 QColor SubalbumPreviewWidget::blendColors( QColor a, QColor b, double alpha) 00381 { 00382 double alpha2 = 1-alpha; 00383 return QColor( (int)(alpha*a.red() + alpha2*b.red()), 00384 (int)(alpha*a.green() + alpha2*b.green()), 00385 (int)(alpha*a.blue() + alpha2*b.blue()) ); 00386 } 00387 //==============================================

Generated on Sun Mar 4 19:42:57 2007 for AlbumShaper by doxygen 1.3.7