Isis 3 Programmer Reference
PlotCurve.cpp
1#include "PlotCurve.h"
2
3#include <iostream>
4
5#include <qwt_text.h>
6#include <qwt_plot.h>
7#include <qwt_plot_curve.h>
8#include <qwt_plot_marker.h>
9#include <qwt_symbol.h>
10
11#include <QBuffer>
12#include <QByteArray>
13
14#include "SpecialPixel.h"
15
16namespace Isis {
22 m_markerSymbol = new QwtSymbol();
23 m_markerSymbol->setStyle(QwtSymbol::NoSymbol);
24 m_markerSymbol->setSize(6, 6);
25 m_color = QColor(Qt::white);
26 m_xUnits = xUnits;
27 m_yUnits = yUnits;
28 }
29
30
31 PlotCurve::~PlotCurve() {
32 if (plot()) {
33 clearMarkers();
34 }
35 }
36
37
46 recreateMarkers();
47 }
48
49
55 QColor PlotCurve::color() const {
56 return m_color;
57 }
58
59
65 QwtSymbol *PlotCurve::markerSymbol() const {
66 return m_markerSymbol;
67 }
68
69
76 return m_xUnits;
77 }
78
79
86 return m_yUnits;
87 }
88
89
96 void PlotCurve::setColor(const QColor &color) {
97 //set the data for the curve
98 m_color = color;
99 setPen(pen());
100 }
101
102
108 void PlotCurve::setData(QwtSeriesData<QPointF> *data) {
109 //set the data for the curve
110 QwtPlotCurve::setData(data);
111 recreateMarkers();
112 }
113
114
119 void PlotCurve::setMarkerSymbol(QwtSymbol::Style style) {
120 m_markerSymbol->setStyle(style);
121 recreateMarkers();
122 }
123
124
130 void PlotCurve::setMarkerVisible(bool visible) {
131 foreach (QwtPlotMarker *marker, m_valuePointMarkers) {
132 marker->setVisible(visible);
133 }
134 }
135
136
137
144 QByteArray PlotCurve::fromByteArray(const QByteArray &classData) {
145 QString expectedHeader("PLOT_CURVE_V1");
146 int headerKeySize = expectedHeader.toUtf8().size();
147
148 if (classData.size() > headerKeySize) {
149 int dataPos = 0;
150 const char *rawClassData = classData.data();
151
152 QString givenKey = QString::fromUtf8(classData.data() + dataPos,
153 headerKeySize);
154 dataPos += headerKeySize;
155 if (givenKey != expectedHeader) {
156 IString msg = "The given byte array does not contain the required "
157 "header";
158 throw IException(IException::Programmer, msg, _FILEINFO_);
159 }
160
161 int titleSize = *(((int *)(rawClassData + dataPos)));
162 dataPos += sizeof(int);
163
164 setTitle(QString::fromUtf8(classData.data() + dataPos, titleSize));
165 dataPos += titleSize;
166
167 m_xUnits = (Units)(*((int *)(rawClassData + dataPos)));
168 dataPos += sizeof(int);
169
170 m_yUnits = (Units)(*((int *)(rawClassData + dataPos)));
171 dataPos += sizeof(int);
172
173 // Read the pen...
174 int penBufferSize = *(((int *)(rawClassData + dataPos)));
175 dataPos += sizeof(int);
176
177 QByteArray penBufferBytes(rawClassData + dataPos, penBufferSize);
178 dataPos += penBufferSize;
179
180 QBuffer penDataBuffer(&penBufferBytes);
181 penDataBuffer.open(QIODevice::ReadWrite);
182
183 QDataStream penDataStream(&penDataBuffer);
184 QPen pen;
185 penDataStream >> pen;
186 setPen(pen);
187
188
189 // Read the color...
190 int colorBufferSize = *(((int *)(rawClassData + dataPos)));
191 dataPos += sizeof(int);
192
193 QByteArray colorBufferBytes(rawClassData + dataPos, colorBufferSize);
194 dataPos += colorBufferSize;
195
196 QBuffer colorDataBuffer(&colorBufferBytes);
197 colorDataBuffer.open(QIODevice::ReadWrite);
198
199 QDataStream colorDataStream(&colorDataBuffer);
200 QColor newColor;
201 colorDataStream >> newColor;
202 setColor(newColor);
203
204 // Read the marker symbol...
205 int markerSymbolBufferSize = *(((int *)(rawClassData + dataPos)));
206 dataPos += sizeof(int);
207
208 QByteArray markerSymbolBufferBytes(rawClassData + dataPos,
209 markerSymbolBufferSize);
210 dataPos += markerSymbolBufferSize;
211
212 QBuffer markerSymbolDataBuffer(&markerSymbolBufferBytes);
213 markerSymbolDataBuffer.open(QIODevice::ReadWrite);
214
215 QDataStream markerSymbolDataStream(&markerSymbolDataBuffer);
216
217 QBrush markerBrush;
218 markerSymbolDataStream >> markerBrush;
219 m_markerSymbol->setBrush(markerBrush);
220
221 QPen markerPen;
222 markerSymbolDataStream >> markerPen;
223 m_markerSymbol->setPen(markerPen);
224
225 QSize markerSize;
226 markerSymbolDataStream >> markerSize;
227 m_markerSymbol->setSize(markerSize);
228
229 int markerStyle;
230 markerSymbolDataStream >> markerStyle;
231 m_markerSymbol->setStyle((QwtSymbol::Style)markerStyle);
232
233 // Done reading the more advanced items, finish up with the data
234 int plotDataSize = *((int *)(rawClassData + dataPos));
235 dataPos += sizeof(int);
236 QVector<QPointF> plotDataValues;
237
238 for (int i = 0; i < plotDataSize; i ++) {
239 double x = *((double *)(rawClassData + dataPos));
240 dataPos += sizeof(double);
241
242 double y = *((double *)(rawClassData + dataPos));
243 dataPos += sizeof(double);
244
245 plotDataValues.append(QPointF(x, y));
246 }
247
248 setData(new QwtPointSeriesData(plotDataValues));
249
250 return classData.right(classData.size() - dataPos);
251 }
252 else {
253 IString msg = "The given byte array is not large enough to contain the "
254 "required header";
255 throw IException(IException::Programmer, msg, _FILEINFO_);
256 }
257 }
258
259
260 QByteArray PlotCurve::toByteArray() const {
261 QByteArray classData;
262
263 QString header("PLOT_CURVE_V1");
264 classData.append(header.toUtf8());
265
266 QByteArray titleArray = title().text().toUtf8();
267 int size = titleArray.size();
268 classData.append((char *)&size, sizeof(int));
269 classData.append(titleArray);
270
271 int xUnitsInt = (int)m_xUnits;
272 int yUnitsInt = (int)m_yUnits;
273 classData.append((char *)&xUnitsInt, sizeof(int));
274 classData.append((char *)&yUnitsInt, sizeof(int));
275
276 // Store the pen... to do this we need to serialize using QPen's operators
277 QBuffer penDataBuffer;
278 penDataBuffer.open(QIODevice::ReadWrite);
279
280 QDataStream penDataStream(&penDataBuffer);
281 penDataStream << pen();
282 penDataBuffer.seek(0);
283
284 size = penDataBuffer.buffer().size();
285 classData.append((char *)&size, sizeof(int));
286 classData.append(penDataBuffer.buffer());
287
288 // Store the color...
289 QBuffer colorDataBuffer;
290 colorDataBuffer.open(QIODevice::ReadWrite);
291
292 QDataStream colorDataStream(&colorDataBuffer);
293 colorDataStream << m_color;
294 colorDataBuffer.seek(0);
295
296 size = colorDataBuffer.buffer().size();
297 classData.append((char *)&size, sizeof(int));
298 classData.append(colorDataBuffer.buffer());
299
300 // Store the marker symbol...
301 QBuffer markerSymbolDataBuffer;
302 markerSymbolDataBuffer.open(QIODevice::ReadWrite);
303
304 QDataStream markerSymbolDataStream(&markerSymbolDataBuffer);
305 markerSymbolDataStream << m_markerSymbol->brush();
306 markerSymbolDataStream << m_markerSymbol->pen();
307 markerSymbolDataStream << m_markerSymbol->size();
308 markerSymbolDataStream << (int)m_markerSymbol->style();
309 markerSymbolDataBuffer.seek(0);
310
311 size = markerSymbolDataBuffer.buffer().size();
312 classData.append((char *)&size, sizeof(int));
313 classData.append(markerSymbolDataBuffer.buffer());
314
315 // Store the X/Y plot values
316 const QwtSeriesData<QPointF> &plotData = *data();
317 size = plotData.size();
318 classData.append((char *)&size, sizeof(int));
319
320 for (int i = 0; i < size; i ++) {
321 double x = plotData.sample(i).x();
322 double y = plotData.sample(i).y();
323
324 classData.append((char *)&x, sizeof(double));
325 classData.append((char *)&y, sizeof(double));
326 }
327
328 return classData;
329 }
330
331
338 void PlotCurve::setPen(const QPen &pen) {
339 QPen newPen(pen);
340 newPen.setColor(m_color);
341
342 QwtPlotCurve::setPen(newPen);
343
344 recreateMarkers();
345 }
346
347
348 void PlotCurve::clearMarkers() {
349 foreach (QwtPlotMarker *marker, m_valuePointMarkers) {
350 marker->detach();
351 //delete marker;
352 }
353
354 m_valuePointMarkers.clear();
355 }
356
357
358 void PlotCurve::recreateMarkers() {
359 bool markersVisible = true;
360 if (m_valuePointMarkers.size()) {
361 markersVisible = m_valuePointMarkers.first()->isVisible();
362 }
363 clearMarkers();
364
365 QPen markerPen = m_markerSymbol->pen();
366 markerPen.setColor(m_color);
367 m_markerSymbol->setPen(markerPen);
368
369 const QwtSeriesData<QPointF> &plotData = *data();
370 for(unsigned int i = 0; i < plotData.size(); i++) {
371 QwtPlotMarker *newMarker = new QwtPlotMarker();
372 newMarker->setValue(plotData.sample(i).x(), plotData.sample(i).y());
373 newMarker->setAxes(xAxis(), yAxis());
374 newMarker->setSymbol(m_markerSymbol);
375 newMarker->setVisible(markersVisible);
376 newMarker->attach(plot());
377 m_valuePointMarkers.append(newMarker);
378 }
379 }
380}
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Adds specific functionality to C++ strings.
Definition IString.h:165
void setColor(const QColor &color)
Set the color of this curve and it's markers.
Definition PlotCurve.cpp:96
QColor color() const
This method returns the color of the curve.
Definition PlotCurve.cpp:55
QwtSymbol * m_markerSymbol
Marker's styles.
Definition PlotCurve.h:124
void setData(QwtSeriesData< QPointF > *data)
This method sets the data for the curve, then sets the value for the markers associated with the curv...
QByteArray fromByteArray(const QByteArray &classData)
Construct the plot curve given the past results of toByteArray(...).
Units yUnits() const
Get the units of the y-axis double data.
Definition PlotCurve.cpp:85
void setMarkerSymbol(QwtSymbol::Style style)
This method sets the shape of the markers.
PlotCurve(Units xUnits, Units yUnits)
Constructs and instance of a PlotCurve with some default properties.
Definition PlotCurve.cpp:21
Units xUnits() const
Get the units of the x-axis double data.
Definition PlotCurve.cpp:75
void attachMarkers()
After attaching this curve to a plot, due to an inheritance/implementation complication with qwt the ...
Definition PlotCurve.cpp:45
Units
These are all the possible units for the x or y data in a plot curve.
Definition PlotCurve.h:54
void setPen(const QPen &pen)
Sets the plot pen to the passed-in pen.
QwtSymbol * markerSymbol() const
This method returns the shape of the markers.
Definition PlotCurve.cpp:65
void setMarkerVisible(bool visible)
This method sets the visibility states of the markers at each value point.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16