1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2004-01-02
7  * Description : album category setup tab.
8  *
9  * Copyright (C) 2004-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "setupcategory.h"
25 
26 // Qt includes
27 
28 #include <QButtonGroup>
29 #include <QCheckBox>
30 #include <QDir>
31 #include <QGridLayout>
32 #include <QGroupBox>
33 #include <QLabel>
34 #include <QPushButton>
35 #include <QRadioButton>
36 #include <QVBoxLayout>
37 #include <QApplication>
38 #include <QStyle>
39 #include <QLineEdit>
40 #include <QUrl>
41 #include <QListWidget>
42 #include <QIcon>
43 
44 // KDE includes
45 
46 #include <klocalizedstring.h>
47 
48 // Local includes
49 
50 #include "applicationsettings.h"
51 #include "thumbnailsize.h"
52 
53 namespace Digikam
54 {
55 
56 class Q_DECL_HIDDEN SetupCategory::Private
57 {
58 public:
59 
Private()60     explicit Private()
61       : addCategoryButton(nullptr),
62         delCategoryButton(nullptr),
63         repCategoryButton(nullptr),
64         albumCategoryBox (nullptr),
65         categoryEdit     (nullptr)
66     {
67     }
68 
69     QPushButton* addCategoryButton;
70     QPushButton* delCategoryButton;
71     QPushButton* repCategoryButton;
72 
73     QListWidget* albumCategoryBox;
74 
75     QLineEdit*   categoryEdit;
76 };
77 
SetupCategory(QWidget * const parent)78 SetupCategory::SetupCategory(QWidget* const parent)
79     : QScrollArea(parent),
80       d          (new Private)
81 {
82     QWidget* const panel    = new QWidget(viewport());
83     setWidget(panel);
84     setWidgetResizable(true);
85 
86     const int spacing       = QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
87     QGridLayout* const grid = new QGridLayout(panel);
88 
89     QLabel* const explanationLabel = new QLabel(panel);
90     explanationLabel->setText(i18n("Manage categories to sort and re-arrange album tree-view."));
91     explanationLabel->setWordWrap(true);
92 
93     // --------------------------------------------------------
94 
95     d->categoryEdit     = new QLineEdit(panel);
96     d->categoryEdit->setClearButtonEnabled(true);
97 
98     d->albumCategoryBox = new QListWidget(panel);
99     d->albumCategoryBox->setWhatsThis(i18n("You can add or remove Album "
100                                            "category types here to improve how "
101                                            "your Albums are sorted in digiKam."));
102 
103     d->albumCategoryBox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
104 
105     d->addCategoryButton = new QPushButton(i18n("&Add..."), panel);
106     d->delCategoryButton = new QPushButton(i18n("&Remove"), panel);
107     d->repCategoryButton = new QPushButton(i18n("&Replace"), panel);
108 
109     d->addCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("list-add")));
110     d->delCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
111     d->repCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
112     d->delCategoryButton->setEnabled(false);
113     d->repCategoryButton->setEnabled(false);
114 
115     grid->setAlignment(Qt::AlignTop);
116     grid->addWidget(explanationLabel,     0, 0, 1, 1);
117     grid->addWidget(d->categoryEdit,      1, 0, 1, 1);
118     grid->addWidget(d->albumCategoryBox,  2, 0, 5, 1);
119     grid->addWidget(d->addCategoryButton, 2, 1, 1, 1);
120     grid->addWidget(d->delCategoryButton, 3, 1, 1, 1);
121     grid->addWidget(d->repCategoryButton, 4, 1, 1, 1);
122     grid->setRowStretch(5, 10);
123     grid->setColumnStretch(0, 10);
124     grid->setContentsMargins(spacing, spacing, spacing, spacing);
125     grid->setSpacing(spacing);
126 
127     // --------------------------------------------------------
128 
129     connect(d->albumCategoryBox, SIGNAL(itemSelectionChanged()),
130             this, SLOT(slotCategorySelectionChanged()));
131 
132     connect(d->addCategoryButton, SIGNAL(clicked()),
133             this, SLOT(slotAddCategory()));
134 
135     connect(d->delCategoryButton, SIGNAL(clicked()),
136             this, SLOT(slotDelCategory()));
137 
138     connect(d->repCategoryButton, SIGNAL(clicked()),
139             this, SLOT(slotRepCategory()));
140 
141     // --------------------------------------------------------
142 
143     adjustSize();
144 }
145 
~SetupCategory()146 SetupCategory::~SetupCategory()
147 {
148     delete d;
149 }
150 
slotDelCategory()151 void SetupCategory::slotDelCategory()
152 {
153     QListWidgetItem* const item = d->albumCategoryBox->currentItem();
154 
155     if (!item)
156     {
157         return;
158     }
159 
160     d->albumCategoryBox->takeItem(d->albumCategoryBox->row(item));
161     delete item;
162 }
163 
slotRepCategory()164 void SetupCategory::slotRepCategory()
165 {
166     QString newCategory = d->categoryEdit->text();
167 
168     if (newCategory.isEmpty())
169     {
170         return;
171     }
172 
173     if (!d->albumCategoryBox->selectedItems().isEmpty())
174     {
175         d->albumCategoryBox->selectedItems().at(0)->setText(newCategory);
176         d->categoryEdit->clear();
177     }
178 }
179 
slotCategorySelectionChanged()180 void SetupCategory::slotCategorySelectionChanged()
181 {
182     if (!d->albumCategoryBox->selectedItems().isEmpty())
183     {
184         d->categoryEdit->setText(d->albumCategoryBox->selectedItems().at(0)->text());
185         d->delCategoryButton->setEnabled(true);
186         d->repCategoryButton->setEnabled(true);
187     }
188     else
189     {
190         d->delCategoryButton->setEnabled(false);
191         d->repCategoryButton->setEnabled(false);
192     }
193 }
194 
slotAddCategory()195 void SetupCategory::slotAddCategory()
196 {
197     QString newCategory = d->categoryEdit->text();
198 
199     if (newCategory.isEmpty())
200     {
201         return;
202     }
203 
204     bool found = false;
205 
206     for (int i = 0 ; i < d->albumCategoryBox->count() ; ++i)
207     {
208         QListWidgetItem* const item = d->albumCategoryBox->item(i);
209 
210         if (newCategory == item->text())
211         {
212             found = true;
213             break;
214         }
215     }
216 
217     if (!found)
218     {
219         d->albumCategoryBox->insertItem(d->albumCategoryBox->count(), newCategory);
220         d->categoryEdit->clear();
221     }
222 }
223 
applySettings()224 void SetupCategory::applySettings()
225 {
226     ApplicationSettings* const settings = ApplicationSettings::instance();
227 
228     if (!settings)
229     {
230         return;
231     }
232 
233     QStringList categoryList;
234 
235     for (int i = 0 ; i < d->albumCategoryBox->count() ; ++i)
236     {
237         QListWidgetItem* const item = d->albumCategoryBox->item(i);
238         categoryList.append(item->text());
239     }
240 
241     settings->setAlbumCategoryNames(categoryList);
242     settings->saveSettings();
243 }
244 
readSettings()245 void SetupCategory::readSettings()
246 {
247     ApplicationSettings* const settings = ApplicationSettings::instance();
248 
249     if (!settings)
250     {
251         return;
252     }
253 
254     d->albumCategoryBox->insertItems(0, settings->getAlbumCategoryNames());
255 }
256 
257 } // namespace Digikam
258