~ubuntu-branches/ubuntu/trusty/digikam/trusty

« back to all changes in this revision

Viewing changes to core/utilities/importui/items/itemviewimportdelegate.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-11-26 18:24:20 UTC
  • mfrom: (1.9.1) (3.1.23 experimental)
  • Revision ID: package-import@ubuntu.com-20121126182420-qoy6z0nx4ai0wzcl
Tags: 4:3.0.0~beta3-0ubuntu1
* New upstream release
  - Add build-deps :  libhupnp-dev, libqtgstreamer-dev, libmagickcore-dev
* Merge from debian, remaining changes:
  - Make sure libqt4-opengl-dev, libgl1-mesa-dev and libglu1-mesa-dev only
    install on i386,amd64 and powerpc
  - Depend on libtiff-dev instead of libtiff4-dev
  - Drop digikam breaks/replaces kipi-plugins-common since we're past the
    LTS release now
  - digikam to recommend mplayerthumbs | ffmpegthumbs. We currently only
    have latter in the archives, even though former is also supposed to
    be part of kdemultimedia. (LP: #890059)
  - kipi-plugins to recommend www-browser rather than konqueror directly
    since 2.8 no direct usage of konqueror is present in the flickr
    plugin anymore (LP: #1011211)
  - Keep kubuntu_mysqld_executable_name.diff
  - Don't install libkipi translations
  - Keep deps on libcv-dev, libcvaux-dev
  - Keep split packaging of libraries
  - Replace icons from KDE 3 time in debian/xpm.d/*.xpm with the new
    versions (LP: #658047)
* Update debian/not-installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2012-07-08
 
7
 * Description : Qt item view for images - the delegate
 
8
 *
 
9
 * Copyright (C) 2012 by Islam Wazery <wazery at ubuntu dot com>
 
10
 *
 
11
 * This program is free software; you can redistribute it
 
12
 * and/or modify it under the terms of the GNU General
 
13
 * Public License as published by the Free Software Foundation;
 
14
 * either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * ============================================================ */
 
23
 
 
24
#include "itemviewimportdelegate.moc"
 
25
#include "itemviewimportdelegatepriv.h"
 
26
 
 
27
// Qt includes
 
28
 
 
29
#include <QCache>
 
30
#include <QPainter>
 
31
#include <QIcon>
 
32
 
 
33
// KDE includes
 
34
 
 
35
#include <kglobal.h>
 
36
#include <kio/global.h>
 
37
#include <klocale.h>
 
38
#include <kiconloader.h>
 
39
#include <kdebug.h>
 
40
#include <kapplication.h>
 
41
 
 
42
// Local includes
 
43
 
 
44
#include "imagedelegateoverlay.h"
 
45
#include "thememanager.h"
 
46
#include "imagescanner.h"
 
47
#include "camiteminfo.h"
 
48
#include "colorlabelwidget.h"
 
49
 
 
50
namespace Digikam
 
51
{
 
52
 
 
53
ItemViewImportDelegatePrivate::ItemViewImportDelegatePrivate()
 
54
{
 
55
    q             = 0;
 
56
    spacing       = 0;
 
57
    thumbSize     = 0;
 
58
 
 
59
    // painting constants
 
60
    radius        = 3;
 
61
    margin        = 5;
 
62
 
 
63
    makeStarPolygon();
 
64
 
 
65
    ratingPixmaps = QVector<QPixmap>(10);
 
66
}
 
67
 
 
68
void ItemViewImportDelegatePrivate::init(ItemViewImportDelegate* const _q)
 
69
{
 
70
    q = _q;
 
71
 
 
72
    q->connect(ThemeManager::instance(), SIGNAL(signalThemeChanged()),
 
73
               q, SLOT(slotThemeChanged()));
 
74
}
 
75
 
 
76
void ItemViewImportDelegatePrivate::clearRects()
 
77
{
 
78
    gridSize   = QSize(0, 0);
 
79
    rect       = QRect(0, 0, 0, 0);
 
80
    ratingRect = QRect(0, 0, 0, 0);
 
81
}
 
82
 
 
83
void ItemViewImportDelegatePrivate::makeStarPolygon()
 
84
{
 
85
    // Pre-computed star polygon for a 15x15 pixmap.
 
86
    starPolygon << QPoint(0,  6);
 
87
    starPolygon << QPoint(5,  5);
 
88
    starPolygon << QPoint(7,  0);
 
89
    starPolygon << QPoint(9,  5);
 
90
    starPolygon << QPoint(14, 6);
 
91
    starPolygon << QPoint(10, 9);
 
92
    starPolygon << QPoint(11, 14);
 
93
    starPolygon << QPoint(7,  11);
 
94
    starPolygon << QPoint(3,  14);
 
95
    starPolygon << QPoint(4,  9);
 
96
 
 
97
    starPolygonSize = QSize(15, 15);
 
98
}
 
99
 
 
100
// ---- ItemViewImportDelegate -----------------------------------------------
 
101
 
 
102
ItemViewImportDelegate::ItemViewImportDelegate(QObject* const parent)
 
103
    : DItemDelegate(parent), d_ptr(new ItemViewImportDelegatePrivate)
 
104
{
 
105
    d_ptr->init(this);
 
106
}
 
107
 
 
108
ItemViewImportDelegate::ItemViewImportDelegate(ItemViewImportDelegatePrivate& dd, QObject* const parent)
 
109
    : DItemDelegate(parent), d_ptr(&dd)
 
110
{
 
111
    d_ptr->init(this);
 
112
}
 
113
 
 
114
ItemViewImportDelegate::~ItemViewImportDelegate()
 
115
{
 
116
    Q_D(ItemViewImportDelegate);
 
117
    removeAllOverlays();
 
118
    delete d;
 
119
}
 
120
 
 
121
ThumbnailSize ItemViewImportDelegate::thumbnailSize() const
 
122
{
 
123
    Q_D(const ItemViewImportDelegate);
 
124
    return d->thumbSize;
 
125
}
 
126
 
 
127
void ItemViewImportDelegate::setThumbnailSize(const ThumbnailSize& thumbSize)
 
128
{
 
129
    Q_D(ItemViewImportDelegate);
 
130
 
 
131
    if ( d->thumbSize != thumbSize)
 
132
    {
 
133
        d->thumbSize = thumbSize;
 
134
        invalidatePaintingCache();
 
135
    }
 
136
}
 
137
 
 
138
void ItemViewImportDelegate::setSpacing(int spacing)
 
139
{
 
140
    Q_D(ItemViewImportDelegate);
 
141
 
 
142
    if (d->spacing == spacing)
 
143
    {
 
144
        return;
 
145
    }
 
146
 
 
147
    d->spacing = spacing;
 
148
    invalidatePaintingCache();
 
149
}
 
150
 
 
151
int ItemViewImportDelegate::spacing() const
 
152
{
 
153
    Q_D(const ItemViewImportDelegate);
 
154
    return d->spacing;
 
155
}
 
156
 
 
157
QRect ItemViewImportDelegate::rect() const
 
158
{
 
159
    Q_D(const ItemViewImportDelegate);
 
160
    return d->rect;
 
161
}
 
162
 
 
163
QRect ItemViewImportDelegate::pixmapRect() const
 
164
{
 
165
    return QRect();
 
166
}
 
167
 
 
168
QRect ItemViewImportDelegate::imageInformationRect() const
 
169
{
 
170
    return QRect();
 
171
}
 
172
 
 
173
QRect ItemViewImportDelegate::ratingRect() const
 
174
{
 
175
    Q_D(const ItemViewImportDelegate);
 
176
    return d->ratingRect;
 
177
}
 
178
 
 
179
void ItemViewImportDelegate::setRatingEdited(const QModelIndex& index)
 
180
{
 
181
    Q_D(ItemViewImportDelegate);
 
182
    d->editingRating = index;
 
183
}
 
184
 
 
185
QSize ItemViewImportDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
 
186
{
 
187
    Q_D(const ItemViewImportDelegate);
 
188
    return d->rect.size();
 
189
}
 
190
 
 
191
QSize ItemViewImportDelegate::gridSize() const
 
192
{
 
193
    Q_D(const ItemViewImportDelegate);
 
194
    return d->gridSize;
 
195
}
 
196
 
 
197
bool ItemViewImportDelegate::acceptsToolTip(const QPoint&, const QRect& visualRect, const QModelIndex&, QRect* retRect) const
 
198
{
 
199
    if (retRect)
 
200
    {
 
201
        *retRect = visualRect;
 
202
    }
 
203
 
 
204
    return true;
 
205
}
 
206
 
 
207
bool ItemViewImportDelegate::acceptsActivation(const QPoint& , const QRect& visualRect, const QModelIndex&, QRect* retRect) const
 
208
{
 
209
    if (retRect)
 
210
    {
 
211
        *retRect = visualRect;
 
212
    }
 
213
 
 
214
    return true;
 
215
}
 
216
 
 
217
QAbstractItemDelegate* ItemViewImportDelegate::asDelegate()
 
218
{
 
219
    return this;
 
220
}
 
221
 
 
222
void ItemViewImportDelegate::overlayDestroyed(QObject* o)
 
223
{
 
224
    ImageDelegateOverlayContainer::overlayDestroyed(o);
 
225
}
 
226
 
 
227
void ItemViewImportDelegate::mouseMoved(QMouseEvent* e, const QRect& visualRect, const QModelIndex& index)
 
228
{
 
229
    // 3-way indirection AbstractImportItemDelegate -> ItemViewImportDelegate -> ImageDelegateOverlayContainer
 
230
    ImageDelegateOverlayContainer::mouseMoved(e, visualRect, index);
 
231
}
 
232
 
 
233
void ItemViewImportDelegate::setDefaultViewOptions(const QStyleOptionViewItem& option)
 
234
{
 
235
    Q_D(ItemViewImportDelegate);
 
236
    d->font = option.font;
 
237
    invalidatePaintingCache();
 
238
}
 
239
 
 
240
void ItemViewImportDelegate::slotThemeChanged()
 
241
{
 
242
    invalidatePaintingCache();
 
243
}
 
244
 
 
245
void ItemViewImportDelegate::slotSetupChanged()
 
246
{
 
247
    invalidatePaintingCache();
 
248
}
 
249
 
 
250
void ItemViewImportDelegate::invalidatePaintingCache()
 
251
{
 
252
    Q_D(ItemViewImportDelegate);
 
253
    QSize oldGridSize = d->gridSize;
 
254
    updateSizeRectsAndPixmaps();
 
255
 
 
256
    if (oldGridSize != d->gridSize)
 
257
    {
 
258
        emit gridSizeChanged(d->gridSize);
 
259
        // emit sizeHintChanged(QModelIndex());
 
260
    }
 
261
 
 
262
    emit visualChange();
 
263
}
 
264
 
 
265
QRect ItemViewImportDelegate::drawThumbnail(QPainter* p, const QRect& thumbRect, const QPixmap& background,
 
266
                                            const QPixmap& thumbnail) const
 
267
{
 
268
    p->drawPixmap(0, 0, background);
 
269
 
 
270
    if (thumbnail.isNull())
 
271
    {
 
272
        return QRect();
 
273
    }
 
274
 
 
275
    QRect r = thumbRect;
 
276
 
 
277
    QRect actualPixmapRect(r.x() + (r.width()-thumbnail.width())/2,
 
278
                           r.y() + (r.height()-thumbnail.height())/2,
 
279
                           thumbnail.width(), thumbnail.height());
 
280
 
 
281
    QPixmap borderPix = thumbnailBorderPixmap(actualPixmapRect.size());
 
282
    p->drawPixmap(actualPixmapRect.x()-3, actualPixmapRect.y()-3, borderPix);
 
283
 
 
284
    p->drawPixmap(r.x() + (r.width()-thumbnail.width())/2,
 
285
                  r.y() + (r.height()-thumbnail.height())/2,
 
286
                  thumbnail);
 
287
    return actualPixmapRect;
 
288
}
 
289
 
 
290
void ItemViewImportDelegate::drawRating(QPainter* p, const QModelIndex& index, const QRect& ratingRect,
 
291
                                       int rating, bool isSelected) const
 
292
{
 
293
    Q_D(const ItemViewImportDelegate);
 
294
 
 
295
    if (d->editingRating != index)
 
296
    {
 
297
        p->drawPixmap(ratingRect, ratingPixmap(rating, isSelected));
 
298
    }
 
299
/*
 
300
    else
 
301
        p->drawPixmap(r, ratingPixmap(-1, isSelected));
 
302
*/
 
303
}
 
304
 
 
305
void ItemViewImportDelegate::drawName(QPainter* p,const QRect& nameRect, const QString& name) const
 
306
{
 
307
    Q_D(const ItemViewImportDelegate);
 
308
    p->setFont(d->fontReg);
 
309
    p->drawText(nameRect, Qt::AlignCenter, name);//squeezedTextCached(p, nameRect.width(), name));
 
310
}
 
311
 
 
312
void ItemViewImportDelegate::drawModificationDate(QPainter* p, const QRect& dateRect, const QDateTime& date) const
 
313
{
 
314
    Q_D(const ItemViewImportDelegate);
 
315
    p->setFont(d->fontXtra);
 
316
    QString str = dateToString(date);
 
317
    str         = i18nc("date of last image modification", "modified: %1",str);
 
318
    p->drawText(dateRect, Qt::AlignCenter, str);//squeezedTextCached(p, dateRect.width(), str));
 
319
}
 
320
 
 
321
void ItemViewImportDelegate::drawImageFormat(QPainter* p, const QRect& r, const QString& mime) const
 
322
{
 
323
    Q_D(const ItemViewImportDelegate);
 
324
 
 
325
    if (!mime.isEmpty() && !r.isNull())
 
326
    {
 
327
        QString type = mime.split("/").at(1);
 
328
        type = ImageScanner::formatToString(type);
 
329
 
 
330
        p->save();
 
331
 
 
332
        QFont fnt(d->fontReg);
 
333
        fnt.setWeight(QFont::Black);
 
334
        fnt.setItalic(false);
 
335
        p->setFont(fnt);
 
336
        p->setPen(QPen(Qt::gray));
 
337
        p->setOpacity(0.50);
 
338
 
 
339
        QRect bRect = p->boundingRect(r, Qt::AlignTop | Qt::AlignHCenter, type.toUpper());
 
340
        bRect.adjust(-1, -1, 1, 1);
 
341
        bRect.translate(0, 1);
 
342
 
 
343
        p->fillRect(bRect, Qt::SolidPattern);
 
344
        p->setPen(QPen(Qt::white));
 
345
        p->setOpacity(1.0);
 
346
        p->drawText(bRect, Qt::AlignTop | Qt::AlignHCenter, type.toUpper());
 
347
 
 
348
        p->restore();
 
349
    }
 
350
}
 
351
 
 
352
void ItemViewImportDelegate::drawImageSize(QPainter* p, const QRect& dimsRect, const QSize& dims) const
 
353
{
 
354
    Q_D(const ItemViewImportDelegate);
 
355
 
 
356
    if (dims.isValid())
 
357
    {
 
358
        p->setFont(d->fontXtra);
 
359
        QString mpixels, resolution;
 
360
        mpixels.setNum(dims.width()*dims.height()/1000000.0, 'f', 2);
 
361
 
 
362
        if (dims.isValid())
 
363
            resolution = i18nc("%1 width, %2 height, %3 mpixels", "%1x%2 (%3Mpx)",
 
364
                               dims.width(), dims.height(), mpixels);
 
365
        else
 
366
        {
 
367
            resolution = i18nc("unknown image resolution", "Unknown");
 
368
        }
 
369
 
 
370
        p->drawText(dimsRect, Qt::AlignCenter, resolution);
 
371
    }
 
372
}
 
373
 
 
374
void ItemViewImportDelegate::drawFileSize(QPainter* p, const QRect& r, qlonglong bytes) const
 
375
{
 
376
    Q_D(const ItemViewImportDelegate);
 
377
    p->setFont(d->fontXtra);
 
378
    p->drawText(r, Qt::AlignCenter, KIO::convertSize(bytes));//squeezedTextCached(p, r.width(), KIO::convertSize(bytes)));
 
379
}
 
380
 
 
381
void ItemViewImportDelegate::drawTags(QPainter* p, const QRect& r, const QString& tagsString,
 
382
                                     bool isSelected) const
 
383
{
 
384
    Q_D(const ItemViewImportDelegate);
 
385
    p->setFont(d->fontCom);
 
386
    p->setPen(isSelected ? kapp->palette().color(QPalette::HighlightedText)
 
387
                         : kapp->palette().color(QPalette::Link));
 
388
 
 
389
    p->drawText(r, Qt::AlignCenter, squeezedTextCached(p, r.width(), tagsString));
 
390
}
 
391
 
 
392
void ItemViewImportDelegate::drawPickLabelIcon(QPainter* p, const QRect& r, int pickId) const
 
393
{
 
394
    // Draw Pick Label icon
 
395
    if (pickId != NoPickLabel)
 
396
    {
 
397
        QIcon icon;
 
398
 
 
399
        if (pickId == RejectedLabel)
 
400
        {
 
401
            icon = KIconLoader::global()->loadIcon("flag-red", KIconLoader::NoGroup, r.width());
 
402
        }
 
403
        else if (pickId == PendingLabel)
 
404
        {
 
405
            icon = KIconLoader::global()->loadIcon("flag-yellow", KIconLoader::NoGroup, r.width());
 
406
        }
 
407
        else if (pickId == AcceptedLabel)
 
408
        {
 
409
            icon = KIconLoader::global()->loadIcon("flag-green", KIconLoader::NoGroup, r.width());
 
410
        }
 
411
        icon.paint(p, r);
 
412
    }
 
413
}
 
414
 
 
415
void ItemViewImportDelegate::drawColorLabelRect(QPainter* p, const QStyleOptionViewItem& option,
 
416
                                               bool isSelected, int colorId) const
 
417
{
 
418
    Q_D(const ItemViewImportDelegate);
 
419
    Q_UNUSED(option);
 
420
    Q_UNUSED(isSelected);
 
421
 
 
422
    if (colorId > NoColorLabel)
 
423
    {
 
424
        // This draw a simple rectangle around item.
 
425
        p->setPen(QPen(ColorLabelWidget::labelColor((ColorLabel)colorId), 5, Qt::SolidLine));
 
426
        p->drawRect(3, 3, d->rect.width()-7, d->rect.height()-7);
 
427
    }
 
428
}
 
429
 
 
430
void ItemViewImportDelegate::drawPanelSideIcon(QPainter* p, bool left, bool right) const
 
431
{
 
432
    Q_D(const ItemViewImportDelegate);
 
433
    int iconSize = KIconLoader::SizeSmall;
 
434
 
 
435
    if (left)
 
436
    {
 
437
        QRect r(3, d->rect.height()/2 - iconSize/2, iconSize, iconSize);
 
438
        QIcon icon = KIconLoader::global()->loadIcon("arrow-left", KIconLoader::NoGroup, iconSize);
 
439
        icon.paint(p, r);
 
440
    }
 
441
 
 
442
    if (right)
 
443
    {
 
444
        QRect r(d->rect.width() - 3 - iconSize, d->rect.height()/2 - iconSize/2, iconSize, iconSize);
 
445
        QIcon icon = KIconLoader::global()->loadIcon("arrow-right", KIconLoader::NoGroup, iconSize);
 
446
        icon.paint(p, r);
 
447
    }
 
448
}
 
449
 
 
450
void ItemViewImportDelegate::drawDownloadIndicator(QPainter* p, const QRect& r, int itemType) const
 
451
{
 
452
    QIcon icon;
 
453
    if (itemType == CamItemInfo::DownloadUnknown)
 
454
    {
 
455
        icon = KIconLoader::global()->loadIcon("dialog-information", KIconLoader::NoGroup, KIconLoader::SizeSmall);
 
456
    }
 
457
 
 
458
    if (itemType == CamItemInfo::NewPicture)
 
459
    {
 
460
        icon = KIconLoader::global()->loadIcon("get-hot-new-stuff", KIconLoader::NoGroup, KIconLoader::SizeSmall);
 
461
    }
 
462
 
 
463
    if (itemType == CamItemInfo::DownloadedYes)
 
464
    {
 
465
        icon = KIconLoader::global()->loadIcon("dialog-ok", KIconLoader::NoGroup, KIconLoader::SizeSmall);
 
466
    }
 
467
 
 
468
    qreal op = p->opacity();
 
469
    p->setOpacity(0.5);
 
470
    icon.paint(p, r);
 
471
    p->setOpacity(op);
 
472
}
 
473
 
 
474
void ItemViewImportDelegate::drawLockIndicator(QPainter* p, const QRect& r, int lockStatus) const
 
475
{
 
476
    QIcon icon;
 
477
    if (lockStatus == 0)
 
478
    {
 
479
        icon = KIconLoader::global()->loadIcon("object-locked", KIconLoader::NoGroup, KIconLoader::SizeSmall);
 
480
    }
 
481
    if (lockStatus == 1)
 
482
    {
 
483
        icon = KIconLoader::global()->loadIcon("object-unlocked", KIconLoader::NoGroup, KIconLoader::SizeSmall);
 
484
    }
 
485
 
 
486
    qreal op = p->opacity();
 
487
    p->setOpacity(0.5);
 
488
    icon.paint(p, r);
 
489
    p->setOpacity(op);
 
490
}
 
491
 
 
492
void ItemViewImportDelegate::drawFocusRect(QPainter* p, const QStyleOptionViewItem& option,
 
493
                                          bool isSelected) const
 
494
{
 
495
    Q_D(const ItemViewImportDelegate);
 
496
 
 
497
    if (option.state & QStyle::State_HasFocus) //?? is current item
 
498
    {
 
499
        p->setPen(QPen(isSelected ? kapp->palette().color(QPalette::HighlightedText)
 
500
                                  : kapp->palette().color(QPalette::Text),
 
501
                       1, Qt::DotLine));
 
502
        p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
 
503
    }
 
504
}
 
505
 
 
506
void ItemViewImportDelegate::drawGroupIndicator(QPainter* p, const QRect& r,
 
507
                                                int numberOfGroupedImages, bool open) const
 
508
{
 
509
    if (numberOfGroupedImages)
 
510
    {
 
511
        QIcon icon;
 
512
 
 
513
        if (open)
 
514
        {
 
515
            icon = KIconLoader::global()->loadIcon("document-import", KIconLoader::NoGroup, r.width());
 
516
        }
 
517
        else
 
518
        {
 
519
            icon = KIconLoader::global()->loadIcon("document-multiple", KIconLoader::NoGroup, r.width());
 
520
        }
 
521
 
 
522
        qreal op = p->opacity();
 
523
        p->setOpacity(0.5);
 
524
        icon.paint(p, r);
 
525
        p->setOpacity(op);
 
526
 
 
527
        QString text = QString::number(numberOfGroupedImages);
 
528
        p->drawText(r, Qt::AlignCenter, text);
 
529
    }
 
530
}
 
531
 
 
532
void ItemViewImportDelegate::drawMouseOverRect(QPainter* p, const QStyleOptionViewItem& option) const
 
533
{
 
534
    Q_D(const ItemViewImportDelegate);
 
535
 
 
536
    if (option.state & QStyle::State_MouseOver)
 
537
    {
 
538
        p->setPen(QPen(option.palette.color(QPalette::Highlight), 3, Qt::SolidLine));
 
539
        p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
 
540
    }
 
541
}
 
542
 
 
543
void ItemViewImportDelegate::prepareFonts()
 
544
{
 
545
    Q_D(ItemViewImportDelegate);
 
546
 
 
547
    d->fontReg  = d->font;
 
548
    d->fontCom  = d->font;
 
549
    d->fontXtra = d->font;
 
550
    d->fontCom.setItalic(true);
 
551
 
 
552
    int fnSz = d->fontReg.pointSize();
 
553
 
 
554
    if (fnSz > 0)
 
555
    {
 
556
        d->fontCom.setPointSize(fnSz-1);
 
557
        d->fontXtra.setPointSize(fnSz-2);
 
558
    }
 
559
    else
 
560
    {
 
561
        fnSz = d->fontReg.pixelSize();
 
562
        d->fontCom.setPixelSize(fnSz-1);
 
563
        d->fontXtra.setPixelSize(fnSz-2);
 
564
    }
 
565
}
 
566
 
 
567
void ItemViewImportDelegate::prepareMetrics(int maxWidth)
 
568
{
 
569
    Q_D(ItemViewImportDelegate);
 
570
 
 
571
    QFontMetrics fm(d->fontReg);
 
572
    d->oneRowRegRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
 
573
                                       Qt::AlignTop | Qt::AlignHCenter,
 
574
                                       "XXXXXXXXX");
 
575
    fm = QFontMetrics(d->fontCom);
 
576
    d->oneRowComRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
 
577
                                       Qt::AlignTop | Qt::AlignHCenter,
 
578
                                       "XXXXXXXXX");
 
579
    fm = QFontMetrics(d->fontXtra);
 
580
    d->oneRowXtraRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
 
581
                                        Qt::AlignTop | Qt::AlignHCenter,
 
582
                                        "XXXXXXXXX");
 
583
}
 
584
 
 
585
void ItemViewImportDelegate::prepareBackground()
 
586
{
 
587
    Q_D(ItemViewImportDelegate);
 
588
 
 
589
    if (!d->rect.isValid())
 
590
    {
 
591
        d->regPixmap = QPixmap();
 
592
        d->selPixmap = QPixmap();
 
593
    }
 
594
    else
 
595
    {
 
596
        d->regPixmap = QPixmap(d->rect.width(), d->rect.height());
 
597
        d->regPixmap.fill(kapp->palette().color(QPalette::Base));
 
598
        QPainter p1(&d->regPixmap);
 
599
        p1.setPen(kapp->palette().color(QPalette::Midlight));
 
600
        p1.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
 
601
 
 
602
        d->selPixmap = QPixmap(d->rect.width(), d->rect.height());
 
603
        d->selPixmap.fill(kapp->palette().color(QPalette::Highlight));
 
604
        QPainter p2(&d->selPixmap);
 
605
        p2.setPen(kapp->palette().color(QPalette::Midlight));
 
606
        p2.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
 
607
    }
 
608
}
 
609
 
 
610
void ItemViewImportDelegate::prepareRatingPixmaps(bool composeOverBackground)
 
611
{
 
612
    /// Please call this method after prepareBackground() and when d->ratingPixmap is set
 
613
 
 
614
    Q_D(ItemViewImportDelegate);
 
615
 
 
616
    if (!d->ratingRect.isValid())
 
617
    {
 
618
        return;
 
619
    }
 
620
 
 
621
    // We use antialiasing and want to pre-render the pixmaps.
 
622
    // So we need the background at the time of painting,
 
623
    // and the background may be a gradient, and will be different for selected items.
 
624
    // This makes 5*2 (small) pixmaps.
 
625
    for (int sel=0; sel<2; ++sel)
 
626
    {
 
627
        QPixmap basePix;
 
628
 
 
629
        if (composeOverBackground)
 
630
        {
 
631
            // do this once for regular, once for selected backgrounds
 
632
            if (sel)
 
633
            {
 
634
                basePix = d->selPixmap.copy(d->ratingRect);
 
635
            }
 
636
            else
 
637
            {
 
638
                basePix = d->regPixmap.copy(d->ratingRect);
 
639
            }
 
640
        }
 
641
        else
 
642
        {
 
643
            basePix = QPixmap(d->ratingRect.size());
 
644
            basePix.fill(Qt::transparent);
 
645
        }
 
646
 
 
647
        for (int rating=1; rating<=5; ++rating)
 
648
        {
 
649
            // we store first the 5 regular, then the 5 selected pixmaps, for simplicity
 
650
            int index = (sel * 5 + rating) - 1;
 
651
 
 
652
            // copy background
 
653
            d->ratingPixmaps[index] = basePix;
 
654
            // open a painter
 
655
            QPainter painter(&d->ratingPixmaps[index]);
 
656
 
 
657
            // use antialiasing
 
658
            painter.setRenderHint(QPainter::Antialiasing, true);
 
659
            painter.setBrush(kapp->palette().color(QPalette::Link));
 
660
            QPen pen(kapp->palette().color(QPalette::Text));
 
661
            // set a pen which joins the lines at a filled angle
 
662
            pen.setJoinStyle(Qt::MiterJoin);
 
663
            painter.setPen(pen);
 
664
 
 
665
            // move painter while drawing polygons
 
666
            painter.translate( lround((d->ratingRect.width() - d->margin - rating*(d->starPolygonSize.width()+1))/2.0) + 2, 1 );
 
667
 
 
668
            for (int s=0; s<rating; ++s)
 
669
            {
 
670
                painter.drawPolygon(d->starPolygon, Qt::WindingFill);
 
671
                painter.translate(d->starPolygonSize.width() + 1, 0);
 
672
            }
 
673
        }
 
674
    }
 
675
}
 
676
 
 
677
QPixmap ItemViewImportDelegate::ratingPixmap(int rating, bool selected) const
 
678
{
 
679
    Q_D(const ItemViewImportDelegate);
 
680
 
 
681
    if (rating < 1 || rating > 5)
 
682
    {
 
683
        /*
 
684
        QPixmap pix;
 
685
        if (selected)
 
686
            pix = d->selPixmap.copy(d->ratingRect);
 
687
        else
 
688
            pix = d->regPixmap.copy(d->ratingRect);
 
689
 
 
690
        return pix;
 
691
        */
 
692
        return QPixmap();
 
693
    }
 
694
 
 
695
    --rating;
 
696
 
 
697
    if (selected)
 
698
    {
 
699
        return d->ratingPixmaps.at(5 + rating);
 
700
    }
 
701
    else
 
702
    {
 
703
        return d->ratingPixmaps.at(rating);
 
704
    }
 
705
}
 
706
 
 
707
 
 
708
} // namespace Digikam