Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / qt / extcap_argument_multiselect.cpp
blobc4eae9e1af54307d73407570662994da2d238751
1 /* extcap_argument_multiselect.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include <extcap_argument.h>
11 #include <extcap_argument_file.h>
13 #include <wsutil/utf8_entities.h>
15 #include <QObject>
16 #include <QWidget>
17 #include <QLabel>
18 #include <QLineEdit>
19 #include <QBoxLayout>
20 #include <QPushButton>
21 #include <QVariant>
23 #include <epan/prefs.h>
24 #include <ui/qt/utils/color_utils.h>
26 #include <extcap_parser.h>
27 #include <extcap_argument_multiselect.h>
29 ExtArgMultiSelect::ExtArgMultiSelect(extcap_arg * argument, QObject *parent) :
30 ExtcapArgument(argument, parent), treeView(0), viewModel(0) {}
32 ExtArgMultiSelect::~ExtArgMultiSelect()
34 if (treeView != 0)
35 delete treeView;
36 if (viewModel != 0)
37 delete viewModel;
40 QList<QStandardItem *> ExtArgMultiSelect::valueWalker(ExtcapValueList list, QStringList &defaults)
42 ExtcapValueList::iterator iter = list.begin();
43 QList<QStandardItem *> items;
45 while (iter != list.end())
47 QStandardItem * item = new QStandardItem((*iter).value());
48 if ((*iter).enabled() == false)
50 item->setCheckable(false);
52 else
54 item->setCheckable(true);
57 item->setData((*iter).call(), Qt::UserRole);
58 if ((*iter).isDefault())
59 defaults << (*iter).call();
61 item->setSelectable(false);
62 item->setEditable(false);
63 QList<QStandardItem *> childs = valueWalker((*iter).children(), defaults);
64 if (childs.length() > 0)
65 item->appendRows(childs);
67 items << item;
68 ++iter;
71 return items;
74 void ExtArgMultiSelect::checkItemsWalker(QStandardItem * item, QStringList defaults)
76 QModelIndexList results;
77 QModelIndex index;
79 if (item->hasChildren())
81 for (int row = 0; row < item->rowCount(); row++)
83 QStandardItem * child = item->child(row);
84 if (child != 0)
86 checkItemsWalker(child, defaults);
91 QString data = item->data(Qt::UserRole).toString();
93 if (defaults.contains(data))
95 item->setCheckState(Qt::Checked);
96 index = item->index();
97 while (index.isValid())
99 treeView->setExpanded(index, true);
100 index = index.parent();
102 } else if (item->isCheckable()) {
103 item->setCheckState(Qt::Unchecked);
107 QWidget * ExtArgMultiSelect::createEditor(QWidget * parent)
109 QStringList checked;
111 QList<QStandardItem *> items = valueWalker(values, checked);
112 if (items.length() == 0)
113 return new QWidget();
115 /* Value can be empty if no items are checked */
116 if (_argument->pref_valptr && (*_argument->pref_valptr))
118 checked = QString(*_argument->pref_valptr).split(",", Qt::SkipEmptyParts);
121 viewModel = new QStandardItemModel();
122 QList<QStandardItem *>::const_iterator iter = items.constBegin();
123 while (iter != items.constEnd())
125 ((QStandardItemModel *)viewModel)->appendRow((*iter));
126 ++iter;
129 treeView = new QTreeView(parent);
130 treeView->setModel(viewModel);
132 /* Shows at minimum 6 entries at most desktops */
133 treeView->setMinimumHeight(100);
134 treeView->setHeaderHidden(true);
135 treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
136 treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
138 for (int row = 0; row < viewModel->rowCount(); row++)
139 checkItemsWalker(((QStandardItemModel*)viewModel)->item(row), checked);
141 connect (viewModel,
142 SIGNAL(itemChanged(QStandardItem *)),
143 SLOT(itemChanged(QStandardItem *)));
145 return treeView;
148 QString ExtArgMultiSelect::value()
150 if (viewModel == 0)
151 return QString();
153 QStringList result;
154 QModelIndexList checked = viewModel->match(viewModel->index(0, 0), Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly | Qt::MatchRecursive);
155 if (checked.size() <= 0)
156 return QString();
158 QModelIndexList::const_iterator iter = checked.constBegin();
159 while (iter != checked.constEnd())
161 QModelIndex index = (QModelIndex)(*iter);
163 result << viewModel->data(index, Qt::UserRole).toString();
165 ++iter;
168 return result.join(QString(','));
171 void ExtArgMultiSelect::itemChanged(QStandardItem *)
173 emit valueChanged();
176 bool ExtArgMultiSelect::isValid()
178 bool valid = true;
180 if (isRequired())
182 if (viewModel == 0)
183 valid = false;
184 else
186 QModelIndexList checked = viewModel->match(viewModel->index(0, 0), Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly | Qt::MatchRecursive);
187 if (checked.size() <= 0)
188 valid = false;
192 QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
193 QString txtStyle("QTreeView { background-color: %1; } ");
194 if (viewModel != 0)
195 treeView->setStyleSheet(txtStyle.arg(valid ? QString("") : lblInvalidColor));
197 return valid;
200 QString ExtArgMultiSelect::defaultValue()
202 QStringList checked;
204 QList<QStandardItem *> items = valueWalker(values, checked);
206 return checked.join(QString(','));
209 bool ExtArgMultiSelect::isSetDefaultValueSupported()
211 return true;
214 void ExtArgMultiSelect::setDefaultValue()
216 QStringList checked;
218 checked = defaultValue().split(",", Qt::SkipEmptyParts);
219 for (int row = 0; row < viewModel->rowCount(); row++)
220 checkItemsWalker(((QStandardItemModel*)viewModel)->item(row), checked);