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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* astringlist_list_model.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QSortFilterProxyModel>
#include <QStringList>
#include <QPalette>
#include <QApplication>
#include <QBrush>
#include <ui/qt/models/astringlist_list_model.h>
#include <ui/qt/utils/color_utils.h>
AStringListListModel::AStringListListModel(QObject * parent):
QAbstractTableModel(parent)
{}
AStringListListModel::~AStringListListModel() { display_data_.clear(); }
void AStringListListModel::appendRow(const QStringList & display_strings, const QString & row_tooltip, const QModelIndex &parent)
{
QStringList columns = headerColumns();
if (display_strings.count() != columns.count())
return;
emit beginInsertRows(parent, rowCount(), rowCount());
display_data_ << display_strings;
tooltip_data_ << row_tooltip;
emit endInsertRows();
}
int AStringListListModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(display_data_.count());
}
int AStringListListModel::columnCount(const QModelIndex &parent) const
{
if (rowCount(parent) == 0)
return 0;
return static_cast<int>(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 QVariant::fromValue(columns[section]);
return QVariant();
}
QVariant AStringListListModel::data(const QModelIndex &index, int role) const
{
if (! index.isValid() || index.row() >= rowCount())
return QVariant();
if (role == Qt::DisplayRole)
{
QStringList data = display_data_.at(index.row());
if (index.column() < columnCount())
return QVariant::fromValue(data.at(index.column()));
}
else if (role == Qt::ToolTipRole)
{
QString tooltip = tooltip_data_.at(index.row());
if (!tooltip.isEmpty()) {
return tooltip;
}
}
return QVariant();
}
AStringListListSortFilterProxyModel::AStringListListSortFilterProxyModel(QObject * parent)
: QSortFilterProxyModel(parent)
{
filter_ = QString();
types_[-1] = FilterByContains;
}
bool AStringListListSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QString leftData = left.data().toString();
QString rightData = right.data().toString();
if (numericColumns_.contains(left.column()) || numericColumns_.contains(right.column()) )
{
float leftD = leftData.toFloat();
float rightD = rightData.toFloat();
return leftD < rightD;
}
return leftData.compare(rightData, sortCaseSensitivity()) < 0;
}
void AStringListListSortFilterProxyModel::setFilter(const QString & filter)
{
filter_ = filter;
invalidateFilter();
}
static bool AContainsB(const QVariant &a, const QVariant &b, Qt::CaseSensitivity cs)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (! a.canConvert<QString>() || ! b.canConvert<QString>())
#else
if (! a.canConvert(QVariant::String) || ! b.canConvert(QVariant::String))
#endif
return false;
return a.toString().contains(b.toString(), cs);
}
static bool AStartsWithB(const QVariant &a, const QVariant &b, Qt::CaseSensitivity cs)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (! a.canConvert<QString>() || ! b.canConvert<QString>())
#else
if (! a.canConvert(QVariant::String) || ! b.canConvert(QVariant::String))
#endif
return false;
return a.toString().startsWith(b.toString(), cs);
}
static bool AIsEquivalentToB(const QVariant &a, const QVariant &b, Qt::CaseSensitivity)
{
return a == b;
}
bool AStringListListSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
if (columnsToFilter_.count() == 0)
return true;
foreach(int column, columnsToFilter_)
{
if (column >= columnCount())
continue;
QModelIndex chkIdx = sourceModel()->index(sourceRow, column, sourceParent);
QString dataString = chkIdx.data().toString();
/* Default is filter by string a contains string b */
bool (*compareFunc)(const QVariant&, const QVariant&, Qt::CaseSensitivity) = AContainsB;
if (types_.keys().contains(column))
{
switch (types_.value(column, FilterByContains))
{
case FilterByStart:
compareFunc = AStartsWithB;
break;
case FilterByEquivalent:
compareFunc = AIsEquivalentToB;
break;
case FilterNone:
return true;
default:
compareFunc = AContainsB;
break;
}
}
if (compareFunc(dataString, filter_, filterCaseSensitivity()))
return true;
}
return false;
}
void AStringListListSortFilterProxyModel::setFilterType(AStringListListFilterType type, int column)
{
if (column >= -1 && column < columnCount())
{
if (! types_.keys().contains(column))
{
types_.insert(column, type);
invalidateFilter();
}
else if (types_.keys().contains(column) && type != types_[column])
{
types_[column] = type;
invalidateFilter();
}
}
}
void AStringListListSortFilterProxyModel::setColumnToFilter(int column)
{
if (column < columnCount() && ! columnsToFilter_.contains(column))
{
columnsToFilter_.append(column);
invalidateFilter();
}
}
void AStringListListSortFilterProxyModel::setColumnsToFilter(QList<int> columns)
{
bool hasBeenAdded = false;
foreach (int column, columns) {
if (column < columnCount() && ! columnsToFilter_.contains(column)) {
columnsToFilter_.append(column);
hasBeenAdded = true;
}
}
if (hasBeenAdded)
invalidateFilter();
}
void AStringListListSortFilterProxyModel::clearColumnsToFilter()
{
columnsToFilter_.clear();
invalidateFilter();
}
void AStringListListSortFilterProxyModel::clearHiddenColumns()
{
hiddenColumns_.clear();
invalidateFilter();
}
void AStringListListSortFilterProxyModel::setColumnToHide(int col)
{
if (! hiddenColumns_.contains(col) && col > -1 && sourceModel() && sourceModel()->columnCount() > col)
{
hiddenColumns_ << col;
invalidateFilter();
}
}
bool AStringListListSortFilterProxyModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &sourceParent) const
{
QModelIndex realIndex = sourceModel()->index(0, sourceColumn, sourceParent);
if (! realIndex.isValid())
return false;
if (hiddenColumns_.contains(sourceColumn))
return false;
return true;
}
void AStringListListSortFilterProxyModel::clearNumericColumns()
{
numericColumns_.clear();
invalidateFilter();
}
void AStringListListSortFilterProxyModel::setColumnAsNumeric(int col)
{
if (! numericColumns_.contains(col) && col > -1 && sourceModel() && sourceModel()->columnCount() > col)
{
numericColumns_ << col;
invalidateFilter();
}
}
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 (role == Qt::ForegroundRole && urls_.contains(index.column())
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
&& result.canConvert<QBrush>())
#else
&& result.canConvert(QVariant::Brush))
#endif
{
QBrush selected = result.value<QBrush>();
selected.setColor(ColorUtils::themeLinkBrush().color());
return selected;
}
return result;
}
|