1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* extcap_argument_multiselect.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <extcap_argument.h>
#include <extcap_argument_file.h>
#include <wsutil/utf8_entities.h>
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QBoxLayout>
#include <QPushButton>
#include <QVariant>
#include <epan/prefs.h>
#include <ui/qt/utils/color_utils.h>
#include <extcap_parser.h>
#include <extcap_argument_multiselect.h>
ExtArgMultiSelect::ExtArgMultiSelect(extcap_arg * argument, QObject *parent) :
ExtcapArgument(argument, parent), treeView(0), viewModel(0) {}
ExtArgMultiSelect::~ExtArgMultiSelect()
{
if (treeView != 0)
delete treeView;
if (viewModel != 0)
delete viewModel;
}
QList<QStandardItem *> ExtArgMultiSelect::valueWalker(ExtcapValueList list, QStringList &defaults)
{
ExtcapValueList::iterator iter = list.begin();
QList<QStandardItem *> items;
while (iter != list.end())
{
QStandardItem * item = new QStandardItem((*iter).value());
if ((*iter).enabled() == false)
{
item->setCheckable(false);
}
else
{
item->setCheckable(true);
}
item->setData((*iter).call(), Qt::UserRole);
if ((*iter).isDefault())
defaults << (*iter).call();
item->setSelectable(false);
item->setEditable(false);
QList<QStandardItem *> childs = valueWalker((*iter).children(), defaults);
if (childs.length() > 0)
item->appendRows(childs);
items << item;
++iter;
}
return items;
}
void ExtArgMultiSelect::checkItemsWalker(QStandardItem * item, QStringList defaults)
{
QModelIndexList results;
QModelIndex index;
if (item->hasChildren())
{
for (int row = 0; row < item->rowCount(); row++)
{
QStandardItem * child = item->child(row);
if (child != 0)
{
checkItemsWalker(child, defaults);
}
}
}
QString data = item->data(Qt::UserRole).toString();
if (defaults.contains(data))
{
item->setCheckState(Qt::Checked);
index = item->index();
while (index.isValid())
{
treeView->setExpanded(index, true);
index = index.parent();
}
} else if (item->isCheckable()) {
item->setCheckState(Qt::Unchecked);
}
}
QWidget * ExtArgMultiSelect::createEditor(QWidget * parent)
{
QStringList checked;
QList<QStandardItem *> items = valueWalker(values, checked);
if (items.length() == 0)
return new QWidget();
/* Value can be empty if no items are checked */
if (_argument->pref_valptr && (*_argument->pref_valptr))
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
checked = QString(*_argument->pref_valptr).split(",", Qt::SkipEmptyParts);
#else
checked = QString(*_argument->pref_valptr).split(",", QString::SkipEmptyParts);
#endif
}
viewModel = new QStandardItemModel();
QList<QStandardItem *>::const_iterator iter = items.constBegin();
while (iter != items.constEnd())
{
((QStandardItemModel *)viewModel)->appendRow((*iter));
++iter;
}
treeView = new QTreeView(parent);
treeView->setModel(viewModel);
/* Shows at minimum 6 entries at most desktops */
treeView->setMinimumHeight(100);
treeView->setHeaderHidden(true);
treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
for (int row = 0; row < viewModel->rowCount(); row++)
checkItemsWalker(((QStandardItemModel*)viewModel)->item(row), checked);
connect (viewModel,
SIGNAL(itemChanged(QStandardItem *)),
SLOT(itemChanged(QStandardItem *)));
return treeView;
}
QString ExtArgMultiSelect::value()
{
if (viewModel == 0)
return QString();
QStringList result;
QModelIndexList checked = viewModel->match(viewModel->index(0, 0), Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly | Qt::MatchRecursive);
if (checked.size() <= 0)
return QString();
QModelIndexList::const_iterator iter = checked.constBegin();
while (iter != checked.constEnd())
{
QModelIndex index = (QModelIndex)(*iter);
result << viewModel->data(index, Qt::UserRole).toString();
++iter;
}
return result.join(QString(','));
}
void ExtArgMultiSelect::itemChanged(QStandardItem *)
{
emit valueChanged();
}
bool ExtArgMultiSelect::isValid()
{
bool valid = true;
if (isRequired())
{
if (viewModel == 0)
valid = false;
else
{
QModelIndexList checked = viewModel->match(viewModel->index(0, 0), Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly | Qt::MatchRecursive);
if (checked.size() <= 0)
valid = false;
}
}
QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
QString txtStyle("QTreeView { background-color: %1; } ");
if (viewModel != 0)
treeView->setStyleSheet(txtStyle.arg(valid ? QString("") : lblInvalidColor));
return valid;
}
QString ExtArgMultiSelect::defaultValue()
{
QStringList checked;
QList<QStandardItem *> items = valueWalker(values, checked);
return checked.join(QString(','));
}
bool ExtArgMultiSelect::isSetDefaultValueSupported()
{
return true;
}
void ExtArgMultiSelect::setDefaultValue()
{
QStringList checked;
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
checked = defaultValue().split(",", Qt::SkipEmptyParts);
#else
checked = defaultValue().split(",", QString::SkipEmptyParts);
#endif
for (int row = 0; row < viewModel->rowCount(); row++)
checkItemsWalker(((QStandardItemModel*)viewModel)->item(row), checked);
}
|