Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
CurvesGraphs.cpp
1 #include "Curve.h"
2 #include "CurvesGraphs.h"
3 #include "CurveStyles.h"
4 #include "DocumentSerialize.h"
5 #include "EngaugeAssert.h"
6 #include "Logger.h"
7 #include "Point.h"
8 #include <qdebug.h>
9 #include <QTextStream>
10 #include <QXmlStreamWriter>
11 #include "Transformation.h"
12 #include "Xml.h"
13 
14 CurvesGraphs::CurvesGraphs()
15 {
16 }
17 
19 {
20  m_curvesGraphs.push_back (curve);
21 }
22 
23 void CurvesGraphs::addPoint (const Point &point)
24 {
25  QString curveName = Point::curveNameFromPointIdentifier (point.identifier());
26 
27  Curve *curve = curveForCurveName (curveName);
28  curve->addPoint (point);
29 }
30 
31 Curve *CurvesGraphs::curveForCurveName (const QString &curveName)
32 {
33  // Search for curve with matching name
34  CurveList::iterator itr;
35  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
36 
37  Curve &curve = *itr;
38  if (curveName == curve.curveName ()) {
39  return &curve;
40  }
41  }
42 
43  return 0;
44 }
45 
46 const Curve *CurvesGraphs::curveForCurveName (const QString &curveName) const
47 {
48  // Search for curve with matching name
49  CurveList::const_iterator itr;
50  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
51 
52  const Curve &curve = *itr;
53  if (curveName == curve.curveName ()) {
54  return &curve;
55  }
56  }
57 
58  return 0;
59 }
60 
61 QStringList CurvesGraphs::curvesGraphsNames () const
62 {
63  QStringList names;
64 
65  CurveList::const_iterator itr;
66  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
67 
68  const Curve &curve = *itr;
69  names << curve.curveName ();
70  }
71 
72  return names;
73 }
74 
75 int CurvesGraphs::curvesGraphsNumPoints (const QString &curveName) const
76 {
77  // Search for curve with matching name
78  CurveList::const_iterator itr;
79  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
80 
81  const Curve &curve = *itr;
82  if (curve.curveName () == curveName) {
83  return curve.numPoints ();
84  }
85  }
86 
87  return 0;
88 }
89 
90 void CurvesGraphs::iterateThroughCurvePoints (const QString &curveNameWanted,
91  const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback)
92 {
93  // Search for curve with matching name
94  CurveList::const_iterator itr;
95  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
96 
97  const Curve &curve = *itr;
98  if (curve.curveName () == curveNameWanted) {
99 
100  curve.iterateThroughCurvePoints (ftorWithCallback);
101  return;
102  }
103  }
104 
105  ENGAUGE_ASSERT (false);
106 }
107 
108 void CurvesGraphs::iterateThroughCurveSegments (const QString &curveNameWanted,
109  const Functor2wRet<const Point &, const Point &, CallbackSearchReturn> &ftorWithCallback) const
110 {
111  // Search for curve with matching name
112  CurveList::const_iterator itr;
113  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
114 
115  const Curve &curve = *itr;
116  if (curve.curveName () == curveNameWanted) {
117 
118  curve.iterateThroughCurveSegments (ftorWithCallback);
119  return;
120  }
121  }
122 
123  ENGAUGE_ASSERT (false);
124 }
125 
126 void CurvesGraphs::iterateThroughCurvesPoints (const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback)
127 {
128  CurveList::const_iterator itr;
129  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
130 
131  const Curve &curve = *itr;
132  curve.iterateThroughCurvePoints (ftorWithCallback);
133  }
134 }
135 
136 void CurvesGraphs::iterateThroughCurvesPoints (const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback) const
137 {
138  CurveList::const_iterator itr;
139  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
140 
141  const Curve &curve = *itr;
142  curve.iterateThroughCurvePoints (ftorWithCallback);
143  }
144 }
145 
146 void CurvesGraphs::loadPreVersion6(QDataStream &str)
147 {
148  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::loadPreVersion6";
149 
150  int i;
151 
152  qint32 numberCurvesGraphs;
153  str >> numberCurvesGraphs;
154  for (i = 0; i < numberCurvesGraphs; i++) {
155  Curve curve (str);
156  m_curvesGraphs.append (curve);
157  }
158 
159  qint32 numberCurvesMeasures;
160  str >> numberCurvesMeasures;
161  for (i = 0; i < numberCurvesMeasures; i++) {
162  Curve curve (str);
163 
164  // Measures get dropped on the floor
165  }
166 }
167 
168 void CurvesGraphs::loadXml(QXmlStreamReader &reader)
169 {
170  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::loadXml";
171 
172  bool success = true;
173 
174  // Read until end of this subtree
175  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
176  (reader.name() != DOCUMENT_SERIALIZE_CURVES_GRAPHS)){
177 
178  if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
179  (reader.name () == DOCUMENT_SERIALIZE_CURVE)) {
180 
181  Curve curve (reader);
182 
183  m_curvesGraphs.push_back (curve);
184 
185  } else {
186 
187  loadNextFromReader(reader);
188  if (reader.hasError()) {
189  // No need to set success flag, which raises the error, since error was already raised. Just
190  // need to exit loop immediately
191  break;
192  }
193  if (reader.atEnd()) {
194  success = false;
195  break;
196  }
197  }
198  }
199 
200  if (!success) {
201  reader.raiseError ("Cannot read graph curves data");
202  }
203 }
204 
206 {
207  return m_curvesGraphs.count ();
208 }
209 
210 void CurvesGraphs::printStream (QString indentation,
211  QTextStream &str) const
212 {
213  str << indentation << "CurvesGraphs\n";
214 
215  indentation += INDENTATION_DELTA;
216 
217  CurveList::const_iterator itr;
218  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
219 
220  const Curve &curve = *itr;
221  curve.printStream (indentation,
222  str);
223  }
224 }
225 
226 void CurvesGraphs::removePoint (const QString &pointIdentifier)
227 {
228  QString curveName = Point::curveNameFromPointIdentifier(pointIdentifier);
229 
230  Curve *curve = curveForCurveName (curveName);
231  curve->removePoint (pointIdentifier);
232 }
233 
234 void CurvesGraphs::saveXml(QXmlStreamWriter &writer) const
235 {
236  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::saveXml";
237 
238  writer.writeStartElement(DOCUMENT_SERIALIZE_CURVES_GRAPHS);
239 
240  CurveList::const_iterator itr;
241  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
242 
243  const Curve &curve = *itr;
244  curve.saveXml (writer);
245  }
246 
247  writer.writeEndElement();
248 }
249 
251 {
252  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::updatePointOrdinals";
253 
254  CurveList::iterator itr;
255  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
256 
257  Curve &curve = *itr;
258  curve.updatePointOrdinals (transformation);
259  }
260 }
void removePoint(const QString &identifier)
Perform the opposite of addPointAtEnd.
Definition: Curve.cpp:428
static QString curveNameFromPointIdentifier(const QString &pointIdentifier)
Parse the curve name from the specified point identifier. This does the opposite of uniqueIdentifierG...
Definition: Point.cpp:204
void saveXml(QXmlStreamWriter &writer) const
Serialize curves.
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
void loadXml(QXmlStreamReader &reader)
Load from serialized xml post-version 5 file.
void iterateThroughCurveSegments(const QString &curveNameWanted, const Functor2wRet< const Point &, const Point &, CallbackSearchReturn > &ftorWithCallback) const
Apply functor to segments on the specified axis or graph Curve.
Curve * curveForCurveName(const QString &curveName)
Return the axis or graph curve for the specified curve name.
int numCurves() const
Current number of graphs curves.
int numPoints() const
Number of points.
Definition: Curve.cpp:350
void updatePointOrdinals(const Transformation &transformation)
See CurveGraphs::updatePointOrdinals.
Definition: Curve.cpp:478
void addGraphCurveAtEnd(Curve curve)
Append new graph Curve to end of Curve list.
void iterateThroughCurvesPoints(const Functor2wRet< const QString &, const Point &, CallbackSearchReturn > &ftorWithCallback)
Apply functor to Points on all of the Curves.
Class that represents one digitized point. The screen-to-graph coordinate transformation is always ex...
Definition: Point.h:17
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
Definition: Curve.cpp:408
QString identifier() const
Unique identifier for a specific Point.
Definition: Point.cpp:220
Affine transformation between screen and graph coordinates, based on digitized axis points...
void addPoint(const Point &point)
Append new Point to the specified Curve.
void iterateThroughCurvePoints(const QString &curveNameWanted, const Functor2wRet< const QString &, const Point &, CallbackSearchReturn > &ftorWithCallback)
Apply functor to Points in the specified axis or graph Curve.
void iterateThroughCurvePoints(const Functor2wRet< const QString &, const Point &, CallbackSearchReturn > &ftorWithCallback) const
Apply functor to Points on Curve.
Definition: Curve.cpp:219
int curvesGraphsNumPoints(const QString &curveName) const
Point count.
Container for one set of digitized Points.
Definition: Curve.h:26
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals to be consistent with their CurveStyle and x/theta coordinate.
void loadPreVersion6(QDataStream &str)
Load from serialized binary pre-version 6 file.
QStringList curvesGraphsNames() const
List of graph curve names.
void removePoint(const QString &pointIdentifier)
Remove the Point from its Curve.
void iterateThroughCurveSegments(const Functor2wRet< const Point &, const Point &, CallbackSearchReturn > &ftorWithCallback) const
Apply functor to successive Points, as line segments, on Curve. This could be a bit slow...
Definition: Curve.cpp:234
void saveXml(QXmlStreamWriter &writer) const
Serialize curve.
Definition: Curve.cpp:441
QString curveName() const
Name of this Curve.
Definition: Curve.cpp:127