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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-04-20
* Description : Qt model-view for items - category drawer
*
* SPDX-FileCopyrightText: 2009-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2011 by Andi Clemens <andi dot clemens at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "itemcategorydrawer.h"
// Qt includes
#include <QPainter>
#include <QApplication>
#include <QLocale>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "album.h"
#include "albummanager.h"
#include "itemalbummodel.h"
#include "itemcategorizedview.h"
#include "itemdelegate.h"
#include "itemfiltermodel.h"
#include "itemmodel.h"
#include "itemscanner.h"
#include "searchfolderview.h"
#include "facetagsiface.h"
namespace Digikam
{
class Q_DECL_HIDDEN ItemCategoryDrawer::Private
{
public:
Private() = default;
QFont font;
QRect rect;
QPixmap pixmap;
int lowerSpacing = 0;
ItemCategorizedView* view = nullptr;
};
ItemCategoryDrawer::ItemCategoryDrawer(ItemCategorizedView* const parent)
: DCategoryDrawer(nullptr),
d (new Private)
{
d->view = parent;
}
ItemCategoryDrawer::~ItemCategoryDrawer()
{
delete d;
}
int ItemCategoryDrawer::categoryHeight(const QModelIndex& /*index*/, const QStyleOption& /*option*/) const
{
return (d->rect.height() + d->lowerSpacing);
}
int ItemCategoryDrawer::maximumHeight() const
{
return (d->rect.height() + d->lowerSpacing);
}
void ItemCategoryDrawer::setLowerSpacing(int spacing)
{
d->lowerSpacing = spacing;
}
void ItemCategoryDrawer::setDefaultViewOptions(const QStyleOptionViewItem& option)
{
d->font = option.font;
if (option.rect.width() != d->rect.width())
{
updateRectsAndPixmaps(option.rect.width());
}
}
void ItemCategoryDrawer::invalidatePaintingCache()
{
if (d->rect.isNull())
{
return;
}
updateRectsAndPixmaps(d->rect.width());
}
void ItemCategoryDrawer::drawCategory(const QModelIndex& index, int /*sortRole*/,
const QStyleOption& option, QPainter* p) const
{
if (option.rect.width() != d->rect.width())
{
const_cast<ItemCategoryDrawer*>(this)->updateRectsAndPixmaps(option.rect.width());
}
p->save();
p->translate(option.rect.topLeft());
ItemSortSettings::CategorizationMode mode = (ItemSortSettings::CategorizationMode)index.data(ItemFilterModel::CategorizationModeRole).toInt();
p->drawPixmap(0, 0, d->pixmap);
QFont fontBold(d->font);
QFont fontNormal(d->font);
fontBold.setBold(true);
int fnSize = fontBold.pointSize();
if (fnSize > 0)
{
fontBold.setPointSize(fnSize+2);
}
else
{
fnSize = fontBold.pixelSize();
fontBold.setPixelSize(fnSize+2);
}
QString header;
QString subLine;
switch (mode)
{
case ItemSortSettings::NoCategories:
{
break;
}
case ItemSortSettings::OneCategory:
{
viewHeaderText(index, &header, &subLine);
break;
}
case ItemSortSettings::CategoryByAlbum:
{
textForAlbum(index, &header, &subLine);
break;
}
case ItemSortSettings::CategoryByFormat:
{
textForFormat(index, &header, &subLine);
break;
}
case ItemSortSettings::CategoryByMonth:
{
textForMonth(index, &header, &subLine);
break;
}
case ItemSortSettings::CategoryByFaces:
{
textForFace(index, &header, &subLine);
break;
}
}
p->setPen(qApp->palette().color(QPalette::HighlightedText));
p->setFont(fontBold);
QRect tr;
p->drawText(5, 5, d->rect.width(), d->rect.height(),
Qt::AlignLeft | Qt::AlignTop,
p->fontMetrics().elidedText(header, Qt::ElideRight, d->rect.width() - 10), &tr);
int y = tr.height() + 2;
p->setFont(fontNormal);
p->drawText(5, y, d->rect.width(), d->rect.height() - y,
Qt::AlignLeft | Qt::AlignVCenter,
p->fontMetrics().elidedText(subLine, Qt::ElideRight, d->rect.width() - 10));
p->restore();
}
void ItemCategoryDrawer::viewHeaderText(const QModelIndex& index, QString* header, QString* subLine) const
{
ItemModel* const sourceModel = index.data(ItemModel::ItemModelPointerRole).value<ItemModel*>();
if (!sourceModel)
{
return;
}
int count = d->view->categoryRange(index).height();
// Add here further model subclasses in use with ItemCategoryDrawer.
// Note you need a Q_OBJECT in the class's header for this to work.
ItemAlbumModel* const albumModel = qobject_cast<ItemAlbumModel*>(sourceModel);<--- Variable 'albumModel' can be declared as pointer to const
if (albumModel)
{
QList<Album*> albums = albumModel->currentAlbums();
Album* album = nullptr;
if (albums.isEmpty())
{
return;
}
album = albums.first();
if (!album)
{
return;
}
switch (album->type())
{
case Album::PHYSICAL:
{
textForPAlbum(static_cast<PAlbum*>(album), albumModel->isRecursingAlbums(), count, header, subLine);
break;
}
case Album::TAG:
{
textForTAlbum(static_cast<TAlbum*>(album), albumModel->isRecursingTags(), count, header, subLine);
break;
}
case Album::DATE:
{
textForDAlbum(static_cast<DAlbum*>(album), count, header, subLine);
break;
}
case Album::SEARCH:
{
textForSAlbum(static_cast<SAlbum*>(album), count, header, subLine);
break;
}
case Album::FACE:
default:
{
break;
}
}
}
}
void ItemCategoryDrawer::textForAlbum(const QModelIndex& index, QString* header, QString* subLine) const
{
int albumId = index.data(ItemFilterModel::CategoryAlbumIdRole).toInt();
PAlbum* const album = AlbumManager::instance()->findPAlbum(albumId);
int count = d->view->categoryRange(index).height();
textForPAlbum(album, false, count, header, subLine);
}
void ItemCategoryDrawer::textForFormat(const QModelIndex& index, QString* header, QString* subLine) const
{
QString format = index.data(ItemFilterModel::CategoryFormatRole).toString();
format = ItemScanner::formatToString(format);
*header = format;
int count = d->view->categoryRange(index).height();
*subLine = i18np("1 Item", "%1 Items", count);
}
void ItemCategoryDrawer::textForMonth(const QModelIndex& index, QString* header, QString* subLine) const
{
QDate date = index.data(ItemFilterModel::CategoryDateRole).toDate();
*header = date.isValid() ? date.toString(QLatin1String("MMM yyyy"))
: i18n("Invalid date");
int count = d->view->categoryRange(index).height();
*subLine = i18np("1 Item", "%1 Items", count);
}
void ItemCategoryDrawer::textForFace(const QModelIndex& index, QString* header, QString* subLine) const
{
*header = index.data(ItemFilterModel::CategoryFaceRole).toString();
int count = d->view->categoryRange(index).height();
*subLine = i18np("1 Item", "%1 Items", count);
}
void ItemCategoryDrawer::textForPAlbum(PAlbum* album, bool recursive, int count, QString* header, QString* subLine) const
{
Q_UNUSED(recursive);
if (!album)
{
return;
}
QDate date = album->date();
QLocale tmpLocale;
// day of month with two digits
QString day = tmpLocale.toString(date, QLatin1String("dd"));
// short form of the month
QString month = tmpLocale.toString(date, QLatin1String("MMM"));
// long form of the year
QString year = tmpLocale.toString(date, QLatin1String("yyyy"));
*subLine = i18ncp("%1: day of month with two digits, %2: short month name, %3: year",
"Album Date: %2 %3 %4 - 1 Item", "Album Date: %2 %3 %4 - %1 Items",
count, day, month, year);
if (!album->caption().isEmpty())
{
QString caption = album->caption();
*subLine += QLatin1String(" - ") + caption.replace(QLatin1Char('\n'), QLatin1Char(' '));
}
*header = album->prettyUrl();
}
void ItemCategoryDrawer::textForTAlbum(TAlbum* talbum, bool recursive, int count, QString* header,
QString* subLine) const
{
*header = talbum->title();
if (recursive && talbum->firstChild())
{
int n = 0;
for (AlbumIterator it(talbum) ; it.current() ; ++it)
{
n++;
}
QString firstPart = i18ncp("%2: a tag title; %3: number of subtags",
"%2 including 1 subtag", "%2 including %1 subtags",
n, talbum->tagPath(false));
*subLine = i18ncp("%2: the previous string (e.g. 'Foo including 7 subtags'); %1: number of items in tag",
"%2 - 1 Item", "%2 - %1 Items",
count, firstPart);
}
else
{
*subLine = i18np("%2 - 1 Item", "%2 - %1 Items", count, talbum->tagPath(false));
}
}
void ItemCategoryDrawer::textForSAlbum(SAlbum* salbum, int count, QString* header, QString* subLine) const
{
QString title = salbum->displayTitle();
*header = title;
if (salbum->isNormalSearch())
{
*subLine = i18np("Keyword Search - 1 Item", "Keyword Search - %1 Items", count);
}
else if (salbum->isAdvancedSearch())
{
*subLine = i18np("Advanced Search - 1 Item", "Advanced Search - %1 Items", count);
}
else
{
*subLine = i18np("1 Item", "%1 Items", count);
}
}
void ItemCategoryDrawer::textForDAlbum(DAlbum* album, int count, QString* header, QString* subLine) const
{
QString year = QLocale().toString(album->date(), QLatin1String("yyyy"));
if (album->range() == DAlbum::Month)
{
*header = i18nc("Month String - Year String", "%1 %2",
QLocale().standaloneMonthName(album->date().month(), QLocale::LongFormat), year);
}
else
{
*header = year;
}
*subLine = i18np("1 Item", "%1 Items", count);
}
void ItemCategoryDrawer::updateRectsAndPixmaps(int width)
{
d->rect = QRect(0, 0, 0, 0);
// Title --------------------------------------------------------
QFont fn(d->font);
int fnSize = fn.pointSize();
bool usePointSize;
if (fnSize > 0)
{
fn.setPointSize(fnSize+2);
usePointSize = true;
}
else
{
fnSize = fn.pixelSize();
fn.setPixelSize(fnSize+2);
usePointSize = false;
}
fn.setBold(true);
QFontMetrics fm(fn);
QRect tr = fm.boundingRect(0, 0, width,
0xFFFFFFFF, Qt::AlignLeft | Qt::AlignVCenter,
QLatin1String("XXX"));
d->rect.setHeight(tr.height());
if (usePointSize)
{
fn.setPointSize(d->font.pointSize());
}
else
{
fn.setPixelSize(d->font.pixelSize());
}
fn.setBold(false);
fm = QFontMetrics(fn);
tr = fm.boundingRect(0, 0, width,
0xFFFFFFFF, Qt::AlignLeft | Qt::AlignVCenter,
QLatin1String("XXX"));
d->rect.setHeight(d->rect.height() + tr.height() + 10);
d->rect.setWidth(width);
d->pixmap = QPixmap(d->rect.width(), d->rect.height());
d->pixmap.fill(qApp->palette().color(QPalette::Highlight));
}
} // namespace Digikam
#include "moc_itemcategorydrawer.cpp"
|