• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

digikam

albumiconitem.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of digiKam project
00004  * http://www.digikam.org
00005  *
00006  * Date        : 2003-04-25
00007  * Description : implementation to render album icon item.
00008  * 
00009  * Copyright (C) 2003-2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
00010  * Copyright (C) 2003-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
00011  *
00012  * This program is free software; you can redistribute it
00013  * and/or modify it under the terms of the GNU General
00014  * Public License as published by the Free Software Foundation;
00015  * either version 2, or (at your option)
00016  * any later version.
00017  * 
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * ============================================================ */
00024 
00025 // Qt includes.
00026 
00027 #include <QPainter>
00028 #include <QPixmap>
00029 #include <QPalette>
00030 #include <QString>
00031 #include <QPen>
00032 #include <QFontMetrics>
00033 #include <QFont>
00034 #include <QDateTime>
00035 #include <QStringList>
00036 
00037 // KDE includes.
00038 
00039 #include <kurl.h>
00040 #include <kglobal.h>
00041 #include <klocale.h>
00042 #include <kio/global.h>
00043 
00044 // Local includes.
00045 
00046 #include "themeengine.h"
00047 #include "thumbnailsize.h"
00048 #include "imageinfo.h"
00049 #include "albumsettings.h"
00050 #include "albummanager.h"
00051 #include "icongroupitem.h"
00052 #include "thumbnailloadthread.h"
00053 #include "albumiconview.h"
00054 #include "albumiconitem.h"
00055 
00056 namespace Digikam
00057 {
00058 
00059 class AlbumIconItemPriv
00060 {
00061 public:
00062 
00063     AlbumIconItemPriv()
00064     {
00065         dirty = true;
00066         view  = 0;
00067     }
00068 
00069     bool           dirty;
00070 
00071     QRect          tightPixmapRect;
00072 
00073     ImageInfo      info;
00074 
00075     AlbumIconView *view;
00076 };
00077 
00078 static void dateToString(const QDateTime& datetime, QString& str)
00079 {
00080     str = KGlobal::locale()->formatDateTime(datetime, KLocale::ShortDate, false);
00081 }
00082 
00083 AlbumIconItem::AlbumIconItem(IconGroupItem* parent, const ImageInfo &info)
00084              : IconItem(parent)
00085 {
00086     d = new AlbumIconItemPriv;
00087     d->view = (AlbumIconView*) parent->iconView();
00088     d->info = info;
00089 }
00090 
00091 AlbumIconItem::~AlbumIconItem()
00092 {
00093     delete d;
00094 }
00095 
00096 QString AlbumIconItem::squeezedText(QPainter* p, int width, const QString& text)
00097 {
00098     QString fullText(text);
00099     fullText.replace("\n"," ");
00100     QFontMetrics fm(p->fontMetrics());
00101     int textWidth = fm.width(fullText);
00102     
00103     if (textWidth > width) 
00104     {
00105         // start with the dots only
00106         QString squeezedText = "...";
00107         int squeezedWidth = fm.width(squeezedText);
00108 
00109         // estimate how many letters we can add to the dots on both sides
00110         int letters = fullText.length() * (width - squeezedWidth) / textWidth;
00111         if (width < squeezedWidth) letters=1;
00112         squeezedText = fullText.left(letters) + "...";
00113         squeezedWidth = fm.width(squeezedText);
00114 
00115         if (squeezedWidth < width) 
00116         {
00117             // we estimated too short
00118             // add letters while text < label
00119             do 
00120             {
00121                 letters++;
00122                 squeezedText = fullText.left(letters) + "..."; 
00123                 squeezedWidth = fm.width(squeezedText);
00124             }
00125             while (squeezedWidth < width);
00126 
00127             letters--;
00128             squeezedText = fullText.left(letters) + "..."; 
00129         }
00130         else if (squeezedWidth > width) 
00131         {
00132             // we estimated too long
00133             // remove letters while text > label
00134             do 
00135             {
00136                 letters--;
00137                 squeezedText = fullText.left(letters) + "...";
00138                 squeezedWidth = fm.width(squeezedText);
00139             }
00140             while (letters && squeezedWidth > width);
00141         }
00142 
00143         if (letters >= 5) 
00144         {
00145             return squeezedText;
00146         }
00147     }
00148     
00149     return fullText;   
00150 }
00151 
00152 bool AlbumIconItem::isDirty()
00153 {
00154     return d->dirty;
00155 }
00156 
00157 ImageInfo AlbumIconItem::imageInfo() const
00158 {
00159     return d->info;
00160 }
00161 
00162 QString AlbumIconItem::filePath() const
00163 {
00164     return d->info.filePath();
00165 }
00166 
00167 int AlbumIconItem::compare(IconItem *item)
00168 {
00169     const AlbumSettings *settings = d->view->settings();
00170     AlbumIconItem *iconItem = static_cast<AlbumIconItem*>(item);
00171     
00172     switch (settings->getImageSortOrder())
00173     {
00174         case(AlbumSettings::ByIName):
00175         {
00176             return d->info.name().localeAwareCompare(iconItem->d->info.name());
00177         }
00178         case(AlbumSettings::ByIPath):
00179         {
00180             return d->info.fileUrl().path().compare(iconItem->d->info.fileUrl().path());
00181         }
00182         case(AlbumSettings::ByIDate):
00183         {
00184             if (d->info.dateTime() < iconItem->d->info.dateTime())
00185                 return -1;
00186             else if (d->info.dateTime() > iconItem->d->info.dateTime())
00187                 return 1;
00188             else
00189                 return 0;
00190         }
00191         case(AlbumSettings::ByISize):
00192         {
00193             int mysize(d->info.fileSize());
00194             int hissize(iconItem->d->info.fileSize());
00195             if (mysize < hissize)
00196                 return -1;
00197             else if (mysize > hissize)
00198                 return 1;
00199             else
00200                 return 0;
00201         }
00202         case(AlbumSettings::ByIRating):
00203         {
00204             int myrating(d->info.rating());
00205             int hisrating(iconItem->d->info.rating());
00206             if (myrating < hisrating)
00207                 return 1;
00208             else if (myrating > hisrating)
00209                 return -1;
00210             else
00211                 return 0;
00212         }
00213     }
00214 
00215     return 0;
00216 }
00217 
00218 QRect AlbumIconItem::clickToOpenRect()
00219 {
00220     if (d->tightPixmapRect.isNull())
00221         return rect();
00222     
00223     QRect pixmapRect = d->tightPixmapRect;
00224     QRect r          = rect();
00225 
00226     pixmapRect.translate(r.x(), r.y());
00227     return pixmapRect;
00228 }
00229 
00230 void AlbumIconItem::paintItem(QPainter *p)
00231 {
00232     QRect   r;
00233     const AlbumSettings *settings = d->view->settings();
00234     ThemeEngine* te = ThemeEngine::instance();
00235 
00236     QPixmap pix;
00237     if (isSelected())
00238         pix = d->view->itemBaseSelPixmap();
00239     else
00240         pix = d->view->itemBaseRegPixmap();
00241 
00242     p->setPen(isSelected() ? te->textSelColor() : te->textRegColor());
00243 
00244     d->dirty = true;
00245 
00246     QPixmap thumbnail;
00247     if (ThumbnailLoadThread::defaultIconViewThread()->find(d->info.filePath(), thumbnail))
00248     {
00249         r = d->view->itemPixmapRect();
00250         p->drawPixmap(r.x() + (r.width()-thumbnail.width())/2,
00251                      r.y() + (r.height()-thumbnail.height())/2,
00252                      thumbnail);
00253         d->tightPixmapRect.setRect(r.x() + (r.width()-thumbnail.width())/2,
00254                                  r.y() + (r.height()-thumbnail.height())/2,
00255                                  thumbnail.width(), thumbnail.height());
00256         d->dirty = false;
00257     }
00258 
00259     p->save();
00260     QRegion pixmapClipRegion = QRegion(d->view->itemRect()) - QRegion(d->tightPixmapRect);
00261     p->setClipRegion(pixmapClipRegion);
00262     p->drawPixmap(0, 0, pix);
00263     p->restore();
00264 
00265     if (settings->getIconShowRating())
00266     {
00267         r = d->view->itemRatingRect();
00268 
00269         int rating = d->info.rating();
00270 
00271         if (rating > 0 && rating <=5)
00272         {
00273             QPixmap ratingPixmap = d->view->ratingPixmap(rating, isSelected());
00274             p->drawPixmap(r, ratingPixmap);
00275         }
00276     }
00277 
00278     if (settings->getIconShowName())
00279     {
00280         r = d->view->itemNameRect();
00281         p->setFont(d->view->itemFontReg());
00282         p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), d->info.name()));
00283     }
00284 
00285     p->setFont(d->view->itemFontCom());
00286 
00287     if (settings->getIconShowComments())
00288     {
00289         QString comments = d->info.comment();
00290 
00291         r = d->view->itemCommentsRect();
00292         p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), comments));
00293     }
00294 
00295     p->setFont(d->view->itemFontXtra());
00296 
00297     if (settings->getIconShowDate())
00298     {
00299         QDateTime date(d->info.dateTime());
00300 
00301         r = d->view->itemDateRect();
00302         p->setFont(d->view->itemFontXtra());
00303         QString str;
00304         dateToString(date, str);
00305         str = i18n("created : %1",str);
00306         p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), str));
00307     }
00308 
00309     if (settings->getIconShowModDate())
00310     {
00311         QDateTime date(d->info.modDateTime());
00312 
00313         r = d->view->itemModDateRect();    
00314         p->setFont(d->view->itemFontXtra());
00315         QString str;
00316         dateToString(date, str);
00317         str = i18n("modified : %1",str);
00318         p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), str));
00319     }
00320 
00321     if (settings->getIconShowResolution())
00322     {
00323         QSize dims = d->info.dimensions();
00324         if (dims.isValid())
00325         {
00326             QString mpixels, resolution;
00327             mpixels.setNum(dims.width()*dims.height()/1000000.0, 'f', 2);
00328             resolution = (!dims.isValid()) ? i18n("Unknown") : i18n("%1x%2 (%3Mpx)",
00329                           dims.width(),dims.height(),mpixels);
00330             r = d->view->itemResolutionRect();
00331             p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), resolution));
00332         }
00333     }
00334 
00335     if (settings->getIconShowSize())
00336     {
00337         r = d->view->itemSizeRect();
00338         p->drawText(r, Qt::AlignCenter,
00339                     squeezedText(p, r.width(), KIO::convertSize(d->info.fileSize())));
00340     }
00341 
00342     p->setFont(d->view->itemFontCom());
00343     p->setPen(isSelected() ? te->textSpecialSelColor() : te->textSpecialRegColor());
00344 
00345     if (settings->getIconShowTags())
00346     {
00347         QString tags = AlbumManager::instance()->tagPaths(d->info.tagIds(), false).join(", ");
00348 
00349         r = d->view->itemTagRect();
00350         p->drawText(r, Qt::AlignCenter, squeezedText(p, r.width(), tags));
00351     }
00352 
00353     if (this == d->view->currentItem())
00354     {
00355         r = d->view->itemRect();
00356         p->setPen(QPen(isSelected() ? te->textSelColor() : te->textRegColor(), 0, Qt::DotLine));
00357         p->drawRect(1, 1, r.width()-2, r.height()-2);
00358     }
00359 }
00360 
00361 QRect AlbumIconItem::thumbnailRect() const
00362 {
00363     QRect pixmapRect = d->view->itemPixmapRect();
00364     QRect r          = rect();
00365 
00366     pixmapRect.translate(r.x(), r.y());
00367     return pixmapRect;
00368 }
00369 
00370 }  // namespace Digikam

digikam

Skip menu "digikam"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • digikam
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal