AlbumShaper  1.0a3
recentAlbumMenuItem.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 <qdir.h>
13 #include <qpainter.h>
14 #include <qapplication.h>
15 #include <qkeysequence.h>
16 
17 //Projectwide includes
18 #include "recentAlbumMenuItem.h"
19 #include "../backend/tools/imageTools.h"
20 
21 //==============================================
23 {
24  this->acceleratorKey = acceleratorKey;
25  changeItem( "unitialized", "unitialized", "unitialized" );
26 }
27 //==============================================
29  QString albumLocation,
30  QString numPhotos )
31 {
32  //set name, and number of photos
33  this->albumName = albumName;
34  this->numPhotos = numPhotos;
35 
36  //compute height
37  QFontMetrics fm( qApp->font() );
38  size.setHeight( 2 + fm.leading() + 2*fm.height() + 2);
39 
40  //attempt to set album image
41  QString albumImageLocation = QDir::convertSeparators( albumLocation + "/img/album.jpg" );
42  QDir tempDir;
43  if( tempDir.exists( albumImageLocation ) )
44  {
45  //ideal image width assuming 4:3 aspect ratio
46  idealImageWidth = (4 * (size.height()-4) ) / 3;
47 
48  //scale image
49  scaleImage( albumImageLocation, albumImage, idealImageWidth, size.height() );
50  }
51  else
52  {
53  idealImageWidth = 0;
54  }
55 
56  //compute menu entry width
57  size.setWidth( idealImageWidth + 2 + fm.width(albumName) );
58 }
59 //==============================================
60 void RecentAlbumMenuItem::paint( QPainter* p,
61  const QColorGroup&,
62  bool, bool,
63  int x, int y, int, int )
64 {
65  //move down and right by two for spacing purposes
66  y+=2;
67  x+=2;
68  int xOffset = 0;
69  int yOffset = 0;
70 
71  //paint album image first if not null
72  if(!albumImage.isNull())
73  {
74  p->drawImage( x + (idealImageWidth - albumImage.width()) / 2,
75  y + (size.height() - albumImage.height() - 4)/2,
76  albumImage );
77  xOffset+=(idealImageWidth + 2);
78  }
79 
80  //paint album name + photo count
81  QFontMetrics fm( qApp->font() );
82  yOffset+=fm.ascent();
83  p->drawText( x+xOffset, y+yOffset, albumName );
84 
85  //if photo count available print it as well
86  if(numPhotos.compare("-1") != 0)
87  {
88  yOffset+=fm.descent() + 1 + fm.leading() + fm.ascent();
89  p->drawText( x+xOffset, y+yOffset,
90  qApp->translate("RecentAlbumMenuItem", "%1 Photos").arg(numPhotos) );
91  }
92 
93  //paint accelerator key
94  if( acceleratorKey != Qt::Key_unknown )
95  {
96  xOffset = maxWidth + 24;
97  yOffset = fm.ascent() + fm.height()/2;
98  QKeySequence seq( Qt::CTRL+acceleratorKey );
99  QString str = (QString)seq;
100  p->drawText( x+xOffset, y+yOffset,
101  str);
102  }
103 }
104 //==============================================
106 { return size; }
107 //==============================================
109 { return true; }
110 //==============================================
112 { maxWidth = val; }
113 //==============================================
bool fullSpan() const
no icon necessary since album image painted here
int maxWidth
max element width
void changeItem(QString albumName, QString albumLocation, QString numPhotos)
updates entry as per arguments passed (used by constructor during intiailization as well) ...
void setMaxWidth(int val)
after all menu items have been refreshed hint at maximum width so we can adequately position the acce...
RecentAlbumMenuItem(Qt::Key acceleratorKey)
void paint(QPainter *p, const QColorGroup &cg, bool act, bool enabled, int x, int y, int w, int h)
paints entry
int idealImageWidth
used for painting purposes
bool scaleImage(QString fileIn, QString fileOut, int newWidth, int newHeight)
Scale image and save copy to disk.
Definition: imageTools.cpp:157
QString numPhotos
number of photos
QSize sizeHint()
returns menu entry size
QString albumName
album name
QSize size
computed size
QImage albumImage
album image