forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporalgraphwindow.cpp
410 lines (353 loc) · 14.2 KB
/
temporalgraphwindow.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "temporalgraphwindow.h"
#include "ui_temporalgraphwindow.h"
#include "helpwindow.h"
#include "mainwindow.h"
QString HexTicker::getTickLabel (double tick, const QLocale& locale, QChar formatChar, int precision)
{
Q_UNUSED(formatChar);
Q_UNUSED(precision);
Q_UNUSED(locale);
int valu = static_cast<int>(tick);
//qDebug() << valu;
return "0x" + QString::number(valu, 16).toUpper().rightJustified(3,'0');
}
TemporalGraphWindow::TemporalGraphWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::TemporalGraphWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
readSettings();
modelFrames = frames;
ui->graphingView->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes);
ui->graphingView->xAxis->setRange(0, 8);
ui->graphingView->yAxis->setRange(0, 255);
ui->graphingView->axisRect()->setupFullAxesBox();
ui->graphingView->xAxis->setLabel("Elapsed Time");
ui->graphingView->yAxis->setLabel("ID");
ui->graphingView->xAxis->setNumberFormat("f");
ui->graphingView->xAxis->setNumberPrecision(6);
HexTicker *tick = new HexTicker;
QSharedPointer<QCPAxisTicker> sharedTicker(tick);
tick->setTickCount(10);
ui->graphingView->yAxis->setTicker(sharedTicker);
// connect slot that ties some axis selections together (especially opposite axes):
connect(ui->graphingView, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
connect(ui->graphingView, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->graphingView, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int)));
// make bottom and left axes transfer their ranges to top and right axes:
connect(ui->graphingView->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->graphingView->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->graphingView->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->graphingView->yAxis2, SLOT(setRange(QCPRange)));
if (useOpenGL)
{
ui->graphingView->setAntialiasedElements(QCP::aeAll);
//ui->graphingView->setNoAntialiasingOnDrag(true);
ui->graphingView->setOpenGl(true);
}
else
{
ui->graphingView->setOpenGl(false);
ui->graphingView->setAntialiasedElements(QCP::aeNone);
}
}
TemporalGraphWindow::~TemporalGraphWindow()
{
delete ui;
}
void TemporalGraphWindow::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
installEventFilter(this);
readSettings();
generateGraph();
ui->graphingView->replot();
}
void TemporalGraphWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
removeEventFilter(this);
writeSettings();
}
void TemporalGraphWindow::readSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
resize(settings.value("Temporal/WindowSize", QSize(800, 600)).toSize());
move(Utility::constrainedWindowPos(settings.value("Temporal/WindowPos", QPoint(50, 50)).toPoint()));
}
useOpenGL = settings.value("Main/UseOpenGL", false).toBool();
}
void TemporalGraphWindow::writeSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
settings.setValue("Temporal/WindowSize", size());
settings.setValue("Temporal/WindowPos", pos());
}
}
void TemporalGraphWindow::updatedFrames(int numFrames)
{
CANFrame thisFrame;
QVector<double> x, y;
bool appendedToGraph = false;
bool needReplot = false;
if (numFrames == -1) //all frames deleted. Kill the display
{
//removeAllGraphs();
//now instead of removing the graphs regenerate them which will blank them out but leave them there in case
//more traffic that matches comes in or someone otherwise loads more data
ui->graphingView->clearGraphs(); //temporarily remove the graphs from the graph view
ui->graphingView->clearPlottables();
ui->graphingView->replot(); //now, redisplay them all
}
else if (numFrames == -2) //all new set of frames. Reset
{
//there shouldn't be any need to actually remove the graphs.
//regenerate them instead
ui->graphingView->clearGraphs(); //temporarily remove the graphs from the graph view
//needScaleSetup = true;
generateGraph();
//ui->graphingView->replot(); //now, redisplay them all
}
else //just got some new frames. See if they are relevant.
{
if (numFrames > modelFrames->count()) return;
appendedToGraph = false;
x.clear();
y.clear();
for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++)
{
thisFrame = modelFrames->at(i);
/*
if (graphParams[j].ID == thisFrame.ID)
{
appendToGraph(graphParams[j], thisFrame, x, y);
appendedToGraph = true;
}*/
}
if (appendedToGraph)
{
//graphParams[j].ref->addData(x, y);
needReplot = true;
}
if (needReplot)
{
if (followGraphEnd)
{
//find the current X span and maintain that span but move the end of it over to match the new end
//of the actual graph. This causes the view to move with the data to always show the end
QCPRange range = ui->graphingView->xAxis->range();
double size = range.size();
bool foundRange;
QCPRange keyRange = ui->graphingView->graph()->getKeyRange(foundRange);
if (foundRange)
{
double end, start;
end = keyRange.upper;
start = end - size;
ui->graphingView->xAxis->setRange(start, end);
}
}
ui->graphingView->replot();
}
}
}
void TemporalGraphWindow::generateGraph()
{
if (modelFrames->count() == 0) return;
ui->graphingView->clearGraphs();
ui->graphingView->clearPlottables();
qDebug() << "Regenerating the graph";
ui->graphingView->addGraph();
graph = ui->graphingView->graph();
QVector<double> x, y;
int frameCount = modelFrames->count();
x.reserve(frameCount);
y.reserve(frameCount);
xminval = xmaxval = modelFrames->at(0).timeStamp().microSeconds() / 1000000.0;
yminval = ymaxval = modelFrames->at(0).frameId();
for (int i = 0; i < frameCount; i++)
{
x.append(modelFrames->at(i).timeStamp().microSeconds() / 1000000.0);
y.append(modelFrames->at(i).frameId());
if (x[i] > xmaxval) xmaxval = x[i];
if (x[i] < xminval) xminval = x[i];
if (y[i] > ymaxval) ymaxval = y[i];
if (y[i] < yminval) yminval = y[i];
}
ui->graphingView->graph()->setData(x,y);
ui->graphingView->graph()->setLineStyle(QCPGraph::lsNone); //no lines
ui->graphingView->graph()->setScatterStyle(QCPScatterStyle::ssCircle);
QPen graphPen;
graphPen.setColor(Qt::blue);
graphPen.setWidth(2);
ui->graphingView->graph()->setPen(graphPen);
qDebug() << "xmin: " << xminval;
qDebug() << "xmax: " << xmaxval;
qDebug() << "ymin: " << yminval;
qDebug() << "ymax: " << ymaxval;
ui->graphingView->xAxis->setRange(xminval, xmaxval);
ui->graphingView->yAxis->setRange(yminval, ymaxval);
ui->graphingView->axisRect()->setupFullAxesBox();
ui->graphingView->replot();
QCPColorMap *colorMap = new QCPColorMap(ui->graphingView->xAxis, ui->graphingView->yAxis);
int ySize = static_cast<int>(ymaxval - yminval) / 30 + 1;
int xSize = static_cast<int>((xmaxval - xminval) * 4.0) + 1;
colorMap->data()->setSize(xSize, ySize);
colorMap->data()->setRange(QCPRange(xminval, xmaxval), QCPRange(yminval, ymaxval));
for (int x = 0; x < xSize; ++x)
{
for (int y = 0; y < ySize; ++y)
{
colorMap->data()->setAlpha(x, y, 180);
colorMap->data()->setCell(x, y, 0.0);
}
}
for (int i = 0; i < frameCount; i++)
{
int x = static_cast<int>(((modelFrames->at(i).timeStamp().microSeconds() / 1000000.0) - xminval) * 4.0);
int y = static_cast<int>(modelFrames->at(i).frameId() - yminval) / 30;
double val = colorMap->data()->cell(x, y);
double inc;
inc = 1 / (val + 1); //logarithmic decay
inc = inc * inc; //square the increment to make it even more stark
val = val + inc;
qDebug() << "X: " << x << " Y: " << y << "Val: " << val;
colorMap->data()->setCell(x, y, val);
}
colorMap->setGradient(QCPColorGradient::gpJet);
colorMap->rescaleDataRange(true);
ui->graphingView->rescaleAxes();
ui->graphingView->replot();
}
void TemporalGraphWindow::selectionChanged()
{
/*
normally, axis base line, axis tick labels and axis labels are selectable separately, but we want
the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels
and the axis base line together. However, the axis label shall be selectable individually.
The selection state of the left and right axes shall be synchronized as well as the state of the
bottom and top axes.
Further, we want to synchronize the selection of the graphs with the selection state of the respective
legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself
or on its legend item.
*/
qDebug() << "SelectionChanged";
// make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:
if (ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->graphingView->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->graphingView->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->graphingView->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->graphingView->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
// make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:
if (ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->graphingView->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->graphingView->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->graphingView->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->graphingView->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
}
void TemporalGraphWindow::mousePress()
{
// if an axis is selected, only allow the direction of that axis to be dragged
// if no axis is selected, both directions may be dragged
if (ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->axisRect()->setRangeDrag(ui->graphingView->xAxis->orientation());
}
else if (ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->axisRect()->setRangeDrag(ui->graphingView->yAxis->orientation());
}
else
{
ui->graphingView->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}
}
void TemporalGraphWindow::mouseWheel()
{
qDebug() << "Mouse WHeel";
// if an axis is selected, only allow the direction of that axis to be zoomed
// if no axis is selected, both directions may be zoomed
if (ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->graphingView->axisRect()->setRangeZoom(ui->graphingView->xAxis->orientation());
else if (ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->graphingView->axisRect()->setRangeZoom(ui->graphingView->yAxis->orientation());
else
ui->graphingView->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
}
bool TemporalGraphWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key())
{
case Qt::Key_Plus:
zoomIn();
break;
case Qt::Key_Minus:
zoomOut();
break;
case Qt::Key_R:
resetView();
break;
case Qt::Key_F1:
HelpWindow::getRef()->showHelp("temporalwindow.md");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
void TemporalGraphWindow::resetView()
{
ui->graphingView->xAxis->setRange(xminval, xmaxval);
ui->graphingView->yAxis->setRange(yminval, ymaxval);
ui->graphingView->axisRect()->setupFullAxesBox();
ui->graphingView->replot();
}
void TemporalGraphWindow::zoomIn()
{
QCPRange xrange = ui->graphingView->xAxis->range();
QCPRange yrange = ui->graphingView->yAxis->range();
if (ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->xAxis->scaleRange(0.666, xrange.center());
}
else if (ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->yAxis->scaleRange(0.666, yrange.center());
}
else
{
ui->graphingView->xAxis->scaleRange(0.666, xrange.center());
ui->graphingView->yAxis->scaleRange(0.666, yrange.center());
}
ui->graphingView->replot();
}
void TemporalGraphWindow::zoomOut()
{
QCPRange xrange = ui->graphingView->xAxis->range();
QCPRange yrange = ui->graphingView->yAxis->range();
if (ui->graphingView->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->xAxis->scaleRange(1.5, xrange.center());
}
else if (ui->graphingView->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
{
ui->graphingView->yAxis->scaleRange(1.5, yrange.center());
}
else
{
ui->graphingView->xAxis->scaleRange(1.5, xrange.center());
ui->graphingView->yAxis->scaleRange(1.5, yrange.center());
}
ui->graphingView->replot();
}