Messagelib

comboboxutils.cpp
1 /******************************************************************************
2  *
3  * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <[email protected]>
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 
15 using namespace MessageList::Utils;
16 
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 }
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
void setCurrentIndex(int index)
void setIntegerOptionComboValue(QComboBox *combo, int value)
Sets the currently selected option in the specified combo.
int toInt(bool *ok) const const
void setEnabled(bool)
QVariant itemData(int index, int role) const const
void clear()
void addItem(const QString &text, const QVariant &userData)
void fillIntegerOptionCombo(QComboBox *combo, const QList< QPair< QString, int >> &optionDescriptors)
Fills the specified QComboBox with the options available in optionDescriptors.
int getIntegerOptionComboValue(QComboBox *combo, int defaultValue)
Returns the identifier of the currently selected option in the specified combo.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 04:03:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.