Messagelib

comboboxutils.cpp
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#include "utils/comboboxutils.h"
10
11#include <QVariant>
12
13#include <QComboBox>
14
15using namespace MessageList::Utils;
16
17void ComboBoxUtils::fillIntegerOptionCombo(QComboBox *combo, const QList<QPair<QString, int>> &optionDescriptors)
18{
19 int val = getIntegerOptionComboValue(combo, -1);
20 combo->clear();
21 int valIdx = -1;
22 int idx = 0;
23
24 QList<QPair<QString, int>>::ConstIterator end(optionDescriptors.end());
25
26 for (QList<QPair<QString, int>>::ConstIterator it = optionDescriptors.constBegin(); it != end; ++it) {
27 if (val == (*it).second) {
28 valIdx = idx;
29 }
30 combo->addItem((*it).first, QVariant((*it).second));
31 ++idx;
32 }
33 if (idx == 0) {
34 combo->addItem(QStringLiteral("-"), QVariant((int)0)); // always default to 0
35 combo->setEnabled(false);
36 } else {
37 if (!combo->isEnabled()) {
38 combo->setEnabled(true);
39 }
40 if (valIdx >= 0) {
41 combo->setCurrentIndex(valIdx);
42 }
43 if (combo->count() == 1) {
44 combo->setEnabled(false); // disable when there is no choice
45 }
46 }
47}
48
50{
51 if (combo->itemData(combo->currentIndex()).toInt() == value) {
52 return;
53 }
54 int index = combo->findData(value);
55 if (index != -1) {
56 combo->setCurrentIndex(index);
57 } else {
58 combo->setCurrentIndex(0); // default
59 }
60}
61
63{
64 const int idx = combo->currentIndex();
65 if (idx < 0) {
66 return defaultValue;
67 }
68
69 QVariant data = combo->itemData(idx);
70 bool ok;
71 const int val = data.toInt(&ok);
72 if (!ok) {
73 return defaultValue;
74 }
75 return val;
76}
void fillIntegerOptionCombo(QComboBox *combo, const QList< QPair< QString, int > > &optionDescriptors)
Fills the specified QComboBox with the options available in optionDescriptors.
void setIntegerOptionComboValue(QComboBox *combo, int value)
Sets the currently selected option in the specified combo.
int getIntegerOptionComboValue(QComboBox *combo, int defaultValue)
Returns the identifier of the currently selected option in the specified combo.
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void clear()
void setCurrentIndex(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
const_iterator constBegin() const const
int toInt(bool *ok) const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.