-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmysortfilterproxymodel.cpp
199 lines (182 loc) · 6.38 KB
/
cmysortfilterproxymodel.cpp
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
#include "cmysortfilterproxymodel.h"
#include <QStandardItemModel>
CMySortFilterProxyModel::CMySortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel{parent}
{
}
#define TABLE_HEADER_SEQ_NO 0
#define TABLE_HEADER_TIME 1
#define TABLE_HEADER_LEN 6
bool CMySortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (!left.isValid() || !right.isValid())
return false;
// 从原始数据源中获取数据
QVariant leftData = sourceModel()->data(left);
QVariant rightData = sourceModel()->data(right);
// seq-no和长度列按整数排序
if (left.column() == CMySortFilterProxyModel::seqNo || left.column() == CMySortFilterProxyModel::length)
{
if (leftData.canConvert<quint32>() && rightData.canConvert<quint32>())
{
quint32 left = leftData.toUInt();
quint32 right = rightData.toUInt();
return left < right ? true : false;
}
}
else if (left.column() == CMySortFilterProxyModel::time)
{
if (leftData.canConvert<double>() && rightData.canConvert<double>())
{
double left = leftData.toDouble();
double right = rightData.toDouble();
return left < right ? true : false;
}
/*
if (leftData.toString().contains(QRegularExpression("[\\x4e00-\\x9f5a]+")))
{
qDebug() << "有汉字" << leftData.toString();
}
*/
}
return QSortFilterProxyModel::lessThan(left, right);
}
// 主窗口设置具体的条件
// 例如:0列 >= 100,1列 == 80,所有条件组合关系为 AND。
void CMySortFilterProxyModel::setFilter(int column, QString oper, QString value)
{
m_filterOper[column] = oper; // 阈值
m_filterValue[column] = value; // 操作符
}
void CMySortFilterProxyModel::clearFilter()
{
m_filterOper.clear();
m_filterValue.clear();
}
bool CMySortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
// 转换sourceModel
QStandardItemModel *srcModel = dynamic_cast<QStandardItemModel *>(sourceModel());
foreach (int column, m_filterValue.keys())
{
QStandardItem *item = srcModel->item(sourceRow, column);
if (item)
{
// 序列号#0,长度#6为整数
if (column == 0 || column == 6)
{
quint32 value = 0;
// 获取列对应的阈值
value = m_filterValue[column].toUInt();
// 根据列操作符判断
if (m_filterOper[column] == ">")
{
if (item->text().toUInt() <= value)
return false;
}
else if (m_filterOper[column] == "<")
{
if (item->text().toUInt() >= value)
return false;
}
else if (m_filterOper[column] == "==")
{
if (item->text().toUInt() != value)
return false;
}
else if (m_filterOper[column] == ">=")
{
if (item->text().toUInt() < value)
return false;
}
else if (m_filterOper[column] == "<=")
{
if (item->text().toUInt() > value)
return false;
}
else if (m_filterOper[column] == "!=")
{
if (item->text().toUInt() == value)
return false;
}
else
{
qDebug() << "exception: unknown oper: " << m_filterOper[column];
}
}
else if (column == 1)
{
// 时间#1为实数
double value = m_filterValue[column].toDouble();
// 根据列操作符判断
if (m_filterOper[column] == ">")
{
if (item->text().toDouble() <= value)
return false;
}
else if (m_filterOper[column] == "<")
{
if (item->text().toDouble() >= value)
return false;
}
else if (m_filterOper[column] == "==")
{
if (item->text().toDouble() != value)
return false;
}
else if (m_filterOper[column] == ">=")
{
if (item->text().toDouble() < value)
return false;
}
else if (m_filterOper[column] == "<=")
{
if (item->text().toDouble() > value)
return false;
}
else if (m_filterOper[column] == "!=")
{
if (item->text().toDouble() == value)
return false;
}
else
{
qDebug() << "exception: unknown oper: " << m_filterOper[column];
}
}
else if (column == 7)
{
// 信息#7 仅支持操作符"contain"
if (m_filterOper[column] == "contains")
{
QString value = m_filterValue[column];
if (!item->text().contains(value))
return false;
}
else
{
qDebug() << "exception: unknown oper: " << m_filterOper[column];
}
}
else
{
// SA、DA、协议类型、子类型仅支持 ==或者!=,其它操作符忽略。
if (m_filterOper[column] == "==")
{
if (item->text() != m_filterValue[column])
return false;
}
else if (m_filterOper[column] == "!=")
{
if (item->text() == m_filterValue[column])
return false;
}
else
{
qDebug() << "not support operator" << m_filterOper[column] << "for column:" << column;
}
}
}
}
return true;
}