AlbumShaper
1.0a3
|
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 <qdir.h> 00013 #include <qpainter.h> 00014 #include <qapplication.h> 00015 #include <qkeysequence.h> 00016 00017 //Projectwide includes 00018 #include "recentAlbumMenuItem.h" 00019 #include "../backend/tools/imageTools.h" 00020 00021 //============================================== 00022 RecentAlbumMenuItem::RecentAlbumMenuItem( Key acceleratorKey ) : QCustomMenuItem() 00023 { 00024 this->acceleratorKey = acceleratorKey; 00025 changeItem( "unitialized", "unitialized", "unitialized" ); 00026 } 00027 //============================================== 00028 void RecentAlbumMenuItem::changeItem( QString albumName, 00029 QString albumLocation, 00030 QString numPhotos ) 00031 { 00032 //set name, and number of photos 00033 this->albumName = albumName; 00034 this->numPhotos = numPhotos; 00035 00036 //compute height 00037 QFontMetrics fm( qApp->font() ); 00038 size.setHeight( 2 + fm.leading() + 2*fm.height() + 2); 00039 00040 //attempt to set album image 00041 QString albumImageLocation = QDir::convertSeparators( albumLocation + "/img/album.jpg" ); 00042 QDir tempDir; 00043 if( tempDir.exists( albumImageLocation ) ) 00044 { 00045 //ideal image width assuming 4:3 aspect ratio 00046 idealImageWidth = (4 * (size.height()-4) ) / 3; 00047 00048 //scale image 00049 scaleImage( albumImageLocation, albumImage, idealImageWidth, size.height() ); 00050 } 00051 else 00052 { 00053 idealImageWidth = 0; 00054 } 00055 00056 //compute menu entry width 00057 size.setWidth( idealImageWidth + 2 + fm.width(albumName) ); 00058 } 00059 //============================================== 00060 void RecentAlbumMenuItem::paint( QPainter* p, 00061 const QColorGroup&, 00062 bool, bool, 00063 int x, int y, int, int ) 00064 { 00065 //move down and right by two for spacing purposes 00066 y+=2; 00067 x+=2; 00068 int xOffset = 0; 00069 int yOffset = 0; 00070 00071 //paint album image first if not null 00072 if(!albumImage.isNull()) 00073 { 00074 p->drawImage( x + (idealImageWidth - albumImage.width()) / 2, 00075 y + (size.height() - albumImage.height() - 4)/2, 00076 albumImage ); 00077 xOffset+=(idealImageWidth + 2); 00078 } 00079 00080 //paint album name + photo count 00081 QFontMetrics fm( qApp->font() ); 00082 yOffset+=fm.ascent(); 00083 p->drawText( x+xOffset, y+yOffset, albumName ); 00084 00085 //if photo count available print it as well 00086 if(numPhotos.compare("-1") != 0) 00087 { 00088 yOffset+=fm.descent() + 1 + fm.leading() + fm.ascent(); 00089 p->drawText( x+xOffset, y+yOffset, 00090 qApp->translate("RecentAlbumMenuItem", "%1 Photos").arg(numPhotos) ); 00091 } 00092 00093 //paint accelerator key 00094 if( acceleratorKey != Key_unknown ) 00095 { 00096 xOffset = maxWidth + 24; 00097 yOffset = fm.ascent() + fm.height()/2; 00098 QKeySequence seq( CTRL+acceleratorKey ); 00099 QString str = (QString)seq; 00100 p->drawText( x+xOffset, y+yOffset, 00101 str); 00102 } 00103 } 00104 //============================================== 00105 QSize RecentAlbumMenuItem::sizeHint () 00106 { return size; } 00107 //============================================== 00108 bool RecentAlbumMenuItem::fullSpan() const 00109 { return true; } 00110 //============================================== 00111 void RecentAlbumMenuItem::setMaxWidth( int val ) 00112 { maxWidth = val; } 00113 //==============================================