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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2004-09-07
* Description : a pop-up menu implementation to display a
* hierarchical view of digiKam tags.
*
* SPDX-FileCopyrightText: 2004 by Renchi Raju <renchi dot raju at gmail dot com>
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* Parts of the drawing code are inspired by from Trolltech ASA implementation.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "tagspopupmenu.h"
// Qt includes
#include <QWidgetAction>
#include <QPainter>
#include <QPixmap>
#include <QSet>
#include <QFont>
#include <QString>
#include <QStyle>
#include <QStyleOptionButton>
#include <QStyleOptionMenuItem>
#include <QStyleOptionViewItem>
#include <QApplication>
#include <QActionGroup>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "album.h"
#include "coredb.h"
#include "albummanager.h"
#include "albumthumbnailloader.h"
#include "tageditdlg.h"
#include "tagscache.h"
namespace Digikam
{
class Q_DECL_HIDDEN TagToggleAction : public QWidgetAction
{
Q_OBJECT
public:
TagToggleAction(const QString& text, QObject* const parent);
TagToggleAction(const QIcon& icon, const QString& text, QObject* const parent);
QWidget* createWidget(QWidget* parent) override;
void setSpecialChecked(bool checked);
bool isChecked() const;
void setCheckBoxHidden(bool hidden);
bool isCheckBoxHidden() const;
private:
bool m_checked = false;
bool m_checkBoxHidden = false;
};
// ------------------------------------------------------------------------
class Q_DECL_HIDDEN TagToggleMenuWidget : public QWidget
{
Q_OBJECT
public:
TagToggleMenuWidget(QMenu* const parent, TagToggleAction* const action);
protected:
QSize sizeHint() const override;
void paintEvent(QPaintEvent*) override;
private:
void initMenuStyleOption(QStyleOptionMenuItem* option) const;
void initViewStyleOption(QStyleOptionViewItem* option) const;
QSize menuItemSize(QStyleOptionMenuItem* opt) const;
QRect checkIndicatorSize(QStyleOption* option) const;
private:
QMenu* m_menu = nullptr;
TagToggleAction* m_action = nullptr;
};
// ------------------------------------------------------------------------
TagToggleMenuWidget::TagToggleMenuWidget(QMenu* const parent, TagToggleAction* const action)
: QWidget (parent),
m_menu (parent),
m_action(action)
{
setMouseTracking(style()->styleHint(QStyle::SH_Menu_MouseTracking, nullptr, this));
}
QSize TagToggleMenuWidget::sizeHint() const
{
// init style option for menu item
QStyleOptionMenuItem opt;
initMenuStyleOption(&opt);
// get the individual sizes
QSize menuSize = menuItemSize(&opt);
QRect checkRect = checkIndicatorSize(&opt);
const int margin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, this) + 1;
// return widget size
int width = margin + checkRect.width() + menuSize.width() + margin;
QSize size(width, qMax(checkRect.height(), menuSize.height()));
return size;
}
void TagToggleMenuWidget::paintEvent(QPaintEvent*)
{
// init style option for menu item
QStyleOptionMenuItem menuOpt;
initMenuStyleOption(&menuOpt);
// init style option for check indicator
QStyleOptionViewItem viewOpt;
initViewStyleOption(&viewOpt);
// get a suitable margin
const int margin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, this);
const int frameMargin = style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, nullptr, this);
// create painter
QPainter p(this);
/**
* the menu rect should not go beyond the parent menu in width
* move by margin and free room for menu frame
*/
if (menuOpt.direction == Qt::RightToLeft)
{
// right-to-left untested
viewOpt.rect.translate(-margin, 0);
menuOpt.rect.translate(-margin, 0);
menuOpt.menuRect.adjust(margin, 0, 0, 0);
}
else
{
viewOpt.rect.translate(margin, 0);
menuOpt.rect.translate(margin, 0);
menuOpt.menuRect.adjust(0, 0, -margin, 0);
}
// clear the background of the check indicator
QStyleOptionMenuItem clearOpt(menuOpt);
clearOpt.state = QStyle::State_None;
clearOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;
clearOpt.checkType = QStyleOptionMenuItem::NotCheckable;
clearOpt.rect = viewOpt.rect;
style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this);
// draw a check indicator like the one used in a treeview
QRect checkRect = checkIndicatorSize(&menuOpt);
viewOpt.rect = checkRect;
if (!m_action->isCheckBoxHidden())
{
style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &viewOpt, &p, this);
}
// move by size of check indicator
if (menuOpt.direction == Qt::RightToLeft)
{
menuOpt.rect.translate( - checkRect.width() - margin, 0);
menuOpt.rect.adjust( checkRect.width() + margin, 0, 0, 0);
}
else
{
menuOpt.rect.translate(checkRect.right() + margin, 0);
menuOpt.rect.adjust(0, 0, - checkRect.right() - margin, 0);
}
// draw a full menu item - icon, text and menu indicator
style()->drawControl(QStyle::CE_MenuItem, &menuOpt, &p, this);
// draw the frame on the right
if (frameMargin)
{
QRegion borderReg;
borderReg += QRect(width() - frameMargin, 0, frameMargin, height()); // right
p.setClipRegion(borderReg);
QStyleOptionFrame frame;
frame.rect = rect();
frame.palette = palette();
frame.state = QStyle::State_None;
frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth);
frame.midLineWidth = 0;
style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
}
}
void TagToggleMenuWidget::initMenuStyleOption(QStyleOptionMenuItem* option) const
{
// set basic option from widget properties
option->initFrom(this);
// set menu item state
option->state = QStyle::State_None;
option->state |= QStyle::State_Enabled;
if (m_menu->activeAction() == m_action) // if hovered etc.
{
option->state |= QStyle::State_Selected;
}
/*
if (mouseDown)
{
option->state |= QStyle::State_Sunken;
}
*/
// We have a special case here: menu items which are checked are not selectable,
// it is an "Assign Tags" menu. To signal this, we change the pallette.
// But only if there is no submenu...
if (m_action->isChecked() && !m_action->menu())
{
option->palette.setCurrentColorGroup(QPalette::Disabled);
option->state &= ~QStyle::State_Enabled;
}
// set options from m_action
option->font = m_action->font();
option->icon = m_action->icon();
option->text = m_action->text();
// we do the check mark ourselves
option->checked = false;
option->menuHasCheckableItems = false;
option->checkType = QStyleOptionMenuItem::NotCheckable;
// Don't forget the submenu indicator
if (m_action->menu())
{
option->menuItemType = QStyleOptionMenuItem::SubMenu;
}
else
{
option->menuItemType = QStyleOptionMenuItem::Normal;
}
// seems QMenu does it like this
option->maxIconWidth = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this);
option->rect = rect();
option->menuRect = parentWidget()->rect();
}
void TagToggleMenuWidget::initViewStyleOption(QStyleOptionViewItem* option) const
{
// set basic option from widget properties
option->initFrom(this);
// set check state
if (m_action->isChecked())
{
option->state |= QStyle::State_On;
}
else
{
option->state |= QStyle::State_Off;
}
}
QSize TagToggleMenuWidget::menuItemSize(QStyleOptionMenuItem* opt) const
{
QSize size;
QFontMetrics fm(fontMetrics());
size.setWidth(fm.horizontalAdvance(m_action->text()));
size.setHeight(fm.height());
if (!m_action->icon().isNull())
{
if (size.height() < opt->maxIconWidth)
{
size.setHeight(opt->maxIconWidth);
}
}
return style()->sizeFromContents(QStyle::CT_MenuItem, opt, size, this);
}
QRect TagToggleMenuWidget::checkIndicatorSize(QStyleOption* option) const
{
if (m_action->isCheckBoxHidden())
{
return QRect();
}
QStyleOptionButton opt;
opt.QStyleOption::operator=(*option);
/*
opt.rect = bounding;
*/
return style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &opt, this);
}
// ------------------------------------------------------------------------
TagToggleAction::TagToggleAction(const QString& text, QObject* const parent)
: QWidgetAction(parent)
{
setText(text);
setCheckable(true);
}
TagToggleAction::TagToggleAction(const QIcon& icon, const QString& text, QObject* const parent)
: QWidgetAction(parent)
{
setIcon(icon);
setText(text);
setCheckable(true);
}
QWidget* TagToggleAction::createWidget(QWidget* parent)
{
QMenu* const menu = qobject_cast<QMenu*>(parent);
if (menu)
{
return (new TagToggleMenuWidget(menu, this));
}
else
{
return nullptr;
}
}
void TagToggleAction::setSpecialChecked(bool checked)
{
// something is resetting the checked property when there is a submenu.
// Use this to store "checked" anyway.
// Note: the method isChecked() is not virtual.
m_checked = checked;
setChecked(checked);
}
bool TagToggleAction::isChecked() const
{
return (m_checked || QWidgetAction::isChecked());
}
void TagToggleAction::setCheckBoxHidden(bool hidden)
{
m_checkBoxHidden = hidden;
}
bool TagToggleAction::isCheckBoxHidden() const
{
return m_checkBoxHidden;
}
// ------------------------------------------------------------------------
class Q_DECL_HIDDEN TagsPopupMenu::Private
{
public:
Private() = default;
QPixmap addTagPix;
QPixmap recentTagPix;
QPixmap tagViewPix;
QSet<int> assignedTags;
QSet<int> parentAssignedTags;
QList<qlonglong> selectedImageIDs;
QActionGroup* addTagActions = nullptr;
QActionGroup* toggleTagActions = nullptr;
TagsPopupMenu::Mode mode = ASSIGN;
};
TagsPopupMenu::TagsPopupMenu(qlonglong selectedImageId, Mode mode, QWidget* const parent)
: QMenu(parent),
d (new Private)
{
d->selectedImageIDs << selectedImageId;
setup(mode);
}
TagsPopupMenu::TagsPopupMenu(const QList<qlonglong>& selectedImageIds, Mode mode, QWidget* const parent)
: QMenu(parent),
d (new Private)
{
d->selectedImageIDs = selectedImageIds;
setup(mode);
}
void TagsPopupMenu::setup(Mode mode)
{
d->mode = mode;
d->addTagPix = QIcon::fromTheme(QLatin1String("tag")).pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize));
d->recentTagPix = QIcon::fromTheme(QLatin1String("tag-assigned")).pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize));
d->tagViewPix = QIcon::fromTheme(QLatin1String("edit-text-frame-update")).pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize));
d->addTagActions = new QActionGroup(this);
d->toggleTagActions = new QActionGroup(this);
setSeparatorsCollapsible(true);
connect(d->addTagActions, SIGNAL(triggered(QAction*)),
this, SLOT(slotAddTag(QAction*)));
connect(d->toggleTagActions, SIGNAL(triggered(QAction*)),
this, SLOT(slotToggleTag(QAction*)));
connect(this, SIGNAL(aboutToShow()),
this, SLOT(slotAboutToShow()));
AlbumThumbnailLoader* const loader = AlbumThumbnailLoader::instance();
connect(loader, SIGNAL(signalThumbnail(Album*,QPixmap)),
this, SLOT(slotTagThumbnail(Album*,QPixmap)));
// we are not interested in signalThumbnailFailed
}
TagsPopupMenu::~TagsPopupMenu()
{
delete d;
}
void TagsPopupMenu::clearPopup()
{
d->assignedTags.clear();
d->parentAssignedTags.clear();
clear();
}
void TagsPopupMenu::slotAboutToShow()
{
clearPopup();
AlbumManager* const man = AlbumManager::instance();<--- Variable 'man' can be declared as pointer to const
if ((d->mode == REMOVE) || (d->mode == DISPLAY))
{
if (d->selectedImageIDs.isEmpty())
{
menuAction()->setEnabled(false);
return;
}
const auto list = CoreDbAccess().db()->getItemCommonTagIDs(d->selectedImageIDs);
d->assignedTags = QSet<int>(list.begin(), list.end());
if (d->assignedTags.isEmpty())
{
menuAction()->setEnabled(false);
return;
}
// also add the parents of the assigned tags
bool hasValidTag = false;
for (QSet<int>::const_iterator it = d->assignedTags.constBegin() ;
it != d->assignedTags.constEnd() ; ++it)
{
TAlbum* const album = man->findTAlbum(*it);<--- Variable 'album' can be declared as pointer to const
if (!album || album->isInternalTag())
{
continue;
}
hasValidTag = true;
Album* a = album->parent();<--- Variable 'a' can be declared as pointer to const
while (a)
{
d->parentAssignedTags << a->id();
a = a->parent();
}
}
if (!hasValidTag)
{
menuAction()->setEnabled(false);
return;
}
}
else if (d->mode == ASSIGN)
{
if (d->selectedImageIDs.count() == 1)
{
const auto list = CoreDbAccess().db()->getItemCommonTagIDs(d->selectedImageIDs);
d->assignedTags = QSet<int>(list.begin(), list.end());
}
}
else if (d->mode == RECENTLYASSIGNED)
{
AlbumList recentTags = man->getRecentlyAssignedTags();
if (recentTags.isEmpty())
{
addSection(d->recentTagPix, i18n("No Recently Assigned Tags"));
}
else
{
addSection(d->recentTagPix, i18n("Recently Assigned Tags"));
for (AlbumList::const_iterator it = recentTags.constBegin() ;
it != recentTags.constEnd() ; ++it)
{
TAlbum* const album = static_cast<TAlbum*>(*it);
if (album)
{
TAlbum* const parent = dynamic_cast<TAlbum*> (album->parent());<--- Variable 'parent' can be declared as pointer to const
if (parent)
{
QString p = parent->prettyUrl().section(QLatin1Char('/'), 1, -1);
p.replace(QLatin1Char('/'), QLatin1String(" / "));
if (!p.isEmpty())
{
p = QLatin1String(" (") + p + QLatin1Char(')');
}
QString t = album->title() + p;
t.replace(QLatin1Char('&'), QLatin1String("&&"));
TagToggleAction* const action = new TagToggleAction(t, d->toggleTagActions);
action->setData(album->id());
action->setCheckBoxHidden(true);
setAlbumIcon(action, album);
addAction(action);
}
else
{
qCDebug(DIGIKAM_GENERAL_LOG) << "Tag" << album
<< "do not have a valid parent";
}
}
}
}
}
if (((d->mode == REMOVE) || (d->mode == DISPLAY)) && (d->assignedTags.count() < 10))
{
buildFlatMenu(this);
}
else
{
TAlbum* const album = man->findTAlbum(0);
if (!album)
{
return;
}
iterateAndBuildMenu(this, album);
if ((d->mode == ASSIGN) || (d->mode == RECENTLYASSIGNED))
{
addSeparator();
TagToggleAction* const addTag = new TagToggleAction(d->addTagPix, i18n("Add New Tag..."), d->addTagActions);
addTag->setData(0); // root id
addTag->setCheckBoxHidden(true);
addAction(addTag);
addSeparator();
TagToggleAction* const moreTag = new TagToggleAction(d->tagViewPix, i18n("More Tags..."), d->addTagActions);
moreTag->setData(-1); // special id to query tag view
moreTag->setCheckBoxHidden(true);
addAction(moreTag);
}
}
}
/// for qSort
bool lessThanByTitle(const Album* first, const Album* second)
{
return first->title() < second->title();
}
void TagsPopupMenu::iterateAndBuildMenu(QMenu* menu, TAlbum* album)
{
QList<Album*> sortedTags;
for (Album* a = album->firstChild() ; a ; a = a->next())
{
sortedTags << a;
}
std::stable_sort(sortedTags.begin(), sortedTags.end(), lessThanByTitle);
for (QList<Album*>::const_iterator it = sortedTags.constBegin() ;
it != sortedTags.constEnd() ; ++it)
{
TAlbum* const a = (TAlbum*)(*it);
if (a->isInternalTag())
{
continue;
}
if (d->mode == RECENTLYASSIGNED)
{
continue;
}
else if ((d->mode == REMOVE) || (d->mode == DISPLAY))
{
if (!d->assignedTags.contains(a->id()) && !d->parentAssignedTags.contains(a->id()))
{
continue;
}
}
QString t = a->title();
t.replace(QLatin1Char('&'), QLatin1String("&&"));
TagToggleAction* action = nullptr;
if (d->mode == ASSIGN)
{
action = new TagToggleAction(t, d->toggleTagActions);
if (d->assignedTags.contains(a->id()))
{
action->setSpecialChecked(true);
}
}
else // REMOVE or DISPLAY mode
{
action = new TagToggleAction(t, d->toggleTagActions);
action->setCheckBoxHidden(true);
}
action->setData(a->id());
menu->addAction(action);
// get icon
setAlbumIcon(action, a);
if (a->firstChild())
{
if (((d->mode != REMOVE) && (d->mode != DISPLAY)) ||
d->parentAssignedTags.contains(a->id()))
{
action->setMenu(buildSubMenu(a->id()));
}
}
}
}
QMenu* TagsPopupMenu::buildSubMenu(int tagid)
{
AlbumManager* const man = AlbumManager::instance();<--- Variable 'man' can be declared as pointer to const
TAlbum* const album = man->findTAlbum(tagid);
if (!album)
{
return nullptr;
}
QMenu* const popup = new QMenu(this);
popup->setSeparatorsCollapsible(true);
if ((d->mode == ASSIGN) && !d->assignedTags.contains(album->id()))
{
TagToggleAction* const action = new TagToggleAction(i18n("Assign this Tag"), d->toggleTagActions);
action->setData(album->id());
action->setCheckBoxHidden(true);
setAlbumIcon(action, album);
popup->addAction(action);
popup->addSeparator();
}
else if ((d->mode == REMOVE) && d->assignedTags.contains(tagid))
{
TagToggleAction* const action = new TagToggleAction(i18n("Remove this Tag"), d->toggleTagActions);
action->setData(album->id());
action->setCheckBoxHidden(true);
setAlbumIcon(action, album);
popup->addAction(action);
popup->addSeparator();
d->toggleTagActions->addAction(action);
}
else if (d->mode == DISPLAY)
{
TagToggleAction* const action = new TagToggleAction(i18n("Go to this Tag"), d->toggleTagActions);
action->setData(album->id());
action->setCheckBoxHidden(true);
setAlbumIcon(action, album);
popup->addAction(action);
popup->addSeparator();
d->toggleTagActions->addAction(action);
}
iterateAndBuildMenu(popup, album);
if (d->mode == ASSIGN)
{
popup->addSeparator();
TagToggleAction* const action = new TagToggleAction(d->addTagPix, i18n("Add New Tag..."), d->addTagActions);
action->setData(album->id());
action->setCheckBoxHidden(true);
popup->addAction(action);
}
return popup;
}
void TagsPopupMenu::buildFlatMenu(QMenu* menu)
{
QList<int> ids;
QStringList shortenedPaths = TagsCache::instance()->shortenedTagPaths(d->assignedTags.values(), &ids,
TagsCache::NoLeadingSlash, TagsCache::NoHiddenTags);
for (int i = 0 ; i < shortenedPaths.size() ; ++i)
{
QString t = shortenedPaths.at(i);
t.replace(QLatin1Char('&'), QLatin1String("&&"));
TAlbum* const a = AlbumManager::instance()->findTAlbum(ids.at(i));
if (!a)
{
continue;
}
TagToggleAction* const action = new TagToggleAction(t, d->toggleTagActions);
if (d->mode == ASSIGN)
{
if (d->assignedTags.contains(a->id()))
{
action->setSpecialChecked(true);
}
}
else // REMOVE or DISPLAY mode
{
action->setCheckBoxHidden(true);
}
action->setData(a->id());
menu->addAction(action);
// get icon
setAlbumIcon(action, a);
}
}
void TagsPopupMenu::setAlbumIcon(QAction* action, TAlbum* album)
{
AlbumThumbnailLoader* const loader = AlbumThumbnailLoader::instance();
QPixmap pix;
if (!loader->getTagThumbnail(album, pix))
{
if (pix.isNull())
{
action->setIcon(loader->getStandardTagIcon(album));
}
else
{
action->setIcon(pix);
}
}
else
{
// for the time while loading, set standard icon
// usually this code path will not be used, as icons are cached
action->setIcon(loader->getStandardTagIcon(album));
}
}
void TagsPopupMenu::slotToggleTag(QAction* action)
{
int tagID = action->data().toInt();
Q_EMIT signalTagActivated(tagID);
}
void TagsPopupMenu::slotAddTag(QAction* action)
{
int tagID = action->data().toInt();
AlbumManager* const man = AlbumManager::instance();<--- Variable 'man' can be declared as pointer to const
if (tagID == -1)
{
Q_EMIT signalPopupTagsView();
return;
}
TAlbum* const parent = man->findTAlbum(tagID);
if (!parent)
{
qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to find album with id " << tagID;
return;
}
QString title, icon;
QKeySequence ks;
if (!TagEditDlg::tagCreate(qApp->activeWindow(), parent, title, icon, ks))
{
return;
}
QMap<QString, QString> errMap;
AlbumList tList = TagEditDlg::createTAlbum(parent, title, icon, ks, errMap);
TagEditDlg::showtagsListCreationError(qApp->activeWindow(), errMap);
for (AlbumList::const_iterator it = tList.constBegin() ;
it != tList.constEnd() ; ++it)
{
Q_EMIT signalTagActivated((*it)->id());
}
}
void TagsPopupMenu::slotTagThumbnail(Album* album, const QPixmap& pix)
{
QList<QAction*> actionList = actions();
for (QAction* const action : std::as_const(actionList))
{
if (action->data().toInt() == album->id())
{ // cppcheck-suppress useStlAlgorithm
action->setIcon(pix);
return;
}
}
}
} // namespace Digikam
#include "tagspopupmenu.moc"
#include "moc_tagspopupmenu.cpp"
|