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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-03-24
* Description : Qt Model for Albums - filter model
*
* SPDX-FileCopyrightText: 2008-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2009 by Johannes Wienke <languitar at semipol dot de>
* SPDX-FileCopyrightText: 2014-2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "albumfiltermodel_p.h"
namespace Digikam
{
AlbumFilterModel::AlbumFilterModel(QObject* const parent)
: QSortFilterProxyModel(parent),
m_parent (parent)
{
setSortRole(AbstractAlbumModel::AlbumSortRole);
setSortCaseSensitivity(Qt::CaseInsensitive);
// sorting may have changed when the string comparison is different
connect(ApplicationSettings::instance(), SIGNAL(setupChanged()),
this, SLOT(invalidate()));
// dynamicSortFilter does not work well for us: a dataChange may, because of our way of filtering,
// also affect parents and children of the changed index, which is not handled by QSortFilterProxyModel.
setDynamicSortFilter(false);
// Instead, we listen directly to AlbumManager's relevant change signals
connect(AlbumManager::instance(), SIGNAL(signalAlbumRenamed(Album*)),
this, SLOT(slotAlbumRenamed(Album*)));
connect(AlbumManager::instance(), SIGNAL(signalAlbumsUpdated(int)),
this, SLOT(slotAlbumsHaveBeenUpdated(int)));
}
void AlbumFilterModel::setFilterBehavior(FilterBehavior behavior)
{
if (behavior == m_filterBehavior)
{
return;
}
m_filterBehavior = behavior;
invalidateFilter();
}
void AlbumFilterModel::setSearchTextSettings(const SearchTextSettings& settings)
{
if (!sourceModel())
{
return;
}
// Don't use isFiltering here because it may be reimplemented
bool wasSearching = settingsFilter(m_settings);
bool willSearch = settingsFilter(settings);
Q_EMIT searchTextSettingsAboutToChange(wasSearching, willSearch);
m_settings = settings;
invalidateFilter();
Q_EMIT signalFilterChanged();
Q_EMIT searchTextSettingsChanged(wasSearching, willSearch);
if (sourceAlbumModel()->albumType() == Album::PHYSICAL)
{
// find out if this setting has some results or not
int validRows = 0;
// for every collection we got
for (int i = 0 ; i < rowCount(rootAlbumIndex()) ; ++i)
{
QModelIndex collectionIndex = index(i, 0, rootAlbumIndex());
// count the number of rows
validRows += rowCount(collectionIndex);
}
bool hasResult = (validRows > 0);
qCDebug(DIGIKAM_GENERAL_LOG) << "new search text settings:" << settings.text
<< ": hasResult =" << hasResult
<< ", validRows =" << validRows;
Q_EMIT hasSearchResult(hasResult);
}
else
{
QModelIndex head = rootAlbumIndex(); // either root, or invalid, thus toplevel
Q_EMIT hasSearchResult(rowCount(head));
}
if (settings.text.isEmpty())
{
sort(0, sortOrder());
}
}
bool AlbumFilterModel::settingsFilter(const SearchTextSettings& settings) const
{
return (!settings.text.isEmpty());
}
bool AlbumFilterModel::isFiltering() const
{
return (settingsFilter(m_settings));
}
void AlbumFilterModel::updateFilter()
{
if (isFiltering())
{
invalidateFilter();
}
}
SearchTextSettings AlbumFilterModel::searchTextSettings() const
{
return m_settings;
}
void AlbumFilterModel::setSourceAlbumModel(AbstractAlbumModel* const source)
{
if (m_chainedModel)
{
m_chainedModel->setSourceAlbumModel(source);
}
else
{
if (source != sourceModel())
{
setSourceModel(source);
}
}
}
void AlbumFilterModel::setSourceFilterModel(AlbumFilterModel* const source)
{
if (source)
{
AbstractAlbumModel* const model = sourceAlbumModel();
if (model)
{
source->setSourceAlbumModel(model);
}
}
if ((m_chainedModel != source) || (sourceModel() != source))
{
m_chainedModel = source;
setSourceModel(source);
}
}
void AlbumFilterModel::setSourceModel(QAbstractItemModel* const model)
{
// made it protected, only setSourceAlbumModel is public
QSortFilterProxyModel::setSourceModel(model);
}
AbstractAlbumModel* AlbumFilterModel::sourceAlbumModel() const
{
if (m_chainedModel)
{
return m_chainedModel->sourceAlbumModel();
}
return static_cast<AbstractAlbumModel*>(sourceModel());
}
AlbumFilterModel* AlbumFilterModel::sourceFilterModel() const
{
return m_chainedModel;
}
QModelIndex AlbumFilterModel::mapToSourceAlbumModel(const QModelIndex& index) const
{
if (!index.isValid())
{
return QModelIndex();
}
if (m_chainedModel)
{
return m_chainedModel->mapToSourceAlbumModel(mapToSource(index));
}
return mapToSource(index);
}
QModelIndex AlbumFilterModel::mapFromSourceAlbumModel(const QModelIndex& albummodel_index) const
{
if (!albummodel_index.isValid())
{
return QModelIndex();
}
if (m_chainedModel)
{
return mapFromSource(m_chainedModel->mapFromSourceAlbumModel(albummodel_index));
}
return mapFromSource(albummodel_index);
}
Album* AlbumFilterModel::albumForIndex(const QModelIndex& index) const
{
return AbstractAlbumModel::retrieveAlbum(index);
}
QModelIndex AlbumFilterModel::indexForAlbum(Album* album) const
{
AbstractAlbumModel* const model = sourceAlbumModel();<--- Variable 'model' can be declared as pointer to const
if (!model)
{
return QModelIndex();
}
return mapFromSourceAlbumModel(model->indexForAlbum(album));
}
QModelIndex AlbumFilterModel::rootAlbumIndex() const
{
AbstractAlbumModel* const model = sourceAlbumModel();<--- Variable 'model' can be declared as pointer to const
if (!model)
{
return QModelIndex();
}
return mapFromSourceAlbumModel(model->rootAlbumIndex());
}
QVariant AlbumFilterModel::dataForCurrentSortRole(Album* album) const
{
if (album)
{
if (album->type() == Album::PHYSICAL)
{
PAlbum* const a = static_cast<PAlbum*>(album);<--- Variable 'a' can be declared as pointer to const
ApplicationSettings::AlbumSortRole sortRole = ApplicationSettings::instance()->getAlbumSortRole();
switch (sortRole)
{
case ApplicationSettings::ByFolder:
{
return a->title();
}
case ApplicationSettings::ByDate:
{
return a->date();
}
default:
{
return a->category();
}
}
}
else if (album->type() == Album::TAG)
{
return static_cast<TAlbum*>(album)->title();
}
else if (album->type() == Album::DATE)
{
return static_cast<DAlbum*>(album)->date();
}
else if (album->type() == Album::SEARCH)
{
return static_cast<SAlbum*>(album)->title();
}
}
return QVariant();
}
bool AlbumFilterModel::matches(Album* album) const
{
// We want to work on the visual representation, so we use model data with AlbumTitleRole,
// not a direct Album method.
// We use direct source's index, not our index,
// because if the item is currently filtered out, we won't have an index for this album.
QModelIndex source_index = sourceAlbumModel()->indexForAlbum(album);
if (m_chainedModel)
{
source_index = m_chainedModel->mapFromSourceAlbumModel(source_index);
}
QString displayTitle = source_index.data(AbstractAlbumModel::AlbumTitleRole).toString();
return displayTitle.contains(m_settings.text, m_settings.caseSensitive);
}
AlbumFilterModel::MatchResult AlbumFilterModel::matchResult(const QModelIndex& index) const
{
return matchResult(albumForIndex(index));
}
AlbumFilterModel::MatchResult AlbumFilterModel::matchResult(Album* album) const
{
if (!album)
{
return NoMatch;
}
PAlbum* const palbum = dynamic_cast<PAlbum*>(album);<--- Variable 'palbum' can be declared as pointer to const
if (album->isRoot() || (palbum && palbum->isAlbumRoot()))
{
return SpecialMatch;
}
TAlbum* const talbum = dynamic_cast<TAlbum*>(album);<--- Variable 'talbum' can be declared as pointer to const
if (talbum && talbum->isInternalTag())
{
return NoMatch;
}
if (matches(album))
{
return DirectMatch;
}
if (m_filterBehavior == SimpleFiltering)
{
return NoMatch;
}
if (m_filterBehavior == FullFiltering)
{
// check if any of the parents match the search
Album* parent = album->parent();
PAlbum* const pparent = palbum ? static_cast<PAlbum*>(parent) : nullptr;<--- Variable 'pparent' can be declared as pointer to const
while (parent && !(parent->isRoot() || (pparent && pparent->isAlbumRoot())))
{
if (matches(parent))
{
return ParentMatch;
}
parent = parent->parent();
}
}
// check if any of the children match the search
AlbumIterator it(album);
while (it.current())
{
if (matches(*it))
{
return ChildMatch;
}
++it;
}
return NoMatch;
}
bool AlbumFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
Album* const album = AbstractAlbumModel::retrieveAlbum(index);
MatchResult result = matchResult(album);
return result;
}
bool AlbumFilterModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
Album* leftAlbum = albumForIndex(left);
Album* rightAlbum = albumForIndex(right);
if (!leftAlbum || !rightAlbum)
{
return QSortFilterProxyModel::lessThan(left, right);
}
if ((leftAlbum->type() == Album::TAG) && (rightAlbum->type() == Album::TAG))
{
if (
(leftAlbum->id() == FaceTags::unconfirmedPersonTagId()) !=
(rightAlbum->id() == FaceTags::unconfirmedPersonTagId())
)
{
// unconfirmed tag album go to the top, regardless of sort role
return (sortOrder() == Qt::AscendingOrder) ? (leftAlbum->id() == FaceTags::unconfirmedPersonTagId())
: (leftAlbum->id() != FaceTags::unconfirmedPersonTagId());
}
if (
(leftAlbum->id() == FaceTags::unknownPersonTagId()) !=
(rightAlbum->id() == FaceTags::unknownPersonTagId())
)
{
// unknown tag albums go to the top, regardless of sort role
return (sortOrder() == Qt::AscendingOrder) ? (leftAlbum->id() == FaceTags::unknownPersonTagId())
: (leftAlbum->id() != FaceTags::unknownPersonTagId());
}
// Verify this, to prevent auto-creation of Ignored Tag.
if (
(leftAlbum->id() == FaceTags::ignoredPersonTagId()) !=
(rightAlbum->id() == FaceTags::ignoredPersonTagId())
)
{
// ignored tag albums go to the top, regardless of sort role
return (sortOrder() == Qt::AscendingOrder) ? (leftAlbum->id() == FaceTags::ignoredPersonTagId())
: (leftAlbum->id() != FaceTags::ignoredPersonTagId());
}
/**
* Implementation to sort Tags that contain
* Unconfirmed Faces, according to the Unconfirmed
* Face Count.
*/
if (sourceAlbumModel() && sourceAlbumModel()->isFaceTagModel())
{
QHash<int, int> unconfirmedFaceCount = AlbumManager::instance()->getUnconfirmedFaceCount();
int leftFaceCount = unconfirmedFaceCount.value(leftAlbum->id(), 0);
int rightFaceCount = unconfirmedFaceCount.value(rightAlbum->id(), 0);
if ((leftFaceCount != 0) || (rightFaceCount != 0))
{
return (sortOrder() == Qt::AscendingOrder) ? (leftFaceCount > rightFaceCount)
: (leftFaceCount < rightFaceCount);
}
}
}
if (leftAlbum->isTrashAlbum() != rightAlbum->isTrashAlbum())
{
// trash albums go to the bottom, regardless of sort role
return (
(sortOrder() == Qt::AscendingOrder) ? !leftAlbum->isTrashAlbum()
: leftAlbum->isTrashAlbum()
);
}
QVariant valLeft = dataForCurrentSortRole(leftAlbum);
QVariant valRight = dataForCurrentSortRole(rightAlbum);
ApplicationSettings::AlbumSortRole role = ApplicationSettings::instance()->getAlbumSortRole();
if (
((role == ApplicationSettings::ByDate) || (role == ApplicationSettings::ByCategory)) &&
(valLeft == valRight)
)
{
return QSortFilterProxyModel::lessThan(left, right);
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if ((valLeft.typeId() == QMetaType::QString) && (valRight.typeId() == QMetaType::QString))
#else
if ((valLeft.type() == QVariant::String) && (valRight.type() == QVariant::String))
#endif
{
ItemSortCollator* const sorter = ItemSortCollator::instance();<--- Variable 'sorter' can be declared as pointer to const
bool natural = ApplicationSettings::instance()->isStringTypeNatural();
return (sorter->albumCompare(valLeft.toString(), valRight.toString(), sortCaseSensitivity(), natural) < 0);
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
else if ((valLeft.typeId() == QMetaType::QDate) && (valRight.typeId() == QMetaType::QDate))
#else
else if ((valLeft.type() == QVariant::Date) && (valRight.type() == QVariant::Date))
#endif
{
return (compareByOrder(valLeft.toDate(), valRight.toDate(), Qt::AscendingOrder) < 0);
}
return QSortFilterProxyModel::lessThan(left, right);
}
void AlbumFilterModel::slotAlbumRenamed(Album* album)
{
if (album)
{
slotAlbumsHaveBeenUpdated(album->type());
}
}
void AlbumFilterModel::slotAlbumsHaveBeenUpdated(int type)
{
if (sourceAlbumModel() && (sourceAlbumModel()->albumType() == type))
{
invalidate();
}
}
} // namespace Digikam
#include "moc_albumfiltermodel.cpp"
|