-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathrytablemodel.cpp
245 lines (225 loc) · 7.25 KB
/
rytablemodel.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
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
#include "rytablemodel.h"
#include "proxy/rypipedata.h"
#include <QVector>
#include <QStringList>
#include <QDebug>
RyTableModel::RyTableModel(QObject *parent) :
QAbstractTableModel(parent),_pipeNumber(0),_maxRequestSize(30){
}
RyTableModel::~RyTableModel(){
blockSignals(true);
removeAllItem();
qDebug()<<"~RyTableModel";
}
int RyTableModel::rowCount( const QModelIndex & ) const{
return pipesVector.count();
}
int RyTableModel::columnCount(const QModelIndex &) const{
return 11;
}
QString rypipeDataGetDataByColumn(RyPipeData_ptr p, int column){
switch(column){
case 0:
return QString::number(p->number);
case 1:
return QString::number(p->socketConnectionId);
case 2:
return ((p->responseStatus.isEmpty())?QString("-"):p->responseStatus);
case 3:
return p->httpVersion;
case 4:
return p->host;
case 5:
return p->serverIp;
case 6:
return p->path;
case 7:
return QString::number(p->responseBodyRawData().size());
case 8:
return p->getResponseHeader("Cache-Control");
case 9:
//qDebug()<<QString::number(p->performances.responseDone)<<QString::number(p->performances.requestBegin);
return QString::number(p->performances.responseDone - p->performances.requestBegin);
/*
return QString::number(p->socketHandle);
case 3:
return ((p->responseStatus.isEmpty())?QString("-"):p->responseStatus);
case 4:
return p->httpVersion;
case 5:
return p->host;
case 6:
return p->serverIp;
case 7:
return p->path;
case 8:
return QString::number(p->responseBodyRawData().size());
case 9:
return p->getResponseHeader("Cache-Control");
case 10:
qDebug()<<QString::number(p->performances.responseDone)<<QString::number(p->performances.requestBegin);
return QString::number(p->performances.responseDone - p->performances.requestBegin);
*/
case 10:
return QString::number(p->performances.requestBegin);
default:
return QString("-");
}
}
QVariant RyTableModel::data(const QModelIndex &index, int role) const{
if(role == Qt::DisplayRole || role == Qt::ToolTipRole){
int row = index.row();
int column = index.column();
RyPipeData_ptr p;
if(pipesVector.count()>row){
p = pipesVector.at(row);
}else{
return tr("unknown..%1").arg(row);
}
if(!p){
return tr("unknown..2");
}
return rypipeDataGetDataByColumn(p,column);
}else if(role == Qt::BackgroundColorRole){
if(pipesVector.count()>index.row()){
RyPipeData_ptr d = pipesVector.at(index.row());
if(d->isMatchingRule){
if(d->isContentReplaced){
return Qt::cyan;
}else{
return Qt::darkGreen;
}
//return QVariant((int)Qt::cyan);
}else if(d->responseStatus.startsWith('4') || d->responseStatus.startsWith('5')){
return Qt::lightGray;
//return QVariant((int)Qt::darkCyan);
}
}else{
return QVariant();
}
}else if(role == RyTableModel::RowDataRole){
if(pipesVector.count()>index.row()){
return QVariant(pipesVector.at(index.row()));
}else{
return QVariant();
}
}else{
return QVariant();
}
}
QVariant RyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
//TODO
QStringList headers;
headers<<"#"<<"#2(socket)"<<"Result"<<"Protocol"<<"Host"<<"ServerIP"<<"URL"<<"Body"<<"Caching"<<"all time"<<"begin time";
if (orientation == Qt::Horizontal) {
if(section< headers.size() ){
return headers[section];
}else{
return QString("custom");
}
}
return QVariant();
}
Qt::ItemFlags RyTableModel::flags(const QModelIndex &index) const{
if(!index.isValid()){
return Qt::ItemIsEnabled;
}
return QAbstractTableModel::flags(index);
}
bool RyTableModel::itemLessThan(RyPipeData_ptr left,int leftColumn,RyPipeData_ptr right,int rightColumn){
if(left.isNull() || right.isNull()){
return false;
}
QString leftData = rypipeDataGetDataByColumn(left,leftColumn) ;
QString rightData = rypipeDataGetDataByColumn(right,rightColumn);
bool isNumber=false;
long long leftNumber = leftData.toLongLong(&isNumber);
long long rightNumber;
if(isNumber){
rightNumber = rightData.toLongLong(&isNumber);
if(isNumber){
return leftNumber<rightNumber;
}
}
return leftData < rightData;
}
bool RyTableModel::itemLessThan(const QModelIndex &left, const QModelIndex &right){
return itemLessThan(getItem(left.row()),left.column(),getItem(right.row()),right.column());
}
RyPipeData_ptr RyTableModel::getItem(int row){
//qDebug()<<pipesVector.size()<<row;
if(pipesVector.size() > row){
return pipesVector.at(row);
}
qDebug()<<QString("getItem invalid row:%1").arg(row);
return RyPipeData_ptr();
}
void RyTableModel::updateItem(RyPipeData_ptr p){
int i = pipesMap.keys().indexOf(p->id);
if(i!=-1){
RyPipeData_ptr ori = pipesMap[p->id];
pipesMap[p->id] = p;
int j = pipesVector.indexOf(ori);
if(j!=-1){
pipesVector.replace(j,p);
}
emit dataChanged(index(i,0),index(i,columnCount()-1));
emit connectionUpdated(p);
}
}
void RyTableModel::addItem(RyPipeData_ptr p){
if(pipesVector.size() > _maxRequestSize){
beginRemoveRows(QModelIndex(),0,0);
RyPipeData_ptr itemToRemove = pipesVector.at(0);
pipesVector.remove(0);
pipesMap.remove(itemToRemove->id);
endRemoveRows();
qDebug()<<"removing..";
}
//qDebug()<<"addItem...."<<p->getRequestHeader(QByteArray("Host"))<<pipesVector.count();
RyPipeData_ptr p1 = p;
++_pipeNumber;
int l = pipesVector.size();
/*
if(l==0){
_pipeNumber = 1;
}
*/
p1->number=_pipeNumber;
this->beginInsertRows(index(l, 0),l,l);
//TODO thread safe?
pipesMap[p1->id] = p1;
pipesVector.append(p1);
this->endInsertRows();
emit connectionAdded(p);
}
void RyTableModel::removeAllItem(){
beginResetModel();
pipesMap.clear();
pipesVector.clear();
//qDebug()<<"length="<<pipesVector.count();
endResetModel();
}
void RyTableModel::removeItems(){
this->removeAllItem();
}
void RyTableModel::setMaxRequestSize(int maxSize){
qDebug()<<"max size "<<maxSize;
_maxRequestSize = maxSize;
int currentSize = pipesVector.size();
if(currentSize > maxSize){
beginRemoveRows(QModelIndex(),0,_maxRequestSize);
int d = currentSize - maxSize;
qDebug()<<"remove n="<<d;
while( d-- > 0 ){
RyPipeData_ptr itemToRemove = pipesVector.at(0);
pipesMap.remove(itemToRemove->id);
pipesVector.remove(0);
}
endRemoveRows();
}
this->submit();
}