2017-11-22 15:54:32 +00:00
|
|
|
/* astringlist_list_model.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QStringList>
|
2017-11-24 14:46:30 +00:00
|
|
|
#include <QPalette>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QBrush>
|
2017-11-22 15:54:32 +00:00
|
|
|
|
|
|
|
#include <ui/qt/models/astringlist_list_model.h>
|
|
|
|
|
|
|
|
AStringListListModel::AStringListListModel(QObject * parent):
|
2017-11-24 14:46:30 +00:00
|
|
|
QAbstractTableModel(parent)
|
2017-11-22 15:54:32 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
AStringListListModel::~AStringListListModel() { modelData.clear(); }
|
|
|
|
|
|
|
|
void AStringListListModel::appendRow(const QStringList & data, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
QStringList columns = headerColumns();
|
|
|
|
if ( data.count() != columns.count() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit beginInsertRows(parent, rowCount(), rowCount());
|
|
|
|
modelData << data;
|
|
|
|
emit endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
int AStringListListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
return modelData.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int AStringListListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
return headerColumns().count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AStringListListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if ( orientation == Qt::Vertical )
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
QStringList columns = headerColumns();
|
|
|
|
if ( role == Qt::DisplayRole && section < columns.count() )
|
|
|
|
return qVariantFromValue(columns[section]);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AStringListListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2017-11-24 14:46:30 +00:00
|
|
|
if ( ! index.isValid() || index.row() >= rowCount() )
|
2017-11-22 15:54:32 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
if ( role == Qt::DisplayRole )
|
|
|
|
{
|
|
|
|
QStringList data = modelData.at(index.row());
|
2017-11-22 15:54:32 +00:00
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
if ( index.column() < columnCount() )
|
|
|
|
return qVariantFromValue(data.at(index.column()));
|
|
|
|
}
|
2017-11-22 15:54:32 +00:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
AStringListListSortFilterProxyModel::AStringListListSortFilterProxyModel(QObject * parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
filter_ = QString();
|
|
|
|
type_ = FilterByContains;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AStringListListSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
|
|
|
QString leftData = sourceModel()->data(left).toStringList().join(",");
|
|
|
|
QString rightData = sourceModel()->data(right).toStringList().join(",");
|
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
return leftData.compare(rightData, sortCaseSensitivity()) < 0;
|
2017-11-22 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AStringListListSortFilterProxyModel::setFilter(const QString & filter)
|
|
|
|
{
|
|
|
|
filter_ = filter;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
static bool AContainsB(const QString &a, const QString &b, Qt::CaseSensitivity cs)
|
2017-11-22 15:54:32 +00:00
|
|
|
{
|
2017-11-24 14:46:30 +00:00
|
|
|
return a.contains(b, cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AStartsWithB(const QString &a, const QString &b, Qt::CaseSensitivity cs)
|
|
|
|
{
|
|
|
|
return a.startsWith(b, cs);
|
|
|
|
}
|
2017-11-22 15:54:32 +00:00
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
bool AStringListListSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
|
|
|
{
|
2017-11-22 15:54:32 +00:00
|
|
|
if ( columnsToFilter_.count() == 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
foreach(int column, columnsToFilter_)
|
|
|
|
{
|
|
|
|
if ( column >= columnCount() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QModelIndex chkIdx = sourceModel()->index(sourceRow, column, sourceParent);
|
2017-11-24 14:46:30 +00:00
|
|
|
QString dataString = sourceModel()->data(chkIdx).toString();
|
|
|
|
|
|
|
|
/* Default is filter by string a contains string b */
|
|
|
|
bool (*compareFunc)(const QString&, const QString&, Qt::CaseSensitivity) = AContainsB;
|
|
|
|
if ( type_ == FilterByStart )
|
|
|
|
compareFunc = AStartsWithB;
|
2017-11-22 15:54:32 +00:00
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
if ( compareFunc(dataString, filter_, filterCaseSensitivity()) )
|
|
|
|
return true;
|
2017-11-22 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
return false;
|
2017-11-22 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AStringListListSortFilterProxyModel::setFilterType(AStringListListFilterType type)
|
|
|
|
{
|
|
|
|
if ( type != type_ )
|
|
|
|
{
|
|
|
|
type_ = type;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AStringListListSortFilterProxyModel::setColumnToFilter(int column)
|
|
|
|
{
|
|
|
|
if ( column < columnCount() && ! columnsToFilter_.contains(column) )
|
|
|
|
{
|
|
|
|
columnsToFilter_.append(column);
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AStringListListSortFilterProxyModel::clearColumnsToFilter()
|
|
|
|
{
|
|
|
|
columnsToFilter_.clear();
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2017-11-24 14:46:30 +00:00
|
|
|
AStringListListUrlProxyModel::AStringListListUrlProxyModel(QObject * parent):
|
|
|
|
QIdentityProxyModel(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void AStringListListUrlProxyModel::setUrlColumn(int column)
|
|
|
|
{
|
|
|
|
if ( column < columnCount() && ! urls_.contains(column) )
|
|
|
|
urls_ << column;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AStringListListUrlProxyModel::isUrlColumn(int column) const
|
|
|
|
{
|
|
|
|
return urls_.contains(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AStringListListUrlProxyModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
QVariant result = QIdentityProxyModel::data(index, role);
|
|
|
|
|
|
|
|
if ( urls_.contains(index.column()) )
|
|
|
|
{
|
2017-11-29 03:00:34 +00:00
|
|
|
if ( role == Qt::ForegroundRole )
|
2017-11-24 14:46:30 +00:00
|
|
|
{
|
|
|
|
if ( result.canConvert(QVariant::Brush) )
|
|
|
|
{
|
|
|
|
QBrush selected = result.value<QBrush>();
|
|
|
|
selected.setColor(QApplication::palette().link().color());
|
|
|
|
return selected;
|
|
|
|
}
|
2017-11-29 03:00:34 +00:00
|
|
|
} else if ( role == Qt::TextColorRole ) {
|
2017-11-24 14:46:30 +00:00
|
|
|
return QApplication::palette().link().color();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-22 15:54:32 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|