File indexing completed on 2025-01-19 03:56:52

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-06-23
0007  * Description : a widget to select metadata template.
0008  *
0009  * SPDX-FileCopyrightText: 2009-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 "templateselector.h"
0016 
0017 // Qt includes
0018 
0019 #include <QAbstractItemView>
0020 #include <QLabel>
0021 #include <QToolButton>
0022 #include <QApplication>
0023 #include <QStyle>
0024 #include <QIcon>
0025 
0026 // KDE includes
0027 
0028 #include <klocalizedstring.h>
0029 
0030 // Local includes
0031 
0032 #include "setup.h"
0033 #include "template.h"
0034 #include "templatemanager.h"
0035 #include "squeezedcombobox.h"
0036 
0037 namespace Digikam
0038 {
0039 
0040 class Q_DECL_HIDDEN TemplateSelector::Private
0041 {
0042 public:
0043 
0044     explicit Private()
0045       : label        (nullptr),
0046         setupButton  (nullptr),
0047         templateCombo(nullptr)
0048     {
0049     }
0050 
0051     QLabel*           label;
0052 
0053     QToolButton*      setupButton;
0054 
0055     SqueezedComboBox* templateCombo;
0056 
0057     Template          metadataTemplate;
0058 };
0059 
0060 TemplateSelector::TemplateSelector(QWidget* const parent)
0061     : DHBox(parent),
0062       d    (new Private)
0063 {
0064     d->label         = new QLabel(i18n("Template: "), this);
0065     d->templateCombo = new SqueezedComboBox(this);
0066     d->setupButton   = new QToolButton(this);
0067     d->setupButton->setIcon(QIcon::fromTheme(QLatin1String("document-edit")));
0068     d->setupButton->setWhatsThis(i18n("Open metadata template editor"));
0069     d->templateCombo->setWhatsThis(i18n("<p>Select here the action to perform using the metadata template.</p>"
0070                                         "<p><b>To remove</b>: delete already-assigned template.</p>"
0071                                         "<p><b>Do not change</b>: Do not touch template information.</p>"
0072                                         "<p>All other values are template titles managed by digiKam. "
0073                                         "Selecting one will assign information as well.</p>"));
0074 
0075     setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0076                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0077     setContentsMargins(QMargins());
0078     setStretchFactor(d->templateCombo, 10);
0079 
0080     connect(d->templateCombo, SIGNAL(activated(int)),
0081             this, SIGNAL(signalTemplateSelected()));
0082 
0083     connect(d->setupButton, SIGNAL(clicked()),
0084             this, SLOT(slotOpenSetup()));
0085 
0086     TemplateManager* const tm = TemplateManager::defaultManager();
0087 
0088     if (tm)
0089     {
0090         connect(tm, SIGNAL(signalTemplateAdded(Template)),
0091                 this, SLOT(slotTemplateListChanged()));
0092 
0093         connect(tm, SIGNAL(signalTemplateRemoved(Template)),
0094                 this, SLOT(slotTemplateListChanged()));
0095     }
0096 
0097     populateTemplates();
0098 }
0099 
0100 TemplateSelector::~TemplateSelector()
0101 {
0102     delete d;
0103 }
0104 
0105 void TemplateSelector::populateTemplates()
0106 {
0107     d->templateCombo->clear();
0108     d->templateCombo->insertSqueezedItem(i18n("To remove"),     REMOVETEMPLATE);
0109     d->templateCombo->insertSqueezedItem(i18n("Do not change"), DONTCHANGE);
0110     d->templateCombo->insertSeparator(DONTCHANGE + 1);
0111 
0112     TemplateManager* const tm = TemplateManager::defaultManager();
0113 
0114     if (tm)
0115     {
0116         int i                 = DONTCHANGE + 2;
0117         QList<Template> list  = tm->templateList();
0118 
0119         Q_FOREACH (const Template& t, list)
0120         {
0121             d->templateCombo->insertSqueezedItem(t.templateTitle(), i);
0122             ++i;
0123         }
0124     }
0125 }
0126 
0127 Template TemplateSelector::getTemplate() const
0128 {
0129     switch (d->templateCombo->currentIndex())
0130     {
0131         case REMOVETEMPLATE:
0132         {
0133             Template t;
0134             t.setTemplateTitle(Template::removeTemplateTitle());
0135             return t;
0136         }
0137 
0138         case DONTCHANGE:
0139         {
0140             return Template();
0141         }
0142 
0143         default:
0144         {
0145             TemplateManager* const tm = TemplateManager::defaultManager();
0146 
0147             if (tm)
0148             {
0149                 return tm->fromIndex(d->templateCombo->currentIndex() - 3);
0150             }
0151 
0152             break;
0153         }
0154     }
0155 
0156     return Template();
0157 }
0158 
0159 void TemplateSelector::setTemplate(const Template& t)
0160 {
0161     d->metadataTemplate = t;
0162     QString title       = d->metadataTemplate.templateTitle();
0163 
0164     if      (title == Template::removeTemplateTitle())
0165     {
0166         d->templateCombo->setCurrentIndex(REMOVETEMPLATE);
0167     }
0168     else if (title.isEmpty())
0169     {
0170         d->templateCombo->setCurrentIndex(DONTCHANGE);
0171     }
0172 
0173     d->templateCombo->setCurrent(title);
0174 }
0175 
0176 int TemplateSelector::getTemplateIndex() const
0177 {
0178     return d->templateCombo->currentIndex();
0179 }
0180 
0181 void TemplateSelector::setTemplateIndex(int i)
0182 {
0183     d->templateCombo->setCurrentIndex(i);
0184 }
0185 
0186 void TemplateSelector::slotOpenSetup()
0187 {
0188     Setup::execTemplateEditor(this, d->metadataTemplate);
0189 }
0190 
0191 void TemplateSelector::slotTemplateListChanged()
0192 {
0193     populateTemplates();
0194 }
0195 
0196 } // namespace Digikam
0197 
0198 #include "moc_templateselector.cpp"