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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-07-04
0007  * Description : metadata template setup page.
0008  *
0009  * SPDX-FileCopyrightText: 2006-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 "setuptemplate.h"
0016 
0017 // Qt includes
0018 
0019 #include <QFrame>
0020 #include <QGridLayout>
0021 #include <QLabel>
0022 #include <QPushButton>
0023 #include <QApplication>
0024 #include <QStyle>
0025 #include <QIcon>
0026 #include <QMessageBox>
0027 
0028 // KDE includes
0029 
0030 #include <kconfiggroup.h>
0031 #include <ksharedconfig.h>
0032 #include <klocalizedstring.h>
0033 
0034 // Local includes
0035 
0036 #include "templatelist.h"
0037 #include "templatepanel.h"
0038 #include "altlangstredit.h"
0039 #include "dtextedit.h"
0040 
0041 namespace Digikam
0042 {
0043 
0044 class Q_DECL_HIDDEN SetupTemplate::Private
0045 {
0046 public:
0047 
0048     explicit Private()
0049       : addButton(nullptr),
0050         delButton(nullptr),
0051         repButton(nullptr),
0052         titleEdit(nullptr),
0053         listView(nullptr),
0054         tview(nullptr)
0055     {
0056     }
0057 
0058     QPushButton*   addButton;
0059     QPushButton*   delButton;
0060     QPushButton*   repButton;
0061 
0062     DTextEdit*     titleEdit;
0063 
0064     TemplateList*  listView;
0065 
0066     TemplatePanel* tview;
0067 };
0068 
0069 SetupTemplate::SetupTemplate(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 
0080     d->listView          = new TemplateList(panel);
0081     d->listView->setFixedHeight(100);
0082 
0083     // --------------------------------------------------------
0084 
0085     QLabel* const label0 = new QLabel(i18n("Template Title:"), panel);
0086     d->titleEdit         = new DTextEdit(panel);
0087     d->titleEdit->setLinesVisible(1);
0088     d->titleEdit->setPlaceholderText(i18n("Enter the metadata template title here."));
0089     d->titleEdit->setWhatsThis(i18n("<p>Enter the metadata template title here. This title will be "
0090                                     "used to identify a template in your collection.</p>"));
0091     label0->setBuddy(d->titleEdit);
0092 
0093     // --------------------------------------------------------
0094 
0095     d->tview             = new TemplatePanel(panel);
0096 
0097     // --------------------------------------------------------
0098 
0099     QLabel* const note = new QLabel(i18n("<b>Note: These information are used to set "
0100                                    "<b><a href='https://en.wikipedia.org/wiki/Extensible_Metadata_Platform'>XMP</a></b> "
0101                                    "and <b><a href='https://en.wikipedia.org/wiki/IPTC'>IPTC</a></b> tag contents. "
0102                                    "There is no limitation with XMP, but note that IPTC text tag sizes are limited. "
0103                                    "Use contextual help for details.</b>"), panel);
0104     note->setOpenExternalLinks(true);
0105     note->setWordWrap(true);
0106     note->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
0107 
0108     // -------------------------------------------------------------
0109 
0110     d->addButton = new QPushButton(panel);
0111     d->delButton = new QPushButton(panel);
0112     d->repButton = new QPushButton(panel);
0113 
0114     d->addButton->setText(i18n("&Add..."));
0115     d->addButton->setIcon(QIcon::fromTheme(QLatin1String("list-add")));
0116     d->delButton->setText(i18n("&Remove"));
0117     d->delButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0118     d->repButton->setText(i18n("&Replace..."));
0119     d->repButton->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
0120     d->delButton->setEnabled(false);
0121     d->repButton->setEnabled(false);
0122 
0123     // -------------------------------------------------------------
0124 
0125     QGridLayout* const grid = new QGridLayout;
0126     grid->setContentsMargins(spacing, spacing, spacing, spacing);
0127     grid->setSpacing(spacing);
0128     grid->setAlignment(Qt::AlignTop);
0129     grid->setColumnStretch(1, 10);
0130     grid->setRowStretch(4, 10);
0131     grid->addWidget(d->listView,  0, 0, 4, 2);
0132     grid->addWidget(d->addButton, 0, 2, 1, 1);
0133     grid->addWidget(d->delButton, 1, 2, 1, 1);
0134     grid->addWidget(d->repButton, 2, 2, 1, 1);
0135     grid->addWidget(label0,       4, 0, 1, 1);
0136     grid->addWidget(d->titleEdit, 4, 1, 1, 1);
0137     grid->addWidget(d->tview,     5, 0, 1, 3);
0138     grid->addWidget(note,         6, 0, 1, 3);
0139     panel->setLayout(grid);
0140 
0141     panel->setTabOrder(d->listView,  d->addButton);
0142     panel->setTabOrder(d->addButton, d->delButton);
0143     panel->setTabOrder(d->delButton, d->repButton);
0144     panel->setTabOrder(d->repButton, d->titleEdit);
0145     panel->setTabOrder(d->titleEdit, d->tview);
0146 
0147     // --------------------------------------------------------
0148 
0149     connect(d->listView, SIGNAL(itemSelectionChanged()),
0150             this, SLOT(slotSelectionChanged()));
0151 
0152     connect(d->addButton, SIGNAL(clicked()),
0153             this, SLOT(slotAddTemplate()));
0154 
0155     connect(d->delButton, SIGNAL(clicked()),
0156             this, SLOT(slotDelTemplate()));
0157 
0158     connect(d->repButton, SIGNAL(clicked()),
0159             this, SLOT(slotRepTemplate()));
0160 
0161     // --------------------------------------------------------
0162 
0163     readSettings();
0164     d->titleEdit->setFocus();
0165 
0166     d->listView->sortItems(0, Qt::AscendingOrder);
0167     d->listView->setSortingEnabled(true);
0168 }
0169 
0170 SetupTemplate::~SetupTemplate()
0171 {
0172     delete d;
0173 }
0174 
0175 void SetupTemplate::applySettings()
0176 {
0177     QString title = d->titleEdit->text();
0178 
0179     if (!title.isEmpty())
0180     {
0181         TemplateListItem* const item = dynamic_cast<TemplateListItem*>(d->listView->currentItem());
0182 
0183         if (item)
0184         {
0185             d->tview->apply();
0186 
0187             Template t = d->tview->getTemplate();
0188             t.setTemplateTitle(title);
0189             item->setTemplate(t);
0190         }
0191     }
0192 
0193     d->listView->applySettings();
0194 
0195     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0196     KConfigGroup group        = config->group(QLatin1String("Setup Dialog"));
0197     group.writeEntry(QLatin1String("Template Tab"), (int)(d->tview->currentIndex()));
0198     config->sync();
0199 }
0200 
0201 void SetupTemplate::readSettings()
0202 {
0203     d->listView->readSettings();
0204 
0205     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0206     KConfigGroup group        = config->group(QLatin1String("Setup Dialog"));
0207     d->tview->setCurrentIndex((TemplatePanel::TemplateTab)group.readEntry(QLatin1String("Template Tab"), (int)TemplatePanel::RIGHTS));
0208 }
0209 
0210 void SetupTemplate::setTemplate(const Template& t)
0211 {
0212     if (!t.isNull())
0213     {
0214         TemplateListItem* const item = d->listView->find(t.templateTitle());
0215         d->listView->setCurrentItem(item);
0216 
0217         return;
0218     }
0219 
0220     populateTemplate(t);
0221 }
0222 
0223 void SetupTemplate::slotSelectionChanged()
0224 {
0225     TemplateListItem* const item = dynamic_cast<TemplateListItem*>(d->listView->currentItem());
0226 
0227     if (!item)
0228     {
0229         d->delButton->setEnabled(false);
0230         d->repButton->setEnabled(false);
0231 
0232         return;
0233     }
0234 
0235     d->delButton->setEnabled(true);
0236     d->repButton->setEnabled(true);
0237     populateTemplate(item->getTemplate());
0238 }
0239 
0240 void SetupTemplate::populateTemplate(const Template& t)
0241 {
0242     d->tview->setTemplate(t);
0243     d->titleEdit->setText(t.templateTitle());
0244     d->titleEdit->setFocus();
0245 }
0246 
0247 void SetupTemplate::slotAddTemplate()
0248 {
0249     QString title = d->titleEdit->text();
0250 
0251     if (title.isEmpty())
0252     {
0253         QMessageBox::critical(this, qApp->applicationName(), i18n("Cannot register new metadata template without title."));
0254 
0255         return;
0256     }
0257 
0258     if (d->listView->find(title))
0259     {
0260         QMessageBox::critical(this, qApp->applicationName(), i18n("A metadata template named '%1' already exists.", title));
0261 
0262         return;
0263     }
0264 
0265     d->tview->apply();
0266 
0267     Template t = d->tview->getTemplate();
0268     t.setTemplateTitle(d->titleEdit->text());
0269     new TemplateListItem(d->listView, t);
0270 }
0271 
0272 void SetupTemplate::slotDelTemplate()
0273 {
0274     TemplateListItem* const item = dynamic_cast<TemplateListItem*>(d->listView->currentItem());
0275     delete item;
0276 }
0277 
0278 void SetupTemplate::slotRepTemplate()
0279 {
0280     QString title = d->titleEdit->text();
0281 
0282     if (title.isEmpty())
0283     {
0284         QMessageBox::critical(this, qApp->applicationName(), i18n("Cannot register new metadata template without title."));
0285 
0286         return;
0287     }
0288 
0289     TemplateListItem* const item = dynamic_cast<TemplateListItem*>(d->listView->currentItem());
0290 
0291     if (!item)
0292     {
0293         return;
0294     }
0295 
0296     d->tview->apply();
0297 
0298     Template t = d->tview->getTemplate();
0299     t.setTemplateTitle(title);
0300     item->setTemplate(t);
0301 }
0302 
0303 } // namespace Digikam
0304 
0305 #include "moc_setuptemplate.cpp"