File indexing completed on 2025-01-19 03:55:41

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-07-05
0007  * Description : A combobox which also has an intermediate state.
0008  *
0009  * SPDX-FileCopyrightText: 2009      by Pieter Edelman <pieter dot edelman at gmx dot net>
0010  * SPDX-FileCopyrightText: 2010-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 "wscomboboxintermediate.h"
0017 
0018 // Qt includes
0019 
0020 #include <QVariant>
0021 
0022 // KDE includes
0023 
0024 #include <klocalizedstring.h>
0025 
0026 namespace Digikam
0027 {
0028 
0029 class Q_DECL_HIDDEN WSComboBoxIntermediate::Private
0030 {
0031 public:
0032 
0033     explicit Private()
0034         : isIntermediate(false)
0035     {
0036     }
0037 
0038     bool    isIntermediate;
0039     QString intermediateText;
0040 };
0041 
0042 WSComboBoxIntermediate::WSComboBoxIntermediate(QWidget* const parent, const QString& text)
0043     : QComboBox(parent),
0044       d        (new Private)
0045 {
0046     d->intermediateText = text.isNull() ? i18n("Various") : text;
0047 
0048     // Whenever the signal changes, there's a chance that the combobox should
0049     // be changed from intermediate to normal.
0050 
0051     connect(this, SIGNAL(currentIndexChanged(int)),
0052             this, SLOT(slotIndexChanged(int)));
0053 }
0054 
0055 WSComboBoxIntermediate::~WSComboBoxIntermediate()
0056 {
0057     delete d;
0058 }
0059 
0060 void WSComboBoxIntermediate::setIntermediate(bool state)
0061 {
0062     if ((state) && (!d->isIntermediate))
0063     {
0064         // If the combobox should be set to intermediate and is not yet done so,
0065         // append a separator and the intermediate text.
0066 
0067         insertSeparator(count());
0068         addItem(d->intermediateText, QVariant(-1));
0069 
0070         // Set the combobox to the intermediate index, while avoiding that it is
0071         // directly unset by the currentIndexChanged signal.
0072 
0073         blockSignals(true);
0074         setCurrentIndex(count() - 1);
0075         blockSignals(false);
0076 
0077         d->isIntermediate = true;
0078     }
0079     else if ((!state) && (d->isIntermediate))
0080     {
0081         // If the intermediate state should be removed, simply remove the latest
0082         // two items, the intermediate text and the separator.
0083 
0084         removeItem(count() - 1);
0085         removeItem(count() - 1);
0086         d->isIntermediate = false;
0087     }
0088 }
0089 
0090 void WSComboBoxIntermediate::slotIndexChanged(int)
0091 {
0092     if (d->isIntermediate)
0093     {
0094         setIntermediate(false);
0095     }
0096 }
0097 
0098 } // namespace Digikam
0099 
0100 #include "moc_wscomboboxintermediate.cpp"