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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2005-05-06
* Description : Albums folder view.
*
* SPDX-FileCopyrightText: 2005-2006 by Joern Ahrens <joern dot ahrens at kdemail dot net>
* SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
* SPDX-FileCopyrightText: 2009-2011 by Johannes Wienke <languitar at semipol dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "albumselectiontreeview.h"
// Qt includes
#include <QMessageBox>
#include <QAction>
#include <QEvent>
#include <QIcon>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "tagscache.h"
#include "itemscanner.h"
#include "albummanager.h"
#include "facescansettings.h"
#include "applicationsettings.h"
#include "contextmenuhelper.h"
#include "itemviewtooltip.h"
#include "tooltipfiller.h"
#include "thumbsgenerator.h"
#include "newitemsfinder.h"
#include "facesdetector.h"
#include "coredbaccess.h"
#include "coredb.h"
namespace Digikam
{
class Q_DECL_HIDDEN AlbumViewToolTip: public ItemViewToolTip
{
Q_OBJECT
public:
explicit AlbumViewToolTip(AlbumSelectionTreeView* const view)
: ItemViewToolTip(view)
{
}
AlbumSelectionTreeView* view() const<--- Derived function 'AlbumViewToolTip::view'
{
return static_cast<AlbumSelectionTreeView*>(ItemViewToolTip::view());
}
protected:
QString tipContents() override
{
PAlbum* const album = view()->albumForIndex(currentIndex());
return (ToolTipFiller::albumTipContents(album, view()->albumModel()->albumCount(album)));
}
};
// ----------------------------------------------------------------------------------------------------
class Q_DECL_HIDDEN AlbumSelectionTreeView::Private
{
public:
Private() = default;
bool enableToolTips = false;
AlbumModificationHelper* albumModificationHelper = nullptr;
AlbumViewToolTip* toolTip = nullptr;
QAction* renameAction = nullptr;
QAction* resetIconAction = nullptr;
QAction* findDuplAction = nullptr;
QAction* scanFacesAction = nullptr;
QAction* repairHiddenAction = nullptr;
QAction* rebuildThumbsAction = nullptr;
QAction* expandSelected = nullptr;
QAction* expandAllAlbums = nullptr;
QAction* collapseSelected = nullptr;
QAction* collapseAllAlbums = nullptr;
class AlbumSelectionTreeViewContextMenuElement;
AlbumSelectionTreeViewContextMenuElement* contextMenuElement = nullptr;
};
// ----------------------------------------------------------------------------------------------------
class Q_DECL_HIDDEN AlbumSelectionTreeView::Private::AlbumSelectionTreeViewContextMenuElement
: public AbstractAlbumTreeView::ContextMenuElement
{
public:
explicit AlbumSelectionTreeViewContextMenuElement(AlbumSelectionTreeView::Private* const dd)
: d(dd)
{
}
void addActions(AbstractAlbumTreeView*, ContextMenuHelper& cmh, Album* a) override
{
if (!a || a->isRoot())
{
return;
}
PAlbum* const album = dynamic_cast<PAlbum*>(a);
if (!album)
{
return;
}
cmh.addActionNewAlbum(d->albumModificationHelper, album);
cmh.addAction(QLatin1String("album_openinfilemanager"));
cmh.addSeparator();
// --------------------------------------------------------
cmh.addActionRenameAlbum(d->albumModificationHelper, album);
cmh.addActionResetAlbumIcon(d->albumModificationHelper, album);
cmh.addSeparator();
// --------------------------------------------------------
cmh.addAction(d->expandSelected);
cmh.addAction(d->collapseSelected);
cmh.addAction(d->expandAllAlbums);
cmh.addAction(d->collapseAllAlbums);
cmh.addSeparator();
// --------------------------------------------------------
cmh.addAction(d->findDuplAction);
d->albumModificationHelper->bindAlbum(d->findDuplAction, album);
cmh.addAction(d->scanFacesAction);
d->albumModificationHelper->bindAlbum(d->scanFacesAction, album);
cmh.addAction(d->rebuildThumbsAction);
d->albumModificationHelper->bindAlbum(d->rebuildThumbsAction, album);
cmh.addImportMenu();
cmh.addExportMenu();
cmh.addSeparator();
// --------------------------------------------------------
cmh.addAction(d->repairHiddenAction);
d->albumModificationHelper->bindAlbum(d->repairHiddenAction, album);
// --------------------------------------------------------
if (!album->isAlbumRoot())
{
cmh.addSeparator();
cmh.addActionDeleteAlbum(d->albumModificationHelper, album);
cmh.addSeparator();
// --------------------------------------------------------
cmh.addActionEditAlbum(d->albumModificationHelper, album);
}
}
public:
AlbumSelectionTreeView::Private* const d = nullptr;
};
// ----------------------------------------------------------------------------------------------------
AlbumSelectionTreeView::AlbumSelectionTreeView(QWidget* const parent,
AlbumModel* const model,
AlbumModificationHelper* const albumModificationHelper)
: AlbumTreeView(parent),
d (new Private)
{
setAlbumModel(model);
d->albumModificationHelper = albumModificationHelper;
d->toolTip = new AlbumViewToolTip(this);
d->expandSelected = new QAction(QIcon::fromTheme(QLatin1String("go-down")),
i18n("Expand Selected Nodes"), this);
d->expandAllAlbums = new QAction(QIcon::fromTheme(QLatin1String("expand-all")),
i18n("Expand all Albums"), this);
d->collapseSelected = new QAction(QIcon::fromTheme(QLatin1String("go-up")),
i18n("Collapse Selected Recursively"), this);
d->collapseAllAlbums = new QAction(QIcon::fromTheme(QLatin1String("collapse-all")),
i18n("Collapse all Albums"), this);
d->findDuplAction = new QAction(QIcon::fromTheme(QLatin1String("tools-wizard")),
i18n("Find Duplicates..."), this);
d->scanFacesAction = new QAction(QIcon::fromTheme(QLatin1String("list-add-user")),
i18n("Scan for Faces"), this);
d->repairHiddenAction = new QAction(QIcon::fromTheme(QLatin1String("edit-group")),
i18n("Repair hidden Items"), this);
d->rebuildThumbsAction = new QAction(QIcon::fromTheme(QLatin1String("view-refresh")),
i18n("Refresh"), this);
connect(d->expandSelected, SIGNAL(triggered()),
this, SLOT(slotExpandNode()));
connect(d->expandAllAlbums, SIGNAL(triggered()),
this, SLOT(expandAll()));
connect(d->collapseSelected, SIGNAL(triggered()),
this, SLOT(slotCollapseNode()));
connect(d->collapseAllAlbums, SIGNAL(triggered()),
this, SLOT(slotCollapseAllNodes()));
connect(d->findDuplAction, SIGNAL(triggered()),
this, SLOT(slotFindDuplicates()));
connect(d->scanFacesAction, SIGNAL(triggered()),
this, SLOT(slotScanForFaces()));
connect(d->repairHiddenAction, SIGNAL(triggered()),
this, SLOT(slotRepairHiddenItems()));
connect(d->rebuildThumbsAction, SIGNAL(triggered()),
this, SLOT(slotRebuildThumbs()));
setSortingEnabled(true);
setSelectAlbumOnClick(true);
setEnableContextMenu(true);
setContextMenuTitle(i18n("Albums"));
d->contextMenuElement = new Private::AlbumSelectionTreeViewContextMenuElement(d);
addContextMenuElement(d->contextMenuElement);
}
AlbumSelectionTreeView::~AlbumSelectionTreeView()
{
delete d->contextMenuElement;
delete d;
}
void AlbumSelectionTreeView::setEnableToolTips(bool enable)
{
d->enableToolTips = enable;
}
void AlbumSelectionTreeView::slotFindDuplicates()
{
Q_EMIT signalFindDuplicates(QList<PAlbum*>
{
d->albumModificationHelper->boundAlbum(sender())
}
);
}
void AlbumSelectionTreeView::slotScanForFaces()
{
PAlbum* const album = d->albumModificationHelper->boundAlbum(sender());
if (!album)
{
return;
}
AlbumList albums;
albums << album;
if (album->childCount())
{
if (QMessageBox::question(this, i18nc("@title:window", "Scan for Faces"),
i18n("Should sub-albums be scanned too?"))
== QMessageBox::Yes)
{
albums << album->childAlbums(true);
}
}
FaceScanSettings settings;
settings.accuracy = ApplicationSettings::instance()->getFaceDetectionAccuracy();
settings.useYoloV3 = ApplicationSettings::instance()->getFaceDetectionYoloV3();
settings.task = FaceScanSettings::DetectAndRecognize;
settings.alreadyScannedHandling = FaceScanSettings::Rescan;
settings.albums = albums;
FacesDetector* const tool = new FacesDetector(settings);
tool->start();
}
void AlbumSelectionTreeView::slotRepairHiddenItems()
{
PAlbum* const album = d->albumModificationHelper->boundAlbum(sender());
if (!album)
{
return;
}
int originalVersionTag = TagsCache::instance()->getOrCreateInternalTag(InternalTagName::originalVersion());
Q_FOREACH (const qlonglong& id, CoreDbAccess().db()->getItemIDsInAlbum(album->id()))
{
ItemInfo info(id);
if (!info.isNull() && info.isGrouped())
{
if (info.groupImage().albumId() != album->id())
{
info.removeFromGroup();
}
}
if (!info.hasDerivedImages() && info.tagIds().contains(originalVersionTag))
{
ItemScanner::resolveImageHistory(info.id());
ItemScanner::tagItemHistoryGraph(info.id());
}
}
}
void AlbumSelectionTreeView::slotRebuildThumbs()
{
PAlbum* const album = d->albumModificationHelper->boundAlbum(sender());
if (!album)
{
return;
}
ThumbsGenerator* const tool = new ThumbsGenerator(true, album->id());
tool->start();
// if physical album, schedule a collection scan of current album's path
if (album->type() == Album::PHYSICAL)
{
NewItemsFinder* const tool2 = new NewItemsFinder(NewItemsFinder::ScheduleCollectionScan,
QStringList() << static_cast<PAlbum*>(album)->folderPath());
tool2->start();
}
}
bool AlbumSelectionTreeView::viewportEvent(QEvent* event)
{
// let the base class handle the event if it is not a tool tip request
if (event->type() != QEvent::ToolTip)
{
return AlbumTreeView::viewportEvent(event);
}
// only show tool tips if requested
if (!d->enableToolTips)
{
return false;
}
// check that we got a correct event
QHelpEvent* const helpEvent = dynamic_cast<QHelpEvent*> (event);
if (!helpEvent)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "Unable to determine the correct type of the event. "
<< "This should not happen.";
return false;
}
// find the item this tool tip belongs to
QModelIndex index = indexAt(helpEvent->pos());
if (!index.isValid())
{
return true;
}
PAlbum* const album = albumForIndex(index);
if (!album || album->isRoot() || album->isAlbumRoot())
{
// there was no album so we really don't want to show a tooltip.
return true;
}
QRect itemRect = visualRect(index);
if (!itemRect.contains(helpEvent->pos()))
{
return true;
}
QStyleOptionViewItem option;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
initViewItemOption(&option);
#else
option = viewOptions();
#endif
option.rect = itemRect;
// visualRect can be larger than viewport, intersect with viewport rect
option.rect &= viewport()->rect();
option.state |= ((index == currentIndex()) ? QStyle::State_HasFocus : QStyle::State_None);
d->toolTip->show(option, index);
return true;
}
} // namespace Digikam
#include "albumselectiontreeview.moc"
#include "moc_albumselectiontreeview.cpp"
|