-
Notifications
You must be signed in to change notification settings - Fork 13
/
traceview.cpp
160 lines (142 loc) · 4.5 KB
/
traceview.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
// SPDX-License-Identifier: GPL-3.0
#include "traceview.h"
#include "tracemodel.h"
#include "ui_traceview.h"
#include "mainwindow.h"
#include "keyeventfilter.h"
#include <QFileDialog>
#include <QFuture>
#include <QStatusBar>
TraceView::TraceView(QWidget *parent) :
QFrame(parent),
ui(new Ui::TraceView)
{
trace_ = nullptr;
lastNode = nullptr;
ui->setupUi(this);
KeyEventFilter::install(this);
KeyEventFilter::install(ui->tvTrace);
ui->tvTrace->setSelectionBehavior (QAbstractItemView::SelectRows);
connect(ui->btOpen, &QAbstractButton::clicked, this, &TraceView::onOpenClick);
connect(ui->txtFilter, &QLineEdit::returnPressed, this, &TraceView::onFilterSubmit);
}
TraceView::~TraceView()
{
delete ui;
}
void TraceView::onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
Q_UNUSED(selected);
Q_UNUSED(deselected);
int no = getPacketNo();
MainWindow::instance->statusBar()->showMessage(QString("Loading packet %1...").arg(no));
watcherNode = new QFutureWatcher<Trace::Node*>;
connect(watcherNode, &QFutureWatcher<Trace::Node*>::finished, [this](){
Trace::Node *sel = futureNode.result();
if (!sel)
return;
if (sel == lastNode)
return;
lastNode = sel;
MainWindow::instance->statusBar()->clearMessage();
emit packetChanged(this);
delete watcherNode;
});
futureNode = QtConcurrent::run([this, no]() {
auto r = trace_->getPacket(no);
return r;
});
watcherNode->setFuture(futureNode);
}
void TraceView::moveSelection(int dir)
{
int row = getRow();
auto tm = ui->tvTrace->model();
if (!tm)
return;
int max = tm->rowCount()-1;
if (row < 0)
row = 0;
else {
row = qBound(0, row+dir, max);
}
auto sm = ui->tvTrace->selectionModel();
if (!sm)
return;
sm->select(ui->tvTrace->model()->index(row, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
int TraceView::getRow()
{
auto sm = ui->tvTrace->selectionModel();
if (!sm)
return -1;
auto sel = sm->selection();
if (sel.isEmpty())
return -1;
return sel[0].topLeft().row();
}
int TraceView::getPacketNo()
{
int no;
int row = getRow();
if (row < 0)
row = 0;
auto tvm = ui->tvTrace->model();
no = tvm->index(row, 0).data().toInt();
return no;
}
void TraceView::onOpenClick(bool checked)
{
Q_UNUSED(checked);
QString fn = QFileDialog::getOpenFileName(this,
tr("Open trace file"), "",
"All Files (*)");
if (fn.isEmpty())
return;
asyncOpen(fn);
}
void TraceView::asyncOpen(QString fn, QString filter)
{
ui->lbName->setText("Loading...");
ui->btOpen->setEnabled(false);
watcherTrace = new QFutureWatcher<Trace*>;
connect(watcherTrace, &QFutureWatcher<Trace*>::finished, [fn, this](){
Trace* oldTrace = this->trace_;
this->trace_ = this->futureTrace.result();
this->lastNode = nullptr;
this->ui->tvTrace->setModel(new TraceModel(this, this));
connect(this->ui->tvTrace->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TraceView::onSelectionChanged);
this->ui->tvTrace->horizontalHeader()->setVisible(true);
int colsize = 50;
this->ui->tvTrace->setColumnWidth(0, colsize); // no
this->ui->tvTrace->setColumnWidth(1, colsize); // time
this->ui->tvTrace->setColumnWidth(2, colsize); // src
this->ui->tvTrace->setColumnWidth(3, colsize); // dst
this->ui->tvTrace->setColumnWidth(4, colsize); // proto
this->ui->tvTrace->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);
//ui->tvTrace->setColumnWidth(5, qMax(colsize, ui->tvTrace->width()-5*colsize)); // info
this->ui->tvTrace->show();
this->ui->lbName->setText(fn);
ui->btOpen->setEnabled(true);
emit packetChanged(this);
delete this->watcherTrace;
delete oldTrace;
});
futureTrace = QtConcurrent::run([=]() {
Trace* trace = new Trace();
if (trace->loadTrace(fn, filter) < 0) {
qDebug("load trace failed");
}
return trace;
});
watcherTrace->setFuture(futureTrace);
}
void TraceView::onFilterSubmit()
{
QString f = ui->txtFilter->text();
if (!trace_)
return;
if (f == trace_->getFilter())
return;
asyncOpen(trace_->getFilename(), f);
}