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  QVector<QPair<QString, int>>::ConstIterator end(optionDescriptors.end());
25 
26  for (QVector<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 clear()
void setIntegerOptionComboValue(QComboBox *combo, int value)
Sets the currently selected option in the specified combo.
void addItem(const QString &text, const QVariant &userData)
void setEnabled(bool)
int toInt(bool *ok) const const
QVariant itemData(int index, int role) const const
int getIntegerOptionComboValue(QComboBox *combo, int defaultValue)
Returns the identifier of the currently selected option in the specified combo.
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
void fillIntegerOptionCombo(QComboBox *combo, const QVector< QPair< QString, int >> &optionDescriptors)
Fills the specified QComboBox with the options available in optionDescriptors.
void setCurrentIndex(int index)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Oct 24 2021 23:14:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.