Debian Codegraph @

core/libs/album/treeview/albumlabelssearchhandler.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2014-05-17
7 * Description : Album Labels Search Tree View.
8 *
9 * SPDX-FileCopyrightText: 2014-2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
10 * SPDX-FileCopyrightText: 2014-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "albumlabelssearchhandler.h"
17
18 // QT includes
19
20 #include <QApplication>
21 #include <QTreeWidget>
22 #include <QPainter>
23 #include <QUrl>
24
25 // KDE includes
26
27 #include <kconfiggroup.h>
28 #include <klocalizedstring.h>
29
30 // Local includes
31
32 #include "digikam_debug.h"
33 #include "digikam_globals.h"
34 #include "coredbsearchxml.h"
35 #include "searchtabheader.h"
36 #include "albummanager.h"
37 #include "albumtreeview.h"
38 #include "coredbconstants.h"
39 #include "itemlister.h"
40 #include "statesavingobject.h"
41 #include "coredbaccess.h"
42 #include "coredb.h"
43 #include "colorlabelfilter.h"
44 #include "picklabelfilter.h"
45 #include "tagscache.h"
46 #include "applicationsettings.h"
47 #include "dnotificationwrapper.h"
48 #include "digikamapp.h"
49 #include "ratingwidget.h"
50 #include "dbjobsmanager.h"
51
52 namespace Digikam
53 {
54
55 class Q_DECL_HIDDEN AlbumLabelsSearchHandler::Private
56 {
57 public:
58
59 Private() = default;
60
61 LabelsTreeView* treeWidget = nullptr;
62 SearchesDBJobsThread* dbJobThread = nullptr;
63 bool restoringSelectionFromHistory = false;
64 bool currentXmlIsEmpty = false;
65 QString oldXml;
66 Album* albumForSelectedItems = nullptr;
67 QString generatedAlbumName;
68 QList<QUrl> urlListForSelectedAlbum;
69 };
70
71 AlbumLabelsSearchHandler::AlbumLabelsSearchHandler(LabelsTreeView* const treeWidget)
72 : d(new Private)
73 {
74 d->treeWidget = treeWidget;
75
76 if (!d->treeWidget->isCheckable())
77 {
78 connect(d->treeWidget, SIGNAL(itemSelectionChanged()),
79 this, SLOT(slotSelectionChanged()));
80
81 connect(d->treeWidget, SIGNAL(signalSetCurrentAlbum()),
82 this, SLOT(slotSetCurrentAlbum()));
83 }
84 else
85 {
86 connect(d->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
87 this, SLOT(slotCheckStateChanged()));
88 }
89 }
90
91 AlbumLabelsSearchHandler::~AlbumLabelsSearchHandler()
92 {
93 delete d;
94 }
95
96 Album *AlbumLabelsSearchHandler::albumForSelectedItems() const
97 {
98 return d->albumForSelectedItems;
99 }
100
101 QList<QUrl> AlbumLabelsSearchHandler::imagesUrls() const
102 {
103 return d->urlListForSelectedAlbum;
104 }
105
106 QString AlbumLabelsSearchHandler::generatedName() const
107 {
108 return d->generatedAlbumName;
109 }
110
111 void AlbumLabelsSearchHandler::restoreSelectionFromHistory(const QHash<LabelsTreeView::Labels,
112 QList<int> >& neededLabels)
113 {
114 d->restoringSelectionFromHistory = true;
115 d->treeWidget->restoreSelectionFromHistory(neededLabels);
116 d->restoringSelectionFromHistory = false;
117 slotSelectionChanged();
118 }
119
120 bool AlbumLabelsSearchHandler::isRestoringSelectionFromHistory() const
121 {
122 return d->restoringSelectionFromHistory;
123 }
124
125 QString AlbumLabelsSearchHandler::createXMLForCurrentSelection(const QHash<LabelsTreeView::Labels,
126 QList<int> >& selectedLabels)
127 {
128 SearchXmlWriter writer;
129 writer.setFieldOperator(SearchXml::standardFieldOperator());
130 QList<int> ratings;
131 QList<int> colorsAndPicks;
132 const auto rates = selectedLabels[LabelsTreeView::Ratings];
133
134 for (int rate : rates)
135 {
136 if (rate == RatingMin)
137 {
138 ratings << NoRating;
139 }
140
141 ratings << rate;
142 }
143
144 const auto cols = selectedLabels[LabelsTreeView::Colors];
145
146 for (int color : cols)
147 {
148 colorsAndPicks << TagsCache::instance()->tagForColorLabel(color);
149 }
150
151 const auto picks = selectedLabels[LabelsTreeView::Picks];
152
153 for (int pick : std::as_const(picks))
154 {
155 colorsAndPicks << TagsCache::instance()->tagForPickLabel(pick);
156 }
157
158 d->currentXmlIsEmpty = ((ratings.isEmpty() && colorsAndPicks.isEmpty()) ? true : false);
159
160 int noColor = TagsCache::instance()->tagForColorLabel(NoColorLabel);
161 int noPick = TagsCache::instance()->tagForPickLabel(NoPickLabel);
162 QList<int> allColors = TagsCache::instance()->colorLabelTags().toList();
163 QList<int> allPicks = TagsCache::instance()->pickLabelTags().toList();
164
165 if (!ratings.isEmpty())
166 {
167 for (int rate : std::as_const(ratings))
168 {
169 writer.writeGroup();
170 writer.writeField(QLatin1String("rating"), SearchXml::Equal);
171 writer.writeValue(rate);
172 writer.finishField();
173 writer.finishGroup();
174 }
175 }
176
177 if (!colorsAndPicks.isEmpty())
178 {
179 writer.writeGroup();
180 writer.writeField(QLatin1String("tagid"), SearchXml::InTree);
181 writer.writeValue(colorsAndPicks);
182 writer.finishField();
183 writer.finishGroup();
184
185 if (colorsAndPicks.contains(noColor))
186 {
187 writer.writeGroup();
188 writer.writeField(QLatin1String("tagid"), SearchXml::NotInTree);
189 writer.writeValue(allColors);
190 writer.finishField();
191 writer.finishGroup();
192 }
193
194 if (colorsAndPicks.contains(noPick))
195 {
196 writer.writeGroup();
197 writer.writeField(QLatin1String("tagid"), SearchXml::NotInTree);
198 writer.writeValue(allPicks);
199 writer.finishField();
200 writer.finishGroup();
201 }
202 }
203
204 if (ratings.isEmpty() && colorsAndPicks.isEmpty())
205 {
206 writer.writeGroup();
207 writer.finishGroup();
208 }
209
210 writer.finish();
211
212 generateAlbumNameForExporting(selectedLabels[LabelsTreeView::Ratings],
213 selectedLabels[LabelsTreeView::Colors],
214 selectedLabels[LabelsTreeView::Picks]);
215 return writer.xml();
216 }
217
218 SAlbum* AlbumLabelsSearchHandler::search(const QString& xml) const
219 {
220 SAlbum* album = nullptr;
221 int id = 0;
222
223 if (!d->treeWidget->isCheckable())
224 {
225 album = AlbumManager::instance()->findSAlbum(SAlbum::getTemporaryTitle(DatabaseSearch::AdvancedSearch));
226
227 if (album)
228 {
229 id = album->id();
230 CoreDbAccess().db()->updateSearch(id, DatabaseSearch::AdvancedSearch,
231 SAlbum::getTemporaryTitle(DatabaseSearch::AdvancedSearch), xml);
232 }
233 else
234 {
235 id = CoreDbAccess().db()->addSearch(DatabaseSearch::AdvancedSearch,
236 SAlbum::getTemporaryTitle(DatabaseSearch::AdvancedSearch), xml);
237 }
238
239 album = new SAlbum(getDefaultTitle(), id);
240 }
241 else
242 {
243 album = AlbumManager::instance()->findSAlbum(getDefaultTitle());
244
245 if (album)
246 {
247 id = album->id();
248 CoreDbAccess().db()->updateSearch(id, DatabaseSearch::AdvancedSearch,
249 getDefaultTitle(), xml);
250 }
251 else
252 {
253 id = CoreDbAccess().db()->addSearch(DatabaseSearch::AdvancedSearch,
254 getDefaultTitle(), xml);
255 }
256
257 album = new SAlbum(d->generatedAlbumName, id);
258 }
259
260 if (!album->isUsedByLabelsTree())
261 {
262 album->setUsedByLabelsTree(true);
263 }
264
265 return album;
266 }
267
268 void AlbumLabelsSearchHandler::generateAlbumNameForExporting(const QList<int>& ratings,
269 const QList<int>& colorsList,
270 const QList<int>& picksList)
271 {
272 QString name;
273 QString ratingsString;
274 QString picksString;
275 QString colorsString;
276
277 if (!ratings.isEmpty())
278 {
279 ratingsString += i18nc("@info: generate album name for exporting", "Rating: ");
280
281 QListIterator<int> it(ratings);
282
283 while (it.hasNext())
284 {
285 int rating = it.next();
286
287 if (rating == -1)
288 {
289 ratingsString += i18nc("@info: rating label", "No Rating");
290 }
291 else
292 {
293 ratingsString += QString::number(rating);
294 }
295
296 if (it.hasNext())
297 {
298 ratingsString += QLatin1String(", ");
299 }
300 }
301 }
302
303 if (!colorsList.isEmpty())
304 {
305 colorsString += i18nc("@info: generate album name for exporting", "Colors: ");
306
307 QListIterator<int> it(colorsList);
308
309 while (it.hasNext())
310 {
311 switch (it.next())
312 {
313 case NoColorLabel:
314 {
315 colorsString += i18nc("@info: color label name", "No Color");
316 break;
317 }
318
319 case RedLabel:
320 {
321 colorsString += i18nc("@info: color label name", "Red");
322 break;
323 }
324
325 case OrangeLabel:
326 {
327 colorsString += i18nc("@info: color label name", "Orange");
328 break;
329 }
330
331 case YellowLabel:
332 {
333 colorsString += i18nc("@info: color label name", "Yellow");
334 break;
335 }
336
337 case GreenLabel:
338 {
339 colorsString += i18nc("@info: color label name", "Green");
340 break;
341 }
342
343 case BlueLabel:
344 {
345 colorsString += i18nc("@info: color label name", "Blue");
346 break;
347 }
348
349 case MagentaLabel:
350 {
351 colorsString += i18nc("@info: color label name", "Magenta");
352 break;
353 }
354
355 case GrayLabel:
356 {
357 colorsString += i18nc("@info: color label name", "Gray");
358 break;
359 }
360
361 case BlackLabel:
362 {
363 colorsString += i18nc("@info: color label name", "Black");
364 break;
365 }
366
367 case WhiteLabel:
368 {
369 colorsString += i18nc("@info: color label name", "White");
370 break;
371 }
372
373 default:
374 {
375 break;
376 }
377 }
378
379 if (it.hasNext())
380 {
381 colorsString += QLatin1String(", ");
382 }
383 }
384 }
385
386 if (!picksList.isEmpty())
387 {
388 picksString += i18nc("@info: generate album name for exporting", "Picks: ");
389
390 QListIterator<int> it(picksList);
391
392 while (it.hasNext())
393 {
394 switch (it.next())
395 {
396 case NoPickLabel:
397 {
398 picksString += i18nc("@info: pick label name", "No Pick");
399 break;
400 }
401
402 case RejectedLabel:
403 {
404 picksString += i18nc("@info: pick label name", "Rejected");
405 break;
406 }
407
408 case PendingLabel:
409 {
410 picksString += i18nc("@info: pick label name", "Pending");
411 break;
412 }
413
414 case AcceptedLabel:
415 {
416 picksString += i18nc("@info: pick label name", "Accepted");
417 break;
418 }
419
420 default:
421 {
422 break;
423 }
424 }
425
426 if (it.hasNext())
427 {
428 picksString += QLatin1String(", ");
429 }
430 }
431 }
432
433 if (ratingsString.isEmpty() && picksString.isEmpty())
434 {
435 name = colorsString;
436 }
437 else if (ratingsString.isEmpty() && colorsString.isEmpty())
438 {
439 name = picksString;
440 }
441 else if (colorsString.isEmpty() && picksString.isEmpty())
442 {
443 name = ratingsString;
444 }
445 else if (ratingsString.isEmpty())
446 {
447 name = picksString + QLatin1String(" | ") + colorsString;
448 }
449 else if (picksString.isEmpty())
450 {
451 name = ratingsString + QLatin1String(" | ") + colorsString;
452 }
453 else if (colorsString.isEmpty())
454 {
455 name = ratingsString + QLatin1String(" | ") + picksString;
456 }
457 else
458 {
459 name = ratingsString + QLatin1String(" | ") + picksString + QLatin1String(" | ") + colorsString;
460 }
461
462 d->generatedAlbumName = name;
463 }
464
465 void AlbumLabelsSearchHandler::imagesUrlsForCurrentAlbum()
466 {
467 QList<int> searchIds = QList<int>() << d->albumForSelectedItems->id();
468 SearchesDBJobInfo jobInfo(std::move(searchIds));
469 jobInfo.setRecursive();
470
471 d->dbJobThread = DBJobsManager::instance()->startSearchesJobThread(jobInfo);
472
473 connect(d->dbJobThread, SIGNAL(signalFinished()),
474 this, SLOT(slotResult()));
475
476 connect(d->dbJobThread, SIGNAL(data(QList<ItemListerRecord>)),
477 this, SLOT(slotData(QList<ItemListerRecord>)));
478 }
479
480 QString AlbumLabelsSearchHandler::getDefaultTitle() const
481 {
482 if (d->treeWidget->isCheckable())
483 {
484 return i18nc("@info: search label default title", "Exported Labels");
485 }
486 else
487 {
488 return i18nc("@info: search label default title", "Labels Album");
489 }
490 }
491
492 void AlbumLabelsSearchHandler::slotSelectionChanged()
493 {
494 if (d->treeWidget->isLoadingState() || d->restoringSelectionFromHistory)
495 {
496 return;
497 }
498
499 QString xml = createXMLForCurrentSelection(d->treeWidget->selectedLabels());
500 SAlbum* const album = search(xml);
501
502 if (album)
503 {
504 AlbumManager::instance()->setCurrentAlbums(QList<Album*>() << album);
505 d->albumForSelectedItems = album;
506 d->oldXml = xml;
507 }
508 }
509
510 void AlbumLabelsSearchHandler::slotCheckStateChanged()
511 {
512 QString currentXml = createXMLForCurrentSelection(d->treeWidget->selectedLabels());
513
514 if (currentXml == d->oldXml)
515 {
516 return;
517 }
518
519 if (d->albumForSelectedItems)
520 {
521 Q_EMIT checkStateChanged(d->albumForSelectedItems, Qt::Unchecked);
522 }
523
524 SAlbum* const album = search(currentXml);
525
526 if (album)
527 {
528 if (!d->currentXmlIsEmpty)
529 {
530 d->albumForSelectedItems = album;
531 imagesUrlsForCurrentAlbum();
532 }
533 else
534 {
535 d->albumForSelectedItems = nullptr;
536 }
537
538 Q_EMIT checkStateChanged(album, Qt::Checked);
539 }
540
541 d->oldXml = currentXml;
542 }
543
544 void AlbumLabelsSearchHandler::slotSetCurrentAlbum()
545 {
546 slotSelectionChanged();
547 }
548
549 void AlbumLabelsSearchHandler::slotResult()
550 {
551 if (d->dbJobThread != sender())
552 {
553 return;
554 }
555
556 if (d->dbJobThread->hasErrors())
557 {
558 qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to list urls: " << d->dbJobThread->errorsList().first();
559
560 // Pop-up a message about the error.
561
562 DNotificationWrapper(QString(),
563 d->dbJobThread->errorsList().first(),
564 DigikamApp::instance(),
565 DigikamApp::instance()->windowTitle());
566 }
567 }
568
569 void AlbumLabelsSearchHandler::slotData(const QList<ItemListerRecord>& data)
570 {
571 if ((d->dbJobThread != sender()) || data.isEmpty())
572 {
573 return;
574 }
575
576 QList<QUrl> urlList;
577
578 for (const ItemListerRecord& record : std::as_const(data))
579 {
580 ItemInfo info(record);
581 urlList << info.fileUrl();
582 }
583
584 d->urlListForSelectedAlbum = urlList;
585 }
586
587 } // namespace Digikam
588
589 #include "moc_albumlabelssearchhandler.cpp"