-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimpletimeplot.cpp
More file actions
164 lines (144 loc) · 5.54 KB
/
simpletimeplot.cpp
File metadata and controls
164 lines (144 loc) · 5.54 KB
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
#include "simpletimeplot.h"
#include <QPainter>
SimpleTimePlot::SimpleTimePlot(QWidget *parent) : QWidget(parent),m_tite("Untitled")
{
this->setMinimumSize(100,100);
}
void SimpleTimePlot::paintEvent(QPaintEvent* event)
{
QMutexLocker locker(&m_dataMutex);
QMutexLocker locker2(&m_lineMutex);
cleanupMap();
QPainter painter(this);
QRectF plotFrame = drawFrame(&painter);
QPointF plotFrameLowRight(plotFrame.x()+plotFrame.width(),
plotFrame.y()+plotFrame.height());
//if(m_data.size() <= 1)
// return;
QVector<QLineF> lineSegments;
lineSegments.reserve(m_data.size()-1);
QPointF lastPoint(0,0);
double x,y;
bool isOutside = false;
bool skippedFirst = false;
double dx = m_xMax-m_xMin;
double dy = m_yMax-m_yMin;
for(auto const& p: m_data) {
// y = INF ?
if(p.second > std::numeric_limits<qreal>::max() ) {
lastPoint.setX(0);
lastPoint.setY(0);
isOutside = false;
skippedFirst = false;
continue;
}
x = plotFrame.x()+(p.first - m_xMin)/dx*plotFrame.width();
y = plotFrame.y()+(1-(p.second - m_yMin)/dy)*plotFrame.height(); // Flip y axis
QPointF newPoint;
// Line is inside ?
if(p.first >= m_xMin && p.first <= m_xMax &&
p.second >= m_yMin && p.second <= m_yMax) {
// Clipping with reentering line
if(isOutside) {
lastPoint.setX(qMax(plotFrame.x(),qMin(plotFrameLowRight.x(),lastPoint.x())));
if(!isnanf(lastPoint.y()))
lastPoint.setY(qMax(plotFrame.y(),qMin(plotFrameLowRight.y(),lastPoint.y())));
else
lastPoint.setY(y);
isOutside = false;
}
newPoint.setX(x);
newPoint.setY(y);
if(skippedFirst && !isnanf(newPoint.y()) && !isnanf(lastPoint.y()))
lineSegments.append(QLineF(lastPoint,newPoint));
skippedFirst = true;
}
// Clip line, that comes from inside and goes to the outside ?
else if(!isOutside) {
// Clip leaving line
newPoint.setX(qMax(plotFrame.x(),qMin(plotFrameLowRight.x(),x)));
if(!isnanf(y))
newPoint.setY(qMax(plotFrame.y(),qMin(plotFrameLowRight.y(),y)));
else
newPoint.setY(y);
isOutside = true;
if(skippedFirst && !isnanf(newPoint.y()) && !isnanf(lastPoint.y()))
lineSegments.append(QLineF(lastPoint,newPoint));
skippedFirst = true;
} // Store point for next run only
else {
newPoint.setX(x);
newPoint.setY(y);
}
lastPoint = newPoint;
}
for(auto const& l: m_lines) {
if(!l.second.active)
continue;
QVector<QLineF> lines;
for(auto const& p: l.second.positions) {
// Horizontal line
x = plotFrame.x()+(p - m_xMin)/dx*plotFrame.width();
lines.append(QLineF(x,plotFrame.y()+1,x,plotFrame.y()+plotFrame.height()-1));
}
painter.setPen(l.second.pen);
painter.drawLines(lines);
}
painter.setPen(QPen(Qt::black));
if(m_yMin < 0 && m_yMax > 0) {
y = plotFrame.y()+(1-(0 - m_yMin)/dy)*plotFrame.height();
lineSegments.append(QLineF(plotFrame.x(),y,plotFrame.x()+plotFrame.width(),y));
}
painter.drawLines(lineSegments);
painter.setPen(QPen(Qt::black));
// Print value next to line end
QFontMetrics fm = painter.fontMetrics();
if(m_data.size() > 0 && !isnanf(m_data.rbegin()->second)) {
QString val = QString("%1").arg(m_data.rbegin()->second);
x = lastPoint.x()-fm.width(val)-1;
// Keep y in range of plot
y = qMin(plotFrame.y()+plotFrame.height(),qMax(plotFrame.y()+fm.height(),lastPoint.y()-5));
painter.drawText(x,y,val);
}
}
QRect SimpleTimePlot::drawFrame(QPainter *painter)
{
int borderX = 50;
int borderY = 20;
QRect plotFrame(borderX,borderY,qMax(0,width()-2*borderX),qMax(0,height()-2*borderY));
painter->setPen(QPen(Qt::black));
//painter->drawRect(0,0,width()-1,height()-1);
painter->drawRect(plotFrame);
painter->setPen(QPen(Qt::black));
QFontMetrics fm = painter->fontMetrics();
int fontHeight = fm.height();
QString xMin = QString("%1").arg(m_xMin);
QString xMax = QString("%1").arg(m_xMax);
QString yMin = QString("%1").arg(m_yMin);
QString yMax = QString("%1").arg(m_yMax);
painter->drawText(plotFrame.x(),fontHeight+plotFrame.y()+plotFrame.height(),xMin);
painter->drawText(plotFrame.x()+ plotFrame.width() - fm.width(xMax),fontHeight+plotFrame.y()+plotFrame.height(),xMax);
painter->drawText(borderX-1-fm.width(yMin),plotFrame.y()+plotFrame.height(),yMin);
painter->drawText(borderX-1-fm.width(yMax),fontHeight+plotFrame.y(),yMax);
// Draw header
painter->drawText(borderX,fontHeight, m_tite);
return plotFrame;
}
void SimpleTimePlot::cleanupMap()
{
auto itEnd = m_data.lower_bound(m_xMin);
int dist = std::distance(m_data.begin(),itEnd);
if(dist > 0) {
// Keep the found element for clipping
// Only delete previous elements
m_data.erase(m_data.begin(),itEnd);
}
for (auto &lg: m_lines) {
auto& pos = lg.second.positions;
auto itEnd2 = std::lower_bound(pos.begin(),pos.end(),m_xMin);
int dist2 = std::distance(pos.begin(),itEnd2);
if(dist2 > 0) {
pos.erase(pos.begin(),itEnd2);
}
}
}