| 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 Tree View - Initialization. |
| 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 "labelstreeview_p.h" |
| 17 |
|
| 18 |
namespace Digikam |
| 19 |
{ |
| 20 |
|
| 21 |
void LabelsTreeView::initTreeView() |
| 22 |
{ |
| 23 |
d->itemIterator = QTreeWidgetItemIterator(this); |
| 24 |
|
| 25 |
setIconSize(QSize(d->iconSizeFromSetting * 5, |
| 26 |
d->iconSizeFromSetting)); |
| 27 |
|
| 28 |
initRatingsTree(); |
| 29 |
initPicksTree(); |
| 30 |
initColorsTree(); |
| 31 |
expandAll(); |
| 32 |
setRootIsDecorated(false); |
| 33 |
} |
| 34 |
|
| 35 |
void LabelsTreeView::initRatingsTree() |
| 36 |
{ |
| 37 |
d->ratings = getOrCreateItem(nullptr); |
| 38 |
d->ratings->setText(0, i18nc("@item: rating tree", "Rating")); |
| 39 |
d->ratings->setFont(0, d->regularFont); |
| 40 |
d->ratings->setFlags(Qt::ItemIsEnabled); |
| 41 |
|
| 42 |
QTreeWidgetItem* const noRate = getOrCreateItem(d->ratings); |
| 43 |
noRate->setText(0, i18nc("@item: rating tree", "No Rating")); |
| 44 |
noRate->setFont(0, d->regularFont); |
| 45 |
QPixmap pix2(goldenStarPixmap().size()); |
| 46 |
pix2.fill(Qt::transparent); |
| 47 |
QPainter p2(&pix2); |
| 48 |
p2.setRenderHint(QPainter::Antialiasing, true); |
| 49 |
p2.setPen(palette().color(QPalette::Active, foregroundRole())); |
| 50 |
p2.drawPixmap(0, 0, goldenStarPixmap(false)); |
| 51 |
noRate->setIcon(0, QIcon(pix2)); |
| 52 |
noRate->setSizeHint(0, d->iconSize); |
| 53 |
|
| 54 |
for (int rate = 1 ; rate <= 5 ; ++rate) |
| 55 |
{ |
| 56 |
QTreeWidgetItem* const rateWidget = getOrCreateItem(d->ratings); |
| 57 |
|
| 58 |
QPixmap pix(goldenStarPixmap().width() * rate, goldenStarPixmap().height()); |
| 59 |
pix.fill(Qt::transparent); |
| 60 |
QPainter p(&pix); |
| 61 |
int offset = 0; |
| 62 |
p.setRenderHint(QPainter::Antialiasing, true); |
| 63 |
p.setPen(palette().color(QPalette::Active, foregroundRole())); |
| 64 |
|
| 65 |
for (int i = 0 ; i < rate ; ++i) |
| 66 |
{ |
| 67 |
p.drawPixmap(offset, 0, goldenStarPixmap()); |
| 68 |
offset += goldenStarPixmap().width(); |
| 69 |
} |
| 70 |
|
| 71 |
rateWidget->setIcon(0, QIcon(pix)); |
| 72 |
rateWidget->setSizeHint(0, d->iconSize); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void LabelsTreeView::initPicksTree() |
| 77 |
{ |
| 78 |
d->picks = getOrCreateItem(nullptr); |
| 79 |
d->picks->setText(0, i18nc("@title: pick tree", "Pick")); |
| 80 |
d->picks->setFont(0, d->regularFont); |
| 81 |
d->picks->setFlags(Qt::ItemIsEnabled); |
| 82 |
|
| 83 |
for (int i = FirstPickLabel ; i <= LastPickLabel ; i++) |
| 84 |
{ |
| 85 |
QTreeWidgetItem* const pickWidgetItem = getOrCreateItem(d->picks); |
| 86 |
pickWidgetItem->setText(0, PickLabelWidget::labelPickName((PickLabel)i)); |
| 87 |
pickWidgetItem->setFont(0, d->regularFont); |
| 88 |
pickWidgetItem->setIcon(0, PickLabelWidget::buildIcon((PickLabel)i).pixmap(64, 64)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void LabelsTreeView::initColorsTree() |
| 93 |
{ |
| 94 |
QMap<int, QString> map = TagsActionMngr::defaultManager()->colorLabelNames(); |
| 95 |
d->colors = getOrCreateItem(nullptr); |
| 96 |
d->colors->setText(0, i18nc("@item: color tree", "Color")); |
| 97 |
d->colors->setFont(0, d->regularFont); |
| 98 |
d->colors->setFlags(Qt::ItemIsEnabled); |
| 99 |
|
| 100 |
ColorLabelDelegate* const delegate = new ColorLabelDelegate(32, this); |
| 101 |
const QList<int> keys = map.keys(); |
| 102 |
|
| 103 |
for (int label : keys) |
| 104 |
{ |
| 105 |
QTreeWidgetItem* const colorWidgetItem = getOrCreateItem(d->colors); |
| 106 |
colorWidgetItem->setData(0, Qt::UserRole, label); |
| 107 |
colorWidgetItem->setFont(0, d->regularFont); |
| 108 |
colorWidgetItem->setText(0, map.value(label)); |
| 109 |
|
| 110 |
setItemDelegateForRow(indexFromItem(colorWidgetItem).row(), delegate); |
| 111 |
|
| 112 |
if (label == NoColorLabel) |
| 113 |
{ |
| 114 |
colorWidgetItem->setIcon(0, QIcon(QIcon::fromTheme(QLatin1String("emblem-unmounted")) |
| 115 |
.pixmap(64, 64, QIcon::Disabled))); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
QPixmap colorIcon = colorRectPixmap(QColor(TagsActionMngr::colorSet().value(label))); |
| 120 |
colorWidgetItem->setIcon(0, QIcon(colorIcon)); |
| 121 |
colorWidgetItem->setSizeHint(0, d->iconSize); |
| 122 |
} |
| 123 |
|
| 124 |
if (!d->isCheckableTreeView) |
| 125 |
{ |
| 126 |
colorWidgetItem->setFlags(colorWidgetItem->flags() | Qt::ItemIsEditable); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
connect(this, &QTreeWidget::itemChanged, |
| 131 |
[this](QTreeWidgetItem* changedItem, int column) |
| 132 |
{ |
| 133 |
if (changedItem && (changedItem->parent() == d->colors) && (column == 0)) |
| 134 |
{ |
| 135 |
Q_EMIT signalColorNameChanged(changedItem->data( |
| 136 |
0, |
| 137 |
Qt::UserRole).toInt(), |
| 138 |
changedItem->text(0) |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
void LabelsTreeView::restoreSelectionFromHistory(QHash<Labels, QList<int> > neededLabels) |
| 146 |
{ |
| 147 |
QTreeWidgetItemIterator it(this, QTreeWidgetItemIterator::Selected); |
| 148 |
|
| 149 |
while (*it) |
| 150 |
{ |
| 151 |
(*it)->setSelected(false); |
| 152 |
++it; |
| 153 |
} |
| 154 |
|
| 155 |
for (int rateItemIndex : std::as_const(neededLabels[Ratings])) |
| 156 |
{ |
| 157 |
d->ratings->child(rateItemIndex)->setSelected(true); |
| 158 |
} |
| 159 |
|
| 160 |
for (int pickItemIndex : std::as_const(neededLabels[Picks])) |
| 161 |
{ |
| 162 |
d->picks->child(pickItemIndex)->setSelected(true); |
| 163 |
} |
| 164 |
|
| 165 |
for (int colorItemIndex : std::as_const(neededLabels[Colors])) |
| 166 |
{ |
| 167 |
d->colors->child(colorItemIndex)->setSelected(true); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} // namespace Digikam |