1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-06-23
 * Description : a widget to select metadata template.
 *
 * SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "templateselector.h"

// Qt includes

#include <QAbstractItemView>
#include <QLabel>
#include <QToolButton>
#include <QApplication>
#include <QStyle>
#include <QIcon>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "setup.h"
#include "template.h"
#include "templatemanager.h"
#include "squeezedcombobox.h"
#include "digikam_globals.h"

namespace Digikam
{

class Q_DECL_HIDDEN TemplateSelector::Private
{
public:

    Private() = default;

    QLabel*           label             = nullptr;

    QToolButton*      setupButton       = nullptr;

    SqueezedComboBox* templateCombo     = nullptr;

    Template          metadataTemplate;
};

TemplateSelector::TemplateSelector(QWidget* const parent)
    : DHBox(parent),
      d    (new Private)
{
    d->label         = new QLabel(i18n("Template: "), this);
    d->templateCombo = new SqueezedComboBox(this);
    d->setupButton   = new QToolButton(this);
    d->setupButton->setIcon(QIcon::fromTheme(QLatin1String("document-edit")));
    d->setupButton->setWhatsThis(i18n("Open metadata template editor"));
    d->templateCombo->setWhatsThis(i18n("<p>Select here the action to perform using the metadata template.</p>"
                                        "<p><b>To remove</b>: delete already-assigned template.</p>"
                                        "<p><b>Do not change</b>: Do not touch template information.</p>"
                                        "<p>All other values are template titles managed by digiKam. "
                                        "Selecting one will assign information as well.</p>"));

    setSpacing(layoutSpacing());
    setContentsMargins(QMargins());
    setStretchFactor(d->templateCombo, 10);

    connect(d->templateCombo, SIGNAL(activated(int)),
            this, SIGNAL(signalTemplateSelected()));

    connect(d->setupButton, SIGNAL(clicked()),
            this, SLOT(slotOpenSetup()));

    TemplateManager* const tm = TemplateManager::defaultManager();<--- Variable 'tm' can be declared as pointer to const

    if (tm)
    {
        connect(tm, SIGNAL(signalTemplateAdded(Template)),
                this, SLOT(slotTemplateListChanged()));

        connect(tm, SIGNAL(signalTemplateRemoved(Template)),
                this, SLOT(slotTemplateListChanged()));
    }

    populateTemplates();
}

TemplateSelector::~TemplateSelector()
{
    delete d;
}

void TemplateSelector::populateTemplates()
{
    d->templateCombo->clear();
    d->templateCombo->insertSqueezedItem(i18n("To remove"),     REMOVETEMPLATE);
    d->templateCombo->insertSqueezedItem(i18n("Do not change"), DONTCHANGE);
    d->templateCombo->insertSeparator(DONTCHANGE + 1);

    TemplateManager* const tm = TemplateManager::defaultManager();<--- Variable 'tm' can be declared as pointer to const

    if (tm)
    {
        int i                 = DONTCHANGE + 2;
        QList<Template> list  = tm->templateList();

        for (const Template& t : std::as_const(list))
        {
            d->templateCombo->insertSqueezedItem(t.templateTitle(), i);
            ++i;
        }
    }
}

Template TemplateSelector::getTemplate() const
{
    switch (d->templateCombo->currentIndex())
    {
        case REMOVETEMPLATE:
        {
            Template t;
            t.setTemplateTitle(Template::removeTemplateTitle());
            return t;
        }

        case DONTCHANGE:
        {
            return Template();
        }

        default:
        {
            TemplateManager* const tm = TemplateManager::defaultManager();<--- Variable 'tm' can be declared as pointer to const

            if (tm)
            {
                return tm->fromIndex(d->templateCombo->currentIndex() - 3);
            }

            break;
        }
    }

    return Template();
}

void TemplateSelector::setTemplate(const Template& t)
{
    d->metadataTemplate = t;
    QString title       = d->metadataTemplate.templateTitle();

    if      (title == Template::removeTemplateTitle())
    {
        d->templateCombo->setCurrentIndex(REMOVETEMPLATE);
    }
    else if (title.isEmpty())
    {
        d->templateCombo->setCurrentIndex(DONTCHANGE);
    }

    d->templateCombo->setCurrent(title);
}

int TemplateSelector::getTemplateIndex() const
{
    return d->templateCombo->currentIndex();
}

void TemplateSelector::setTemplateIndex(int i)
{
    d->templateCombo->setCurrentIndex(i);
}

void TemplateSelector::slotOpenSetup()
{
    Setup::execTemplateEditor(this, d->metadataTemplate);
}

void TemplateSelector::slotTemplateListChanged()
{
    populateTemplates();
}

} // namespace Digikam

#include "moc_templateselector.cpp"