-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaspgraph.cpp
More file actions
244 lines (229 loc) · 8.66 KB
/
yaspgraph.cpp
File metadata and controls
244 lines (229 loc) · 8.66 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
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
#include "yaspgraph.h"
yaspGraph::yaspGraph(int id, QCPGraph* g, QCPItemText* info, QCPItemStraightLine* rLine, QString plotStr,
QColor color, double plotTimeInMilliSeconds)
: id(id), infoGraph(g), infoText(info), refLine(rLine) {
yOffset = 0;
yMult = 1.0;
plotDashPattern = QVector<qreal>() << 16 << 4 << 8 << 4;
rLineDashPattern = QVector<qreal>() << 64 << 4 ;
minY = DBL_MAX;
maxY = -DBL_MAX;
QPen pen = QPen(color);
infoGraph->setPen(pen);
infoGraph->setName(plotStr);
// info->layer()->setMode(QCPLayer::lmBuffered);
info->setColor(color);
info->setPositionAlignment(Qt::AlignTop|Qt::AlignRight);
info->position->setType(QCPItemPosition::ptAbsolute );
// info->setSelectable(true);
info->setPadding(QMargins(2,2,2,2));
pen.setWidthF(0.5);
pen.setDashPattern(rLineDashPattern);
// rLine->layer()->setMode(QCPLayer::lmBuffered);
rLine->setPen(pen);
// rLine->setSelectable(false);
rLine->point1->setCoords(0,0);
rLine->point2->setCoords(plotTimeInMilliSeconds, 0);
hidden = false;
// textTicker = QSharedPointer<QCPAxisTickerText>(new QCPAxisTickerText());
// dataContainer = QSharedPointer<QCPGraphDataContainer>(new QCPGraphDataContainer());
}
//-----------------------------------------------------------------------------------------
yaspGraph::~yaspGraph() {
qDebug() << "====================== yaspGraph DESTRUCTOR ======================";
}
//-----------------------------------------------------------------------------------------
void yaspGraph::reset() {
QSharedPointer<QCPGraphDataContainer> gData = infoGraph->data();
for (QCPDataContainer<QCPGraphData>::iterator it = gData->begin(); it != gData->end(); ++it){
double val = it->value;
val -= yOffset;
val /= yMult;
it->value = val;
}
minY -= yOffset;
minY /= yMult;
maxY -= yOffset;
maxY /= yMult;
yOffset = 0;
yMult = 1.0;
}
//-----------------------------------------------------------------------------------------
void yaspGraph::hide(bool h) {
hidden = h;
infoGraph->setVisible(!h);
refLine->setVisible(!h);
}
//-----------------------------------------------------------------------------------------
void yaspGraph::toggleVisibility(bool show) {
// qDebug() << "yaspGraph::toggleVisibility: " << infoGraph->name() << " / hidden " << hidden << " / show " << show;
if (hidden) {
infoGraph->setVisible(false);
refLine->setVisible(false);
} else {
infoGraph->setVisible(show);
refLine->setVisible(show);
}
}
//-----------------------------------------------------------------------------------------
void yaspGraph::setSelected(bool sel) {
// qDebug() << "yaspGraph::setSelected: " << infoGraph->name() << " / hidden " << hidden << " / sel " << sel;
QPen pen = infoGraph->pen();
if (sel) {
pen.setDashPattern(plotDashPattern);
infoGraph->setPen(pen);
pen.setWidth(1);
infoText->setPen(pen);
infoText->setSelectedPen(pen);
infoText->setSelectedColor(pen.color());
infoText->setColor(pen.color());
} else {
pen.setStyle(Qt::SolidLine);
infoGraph->setPen(pen);
pen.setWidth(1);
infoText->setPen(Qt::NoPen);
infoText->setSelectedPen(Qt::NoPen);
infoText->setSelectedColor(pen.color());
infoText->setColor(pen.color());
}
pen.setDashPattern(rLineDashPattern);
pen.setWidthF(0.5);
refLine->setPen(pen);
QFont font = infoText->font();
font.setStrikeOut(hidden);
infoText->setFont(font);
infoText->setSelectedFont(font);
}
//-----------------------------------------------------------------------------------------
void yaspGraph::updateLabel(QString str, int margin) {
// lastX = lX;
plotInfoStr = str;
QString info = infoGraph->name() + " -> " + plotInfoStr;
QColor color = infoGraph->pen().color();
QFont font = infoText->font();
// font.setPointSize(8);
//// font.setStyleHint(QFont::Monospace);
font.setWeight(QFont::Bold);
// font.setStyle(QFont::StyleItalic);
QFontMetricsF fm(font);
qreal pixelsWide = fm.width(info);
qreal pixelsHigh = fm.height();
QPointF labelPos;
labelPos.setX(pixelsWide + infoText->padding().left()+ infoText->padding().right() + margin + INFO_TEXT_LEFT_MARGIN );
labelPos.setY( (static_cast<double>(id)+1) * (pixelsHigh + infoText->padding().top() + infoText->padding().bottom()));
infoText->setColor(color);
// QPen penT(color);
font.setStrikeOut(hidden);
infoText->setFont(font);
infoText->setSelectedFont(font);
// infoText->setPen(penT);
infoText->setSelectedColor(color);
QPen pen = infoGraph->pen();
pen.setWidth(1);
infoText->setSelectedPen(pen);
infoText->position->setCoords(labelPos.x(), labelPos.y());
infoText->setText(info);
// infoText->layer()->replot();
QPen penL(color, 0.5);
penL.setDashPattern(rLineDashPattern);
refLine->setPen(penL);
refLine->pen().setColor(color);
refLine->point1->setCoords(0, yOffset);
refLine->point2->setCoords(1, yOffset);
// refLine->layer()->replot();
}
//-----------------------------------------------------------------------------------------
void yaspGraph::updateMinMax(double v) {
if (v < minY) minY = v;
if (v > maxY) maxY = v;
// if (qFuzzyCompare(minY, maxY)) {
// if (minY > yOffset) {
// minY = yOffset;
// }
// if (maxY < yOffset) {
// maxY = yOffset;
// }
// }
}
//-----------------------------------------------------------------------------------------
QCPItemText* yaspGraph::info() {
return infoText;
}
////-----------------------------------------------------------------------------------------
//QCPItemStraightLine *yaspGraph::rLine() {
// return refLine;
//}
////-----------------------------------------------------------------------------------------
//double yaspGraph::refY() {
// return refLine->point1->coords().y();
//}
//-----------------------------------------------------------------------------------------
QCPGraph* yaspGraph::plot() {
return infoGraph;
}
//-----------------------------------------------------------------------------------------
void yaspGraph::save(QTextStream& streamData) {
QLocale locale = QLocale("fr_FR");
locale.setNumberOptions(QLocale::OmitGroupSeparator);
//QTextCodec *codec = QTextCodec::codecForName("UTF-8");
streamData.setLocale(locale);
// Set Headers
// streamData << "NAME" << ";" << "TIME" << ";" << "VALUE" << "\n";
streamData << "TIME" << ";" << "VALUE" << "\n";
QSharedPointer<QCPGraphDataContainer> gData = infoGraph->data();
// qDebug() << "SAVE PLOT: " << gData->size();
for (QCPDataContainer<QCPGraphData>::iterator it = gData->begin(); it != gData->end(); ++it){
streamData << it->key << ";" << it->value << "\n";
}
}
//-----------------------------------------------------------------------------------------
void yaspGraph::setOffset(double o) {
// qDebug() << "yOffset " << yOffset << " o-> " << o;
QSharedPointer<QCPGraphDataContainer> gData = infoGraph->data();
for (QCPDataContainer<QCPGraphData>::iterator it = gData->begin(); it != gData->end(); ++it){
it->value += o;
}
yOffset += o;
minY += o;
maxY += o;
refLine->point1->setCoords(0, yOffset);
refLine->point2->setCoords(1, yOffset);
}
//-----------------------------------------------------------------------------------------
double yaspGraph::offset() {
return yOffset;
}
//-----------------------------------------------------------------------------------------
void yaspGraph::setMult(double m) {
double nm = yMult * m;
// qDebug() << "yMult " << yMult << " m-> " << m;
QSharedPointer<QCPGraphDataContainer> gData = infoGraph->data();
for (QCPDataContainer<QCPGraphData>::iterator it = gData->begin(); it != gData->end(); ++it){
double val = it->value;
val -= yOffset;
val /= yMult;
val *= nm;
val += yOffset;
it->value = val;
}
minY -= yOffset;
minY /= yMult;
minY *= nm;
minY += yOffset;
maxY -= yOffset;
maxY /= yMult;
maxY *= nm;
maxY += yOffset;
yMult = nm;
refLine->point1->setCoords(0, yOffset);
refLine->point2->setCoords(1, yOffset);
}
//-----------------------------------------------------------------------------------------
double yaspGraph::mult() {
return yMult;
}
////-----------------------------------------------------------------------------------------
//void yaspGraph::updateRefLine(int dpn) {
// refLine->start->setCoords(0, yOffset);
// refLine->end->setCoords(dpn, yOffset);
//}