File indexing completed on 2022-09-27 13:43:07

0001 /* ============================================================
0002  *
0003  * This file is a part of KDE project
0004  *
0005  *
0006  * Date        : 2009-07-05
0007  * Description : A combobox which also has an intermediate state.
0008  *
0009  * Copyright (C) 2009 by Pieter Edelman <pieter dot edelman at gmx dot net>
0010  *
0011  * This program is free software; you can redistribute it
0012  * and/or modify it under the terms of the GNU General
0013  * Public License as published by the Free Software Foundation;
0014  * either version 2, or (at your option) any later version.
0015  *
0016  * This program is distributed in the hope that it will be useful,
0017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0019  * GNU General Public License for more details.
0020  *
0021  * ============================================================ */
0022 
0023 #include "comboboxintermediate.h"
0024 
0025 // Qt includes
0026 
0027 #include <QComboBox>
0028 #include <QString>
0029 #include <QVariant>
0030 
0031 namespace KIPIFlickrPlugin
0032 {
0033 
0034 ComboBoxIntermediate::ComboBoxIntermediate(QWidget* const parent, const QString& text)
0035     : QComboBox(parent),
0036       m_isIntermediate(false),
0037       m_intermediateText(text)
0038 {
0039     // Whenever the signal changes, there's a chance that the combobox should
0040     // be changed from intermediate to normal.
0041     connect(this, SIGNAL(currentIndexChanged(int)),
0042             this, SLOT(slotIndexChanged(int)));
0043 }
0044 
0045 ComboBoxIntermediate::~ComboBoxIntermediate()
0046 {
0047 }
0048 
0049 void ComboBoxIntermediate::setIntermediate(bool state)
0050 {
0051     if ((state) && (!m_isIntermediate))
0052     {
0053         // If the combobox should be set to intermediate and is not yet done so,
0054         // append a separator and the intermediate text.
0055         insertSeparator(count());
0056         addItem(m_intermediateText, QVariant(-1));
0057 
0058         // Set the combobox to the intermediate index, while avoiding that it is
0059         // directly unset by the currentIndexChanged signal.
0060         blockSignals(true);
0061         setCurrentIndex(count() - 1);
0062         blockSignals(false);
0063 
0064         m_isIntermediate = true;
0065     }
0066     else if ((!state) && (m_isIntermediate))
0067     {
0068         // If the intermediate state should be removed, simply remove the latest
0069         // two items, the intermediate text and the separator.
0070         removeItem(count() - 1);
0071         removeItem(count() - 1);
0072         m_isIntermediate = false;
0073     }
0074 }
0075 
0076 void ComboBoxIntermediate::slotIndexChanged(int)
0077 {
0078     if (m_isIntermediate)
0079     {
0080         setIntermediate(false);
0081     }
0082 }
0083 
0084 } // namespace KIPIFlickrPlugin