File indexing completed on 2025-04-27 03:58:21

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-08-16
0007  * Description : a combo box widget re-implemented with a
0008  *               reset button to switch to a default item
0009  *
0010  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "dcombobox.h"
0017 
0018 // Qt includes
0019 
0020 #include <QApplication>
0021 #include <QStyle>
0022 #include <QToolButton>
0023 #include <QHBoxLayout>
0024 #include <QIcon>
0025 
0026 // KDE includes
0027 
0028 #include <klocalizedstring.h>
0029 
0030 namespace Digikam
0031 {
0032 
0033 class Q_DECL_HIDDEN DComboBox::Private
0034 {
0035 
0036 public:
0037 
0038     explicit Private()
0039       : defaultIndex(0),
0040         resetButton (nullptr),
0041         combo       (nullptr)
0042     {
0043     }
0044 
0045     int          defaultIndex;
0046 
0047     QToolButton* resetButton;
0048     QComboBox*   combo;
0049 };
0050 
0051 DComboBox::DComboBox(QWidget* const parent)
0052     : QWidget(parent),
0053       d      (new Private)
0054 {
0055     QHBoxLayout* const hlay = new QHBoxLayout(this);
0056     d->combo                = new QComboBox(this);
0057     d->resetButton          = new QToolButton(this);
0058     d->resetButton->setAutoRaise(true);
0059     d->resetButton->setFocusPolicy(Qt::NoFocus);
0060     d->resetButton->setIcon(QIcon::fromTheme(QLatin1String("document-revert")));
0061     d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value"));
0062 
0063     hlay->addWidget(d->combo);
0064     hlay->addWidget(d->resetButton);
0065     hlay->setStretchFactor(d->combo, 10);
0066     hlay->setContentsMargins(QMargins());
0067     hlay->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0068                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0069 
0070     // -------------------------------------------------------------
0071 
0072     connect(d->resetButton, &QToolButton::clicked,
0073             this, &DComboBox::slotReset);
0074 
0075     connect(d->combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
0076             this, &DComboBox::slotItemActivated);
0077 
0078     connect(d->combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
0079             this, &DComboBox::slotCurrentIndexChanged);
0080 }
0081 
0082 DComboBox::~DComboBox()
0083 {
0084     delete d;
0085 }
0086 
0087 QComboBox* DComboBox::combo() const
0088 {
0089     return d->combo;
0090 }
0091 
0092 void DComboBox::addItem(const QString& t, const QVariant& data)
0093 {
0094     d->combo->addItem(t, data);
0095 }
0096 
0097 void DComboBox::insertItem(int index, const QString& t, const QVariant& data)
0098 {
0099     d->combo->insertItem(index, t, data);
0100 }
0101 
0102 int DComboBox::currentIndex() const
0103 {
0104     return d->combo->currentIndex();
0105 }
0106 
0107 void DComboBox::setCurrentIndex(int v)
0108 {
0109     d->combo->setCurrentIndex(v);
0110 }
0111 
0112 int DComboBox::defaultIndex() const
0113 {
0114     return d->defaultIndex;
0115 }
0116 
0117 void DComboBox::setDefaultIndex(int v)
0118 {
0119     d->defaultIndex = v;
0120     d->combo->setCurrentIndex(d->defaultIndex);
0121     slotItemActivated(v);
0122 }
0123 
0124 void DComboBox::slotReset()
0125 {
0126     d->combo->setCurrentIndex(d->defaultIndex);
0127     d->resetButton->setEnabled(false);
0128     slotItemActivated(d->defaultIndex);
0129     Q_EMIT reset();
0130 }
0131 
0132 void DComboBox::slotItemActivated(int v)
0133 {
0134     d->resetButton->setEnabled(v != d->defaultIndex);
0135     Q_EMIT activated(v);
0136 }
0137 
0138 void DComboBox::slotCurrentIndexChanged(int v)
0139 {
0140     d->resetButton->setEnabled(v != d->defaultIndex);
0141     Q_EMIT currentIndexChanged(v);
0142 }
0143 
0144 } // namespace Digikam
0145 
0146 #include "moc_dcombobox.cpp"