1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2013-08-01
* Description : Qt model view for Showfoto item - the delegate
*
* SPDX-FileCopyrightText: 2013 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "showfotoitemviewdelegate_p.h"
namespace ShowFoto
{
void ShowfotoItemViewDelegatePrivate::init(ShowfotoItemViewDelegate* const _q, QWidget* const _widget)
{
q = _q;
displayWidget = _widget;
q->connect(ThemeManager::instance(), SIGNAL(signalThemeChanged()),
q, SLOT(slotThemeChanged()));
}
void ShowfotoItemViewDelegatePrivate::clearRects()
{
gridSize = QSize(0, 0);
rect = QRect(0, 0, 0, 0);
}
// ---- ShowfotoItemViewDelegate -----------------------------------------------
ShowfotoItemViewDelegate::ShowfotoItemViewDelegate(QWidget* const parent)
: DItemDelegate(parent),
d_ptr (new ShowfotoItemViewDelegatePrivate)
{
d_ptr->init(this, parent);
}
ShowfotoItemViewDelegate::ShowfotoItemViewDelegate(ShowfotoItemViewDelegatePrivate& dd, QWidget* const parent)
: DItemDelegate(parent),
d_ptr (&dd)
{
d_ptr->init(this, parent);
}
ShowfotoItemViewDelegate::~ShowfotoItemViewDelegate()
{
Q_D(ShowfotoItemViewDelegate);
removeAllOverlays();
delete d;
}
ThumbnailSize ShowfotoItemViewDelegate::thumbnailSize() const
{
Q_D(const ShowfotoItemViewDelegate);
return d->thumbSize;
}
double ShowfotoItemViewDelegate::displayRatio() const
{
Q_D(const ShowfotoItemViewDelegate);
return d->displayWidget->devicePixelRatio();
}
void ShowfotoItemViewDelegate::setThumbnailSize(const ThumbnailSize& thumbSize)
{
Q_D(ShowfotoItemViewDelegate);
if (d->thumbSize != thumbSize)
{
d->thumbSize = thumbSize;
invalidatePaintingCache();
}
}
void ShowfotoItemViewDelegate::setSpacing(int spacing)
{
Q_D(ShowfotoItemViewDelegate);
if (d->spacing == spacing)
{
return;
}
d->spacing = spacing;
invalidatePaintingCache();
}
int ShowfotoItemViewDelegate::spacing() const
{
Q_D(const ShowfotoItemViewDelegate);
return d->spacing;
}
QRect ShowfotoItemViewDelegate::rect() const
{
Q_D(const ShowfotoItemViewDelegate);
return d->rect;
}
QRect ShowfotoItemViewDelegate::pixmapRect() const
{
return QRect();
}
QRect ShowfotoItemViewDelegate::imageInformationRect() const
{
return QRect();
}
QSize ShowfotoItemViewDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
{
Q_D(const ShowfotoItemViewDelegate);
return d->rect.size();
}
QSize ShowfotoItemViewDelegate::gridSize() const
{
Q_D(const ShowfotoItemViewDelegate);
return d->gridSize;
}
bool ShowfotoItemViewDelegate::acceptsToolTip(const QPoint&, const QRect& visualRect, const QModelIndex&, QRect* retRect) const
{
if (retRect)
{
*retRect = visualRect;
}
return true;
}
bool ShowfotoItemViewDelegate::acceptsActivation(const QPoint& , const QRect& visualRect, const QModelIndex&, QRect* retRect) const
{
if (retRect)
{
*retRect = visualRect;
}
return true;
}
QAbstractItemDelegate* ShowfotoItemViewDelegate::asDelegate()
{
return this;
}
void ShowfotoItemViewDelegate::overlayDestroyed(QObject* o)
{
ItemDelegateOverlayContainer::overlayDestroyed(o);
}
void ShowfotoItemViewDelegate::mouseMoved(QMouseEvent* e, const QRect& visualRect, const QModelIndex& index)
{
// 3-way indirection AbstractShowfotoItemDelegate -> ShowfotoItemViewDelegate -> ItemDelegateOverlayContainer
ItemDelegateOverlayContainer::mouseMoved(e, visualRect, index);
}
void ShowfotoItemViewDelegate::setDefaultViewOptions(const QStyleOptionViewItem& option)
{
Q_D(ShowfotoItemViewDelegate);
d->font = option.font;
invalidatePaintingCache();
}
void ShowfotoItemViewDelegate::slotThemeChanged()
{
invalidatePaintingCache();
}
void ShowfotoItemViewDelegate::slotSetupChanged()
{
invalidatePaintingCache();
}
void ShowfotoItemViewDelegate::invalidatePaintingCache()
{
Q_D(ShowfotoItemViewDelegate);
QSize oldGridSize = d->gridSize;<--- oldGridSize is assigned 'd->gridSize' here.
updateSizeRectsAndPixmaps();
if (oldGridSize != d->gridSize)<--- Condition 'oldGridSize!=d->gridSize' is always false
{
Q_EMIT gridSizeChanged(d->gridSize);
/*
// Q_EMIT sizeHintChanged(QModelIndex());
*/
}
Q_EMIT visualChange();
}
QRect ShowfotoItemViewDelegate::drawThumbnail(QPainter* p, const QRect& thumbRect, const QPixmap& background,
const QPixmap& thumbnail) const
{
p->drawPixmap(0, 0, background);
if (thumbnail.isNull())
{
return QRect();
}
QRect r = thumbRect;
double dpr = displayRatio();
int thumbW = qRound((double)thumbnail.width() / dpr);
int thumbH = qRound((double)thumbnail.height() / dpr);
QRect actualPixmapRect(r.x() + (r.width() - thumbW) / 2,
r.y() + (r.height() - thumbH) / 2,
thumbW, thumbH);
QPixmap borderPix = thumbnailBorderPixmap(actualPixmapRect.size());
p->drawPixmap(actualPixmapRect.x() - 3,
actualPixmapRect.y() - 3, borderPix);
p->drawPixmap(r.x() + (r.width() - thumbW) / 2,
r.y() + (r.height() - thumbH) / 2,
thumbW, thumbH, thumbnail);
return actualPixmapRect;
}
void ShowfotoItemViewDelegate::drawName(QPainter* p,const QRect& nameRect, const QString& name) const
{
Q_D(const ShowfotoItemViewDelegate);
p->setFont(d->fontReg);
p->drawText(nameRect, Qt::AlignCenter, name); //squeezedTextCached(p, nameRect.width(), name));
}
void ShowfotoItemViewDelegate::drawCreationDate(QPainter* p, const QRect& dateRect, const QDateTime& date) const
{
Q_D(const ShowfotoItemViewDelegate);
p->setFont(d->fontXtra);
QString str = dateToString(date);
str = i18nc("date of image creation", "created: %1", str);
p->drawText(dateRect, Qt::AlignCenter, str); //squeezedTextCached(p, dateRect.width(), str));
}
void ShowfotoItemViewDelegate::drawImageFormat(QPainter* p, const QRect& r, const QString& mime) const
{
Q_D(const ShowfotoItemViewDelegate);
if (!mime.isEmpty() && !r.isNull())
{
QString type = mime;
p->save();
QFont fnt(d->fontReg);
fnt.setWeight(QFont::Black);
fnt.setItalic(false);
p->setFont(fnt);
p->setPen(QPen(Qt::gray));
p->setOpacity(0.50);
QRect bRect = p->boundingRect(r, Qt::AlignBottom | Qt::AlignHCenter, type.toUpper());
bRect.adjust(1, 1, -1, -1);
bRect.translate(0, 1);
p->fillRect(bRect, Qt::SolidPattern);
p->setPen(QPen(Qt::white));
p->setOpacity(1.0);
p->drawText(bRect, Qt::AlignBottom | Qt::AlignHCenter, type.toUpper());
p->restore();
}
}
void ShowfotoItemViewDelegate::drawGeolocationIndicator(QPainter* p, const QRect& r) const
{
if (!r.isNull())
{
QIcon icon(QIcon::fromTheme(QLatin1String("globe")).pixmap(r.size()));
QBrush brush = p->brush();
p->setOpacity(0.50);
p->setPen(QPen(p->background().color()));
p->setBrush(QBrush(p->background().color()));
p->drawEllipse(r);
p->setBrush(brush);
p->setOpacity(1.0);
icon.paint(p, r);
}
}
void ShowfotoItemViewDelegate::drawImageSize(QPainter* p, const QRect& dimsRect, const QSize& dims) const
{
Q_D(const ShowfotoItemViewDelegate);
if (dims.isValid())
{
p->setFont(d->fontXtra);
QString mpixels, resolution;
mpixels = QLocale().toString(dims.width()*dims.height()/1000000.0, 'f', 1);
if (dims.isValid())
{
resolution = i18nc("%1 width, %2 height, %3 mpixels", "%1x%2 (%3Mpx)",
dims.width(), dims.height(), mpixels);
}
else
{
resolution = i18nc("unknown image resolution", "Unknown");
}
p->drawText(dimsRect, Qt::AlignCenter, resolution);
}
}
void ShowfotoItemViewDelegate::drawFileSize(QPainter* p, const QRect& r, qlonglong bytes) const
{
Q_D(const ShowfotoItemViewDelegate);
p->setFont(d->fontXtra);
p->drawText(r, Qt::AlignCenter, ItemPropertiesTab::humanReadableBytesCount(bytes));
}
void ShowfotoItemViewDelegate::drawFocusRect(QPainter* p, const QStyleOptionViewItem& option,
bool isSelected) const
{
Q_D(const ShowfotoItemViewDelegate);
if (option.state & QStyle::State_HasFocus) //?? is current item
{
p->setPen(QPen(isSelected ? qApp->palette().color(QPalette::HighlightedText)
: qApp->palette().color(QPalette::Text),
1, Qt::DotLine));
p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
}
}
void ShowfotoItemViewDelegate::drawMouseOverRect(QPainter* p, const QStyleOptionViewItem& option) const
{
Q_D(const ShowfotoItemViewDelegate);
if (option.state & QStyle::State_MouseOver)
{
p->setPen(QPen(option.palette.color(QPalette::Highlight), 3, Qt::SolidLine));
p->drawRect(1, 1, d->rect.width()-3, d->rect.height()-3);
}
}
void ShowfotoItemViewDelegate::prepareFonts()
{
Q_D(ShowfotoItemViewDelegate);
d->fontReg = d->font;
d->fontCom = d->font;
d->fontXtra = d->font;
d->fontCom.setItalic(true);
int fnSz = d->fontReg.pointSize();
if (fnSz > 0)
{
d->fontCom.setPointSize(fnSz-1);
d->fontXtra.setPointSize(fnSz-2);
}
else
{
fnSz = d->fontReg.pixelSize();
d->fontCom.setPixelSize(fnSz-1);
d->fontXtra.setPixelSize(fnSz-2);
}
}
void ShowfotoItemViewDelegate::prepareMetrics(int maxWidth)
{
Q_D(ShowfotoItemViewDelegate);
QFontMetrics fm(d->fontReg);
d->oneRowRegRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
Qt::AlignTop | Qt::AlignHCenter,
QLatin1String("XXXXXXXXX"));
fm = QFontMetrics(d->fontCom);
d->oneRowComRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
Qt::AlignTop | Qt::AlignHCenter,
QLatin1String("XXXXXXXXX"));
fm = QFontMetrics(d->fontXtra);
d->oneRowXtraRect = fm.boundingRect(0, 0, maxWidth, 0xFFFFFFFF,
Qt::AlignTop | Qt::AlignHCenter,
QLatin1String("XXXXXXXXX"));
}
void ShowfotoItemViewDelegate::prepareBackground()
{
Q_D(ShowfotoItemViewDelegate);
if (!d->rect.isValid())
{
d->regPixmap = QPixmap();
d->selPixmap = QPixmap();
}
else
{
d->regPixmap = QPixmap(d->rect.width(), d->rect.height());
d->regPixmap.fill(qApp->palette().color(QPalette::Base));
QPainter p1(&d->regPixmap);
p1.setPen(qApp->palette().color(QPalette::Midlight));
p1.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
d->selPixmap = QPixmap(d->rect.width(), d->rect.height());
d->selPixmap.fill(qApp->palette().color(QPalette::Highlight));
QPainter p2(&d->selPixmap);
p2.setPen(qApp->palette().color(QPalette::Midlight));
p2.drawRect(0, 0, d->rect.width()-1, d->rect.height()-1);
}
}
} // namespace ShowFoto
#include "moc_showfotoitemviewdelegate.cpp"
|