~ubuntu-branches/ubuntu/intrepid/digikam/intrepid

« back to all changes in this revision

Viewing changes to digikam/digikam/albumiconitem.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.3.2 upstream) (37 hardy)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20080717202539-1bw3w3nrsso7yj4z
* New upstream release
  - digiKam 0.9.4 Release Plan (KDE3) ~ 13 July 08 (Closes: #490144)
* DEB_CONFIGURE_EXTRA_FLAGS := --without-included-sqlite3
* Debhelper compatibility level V7
* Install pixmaps in debian/*.install
* Add debian/digikam.lintian-overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//////////////////////////////////////////////////////////////////////////////
2
 
//
3
 
//    ALBUMICONITEM.CPP
4
 
//
5
 
//    Copyright (C) 2003-2004 Renchi Raju <renchi at pooh.tam.uiuc.edu>
6
 
//                            Gilles Caulier <caulier dot gilles at free.fr>
7
 
//
8
 
//    This program is free software; you can redistribute it and/or modify
9
 
//    it under the terms of the GNU General Public License as published by
10
 
//    the Free Software Foundation; either version 2 of the License, or
11
 
//    (at your option) any later version.
12
 
//
13
 
//    This program is distributed in the hope that it will be useful,
14
 
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
//    GNU General Public License for more details.
17
 
//
18
 
//    You should have received a copy of the GNU General Public License
19
 
//    along with this program; if not, write to the Free Software
20
 
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
 
//
22
 
//////////////////////////////////////////////////////////////////////////////
23
 
 
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2003-04-25
 
7
 * Description : implementation to render album icon item.
 
8
 * 
 
9
 * Copyright (C) 2003-2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 
10
 * Copyright (C) 2003-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
11
 *
 
12
 * This program is free software; you can redistribute it
 
13
 * and/or modify it under the terms of the GNU General
 
14
 * Public License as published by the Free Software Foundation;
 
15
 * either version 2, or (at your option)
 
16
 * any later version.
 
17
 * 
 
18
 * This program is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 *
 
23
 * ============================================================ */
 
24
 
24
25
// Qt includes.
25
26
 
26
27
#include <qpainter.h>
27
28
#include <qpixmap.h>
28
29
#include <qpalette.h>
 
30
#include <qstring.h>
29
31
#include <qpen.h>
30
32
#include <qfontmetrics.h>
31
33
#include <qfont.h>
50
52
#include "albumiconview.h"
51
53
#include "albumiconitem.h"
52
54
 
 
55
namespace Digikam
 
56
{
 
57
 
 
58
class AlbumIconItemPriv
 
59
{
 
60
public:
 
61
 
 
62
    AlbumIconItemPriv()
 
63
    {
 
64
        dirty = true;
 
65
        info  = 0;
 
66
        view  = 0;
 
67
    }
 
68
 
 
69
    bool           dirty;
 
70
 
 
71
    QRect          tightPixmapRect;
 
72
 
 
73
    ImageInfo     *info;
 
74
 
 
75
    AlbumIconView *view;
 
76
};
 
77
 
53
78
static void dateToString(const QDateTime& datetime, QString& str)
54
79
{
55
80
    str = KGlobal::locale()->formatDateTime(datetime, true, false);
56
81
}
57
82
 
58
 
static QString squeezedText(QPainter* p, int width, const QString& text)
 
83
AlbumIconItem::AlbumIconItem(IconGroupItem* parent, ImageInfo* info)
 
84
             : IconItem(parent)
 
85
{
 
86
    d = new AlbumIconItemPriv;
 
87
    d->view = (AlbumIconView*) parent->iconView();
 
88
    d->info = info;
 
89
}
 
90
 
 
91
AlbumIconItem::~AlbumIconItem()
 
92
{
 
93
    delete d;
 
94
}
 
95
 
 
96
QString AlbumIconItem::squeezedText(QPainter* p, int width, const QString& text)
59
97
{
60
98
    QString fullText(text);
61
99
    fullText.replace("\n"," ");
62
100
    QFontMetrics fm(p->fontMetrics());
63
101
    int textWidth = fm.width(fullText);
64
 
    if (textWidth > width) {
 
102
    
 
103
    if (textWidth > width) 
 
104
    {
65
105
        // start with the dots only
66
106
        QString squeezedText = "...";
67
107
        int squeezedWidth = fm.width(squeezedText);
72
112
        squeezedText = fullText.left(letters) + "...";
73
113
        squeezedWidth = fm.width(squeezedText);
74
114
 
75
 
        if (squeezedWidth < width) {
 
115
        if (squeezedWidth < width) 
 
116
        {
76
117
            // we estimated too short
77
118
            // add letters while text < label
78
 
            do {
 
119
            do 
 
120
            {
79
121
                letters++;
80
122
                squeezedText = fullText.left(letters) + "..."; 
81
123
                squeezedWidth = fm.width(squeezedText);
82
 
            } while (squeezedWidth < width);
 
124
            }
 
125
            while (squeezedWidth < width);
 
126
 
83
127
            letters--;
84
128
            squeezedText = fullText.left(letters) + "..."; 
85
 
        } else if (squeezedWidth > width) {
 
129
        }
 
130
        else if (squeezedWidth > width) 
 
131
        {
86
132
            // we estimated too long
87
133
            // remove letters while text > label
88
 
            do {
 
134
            do 
 
135
            {
89
136
                letters--;
90
137
                squeezedText = fullText.left(letters) + "...";
91
138
                squeezedWidth = fm.width(squeezedText);
92
 
            } while (letters && squeezedWidth > width);
 
139
            }
 
140
            while (letters && squeezedWidth > width);
93
141
        }
94
142
 
95
 
 
96
 
        if (letters >= 5) {
 
143
        if (letters >= 5) 
 
144
        {
97
145
            return squeezedText;
98
146
        }
99
147
    }
101
149
    return fullText;   
102
150
}
103
151
 
104
 
AlbumIconItem::AlbumIconItem(IconGroupItem* parent, ImageInfo* info)
105
 
             : IconItem(parent)
106
 
{
107
 
    view_        = (AlbumIconView*) parent->iconView();
108
 
    info_        = info;
109
 
    dirty_       = true;
110
 
}
111
 
 
112
 
 
113
 
AlbumIconItem::~AlbumIconItem()
114
 
{
 
152
bool AlbumIconItem::isDirty()
 
153
{
 
154
    return d->dirty;
115
155
}
116
156
 
117
157
ImageInfo* AlbumIconItem::imageInfo() const
118
158
{
119
 
    return info_;
 
159
    return d->info;
120
160
}
121
161
 
122
162
int AlbumIconItem::compare(IconItem *item)
123
163
{
124
 
    const AlbumSettings *settings = view_->settings();
 
164
    const AlbumSettings *settings = d->view->settings();
125
165
    AlbumIconItem *iconItem = static_cast<AlbumIconItem*>(item);
126
166
    
127
167
    switch (settings->getImageSortOrder())
128
168
    {
129
 
    case(AlbumSettings::ByIName):
130
 
    {
131
 
        return info_->name().localeAwareCompare(iconItem->info_->name());
132
 
    }
133
 
    case(AlbumSettings::ByIPath):
134
 
    {
135
 
        return info_->kurl().path().compare(iconItem->info_->kurl().path());
136
 
    }
137
 
    case(AlbumSettings::ByIDate):
138
 
    {
139
 
        if (info_->dateTime() < iconItem->info_->dateTime())
140
 
            return -1;
141
 
        else if (info_->dateTime() > iconItem->info_->dateTime())
142
 
            return 1;
143
 
        else
144
 
            return 0;
145
 
    }
146
 
    case(AlbumSettings::ByISize):
147
 
    {
148
 
        int mysize(info_->fileSize());
149
 
        int hissize(iconItem->info_->fileSize());
150
 
        if (mysize < hissize)
151
 
            return -1;
152
 
        else if (mysize > hissize)
153
 
            return 1;
154
 
        else
155
 
            return 0;
156
 
    }
157
 
    case(AlbumSettings::ByIRating):
158
 
    {
159
 
        int myrating(info_->rating());
160
 
        int hisrating(iconItem->info_->rating());
161
 
        if (myrating < hisrating)
162
 
            return 1;
163
 
        else if (myrating > hisrating)
164
 
            return -1;
165
 
        else
166
 
            return 0;
167
 
    }
 
169
        case(AlbumSettings::ByIName):
 
170
        {
 
171
            return d->info->name().localeAwareCompare(iconItem->d->info->name());
 
172
        }
 
173
        case(AlbumSettings::ByIPath):
 
174
        {
 
175
            return d->info->kurl().path().compare(iconItem->d->info->kurl().path());
 
176
        }
 
177
        case(AlbumSettings::ByIDate):
 
178
        {
 
179
            if (d->info->dateTime() < iconItem->d->info->dateTime())
 
180
                return -1;
 
181
            else if (d->info->dateTime() > iconItem->d->info->dateTime())
 
182
                return 1;
 
183
            else
 
184
                return 0;
 
185
        }
 
186
        case(AlbumSettings::ByISize):
 
187
        {
 
188
            int mysize(d->info->fileSize());
 
189
            int hissize(iconItem->d->info->fileSize());
 
190
            if (mysize < hissize)
 
191
                return -1;
 
192
            else if (mysize > hissize)
 
193
                return 1;
 
194
            else
 
195
                return 0;
 
196
        }
 
197
        case(AlbumSettings::ByIRating):
 
198
        {
 
199
            int myrating(d->info->rating());
 
200
            int hisrating(iconItem->d->info->rating());
 
201
            if (myrating < hisrating)
 
202
                return 1;
 
203
            else if (myrating > hisrating)
 
204
                return -1;
 
205
            else
 
206
                return 0;
 
207
        }
168
208
    }
169
209
 
170
210
    return 0;
172
212
 
173
213
QRect AlbumIconItem::clickToOpenRect()
174
214
{
175
 
    if (tightPixmapRect_.isNull())
 
215
    if (d->tightPixmapRect.isNull())
176
216
        return rect();
177
217
    
178
 
    QRect pixmapRect = tightPixmapRect_;
 
218
    QRect pixmapRect = d->tightPixmapRect;
179
219
    QRect r          = rect();
180
220
 
181
221
    pixmapRect.moveBy(r.x(), r.y());
186
226
{
187
227
    QPixmap pix;
188
228
    QRect   r;
189
 
    const AlbumSettings *settings = view_->settings();
 
229
    const AlbumSettings *settings = d->view->settings();
190
230
    
191
231
    if (isSelected())
192
 
        pix = *(view_->itemBaseSelPixmap());
 
232
        pix = *(d->view->itemBaseSelPixmap());
193
233
    else
194
 
        pix = *(view_->itemBaseRegPixmap());
 
234
        pix = *(d->view->itemBaseRegPixmap());
195
235
 
196
 
    Digikam::ThemeEngine* te = Digikam::ThemeEngine::instance();
 
236
    ThemeEngine* te = ThemeEngine::instance();
197
237
    
198
238
    QPainter p(&pix);
199
239
    p.setPen(isSelected() ? te->textSelColor() : te->textRegColor());
200
240
 
201
241
 
202
 
    dirty_ = true;
 
242
    d->dirty = true;
203
243
    
204
 
    QPixmap *thumbnail = view_->pixmapManager()->find(info_->kurl());
 
244
    QPixmap *thumbnail = d->view->pixmapManager()->find(d->info->kurl());
205
245
    if (thumbnail)
206
246
    {
207
 
        r = view_->itemPixmapRect();
 
247
        r = d->view->itemPixmapRect();
208
248
        p.drawPixmap(r.x() + (r.width()-thumbnail->width())/2,
209
249
                     r.y() + (r.height()-thumbnail->height())/2,
210
250
                     *thumbnail);
211
 
        tightPixmapRect_.setRect(r.x() + (r.width()-thumbnail->width())/2,
 
251
        d->tightPixmapRect.setRect(r.x() + (r.width()-thumbnail->width())/2,
212
252
                                 r.y() + (r.height()-thumbnail->height())/2,
213
253
                                 thumbnail->width(), thumbnail->height());
214
 
        dirty_ = false;
 
254
        d->dirty = false;
215
255
    }
216
256
 
217
257
    if (settings->getIconShowRating())
218
258
    {
219
 
        r = view_->itemRatingRect();
220
 
        QPixmap ratingPixmap = view_->ratingPixmap();
 
259
        r = d->view->itemRatingRect();
 
260
        QPixmap ratingPixmap = d->view->ratingPixmap();
221
261
 
222
 
        int rating = info_->rating();
 
262
        int rating = d->info->rating();
223
263
        
224
264
        int x, w;
225
265
        x = r.x() + (r.width() - rating * ratingPixmap.width())/2;
226
266
        w = rating * ratingPixmap.width();
227
267
        
228
268
        p.drawTiledPixmap(x, r.y(), w, r.height(), ratingPixmap);
229
 
    }
230
 
    
 
269
    }    
231
270
    
232
271
    if (settings->getIconShowName())
233
272
    {
234
 
        r = view_->itemNameRect();
235
 
        p.setFont(view_->itemFontReg());
 
273
        r = d->view->itemNameRect();
 
274
        p.setFont(d->view->itemFontReg());
236
275
        p.drawText(r, Qt::AlignCenter, squeezedText(&p, r.width(),
237
 
                                                    info_->name()));
 
276
                                                    d->info->name()));
238
277
    }
239
278
 
240
 
    p.setFont(view_->itemFontCom());
 
279
    p.setFont(d->view->itemFontCom());
241
280
    
242
281
    if (settings->getIconShowComments())
243
282
    {
244
 
        QString comments = info_->caption();
 
283
        QString comments = d->info->caption();
245
284
        
246
 
        r = view_->itemCommentsRect();
 
285
        r = d->view->itemCommentsRect();
247
286
        p.drawText(r, Qt::AlignCenter, squeezedText(&p, r.width(), comments));
248
287
    }
249
288
 
250
 
    p.setFont(view_->itemFontXtra());
 
289
    p.setFont(d->view->itemFontXtra());
251
290
 
252
291
    if (settings->getIconShowDate())
253
292
    {
254
 
        QDateTime date(info_->dateTime());
255
 
 
256
 
        r = view_->itemDateRect();    
257
 
        p.setFont(view_->itemFontXtra());
258
 
        QString str;
259
 
        dateToString(date, str);
 
293
        QDateTime date(d->info->dateTime());
 
294
 
 
295
        r = d->view->itemDateRect();    
 
296
        p.setFont(d->view->itemFontXtra());
 
297
        QString str;
 
298
        dateToString(date, str);
 
299
        str = i18n("created : %1").arg(str);
 
300
        p.drawText(r, Qt::AlignCenter, squeezedText(&p, r.width(), str));
 
301
    }
 
302
    
 
303
    if (settings->getIconShowModDate())
 
304
    {
 
305
        QDateTime date(d->info->modDateTime());
 
306
 
 
307
        r = d->view->itemModDateRect();    
 
308
        p.setFont(d->view->itemFontXtra());
 
309
        QString str;
 
310
        dateToString(date, str);
 
311
        str = i18n("modified : %1").arg(str);
260
312
        p.drawText(r, Qt::AlignCenter, squeezedText(&p, r.width(), str));
261
313
    }
262
314
    
263
315
    if (settings->getIconShowResolution())
264
316
    {
265
 
        QSize dims = info_->dimensions();
 
317
        QSize dims = d->info->dimensions();
266
318
        if (dims.isValid())
267
319
        {
268
 
            QString resolution = QString("%1x%2 %3")
269
 
                                 .arg(dims.width())
270
 
                                 .arg(dims.height())
271
 
                                 .arg(i18n("pixels"));
272
 
            r = view_->itemResolutionRect();    
 
320
            QString mpixels, resolution;
 
321
            mpixels.setNum(dims.width()*dims.height()/1000000.0, 'f', 2);
 
322
            resolution = (!dims.isValid()) ? i18n("Unknown") : i18n("%1x%2 (%3Mpx)")
 
323
                         .arg(dims.width()).arg(dims.height()).arg(mpixels);
 
324
            r = d->view->itemResolutionRect();    
273
325
            p.drawText(r, Qt::AlignCenter, squeezedText(&p, r.width(), resolution));
274
326
        }
275
327
    }
276
328
 
277
329
    if (settings->getIconShowSize())
278
330
    {
279
 
        r = view_->itemSizeRect();    
 
331
        r = d->view->itemSizeRect();    
280
332
        p.drawText(r, Qt::AlignCenter,
281
333
                   squeezedText(&p, r.width(),
282
 
                                KIO::convertSize(info_->fileSize())));
 
334
                                KIO::convertSize(d->info->fileSize())));
283
335
    }
284
336
 
285
 
    p.setFont(view_->itemFontCom());
 
337
    p.setFont(d->view->itemFontCom());
286
338
    p.setPen(isSelected() ? te->textSpecialSelColor() : te->textSpecialRegColor());
287
339
 
288
340
    if (settings->getIconShowTags())
289
341
    {
290
 
        QString tags = info_->tagNames().join(", ");
 
342
        QString tags = d->info->tagNames().join(", ");
291
343
        
292
 
        r = view_->itemTagRect();    
 
344
        r = d->view->itemTagRect();    
293
345
        p.drawText(r, Qt::AlignCenter, 
294
346
                   squeezedText(&p, r.width(), tags));
295
347
    }
296
348
 
297
 
    if (this == view_->currentItem())
 
349
    if (this == d->view->currentItem())
298
350
    {
299
351
        p.setPen(QPen(isSelected() ? te->textSelColor() : te->textRegColor(),
300
352
                      0, Qt::DotLine));
304
356
    p.end();
305
357
    
306
358
    r = rect();
307
 
    r = QRect(view_->contentsToViewport(QPoint(r.x(), r.y())),
 
359
    r = QRect(d->view->contentsToViewport(QPoint(r.x(), r.y())),
308
360
              QSize(r.width(), r.height()));
309
361
 
310
 
    bitBlt(view_->viewport(), r.x(), r.y(), &pix,
 
362
    bitBlt(d->view->viewport(), r.x(), r.y(), &pix,
311
363
           0, 0, r.width(), r.height());
312
364
}
313
365
 
314
366
QRect AlbumIconItem::thumbnailRect() const
315
367
{
316
 
    QRect pixmapRect = view_->itemPixmapRect();
 
368
    QRect pixmapRect = d->view->itemPixmapRect();
317
369
    QRect r          = rect();
318
370
 
319
371
    pixmapRect.moveBy(r.x(), r.y());
320
372
    return pixmapRect;
321
373
}
322
374
 
 
375
}  // namespace Digikam