-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPlot2DGrid.cpp
89 lines (71 loc) · 3.4 KB
/
Plot2DGrid.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
#include "Plot2D.h"
#include <QObject>
Plot2DGrid::Plot2DGrid() : angleVisibility_(false)
{}
bool Plot2DGrid::draw(Canvas& canvas, Dataset* dataset, DatasetCursor cursor)
{
if (!isVisible())
return false;
QPen pen(_lineColor);
pen.setWidth(_lineWidth);
QPainter* p = canvas.painter();
p->setPen(pen);
p->setFont(QFont("Asap", 14, QFont::Normal));
QFontMetrics fm(p->font());
const int imageHeight{ canvas.height() }, imageWidth{ canvas.width() },
linesCount{ _lines }, textXOffset{ 30 }, textYOffset{ 10 };
for (int i = 1; i < linesCount; ++i) {
const int posY = i * imageHeight / linesCount;
QString lineText;
if (_velocityVisible && cursor.velocity.isValid()) { // velocity
const float velFrom{ cursor.velocity.from }, velTo{ cursor.velocity.to },
velRange{ velTo - velFrom }, attVal{ velRange * i / linesCount + velFrom };
lineText.append({ QString::number(attVal , 'f', 2) + QObject::tr(" m/s ")});
}
if (angleVisibility_ && cursor.attitude.isValid()) { // angle
const float attFrom{ cursor.attitude.from }, attTo{ cursor.attitude.to },
attRange{ attTo - attFrom }, attVal{ attRange * i / linesCount + attFrom };
QString text{ QString::number(attVal, 'f', 0) + QStringLiteral("° ") };
lineText.append(text);
}
if (cursor.distance.isValid()) { // depth
const float distFrom{ cursor.distance.from }, distTo{ cursor.distance.to },
distRange{ distTo - distFrom }, rangeVal{ distRange * i / linesCount + distFrom };
lineText.append( { QString::number(rangeVal, 'f', 2) + QObject::tr(" m") } );
}
if (isFillWidth())
p->drawLine(0, posY, imageWidth, posY);
else
p->drawLine(imageWidth - fm.horizontalAdvance(lineText) - textXOffset, posY, imageWidth, posY); // line
if (!lineText.isEmpty())
p->drawText(imageWidth - fm.horizontalAdvance(lineText) - textXOffset, posY - textYOffset, lineText);
}
if (cursor.distance.isValid()) {
p->setFont(QFont("Asap", 26, QFont::Normal));
float val{ cursor.distance.to };
QString range_text = QString::number(val, 'f', (val == static_cast<int>(val)) ? 0 : 2) + QObject::tr(" m");
p->drawText(imageWidth - textXOffset / 2 - range_text.count() * 25, imageHeight - 10, range_text);
}
if (_rangeFinderLastVisible && cursor.distance.isValid()) {
Epoch* lastEpoch = dataset->last();
Epoch* preLastEpoch = dataset->lastlast();
float distance = NAN;
if (lastEpoch != NULL && isfinite(lastEpoch->rangeFinder()))
distance = lastEpoch->rangeFinder();
else if (preLastEpoch != NULL && isfinite(preLastEpoch->rangeFinder()))
distance = preLastEpoch->rangeFinder();
if (isfinite(distance)) {
pen.setColor(QColor(250, 100, 0));
p->setPen(pen);
p->setFont(QFont("Asap", 40, QFont::Normal));
float val{ round(distance * 100.f) / 100.f };
QString rangeText = QString::number(val, 'f', (val == static_cast<int>(val)) ? 0 : 2) + QObject::tr(" m");
p->drawText(imageWidth / 2 - rangeText.count() * 32, imageHeight - 15, rangeText);
}
}
return true;
}
void Plot2DGrid::setAngleVisibility(bool state)
{
angleVisibility_ = state;
}