| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
1 |
#include "thumbnaildelegate.h" |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
2 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
3 |
#include <QPainter> |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
4 |
#include <QApplication> |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
5 |
#include <QStyle> |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
6 |
#include <QTextOption> |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
7 |
#include <QFontMetrics> |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
8 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
9 |
ThumbnailDelegate::ThumbnailDelegate(QObject *parent) |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
10 |
: QStyledItemDelegate(parent) |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
11 |
{ |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
12 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
13 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
14 |
void ThumbnailDelegate::paint(QPainter *painter, |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
15 |
const QStyleOptionViewItem &option, |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
16 |
const QModelIndex &index) const |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
17 |
{ |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
18 |
painter->save(); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
19 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
20 |
// Folder items use the default delegate rendering unchanged |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
21 |
if (index.data(Qt::UserRole).toBool()) { |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
22 |
painter->restore(); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
23 |
QStyledItemDelegate::paint(painter, option, index); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
24 |
return; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
25 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
26 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
27 |
// Draw selection / hover background |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
28 |
QStyle *style = option.widget ? option.widget->style() : QApplication::style(); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
29 |
QStyleOptionViewItem bgOption = option; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
30 |
bgOption.showDecorationSelected = true; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
31 |
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &bgOption, painter, option.widget); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
32 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
33 |
const QRect cellRect = option.rect; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
34 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
35 |
// Fixed icon zone: top IconZoneHeight pixels of the cell |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
36 |
const QRect iconZone(cellRect.left(), cellRect.top(), |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
37 |
cellRect.width(), IconZoneHeight); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
38 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
39 |
// Fixed text zone: everything below the icon zone |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
40 |
const QRect textZone(cellRect.left(), cellRect.top() + IconZoneHeight, |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
41 |
cellRect.width(), cellRect.height() - IconZoneHeight); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
42 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
43 |
// --- Draw icon centered within the icon zone --- |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
44 |
const QVariant decorData = index.data(Qt::DecorationRole); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
45 |
QPixmap pixmap; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
46 |
if (decorData.userType() == QMetaType::QPixmap) { |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
47 |
pixmap = qvariant_cast<QPixmap>(decorData); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
48 |
} else if (decorData.userType() == QMetaType::QIcon) { |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
49 |
pixmap = qvariant_cast<QIcon>(decorData).pixmap(option.decorationSize); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
50 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
51 |
if (!pixmap.isNull()) { |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
52 |
// Center the pixmap in the icon zone |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
53 |
const int x = iconZone.left() + (iconZone.width() - pixmap.width()) / 2; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
54 |
const int y = iconZone.top() + (iconZone.height() - pixmap.height()) / 2; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
55 |
painter->drawPixmap(x, y, pixmap); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
56 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
57 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
58 |
// --- Draw text top-aligned within the text zone --- |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
59 |
const QString text = index.data(Qt::DisplayRole).toString(); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
60 |
if (!text.isEmpty()) { |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
61 |
QColor textColor = option.palette.color( |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
62 |
(option.state & QStyle::State_Selected) ? QPalette::HighlightedText |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
63 |
: QPalette::Text); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
64 |
painter->setPen(textColor); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
65 |
painter->setFont(option.font); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
66 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
67 |
QTextOption textOpt; |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
68 |
textOpt.setAlignment(Qt::AlignTop | Qt::AlignHCenter); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
69 |
textOpt.setWrapMode(QTextOption::WordWrap); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
70 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
71 |
// Elide text that would overflow the text zone |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
72 |
const QFontMetrics fm(option.font); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
73 |
const QString elidedText = fm.elidedText( |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
74 |
text, option.textElideMode, textZone.width() * 2); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
75 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
76 |
painter->drawText(QRectF(textZone), elidedText, textOpt); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
77 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
78 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
79 |
painter->restore(); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
80 |
} |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
81 |
|
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
82 |
QSize ThumbnailDelegate::sizeHint(const QStyleOptionViewItem &option, |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
83 |
const QModelIndex &index) const |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
84 |
{ |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
85 |
Q_UNUSED(option) |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
86 |
Q_UNUSED(index) |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
87 |
// Return the grid size set on the view (148×168) |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
88 |
return option.rect.size().isValid() ? option.rect.size() |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
89 |
: QStyledItemDelegate::sizeHint(option, index); |
| 1dcea855 |
Poul Sander |
2026-05-26 21:48 |
90 |
} |
|
|
1970-01-01 00:00 |
91 |
|