Debian Codegraph @

core/libs/album/engine/albumhistory.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2004-11-17
7 * Description : albums history manager.
8 *
9 * SPDX-FileCopyrightText: 2004 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: 2014 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "albumhistory.h"
18
19 // Qt includes
20
21 #include <QMutexLocker>
22 #include <QString>
23 #include <QWidget>
24 #include <QHash>
25
26 // Local includes
27
28 #include "digikam_debug.h"
29 #include "album.h"
30 #include "albummanager.h"
31
32 // NOTE: must be declared outside namespace.
33
34 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
35
36 inline size_t qHash(const QList<Digikam::Album*>& key)
37
38 #else
39
40 inline uint qHash(const QList<Digikam::Album*>& key)
41
42 #endif
43
44 {
45 if (key.isEmpty())
46 {
47 return 0;
48 }
49
50 const Digikam::Album* const temp = key.first();
51 quint64 myint = (unsigned long long)temp;
52
53 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
54
55 size_t value = ::qHash(myint);
56
57 #else
58
59 uint value = ::qHash(myint);
60
61 #endif
62
63 for (int it = 1 ; it < key.size() ; ++it)
64 {
65 const Digikam::Album* const al = key.at(it);
66 quint64 myint2 = (unsigned long long)al; // clazy:exclude=qt6-qhash-signature
67
68 value ^= ::qHash(myint2);
69 }
70
71 return value;
72 }
73
74 namespace Digikam
75 {
76
77 /**
78 * Stores an album along with the sidebar view, where the album
79 * is selected
80 */
81 class Q_DECL_HIDDEN HistoryItem
82 {
83 public:
84
85 HistoryItem() = default;
86
87 HistoryItem(const QList<Album*>& a, QWidget* const w)
88 : widget(w)
89 {
90 albums.append(a);
91 }
92
93 HistoryItem(const QList<Album*>& a, QWidget* const w,
94 const QHash<LabelsTreeView::Labels,
95 QList<int> >& selectedLabels)
96 : widget(w),
97 labels(selectedLabels)
98 {
99 albums.append(a);
100 }
101
102 bool operator==(const HistoryItem& item) const
103 {
104 if (widget != item.widget)
105 {
106 return false;
107 }
108
109 return (albums == item.albums);
110 }
111
112 public:
113
114 QList<Album*> albums;
115 QWidget* widget = nullptr;
116 QHash<LabelsTreeView::Labels, QList<int> > labels;
117 };
118
119 // ---------------------------------------------------------------------
120
121 class Q_DECL_HIDDEN HistoryPosition
122 {
123 public:
124
125 HistoryPosition() = default;
126
127 HistoryPosition(const ItemInfo& c, const QList<ItemInfo>& s)
128 : current(c),
129 select (s)
130 {
131 }
132
133 bool operator==(const HistoryPosition& item)
134 {
135 return ((current == item.current) && (select == item.select));
136 }
137
138 public:
139
140 ItemInfo current;
141 QList<ItemInfo> select;
142 };
143
144 // ---------------------------------------------------------------------
145
146 class Q_DECL_HIDDEN AlbumHistory::Private
147 {
148 public:
149
150 Private() = default;
151
152 void forward(unsigned int steps = 1);
153
154 public:
155
156 bool moving = false;
157 bool blockSelection = false;
158
159 QMutex mutex;
160
161 QList<HistoryItem> backwardStack;
162 QList<HistoryItem> forwardStack;
163 QHash<QList<Album*>, HistoryPosition> historyPos;
164 QHash<LabelsTreeView::Labels, QList<int> > neededLabels;
165 };
166
167 void AlbumHistory::Private::forward(unsigned int steps)
168 {
169 if (forwardStack.isEmpty() || ((int)steps > forwardStack.count()))
170 {
171 return;
172 }
173
174 while (steps)
175 {
176 backwardStack << forwardStack.takeFirst();
177 --steps;
178 }
179
180 moving = true;
181 }
182
183 AlbumHistory::AlbumHistory(QObject* const parent)
184 : QObject(parent),
185 d (new Private)
186 {
187 }
188
189 AlbumHistory::~AlbumHistory()
190 {
191 clearHistory();
192 delete d;
193 }
194
195 void AlbumHistory::clearHistory()
196 {
197 QMutexLocker locker(&d->mutex);
198
199 d->backwardStack.clear();
200 d->forwardStack.clear();
201 d->historyPos.clear();
202
203 d->moving = false;
204 }
205
206 void AlbumHistory::addAlbums(const QList<Album*>& albums, QWidget* const widget)
207 {
208 QMutexLocker locker(&d->mutex);
209
210 if (albums.isEmpty() || !widget || d->moving)
211 {
212 d->moving = false;
213
214 return;
215 }
216
217 // Same album as before in the history
218
219 if (!d->backwardStack.isEmpty() && (d->backwardStack.last().albums == albums))
220 {
221 d->backwardStack.last().widget = widget;
222
223 return;
224 }
225
226 d->backwardStack << HistoryItem(albums, widget);
227
228 // The forward stack has to be cleared, if backward stack was changed
229
230 d->forwardStack.clear();
231 }
232
233 /**
234 * @brief AlbumHistory::addAlbums
235 * A special overloaded function for handling AlbumHistory
236 * for the Labels tree-view
237 *
238 * @author Mohamed_Anwer
239 */
240 void AlbumHistory::addAlbums(const QList<Album*>& albums,
241 QWidget* const widget,
242 const QHash<LabelsTreeView::Labels, QList<int> >& selectedLabels)
243 {
244 QMutexLocker locker(&d->mutex);
245
246 if (albums.isEmpty() || !widget || d->moving)
247 {
248 d->moving = false;
249 return;
250 }
251
252 if (!d->backwardStack.isEmpty() && d->backwardStack.last().albums.first()->isUsedByLabelsTree())
253 {
254 d->backwardStack.last().widget = widget;
255 d->backwardStack.last().labels = selectedLabels;
256 return;
257 }
258
259 d->backwardStack << HistoryItem(albums, widget, selectedLabels);
260
261 // The forward stack has to be cleared, if backward stack was changed
262
263 d->forwardStack.clear();
264 }
265
266 void AlbumHistory::deleteAlbum(Album* const album)
267 {
268 QMutexLocker locker(&d->mutex);
269
270 if (!album || d->backwardStack.isEmpty())
271 {
272 return;
273 }
274
275 QList<Album*> albums;
276 albums << album;
277
278 // Search all HistoryItems, with album and delete them
279
280 QList<HistoryItem>::iterator it = d->backwardStack.begin();
281
282 while (it != d->backwardStack.end())
283 {
284 if (it->albums == albums)
285 {
286 it = d->backwardStack.erase(it);
287 }
288 else
289 {
290 ++it;
291 }
292 }
293
294 it = d->forwardStack.begin();
295
296 while (it != d->forwardStack.end())
297 {
298 if (it->albums == albums)
299 {
300 it = d->forwardStack.erase(it);
301 }
302 else
303 {
304 ++it;
305 }
306 }
307
308 if (d->backwardStack.isEmpty() && d->forwardStack.isEmpty())
309 {
310 return;
311 }
312
313 // If backwardStack is empty, then there is no current album.
314 // So make the first album of the forwardStack the current one.
315
316 if (d->backwardStack.isEmpty())
317 {
318 d->forward();
319 }
320
321 // After the album is deleted from the history it has to be ensured,
322 // that neighboring albums are different
323
324 QList<HistoryItem>::iterator lhs = d->backwardStack.begin();
325 QList<HistoryItem>::iterator rhs = lhs;
326 ++rhs;
327
328 while (rhs != d->backwardStack.end())
329 {
330 if (*lhs == *rhs)
331 {
332 rhs = d->backwardStack.erase(rhs);
333 }
334 else
335 {
336 ++lhs;
337 rhs = lhs;
338 ++rhs;
339 }
340 }
341
342 rhs = d->forwardStack.begin();
343
344 while (rhs != d->forwardStack.end())
345 {
346 if (*lhs == *rhs)
347 {
348 rhs = d->forwardStack.erase(rhs);
349 }
350 else
351 {
352 if (lhs == (d->backwardStack.isEmpty() ? d->backwardStack.end()
353 : --d->backwardStack.end()))
354 {
355 lhs = d->forwardStack.begin();
356 }
357 else
358 {
359 ++lhs;
360 rhs = lhs;
361 }
362
363 ++rhs;
364 }
365 }
366
367 if (d->backwardStack.isEmpty() && !d->forwardStack.isEmpty())
368 {
369 d->forward();
370 }
371 }
372
373 void AlbumHistory::getBackwardHistory(QStringList& list) const
374 {
375 QMutexLocker locker(&d->mutex);
376
377 if (d->backwardStack.isEmpty())
378 {
379 return;
380 }
381
382 QList<HistoryItem>::const_iterator it = d->backwardStack.constBegin();
383
384 for ( ; it != (d->backwardStack.isEmpty() ? d->backwardStack.constEnd()
385 : --d->backwardStack.constEnd())
386 ; ++it)
387 {
388 if (!(it->albums.isEmpty()))
389 {
390 QString name;
391
392 for (int iter = 0 ; iter < it->albums.size() ; ++iter)
393 {
394 name.append(it->albums.at(iter)->title());
395
396 if (iter+1 < it->albums.size())
397 {
398 name.append(QLatin1Char('/'));
399 }
400 }
401
402 list.prepend(name);
403 }
404 }
405 }
406
407 void AlbumHistory::getForwardHistory(QStringList& list) const
408 {
409 QMutexLocker locker(&d->mutex);
410
411 if (d->forwardStack.isEmpty())
412 {
413 return;
414 }
415
416 QList<HistoryItem>::const_iterator it;
417
418 for (it = d->forwardStack.constBegin() ; it != d->forwardStack.constEnd() ; ++it)
419 {
420 if (!(it->albums.isEmpty()))
421 {
422 QString name;
423
424 for (int iter = 0 ; iter < it->albums.size() ; ++iter)
425 {
426 name.append(it->albums.at(iter)->title());
427
428 if (iter+1 < it->albums.size())
429 {
430 name.append(QLatin1Char('/'));
431 }
432 }
433
434 list.append(name);
435 }
436 }
437 }
438
439 void AlbumHistory::back(QList<Album*>& album, QWidget** const widget, unsigned int steps)
440 {
441 QMutexLocker locker(&d->mutex);
442
443 *widget = nullptr;
444
445 if ((d->backwardStack.count() <= 1) || ((int)steps > d->backwardStack.count()))
446 {
447 return; // Only the current album available
448 }
449
450 while (steps)
451 {
452 d->forwardStack.prepend(d->backwardStack.takeLast());
453 --steps;
454 }
455
456 d->moving = true;
457
458 if (d->backwardStack.isEmpty())
459 {
460 return;
461 }
462
463 album.append(d->backwardStack.last().albums);
464 *widget = d->backwardStack.last().widget;
465 d->neededLabels = d->backwardStack.last().labels;
466 }
467
468 void AlbumHistory::forward(QList<Album*>& album, QWidget** const widget, unsigned int steps)
469 {
470 QMutexLocker locker(&d->mutex);
471
472 *widget = nullptr;
473
474 if (d->forwardStack.isEmpty() || ((int)steps > d->forwardStack.count()))
475 {
476 return;
477 }
478
479 d->forward(steps);
480
481 if (d->backwardStack.isEmpty())
482 {
483 return;
484 }
485
486 album.append(d->backwardStack.last().albums);
487 *widget = d->backwardStack.last().widget;
488 d->neededLabels = d->backwardStack.last().labels;
489 }
490
491 void AlbumHistory::getCurrentAlbum(Album** const album, QWidget** const widget) const
492 {
493 QMutexLocker locker(&d->mutex);
494
495 *album = nullptr;
496 *widget = nullptr;
497
498 if (d->backwardStack.isEmpty())
499 {
500 return;
501 }
502
503 if (!(d->backwardStack.last().albums.isEmpty()))
504 {
505 *album = d->backwardStack.last().albums.first();
506 }
507
508 *widget = d->backwardStack.last().widget;
509 }
510
511 bool AlbumHistory::isForwardEmpty() const
512 {
513 QMutexLocker locker(&d->mutex);
514
515 return d->forwardStack.isEmpty();
516 }
517
518 bool AlbumHistory::isBackwardEmpty() const
519 {
520 QMutexLocker locker(&d->mutex);
521
522 // the last album of the backwardStack is the currently shown
523 // album, and therefore not really a previous album
524
525 return ((d->backwardStack.count() <= 1) ? true : false);
526 }
527
528 QHash<LabelsTreeView::Labels, QList<int> > AlbumHistory::neededLabels()
529 {
530 return d->neededLabels;
531 }
532
533 void AlbumHistory::slotAlbumSelected()
534 {
535 QList<Album*> albumList = AlbumManager::instance()->currentAlbums();
536
537 if (d->historyPos.contains(albumList))
538 {
539 d->blockSelection = true;
540
541 Q_EMIT signalSetCurrent(d->historyPos[albumList].current.id());
542 }
543 }
544
545 void AlbumHistory::slotAlbumCurrentChanged()
546 {
547 QList<Album*> albumList = AlbumManager::instance()->currentAlbums();
548
549 if (!(albumList.isEmpty()) && d->historyPos.contains(albumList))
550 {
551 if (d->historyPos[albumList].select.size())
552 {
553 Q_EMIT signalSetSelectedInfos(d->historyPos[albumList].select);
554 }
555 }
556
557 d->blockSelection = false;
558 }
559
560 void AlbumHistory::slotCurrentChange(const ItemInfo& info)
561 {
562 QList<Album*> albumList = AlbumManager::instance()->currentAlbums();
563
564 if (albumList.isEmpty())
565 {
566 return;
567 }
568
569 d->historyPos[albumList].current = info;
570 }
571
572 void AlbumHistory::slotImageSelected(const ItemInfoList& selectedImages)
573 {
574 if (d->blockSelection)
575 {
576 return;
577 }
578
579 QList<Album*> albumList = AlbumManager::instance()->currentAlbums();
580
581 if (d->historyPos.contains(albumList))
582 {
583 d->historyPos[albumList].select = selectedImages;
584 }
585 }
586
587 void AlbumHistory::slotClearSelectPAlbum(const ItemInfo& imageInfo)
588 {
589 Album* const album = dynamic_cast<Album*>(AlbumManager::instance()->findPAlbum(imageInfo.albumId())); // cppcheck-suppress constVariablePointer
590 QList<Album*> albums;
591 albums << album;
592
593 if (d->historyPos.contains(albums))
594 {
595 d->historyPos[albums].select.clear();
596 }
597 }
598
599 void AlbumHistory::slotClearSelectTAlbum(int id)
600 {
601 Album* const album = dynamic_cast<Album*>(AlbumManager::instance()->findTAlbum(id)); // cppcheck-suppress constVariablePointer
602 QList<Album*> albums;
603 albums << album;
604
605 if (d->historyPos.contains(albums))
606 {
607 d->historyPos[albums].select.clear();
608 }
609 }
610
611 void AlbumHistory::slotAlbumDeleted(Album* album)
612 {
613 deleteAlbum(album);
614 QList<Album*> albums;
615 albums << album;
616
617 if (d->historyPos.contains(albums))
618 {
619 d->historyPos.remove(albums);
620 }
621 }
622
623 void AlbumHistory::slotAlbumsCleared()
624 {
625 clearHistory();
626 }
627
628 } // namespace Digikam
629
630 #include "moc_albumhistory.cpp"