AlbumShaper  1.0a3
photoPreviewWidget.cpp
Go to the documentation of this file.
1 //==============================================
2 // copyright : (C) 2003-2005 by Will Stokes
3 //==============================================
4 // This program is free software; you can redistribute it
5 // and/or modify it under the terms of the GNU General
6 // Public License as published by the Free Software
7 // Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //==============================================
10 
11 //Systemwide includes
12 #include <qpixmap.h>
13 #include <qstring.h>
14 #include <qpainter.h>
15 #include <qfontmetrics.h>
16 #include <qapplication.h>
17 #include <qrect.h>
18 #include <qbitmap.h>
19 
20 //Projectwide includes
21 #include "photoPreviewWidget.h"
22 #include "photosIconView.h"
23 #include "window.h"
24 #include "../config.h"
25 #include "../backend/photo.h"
26 #include "../backend/tools/guiTools.h"
27 
28 //==============================================
30  Q3IconViewItem( parent, QString(""), QPixmap(phto->getThumbnailFilename()) )
31 {
32  //initially item not moused over, set photo pointer
33  mousedOver = false;
34  this->phto = phto;
35 
36  //calibrated text width is icon width minus margin + info button size (info button is sized to be a square of height
37  //equal to text height, aka fm.height)
38  QFontMetrics fm( qApp->font() );
40 
41  //actually set the items text by clipping it using the calibration width we just computed
42  setText( phto->getDescription() );
43 
44  //update the items rectange which is a function of the text width, icon rect,
45  //and margins for displaying selection and mouse over ovals
47 }
48 //==============================================
50 {
51  return phto;
52 }
53 //==============================================
55 {
56  setPixmap( QPixmap(phto->getThumbnailFilename()), false);
57 }
58 //==============================================
59 void PhotoPreviewWidget::setPixmap(const QPixmap& p, bool redraw )
60 {
61  pixmapXOffset = (THUMBNAIL_WIDTH - p.width() ) / 2;
62  pixmapYOffset = (THUMBNAIL_HEIGHT - p.height() ) / 2;
63  Q3IconViewItem::setPixmap( p, redraw );
64 }
65 //==============================================
67 {
69 }
70 //==============================================
71 void PhotoPreviewWidget::setText ( const QString & text )
72 {
73  Q3IconViewItem::setText( clipText(text, 1, calibratedWidth), false );
74 }
75 //==============================================
76 void PhotoPreviewWidget::paint( QPainter *p )
77 {
78  //create colors
79  QColor offWhite( 255, 255, 255 );
80  QColor darkBlue(35, 75, 139);
81  QColor paperColor;
82 
83  //draw offwhite or selected color depending on if photo is selected
84  QRect paperRect( x(), y(),
85  2*PHOTO_MARGIN + pixmapRect().width(),
86  2*PHOTO_MARGIN + pixmapRect().height() + PHOTO_TEXT_MARGIN + textRect().height() );
87  if(isSelected())
88  paperColor = darkBlue;
89  else
90  paperColor = offWhite;
91  p->fillRect( paperRect, QBrush( paperColor ) );
92 
93  //paint pixmap
94  p->drawPixmap( x() + pixmapRect().x() + pixmapXOffset + 1,
95  y() + pixmapRect().y() + pixmapYOffset + 1,
96  *pixmap());
97 
98  //paint text
99  int align = Qt::AlignLeft | Qt::AlignTop | Qt::TextWrapAnywhere;
100  if(isSelected())
101  p->setPen( Qt::white );
102  else
103  p->setPen( Qt::black );
104  p->drawText( x() + textRect().x() + 1, y() + textRect().y() + 1,
105  textRect().width(), textRect().height(),
106  align, text() );
107 }
108 //==============================================
109 void PhotoPreviewWidget::paintItem( QPainter* p, const QColorGroup&)
110 {
111  //resize old static buffer to new needed size, fill with widget background color
112  static QPixmap buffer;
113  QRect r = rect();
114  QSize newSize = r.size().expandedTo(buffer.size() );
115  buffer.resize(newSize);
116  buffer.fill( Qt::white );
117 
118  //construct painter for buffer
119  QPainter bufferPainter(&buffer);
120  bufferPainter.translate( -r.x(), -r.y() );
121 
122  //paint item
123  paint(&bufferPainter);
124 
125  //paint edit button
126  if(mousedOver)
127  {
129  bufferPainter.drawPixmap( photoInfoRect, * (((Window*) qApp->mainWidget())->photoInfo) );
130  }
131 
132  //paint shadows
133  QPixmap* shadowBL, *shadowB, *shadowBR, *shadowR, *shadowTR;
134  Window* window = (Window*) qApp->mainWidget();
135  shadowBL = window->shadowBL;
136  shadowB = window->shadowB;
137  shadowBR = window->shadowBR;
138  shadowR = window->shadowR;
139  shadowTR = window->shadowTR;
140 
141  QRect shadowRect;
142  shadowRect.setLeft( x() + PHOTO_SHADOW_END_OFFSET );
143  shadowRect.setRight( shadowRect.left() + PHOTO_SHADOW );
144  shadowRect.setTop( y() + rect().height() - PHOTO_SHADOW );
145  shadowRect.setBottom( shadowRect.top() + PHOTO_SHADOW );
146  bufferPainter.drawPixmap( shadowRect, *shadowBL );
147 
148  shadowRect.setLeft( shadowRect.right() + 1 );
149  shadowRect.setRight( x() + rect().width() - PHOTO_SHADOW - 1 );
150  bufferPainter.drawPixmap( shadowRect, *shadowB );
151 
152  shadowRect.setLeft( shadowRect.right() + 1 );
153  shadowRect.setRight( shadowRect.left() + PHOTO_SHADOW );
154  bufferPainter.drawPixmap( shadowRect, *shadowBR );
155 
156  shadowRect.setBottom( shadowRect.top() - 1 );
157  shadowRect.setTop( y() +PHOTO_SHADOW_END_OFFSET + PHOTO_SHADOW );
158  bufferPainter.drawPixmap( shadowRect, *shadowR );
159 
160  shadowRect.setBottom( shadowRect.top() - 1 );
161  shadowRect.setTop( y() +PHOTO_SHADOW_END_OFFSET );
162  bufferPainter.drawPixmap( shadowRect, *shadowTR );
163 
164  //draw buffer to screen
165  p->drawPixmap( x(), y(), buffer );
166 }
167 //==============================================
168 void PhotoPreviewWidget::paintFocus( QPainter*, const QColorGroup& ) { }
169 //==============================================
170 bool PhotoPreviewWidget::acceptDrop( const QMimeSource *) const
171 {
172  return true;
173 }
174 //==============================================
176 {
177  if( pos().y() > (i->pos().y() + height()) ||
178  (
179  pos().y() >= i->pos().y() &&
180  pos().x() >= i->pos().x()
181  ))
182  { return 1; }
183  else
184  { return -1; }
185 }
186 //==============================================
188 {
189  //set pixmap rect to be offset slightly from top left corner (by photo margin)
190  QRect pr = pixmapRect();
191  int itemLeft = x();
192  int itemTop = y();
193 
194  pixmapXOffset = (THUMBNAIL_WIDTH - pixmap()->width() ) / 2;
195  pixmapYOffset = (THUMBNAIL_HEIGHT - pixmap()->height() ) / 2;
196 
197  pr.setLeft( x() + PHOTO_MARGIN );
198  pr.setRight( pr.left() + THUMBNAIL_WIDTH );
199  pr.setTop( y() + PHOTO_MARGIN );
200  pr.setBottom( pr.top() + THUMBNAIL_HEIGHT );
201  setPixmapRect( pr );
202 
203  //move text rect to be below new pixmap region.
204  //reset height to allow for up to 3 lines of text.
205  QFontMetrics fm( qApp->font() );
206  QRect tr = QRect();
207  tr.setLeft( x() + PHOTO_MARGIN );
208  tr.setRight( tr.left() +THUMBNAIL_WIDTH );
209  tr.setTop( y() + PHOTO_MARGIN + THUMBNAIL_HEIGHT + PHOTO_TEXT_MARGIN );
210  tr.setBottom( tr.top() + 0*fm.leading() + 1*fm.height() );
211  setTextRect( tr );
212 
213  //set overall item rect
214  int itemW = THUMBNAIL_WIDTH + 2*PHOTO_MARGIN + PHOTO_SHADOW;
215  int itemH = THUMBNAIL_HEIGHT + PHOTO_TEXT_MARGIN + textRect().height() + 2*PHOTO_MARGIN + PHOTO_SHADOW;
216  setItemRect( QRect( itemLeft, itemTop, itemW, itemH ) );
217 }
218 //==============================================
220 {
221  mousedOver = val;
222 }
223 //==============================================
225 {
226  QRect photoInfoRect;
227  QFontMetrics fm( qApp->font() );
228  photoInfoRect.setLeft( x() + rect().width() - fm.height() - PHOTO_MARGIN - PHOTO_SHADOW - 1 );
229  photoInfoRect.setRight( photoInfoRect.left() + fm.height() );
230  photoInfoRect.setTop( y() + rect().height() - fm.height() - PHOTO_MARGIN - PHOTO_SHADOW - 1 );
231  photoInfoRect.setBottom( photoInfoRect.top() + fm.height() );
232  return photoInfoRect;
233 }
234 //==============================================
236 {
237  //get widget coordiantes of item
238  int xpos,ypos;
239  xpos = x() + pixmapRect().x() + pixmapXOffset + 1;
240  ypos = y() + pixmapRect().y() + pixmapYOffset + 1;
241 
242  //shift by scrolled amount
243  xpos-= iconView()->contentsX();
244  ypos-= iconView()->contentsY();
245 
246  //offset by viewport top left
247  //(why not iconview topleft? item actually placed in viewport which is placed in iconview. this
248  //viewport can be offset (and when I wrote this code it was) from the iconview depending on Trolltech's
249  //scrollview code which can using spacing between the viewport and scrolls widgets. since the viewport
250  //is a full blown widget, we can figure out it's reall screen coordinates and need not consult the iconview object at all.
251  QPoint viewportTL = iconView()->viewport()->mapToGlobal( QPoint(0,0) );
252  xpos+= viewportTL.x();
253  ypos+= viewportTL.y();
254 
255  return QPoint(xpos,ypos);
256 }
257 //==============================================
void paintFocus(QPainter *p, const QColorGroup &cg)
int compare(Q3IconViewItem *i) const
A photo consists of a full size image, a smaller slide show image, a very small thumbnail image...
Definition: photo.h:44
void paintItem(QPainter *p, const QColorGroup &cg)
Repain iconview item.
void updateDescription()
Update photo description.
void setText(const QString &text)
Photo * getPhoto()
Returns photo pointer.
QPixmap * shadowBL
Definition: window.h:70
QPixmap * shadowR
Definition: window.h:70
int pixmapXOffset
offsets used to center pixmap
Top level widget, encapsulates the title widget, the layout widget, and the toolbar widget...
Definition: window.h:39
QString getThumbnailFilename()
Gets the thumbnail filename.
Definition: photo.cpp:194
bool acceptDrop(const QMimeSource *e) const
#define PHOTO_SHADOW_END_OFFSET
#define PHOTO_TEXT_MARGIN
Photo * phto
Pointer to photo backend object.
#define THUMBNAIL_HEIGHT
Definition: config.h:25
int width
Definition: blur.cpp:79
#define PHOTO_SHADOW
QString clipText(QString string, int lines, int lineWidth)
clip text to fit within numer of lines and max width
Definition: guiTools.cpp:72
PhotoPreviewWidget(Q3IconView *parent, Photo *phto)
Sets subalbum pointer.
QPixmap * shadowTR
Definition: window.h:70
float * buffer
Definition: blur.cpp:80
#define PHOTO_MARGIN
void setMousedOver(bool val)
QString getDescription()
Gets the description.
Definition: photo.cpp:208
QPixmap * shadowB
Definition: window.h:70
QPixmap * shadowBR
Definition: window.h:70
#define THUMBNAIL_WIDTH
Definition: config.h:24
void updateImage()
Update photo thumbnail from background object.
void paint(QPainter *p)
void setPixmap(const QPixmap &p, bool redraw)
int height
Definition: blur.cpp:79