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
10 #include <extcap_argument.h>
11 #include <extcap_argument_file.h>
13 #include <wsutil/utf8_entities.h>
20 #include <QPushButton>
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()
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);
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
);
74 void ExtArgMultiSelect::checkItemsWalker(QStandardItem
* item
, QStringList defaults
)
76 QModelIndexList results
;
79 if (item
->hasChildren())
81 for (int row
= 0; row
< item
->rowCount(); row
++)
83 QStandardItem
* child
= item
->child(row
);
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
)
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
));
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
);
142 SIGNAL(itemChanged(QStandardItem
*)),
143 SLOT(itemChanged(QStandardItem
*)));
148 QString
ExtArgMultiSelect::value()
154 QModelIndexList checked
= viewModel
->match(viewModel
->index(0, 0), Qt::CheckStateRole
, Qt::Checked
, -1, Qt::MatchExactly
| Qt::MatchRecursive
);
155 if (checked
.size() <= 0)
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();
168 return result
.join(QString(','));
171 void ExtArgMultiSelect::itemChanged(QStandardItem
*)
176 bool ExtArgMultiSelect::isValid()
186 QModelIndexList checked
= viewModel
->match(viewModel
->index(0, 0), Qt::CheckStateRole
, Qt::Checked
, -1, Qt::MatchExactly
| Qt::MatchRecursive
);
187 if (checked
.size() <= 0)
192 QString lblInvalidColor
= ColorUtils::fromColorT(prefs
.gui_text_invalid
).name();
193 QString
txtStyle("QTreeView { background-color: %1; } ");
195 treeView
->setStyleSheet(txtStyle
.arg(valid
? QString("") : lblInvalidColor
));
200 QString
ExtArgMultiSelect::defaultValue()
204 QList
<QStandardItem
*> items
= valueWalker(values
, checked
);
206 return checked
.join(QString(','));
209 bool ExtArgMultiSelect::isSetDefaultValueSupported()
214 void ExtArgMultiSelect::setDefaultValue()
218 checked
= defaultValue().split(",", Qt::SkipEmptyParts
);
219 for (int row
= 0; row
< viewModel
->rowCount(); row
++)
220 checkItemsWalker(((QStandardItemModel
*)viewModel
)->item(row
), checked
);