| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2005-05-06 |
| 7 |
* Description : Albums folder view. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2005-2006 by Joern Ahrens <joern dot ahrens at kdemail dot net> |
| 10 |
* SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com> |
| 12 |
* SPDX-FileCopyrightText: 2009-2011 by Johannes Wienke <languitar at semipol dot de> |
| 13 |
* |
| 14 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 15 |
* |
| 16 |
* ============================================================ */ |
| 17 |
|
| 18 |
#include "albumselectiontreeview.h" |
| 19 |
|
| 20 |
// Qt includes |
| 21 |
|
| 22 |
#include <QMessageBox> |
| 23 |
#include <QAction> |
| 24 |
#include <QEvent> |
| 25 |
#include <QIcon> |
| 26 |
|
| 27 |
// KDE includes |
| 28 |
|
| 29 |
#include <klocalizedstring.h> |
| 30 |
|
| 31 |
// Local includes |
| 32 |
|
| 33 |
#include "digikam_debug.h" |
| 34 |
#include "tagscache.h" |
| 35 |
#include "itemscanner.h" |
| 36 |
#include "albummanager.h" |
| 37 |
#include "facescansettings.h" |
| 38 |
#include "applicationsettings.h" |
| 39 |
#include "contextmenuhelper.h" |
| 40 |
#include "itemviewtooltip.h" |
| 41 |
#include "tooltipfiller.h" |
| 42 |
#include "thumbsgenerator.h" |
| 43 |
#include "newitemsfinder.h" |
| 44 |
#include "coredbaccess.h" |
| 45 |
#include "coredb.h" |
| 46 |
#include "facesengine.h" |
| 47 |
|
| 48 |
namespace Digikam |
| 49 |
{ |
| 50 |
|
| 51 |
class Q_DECL_HIDDEN AlbumViewToolTip: public ItemViewToolTip |
| 52 |
{ |
| 53 |
Q_OBJECT |
| 54 |
|
| 55 |
public: |
| 56 |
|
| 57 |
explicit AlbumViewToolTip(AlbumSelectionTreeView* const view) |
| 58 |
: ItemViewToolTip(view) |
| 59 |
{ |
| 60 |
} |
| 61 |
|
| 62 |
AlbumSelectionTreeView* view() const override |
| 63 |
{ |
| 64 |
return static_cast<AlbumSelectionTreeView*>(ItemViewToolTip::view()); |
| 65 |
} |
| 66 |
|
| 67 |
protected: |
| 68 |
|
| 69 |
QString tipContents() override |
| 70 |
{ |
| 71 |
PAlbum* const album = view()->palbumForIndex(currentIndex()); |
| 72 |
|
| 73 |
return (ToolTipFiller::albumTipContents(album, view()->checkableAlbumModel()->albumCount(album))); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
// ---------------------------------------------------------------------------------------------------- |
| 78 |
|
| 79 |
class Q_DECL_HIDDEN AlbumSelectionTreeView::Private |
| 80 |
{ |
| 81 |
public: |
| 82 |
|
| 83 |
Private() = default; |
| 84 |
|
| 85 |
public: |
| 86 |
|
| 87 |
bool enableToolTips = false; |
| 88 |
|
| 89 |
AlbumModificationHelper* albumModificationHelper = nullptr; |
| 90 |
AlbumViewToolTip* toolTip = nullptr; |
| 91 |
|
| 92 |
QAction* renameAction = nullptr; |
| 93 |
QAction* resetIconAction = nullptr; |
| 94 |
QAction* findDuplAction = nullptr; |
| 95 |
QAction* scanFacesAction = nullptr; |
| 96 |
QAction* repairHiddenAction = nullptr; |
| 97 |
QAction* rebuildThumbsAction = nullptr; |
| 98 |
QAction* expandSelected = nullptr; |
| 99 |
QAction* expandAllAlbums = nullptr; |
| 100 |
QAction* collapseSelected = nullptr; |
| 101 |
QAction* collapseAllAlbums = nullptr; |
| 102 |
|
| 103 |
class AlbumSelectionTreeViewContextMenuElement; |
| 104 |
AlbumSelectionTreeViewContextMenuElement* contextMenuElement = nullptr; |
| 105 |
}; |
| 106 |
|
| 107 |
// ---------------------------------------------------------------------------------------------------- |
| 108 |
|
| 109 |
class Q_DECL_HIDDEN AlbumSelectionTreeView::Private::AlbumSelectionTreeViewContextMenuElement |
| 110 |
: public AbstractAlbumTreeView::ContextMenuElement |
| 111 |
{ |
| 112 |
|
| 113 |
public: |
| 114 |
|
| 115 |
explicit AlbumSelectionTreeViewContextMenuElement(AlbumSelectionTreeView::Private* const dd) |
| 116 |
: d(dd) |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
void addActions(AbstractAlbumTreeView*, ContextMenuHelper& cmh, Album* a) override |
| 121 |
{ |
| 122 |
if (!a || a->isRoot()) |
| 123 |
{ |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
PAlbum* const album = dynamic_cast<PAlbum*>(a); |
| 128 |
|
| 129 |
if (!album) |
| 130 |
{ |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
cmh.addActionNewAlbum(d->albumModificationHelper, album); |
| 135 |
cmh.addAction(QLatin1String("album_openinfilemanager")); |
| 136 |
cmh.addSeparator(); |
| 137 |
|
| 138 |
// -------------------------------------------------------- |
| 139 |
|
| 140 |
cmh.addActionRenameAlbum(d->albumModificationHelper, album); |
| 141 |
cmh.addActionResetAlbumIcon(d->albumModificationHelper, album); |
| 142 |
cmh.addSeparator(); |
| 143 |
|
| 144 |
// -------------------------------------------------------- |
| 145 |
|
| 146 |
cmh.addAction(d->expandSelected); |
| 147 |
cmh.addAction(d->collapseSelected); |
| 148 |
cmh.addAction(d->expandAllAlbums); |
| 149 |
cmh.addAction(d->collapseAllAlbums); |
| 150 |
cmh.addSeparator(); |
| 151 |
|
| 152 |
// -------------------------------------------------------- |
| 153 |
|
| 154 |
cmh.addAction(d->findDuplAction); |
| 155 |
d->albumModificationHelper->bindAlbum(d->findDuplAction, album); |
| 156 |
cmh.addAction(d->scanFacesAction); |
| 157 |
d->albumModificationHelper->bindAlbum(d->scanFacesAction, album); |
| 158 |
cmh.addAction(d->rebuildThumbsAction); |
| 159 |
d->albumModificationHelper->bindAlbum(d->rebuildThumbsAction, album); |
| 160 |
cmh.addImportMenu(); |
| 161 |
cmh.addExportMenu(); |
| 162 |
cmh.addSeparator(); |
| 163 |
|
| 164 |
// -------------------------------------------------------- |
| 165 |
|
| 166 |
cmh.addAction(d->repairHiddenAction); |
| 167 |
d->albumModificationHelper->bindAlbum(d->repairHiddenAction, album); |
| 168 |
|
| 169 |
// -------------------------------------------------------- |
| 170 |
|
| 171 |
if (!album->isAlbumRoot()) |
| 172 |
{ |
| 173 |
cmh.addSeparator(); |
| 174 |
cmh.addActionDeleteAlbum(d->albumModificationHelper, album); |
| 175 |
cmh.addSeparator(); |
| 176 |
|
| 177 |
// -------------------------------------------------------- |
| 178 |
|
| 179 |
cmh.addActionEditAlbum(d->albumModificationHelper, album); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public: |
| 184 |
|
| 185 |
AlbumSelectionTreeView::Private* const d = nullptr; |
| 186 |
}; |
| 187 |
|
| 188 |
// ---------------------------------------------------------------------------------------------------- |
| 189 |
|
| 190 |
AlbumSelectionTreeView::AlbumSelectionTreeView(QWidget* const parent, |
| 191 |
AlbumModel* const model, |
| 192 |
AlbumModificationHelper* const albumModificationHelper) |
| 193 |
: AlbumTreeView(parent), |
| 194 |
d (new Private) |
| 195 |
{ |
| 196 |
setPAlbumModel(model); |
| 197 |
d->albumModificationHelper = albumModificationHelper; |
| 198 |
d->toolTip = new AlbumViewToolTip(this); |
| 199 |
|
| 200 |
d->expandSelected = new QAction(QIcon::fromTheme(QLatin1String("go-down")), |
| 201 |
i18n("Expand Selected Nodes"), this); |
| 202 |
|
| 203 |
d->expandAllAlbums = new QAction(QIcon::fromTheme(QLatin1String("expand-all")), |
| 204 |
i18n("Expand all Albums"), this); |
| 205 |
|
| 206 |
d->collapseSelected = new QAction(QIcon::fromTheme(QLatin1String("go-up")), |
| 207 |
i18n("Collapse Selected Recursively"), this); |
| 208 |
|
| 209 |
d->collapseAllAlbums = new QAction(QIcon::fromTheme(QLatin1String("collapse-all")), |
| 210 |
i18n("Collapse all Albums"), this); |
| 211 |
|
| 212 |
d->findDuplAction = new QAction(QIcon::fromTheme(QLatin1String("tools-wizard")), |
| 213 |
i18n("Find Duplicates..."), this); |
| 214 |
|
| 215 |
d->scanFacesAction = new QAction(QIcon::fromTheme(QLatin1String("list-add-user")), |
| 216 |
i18n("Scan for Faces"), this); |
| 217 |
|
| 218 |
d->repairHiddenAction = new QAction(QIcon::fromTheme(QLatin1String("edit-group")), |
| 219 |
i18n("Repair hidden Items"), this); |
| 220 |
|
| 221 |
d->rebuildThumbsAction = new QAction(QIcon::fromTheme(QLatin1String("view-refresh")), |
| 222 |
i18n("Refresh"), this); |
| 223 |
|
| 224 |
connect(d->expandSelected, SIGNAL(triggered()), |
| 225 |
this, SLOT(slotExpandNode())); |
| 226 |
|
| 227 |
connect(d->expandAllAlbums, SIGNAL(triggered()), |
| 228 |
this, SLOT(expandAll())); |
| 229 |
|
| 230 |
connect(d->collapseSelected, SIGNAL(triggered()), |
| 231 |
this, SLOT(slotCollapseNode())); |
| 232 |
|
| 233 |
connect(d->collapseAllAlbums, SIGNAL(triggered()), |
| 234 |
this, SLOT(slotCollapseAllNodes())); |
| 235 |
|
| 236 |
connect(d->findDuplAction, SIGNAL(triggered()), |
| 237 |
this, SLOT(slotFindDuplicates())); |
| 238 |
|
| 239 |
connect(d->scanFacesAction, SIGNAL(triggered()), |
| 240 |
this, SLOT(slotScanForFaces())); |
| 241 |
|
| 242 |
connect(d->repairHiddenAction, SIGNAL(triggered()), |
| 243 |
this, SLOT(slotRepairHiddenItems())); |
| 244 |
|
| 245 |
connect(d->rebuildThumbsAction, SIGNAL(triggered()), |
| 246 |
this, SLOT(slotRebuildThumbs())); |
| 247 |
|
| 248 |
setSortingEnabled(true); |
| 249 |
setSelectAlbumOnClick(true); |
| 250 |
setEnableContextMenu(true); |
| 251 |
setContextMenuTitle(i18n("Albums")); |
| 252 |
|
| 253 |
d->contextMenuElement = new Private::AlbumSelectionTreeViewContextMenuElement(d); |
| 254 |
addContextMenuElement(d->contextMenuElement); |
| 255 |
} |
| 256 |
|
| 257 |
AlbumSelectionTreeView::~AlbumSelectionTreeView() |
| 258 |
{ |
| 259 |
delete d->contextMenuElement; |
| 260 |
delete d; |
| 261 |
} |
| 262 |
|
| 263 |
void AlbumSelectionTreeView::setEnableToolTips(bool enable) |
| 264 |
{ |
| 265 |
d->enableToolTips = enable; |
| 266 |
} |
| 267 |
|
| 268 |
void AlbumSelectionTreeView::slotFindDuplicates() |
| 269 |
{ |
| 270 |
Q_EMIT signalFindDuplicates(QList<PAlbum*> |
| 271 |
{ |
| 272 |
d->albumModificationHelper->boundAlbum(sender()) |
| 273 |
} |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
void AlbumSelectionTreeView::slotScanForFaces() |
| 278 |
{ |
| 279 |
PAlbum* const album = d->albumModificationHelper->boundAlbum(sender()); |
| 280 |
|
| 281 |
if (!album) |
| 282 |
{ |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
AlbumList albums; |
| 287 |
albums << album; |
| 288 |
|
| 289 |
if (album->childCount()) |
| 290 |
{ |
| 291 |
if (QMessageBox::question(this, i18nc("@title:window", "Scan for Faces"), |
| 292 |
i18n("Should sub-albums be scanned too?")) |
| 293 |
== QMessageBox::Yes) |
| 294 |
{ |
| 295 |
albums << album->childAlbums(true); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
FaceScanSettings settings; |
| 300 |
settings.readFromConfig(); |
| 301 |
settings.task = FaceScanSettings::DetectAndRecognize; |
| 302 |
settings.alreadyScannedHandling = FaceScanSettings::Rescan; |
| 303 |
settings.albums = albums; |
| 304 |
settings.source = FaceScanSettings::ItemIconView; |
| 305 |
|
| 306 |
try |
| 307 |
{ |
| 308 |
FacesEngine* const tool = new FacesEngine(settings); |
| 309 |
tool->start(); |
| 310 |
} |
| 311 |
catch (...) |
| 312 |
{ |
| 313 |
// do nothing. Continue gracefully |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void AlbumSelectionTreeView::slotRepairHiddenItems() |
| 318 |
{ |
| 319 |
const PAlbum* const album = d->albumModificationHelper->boundAlbum(sender()); |
| 320 |
|
| 321 |
if (!album) |
| 322 |
{ |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
int originalVersionTag = TagsCache::instance()->getOrCreateInternalTag(InternalTagName::originalVersion()); |
| 327 |
const auto ids = CoreDbAccess().db()->getItemIDsInAlbum(album->id()); |
| 328 |
|
| 329 |
for (const qlonglong& id : ids) |
| 330 |
{ |
| 331 |
ItemInfo info(id); |
| 332 |
|
| 333 |
if (!info.isNull() && info.isGrouped()) |
| 334 |
{ |
| 335 |
if (info.groupImage().albumId() != album->id()) |
| 336 |
{ |
| 337 |
info.removeFromGroup(); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
if (!info.hasDerivedImages() && info.tagIds().contains(originalVersionTag)) |
| 342 |
{ |
| 343 |
ItemScanner::resolveImageHistory(info.id()); |
| 344 |
ItemScanner::tagItemHistoryGraph(info.id()); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
void AlbumSelectionTreeView::slotRebuildThumbs() |
| 350 |
{ |
| 351 |
PAlbum* const album = d->albumModificationHelper->boundAlbum(sender()); |
| 352 |
|
| 353 |
if (!album) |
| 354 |
{ |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
ThumbsGenerator* const tool = new ThumbsGenerator(true, album->id()); |
| 359 |
tool->start(); |
| 360 |
|
| 361 |
// if physical album, schedule a collection scan of current album's path |
| 362 |
|
| 363 |
if (album->type() == Album::PHYSICAL) |
| 364 |
{ |
| 365 |
NewItemsFinder* const tool2 = new NewItemsFinder(NewItemsFinder::ScheduleCollectionScan, |
| 366 |
QStringList() << static_cast<PAlbum*>(album)->folderPath()); |
| 367 |
tool2->start(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
bool AlbumSelectionTreeView::viewportEvent(QEvent* event) |
| 372 |
{ |
| 373 |
// let the base class handle the event if it is not a tool tip request |
| 374 |
|
| 375 |
if (event->type() != QEvent::ToolTip) |
| 376 |
{ |
| 377 |
return AlbumTreeView::viewportEvent(event); |
| 378 |
} |
| 379 |
|
| 380 |
// only show tool tips if requested |
| 381 |
|
| 382 |
if (!d->enableToolTips) |
| 383 |
{ |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
// check that we got a correct event |
| 388 |
|
| 389 |
QHelpEvent* const helpEvent = dynamic_cast<QHelpEvent*> (event); |
| 390 |
|
| 391 |
if (!helpEvent) |
| 392 |
{ |
| 393 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Unable to determine the correct type of the event. " |
| 394 |
<< "This should not happen."; |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
// find the item this tool tip belongs to |
| 399 |
|
| 400 |
QModelIndex index = indexAt(helpEvent->pos()); |
| 401 |
|
| 402 |
if (!index.isValid()) |
| 403 |
{ |
| 404 |
return true; |
| 405 |
} |
| 406 |
|
| 407 |
const PAlbum* const album = palbumForIndex(index); |
| 408 |
|
| 409 |
if (!album || album->isRoot() || album->isAlbumRoot()) |
| 410 |
{ |
| 411 |
// there was no album so we really don't want to show a tooltip. |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
QRect itemRect = visualRect(index); |
| 417 |
|
| 418 |
if (!itemRect.contains(helpEvent->pos())) |
| 419 |
{ |
| 420 |
return true; |
| 421 |
} |
| 422 |
|
| 423 |
QStyleOptionViewItem option; |
| 424 |
|
| 425 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 426 |
|
| 427 |
initViewItemOption(&option); |
| 428 |
|
| 429 |
#else |
| 430 |
|
| 431 |
option = viewOptions(); |
| 432 |
|
| 433 |
#endif |
| 434 |
|
| 435 |
option.rect = itemRect; |
| 436 |
|
| 437 |
// visualRect can be larger than viewport, intersect with viewport rect |
| 438 |
|
| 439 |
option.rect &= viewport()->rect(); |
| 440 |
option.state |= ((index == currentIndex()) ? QStyle::State_HasFocus : QStyle::State_None); |
| 441 |
d->toolTip->show(option, index); |
| 442 |
|
| 443 |
return true; |
| 444 |
} |
| 445 |
|
| 446 |
} // namespace Digikam |
| 447 |
|
| 448 |
#include "albumselectiontreeview.moc" |
| 449 |
|
| 450 |
#include "moc_albumselectiontreeview.cpp" |