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