File indexing completed on 2025-01-05 04:00:08

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-01-02
0007  * Description : album category setup tab.
0008  *
0009  * SPDX-FileCopyrightText: 2004-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "setupcategory.h"
0016 
0017 // Qt includes
0018 
0019 #include <QButtonGroup>
0020 #include <QCheckBox>
0021 #include <QDir>
0022 #include <QGridLayout>
0023 #include <QGroupBox>
0024 #include <QLabel>
0025 #include <QPushButton>
0026 #include <QRadioButton>
0027 #include <QVBoxLayout>
0028 #include <QApplication>
0029 #include <QStyle>
0030 #include <QUrl>
0031 #include <QListWidget>
0032 #include <QIcon>
0033 
0034 // KDE includes
0035 
0036 #include <klocalizedstring.h>
0037 
0038 // Local includes
0039 
0040 #include "applicationsettings.h"
0041 #include "thumbnailsize.h"
0042 #include "dtextedit.h"
0043 
0044 namespace Digikam
0045 {
0046 
0047 class Q_DECL_HIDDEN SetupCategory::Private
0048 {
0049 public:
0050 
0051     explicit Private()
0052       : addCategoryButton(nullptr),
0053         delCategoryButton(nullptr),
0054         repCategoryButton(nullptr),
0055         albumCategoryBox (nullptr),
0056         categoryEdit     (nullptr)
0057     {
0058     }
0059 
0060     QPushButton* addCategoryButton;
0061     QPushButton* delCategoryButton;
0062     QPushButton* repCategoryButton;
0063 
0064     QListWidget* albumCategoryBox;
0065 
0066     DTextEdit*   categoryEdit;
0067 };
0068 
0069 SetupCategory::SetupCategory(QWidget* const parent)
0070     : QScrollArea(parent),
0071       d          (new Private)
0072 {
0073     QWidget* const panel    = new QWidget(viewport());
0074     setWidget(panel);
0075     setWidgetResizable(true);
0076 
0077     const int spacing       = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0078                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0079     QGridLayout* const grid = new QGridLayout(panel);
0080 
0081     QLabel* const explanationLabel = new QLabel(panel);
0082     explanationLabel->setText(i18n("Manage categories to sort and re-arrange album tree-view."));
0083     explanationLabel->setWordWrap(true);
0084 
0085     // --------------------------------------------------------
0086 
0087     d->categoryEdit     = new DTextEdit(panel);
0088     d->categoryEdit->setLinesVisible(1);
0089     d->categoryEdit->setPlaceholderText(i18n("Set here the new category"));
0090 
0091     d->albumCategoryBox = new QListWidget(panel);
0092     d->albumCategoryBox->setWhatsThis(i18n("You can add or remove Album "
0093                                            "category types here to improve how "
0094                                            "your Albums are sorted in digiKam."));
0095 
0096     d->albumCategoryBox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0097 
0098     d->addCategoryButton = new QPushButton(i18n("&Add..."), panel);
0099     d->delCategoryButton = new QPushButton(i18n("&Remove"), panel);
0100     d->repCategoryButton = new QPushButton(i18n("&Replace"), panel);
0101 
0102     d->addCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("list-add")));
0103     d->delCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0104     d->repCategoryButton->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
0105     d->delCategoryButton->setEnabled(false);
0106     d->repCategoryButton->setEnabled(false);
0107 
0108     grid->setAlignment(Qt::AlignTop);
0109     grid->addWidget(explanationLabel,     0, 0, 1, 1);
0110     grid->addWidget(d->categoryEdit,      1, 0, 1, 1);
0111     grid->addWidget(d->albumCategoryBox,  2, 0, 5, 1);
0112     grid->addWidget(d->addCategoryButton, 2, 1, 1, 1);
0113     grid->addWidget(d->delCategoryButton, 3, 1, 1, 1);
0114     grid->addWidget(d->repCategoryButton, 4, 1, 1, 1);
0115     grid->setRowStretch(5, 10);
0116     grid->setColumnStretch(0, 10);
0117     grid->setContentsMargins(spacing, spacing, spacing, spacing);
0118     grid->setSpacing(spacing);
0119 
0120     // --------------------------------------------------------
0121 
0122     connect(d->albumCategoryBox, SIGNAL(itemSelectionChanged()),
0123             this, SLOT(slotCategorySelectionChanged()));
0124 
0125     connect(d->addCategoryButton, SIGNAL(clicked()),
0126             this, SLOT(slotAddCategory()));
0127 
0128     connect(d->delCategoryButton, SIGNAL(clicked()),
0129             this, SLOT(slotDelCategory()));
0130 
0131     connect(d->repCategoryButton, SIGNAL(clicked()),
0132             this, SLOT(slotRepCategory()));
0133 
0134     // --------------------------------------------------------
0135 
0136     adjustSize();
0137 }
0138 
0139 SetupCategory::~SetupCategory()
0140 {
0141     delete d;
0142 }
0143 
0144 void SetupCategory::slotDelCategory()
0145 {
0146     QListWidgetItem* const item = d->albumCategoryBox->currentItem();
0147 
0148     if (!item)
0149     {
0150         return;
0151     }
0152 
0153     d->albumCategoryBox->takeItem(d->albumCategoryBox->row(item));
0154     delete item;
0155 }
0156 
0157 void SetupCategory::slotRepCategory()
0158 {
0159     QString newCategory = d->categoryEdit->text();
0160 
0161     if (newCategory.isEmpty())
0162     {
0163         return;
0164     }
0165 
0166     if (!d->albumCategoryBox->selectedItems().isEmpty())
0167     {
0168         d->albumCategoryBox->selectedItems().at(0)->setText(newCategory);
0169         d->categoryEdit->clear();
0170     }
0171 }
0172 
0173 void SetupCategory::slotCategorySelectionChanged()
0174 {
0175     if (!d->albumCategoryBox->selectedItems().isEmpty())
0176     {
0177         d->categoryEdit->setText(d->albumCategoryBox->selectedItems().at(0)->text());
0178         d->delCategoryButton->setEnabled(true);
0179         d->repCategoryButton->setEnabled(true);
0180     }
0181     else
0182     {
0183         d->delCategoryButton->setEnabled(false);
0184         d->repCategoryButton->setEnabled(false);
0185     }
0186 }
0187 
0188 void SetupCategory::slotAddCategory()
0189 {
0190     QString newCategory = d->categoryEdit->text();
0191 
0192     if (newCategory.isEmpty())
0193     {
0194         return;
0195     }
0196 
0197     bool found = false;
0198 
0199     for (int i = 0 ; i < d->albumCategoryBox->count() ; ++i)
0200     {
0201         QListWidgetItem* const item = d->albumCategoryBox->item(i);
0202 
0203         if (newCategory == item->text())
0204         {
0205             found = true;
0206             break;
0207         }
0208     }
0209 
0210     if (!found)
0211     {
0212         d->albumCategoryBox->insertItem(d->albumCategoryBox->count(), newCategory);
0213         d->categoryEdit->clear();
0214     }
0215 }
0216 
0217 void SetupCategory::applySettings()
0218 {
0219     ApplicationSettings* const settings = ApplicationSettings::instance();
0220 
0221     if (!settings)
0222     {
0223         return;
0224     }
0225 
0226     QStringList categoryList;
0227 
0228     for (int i = 0 ; i < d->albumCategoryBox->count() ; ++i)
0229     {
0230         QListWidgetItem* const item = d->albumCategoryBox->item(i);
0231         categoryList.append(item->text());
0232     }
0233 
0234     settings->setAlbumCategoryNames(categoryList);
0235     settings->saveSettings();
0236 }
0237 
0238 void SetupCategory::readSettings()
0239 {
0240     ApplicationSettings* const settings = ApplicationSettings::instance();
0241 
0242     if (!settings)
0243     {
0244         return;
0245     }
0246 
0247     d->albumCategoryBox->insertItems(0, settings->getAlbumCategoryNames());
0248 }
0249 
0250 } // namespace Digikam
0251 
0252 #include "moc_setupcategory.cpp"