Qt: Filter TrafficListTypes

Add a textbox to allow for filtering the traffic types
in the conversation and endpoint dialog. The current implementation
allows for an easier growth of the list entries. To ensure that
the user can find the entry he/she/they is looking for fast, add
a textbox for filtering
This commit is contained in:
Roland Knall 2022-07-01 09:56:13 +02:00
parent 54aed0aa10
commit 2cebafc613
4 changed files with 60 additions and 4 deletions

View File

@ -63,6 +63,9 @@ TrafficTableDialog::TrafficTableDialog(QWidget &parent, CaptureFile &cf, const Q
connect(ui->absoluteTimeCheckBox, &QCheckBox::toggled, ui->trafficTab, &TrafficTab::useAbsoluteTime);
connect(ui->trafficTab, &TrafficTab::retapRequired, &cap_file_, &CaptureFile::delayedRetapPackets);
connect(ui->trafficListSearch, &QLineEdit::textChanged, ui->trafficList, &TrafficTypesList::filterList);
connect(ui->trafficList, &TrafficTypesList::clearFilterList, ui->trafficListSearch, &QLineEdit::clear);
QPushButton *close_bt = ui->buttonBox->button(QDialogButtonBox::Close);
if (close_bt)
close_bt->setDefault(true);

View File

@ -93,6 +93,19 @@
<item>
<widget class="TrafficTypesList" name="trafficList"/>
</item>
<item>
<widget class="QLineEdit" name="trafficListSearch">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Only show types matching the filter value&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>Filter list for specific type</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -199,6 +199,30 @@ bool TrafficListSortModel::lessThan(const QModelIndex &source_left, const QModel
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
void TrafficListSortModel::setFilter(QString filter)
{
if ( filter.compare(_filter) != 0 ) {
_filter = filter;
invalidateFilter();
}
}
bool TrafficListSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (sourceModel() && _filter.length() > 0) {
QModelIndex idx = sourceModel()->index(source_row, TrafficTypesModel::COL_NAME);
if (idx.isValid()) {
QString name = idx.data().toString();
if (name.contains(_filter, Qt::CaseInsensitive))
return true;
return false;
}
}
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
TrafficTypesList::TrafficTypesList(QWidget *parent) :
QTreeView(parent)
@ -214,11 +238,11 @@ void TrafficTypesList::setProtocolInfo(QString name, GList ** recentList)
{
_name = name;
TrafficListSortModel * sortModel = new TrafficListSortModel();
_sortModel = new TrafficListSortModel();
_model = new TrafficTypesModel(recentList);
sortModel->setSourceModel(_model);
setModel(sortModel);
_sortModel->setSourceModel(_model);
setModel(_sortModel);
setSortingEnabled(true);
sortByColumn(TrafficTypesModel::COL_NAME, Qt::AscendingOrder);
@ -231,8 +255,10 @@ void TrafficTypesList::setProtocolInfo(QString name, GList ** recentList)
void TrafficTypesList::selectProtocols(QList<int> protocols)
{
if (_model)
if (_model) {
_model->selectProtocols(protocols);
emit clearFilterList();
}
}
QList<int> TrafficTypesList::protocols(bool onlySelected) const
@ -250,3 +276,8 @@ QList<int> TrafficTypesList::protocols(bool onlySelected) const
return entries;
}
void TrafficTypesList::filterList(QString filter)
{
_sortModel->setFilter(filter);
}

View File

@ -87,8 +87,14 @@ class TrafficListSortModel : public QSortFilterProxyModel
public:
TrafficListSortModel(QObject * parent = nullptr);
void setFilter(QString filter = QString());
protected:
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
private:
QString _filter;
};
@ -104,13 +110,16 @@ public:
public slots:
void selectProtocols(QList<int> protocols);
void filterList(QString);
signals:
void protocolsChanged(QList<int> protocols);
void clearFilterList();
private:
QString _name;
TrafficTypesModel * _model;
TrafficListSortModel * _sortModel;
};
#endif // TRAFFIC_TYPES_LIST_H