1/* -*- C++ -*-
2 This file implements the SMIVItemDelegate class.
3
4 SPDX-FileCopyrightText: 2005 Mirko Boehm <mirko@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7
8 License: LGPL with the following explicit clarification:
9 This code may be linked against any version of the Qt toolkit
10 from Trolltech, Norway.
11
12 $Id: SMIVItemDelegate.cpp 30 2005-08-16 16:16:04Z mirko $
13*/
14
15#include <QModelIndex>
16#include <QPainter>
17
18#include "Image.h"
19#include "ItemDelegate.h"
20#include "Model.h"
21
22const int ItemDelegate::FrameWidth = 2;
23const int ItemDelegate::TextMargin = 6;
24const int ItemDelegate::Margin = 3;
25
26ItemDelegate::ItemDelegate(QObject *parent)
27 : QItemDelegate(parent)
28{
29}
30
31void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
32{
33 const bool itemIsSelected = option.showDecorationSelected && (option.state & QStyle::State_Selected);
34 const Image *image = index.data(arole: Model::Role_ImageRole).value<const Image *>();
35 // calculate some constants:
36 const int y0 = option.rect.top();
37 const int x0 = option.rect.left();
38 const int width = option.rect.width();
39 // const int height = option.rect.height();
40
41 painter->save();
42
43 // draw the background color, depending on focus:
44 if (itemIsSelected) {
45 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
46 painter->fillRect(option.rect, option.palette.brush(cg, cr: QPalette::Highlight));
47 }
48
49 // draw the image frame:
50 painter->setPen(Qt::blue);
51 painter->setBrush(Qt::white);
52 painter->drawRect(x: x0 + FrameWidth + Margin, y: y0 + FrameWidth + Margin, w: Image::ThumbWidth + 1, h: Image::ThumbHeight + 1);
53
54 // draw the image:
55 if (image->progress().first >= Image::Step_LoadImage) {
56 const QImage &thumb = image->thumbNail();
57 QPoint orig = QPoint(x0 + FrameWidth + Margin + 1, y0 + FrameWidth + Margin + 1);
58 painter->drawImage(p: orig, image: thumb, sr: QRect(0, qMax(a: 0, b: (thumb.height() - Image::ThumbHeight) / 2), Image::ThumbWidth, Image::ThumbHeight));
59 }
60
61 // render the text next to the image:
62 painter->setPen(Qt::black);
63 QFontMetrics font1Metrics(option.font);
64 int textx0 = x0 + FrameWidth + Margin + Image::ThumbWidth + TextMargin;
65 QRect text1Rect = QRect(textx0, y0 + TextMargin, width - TextMargin - textx0, font1Metrics.lineSpacing());
66 painter->drawText(r: text1Rect, text: image->description());
67 if (itemIsSelected) {
68 painter->setPen(Qt::white);
69 } else {
70 painter->setPen(Qt::darkGray);
71 }
72 QFont font2 = option.font;
73 font2.setPointSize((int)(0.9 * option.font.pointSize()));
74 painter->setFont(font2);
75 QFontMetrics font2Metrics(font2);
76 QRect text2Rect = text1Rect.adjusted(xp1: 0, yp1: font1Metrics.lineSpacing(), xp2: 0, yp2: font2Metrics.lineSpacing());
77 painter->drawText(r: text2Rect, text: image->details());
78 QRect text3Rect = text2Rect.adjusted(xp1: 0, yp1: font2Metrics.lineSpacing(), xp2: 0, yp2: font2Metrics.lineSpacing());
79 painter->drawText(r: text3Rect, text: image->details2());
80
81 painter->restore();
82}
83
84QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
85{
86 static const int Width = Image::ThumbWidth + 2 * FrameWidth + 2 * Margin + 2;
87 static const int Height = Image::ThumbHeight + 2 * FrameWidth + 2 * Margin + 2;
88 return QSize(Width, Height);
89}
90
91#include "moc_ItemDelegate.cpp"
92

source code of threadweaver/examples/ThumbNailer/ItemDelegate.cpp