Engauge Digitizer  2
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 <QTextStream>
9 #include <QXmlStreamWriter>
10 #include "Transformation.h"
11 #include "Xml.h"
12 
13 CurvesGraphs::CurvesGraphs()
14 {
15 }
16 
18 {
19  m_curvesGraphs.push_back (curve);
20 }
21 
22 void CurvesGraphs::addPoint (const Point &point)
23 {
24  QString curveName = Point::curveNameFromPointIdentifier (point.identifier());
25 
26  Curve *curve = curveForCurveName (curveName);
27  curve->addPoint (point);
28 }
29 
30 Curve *CurvesGraphs::curveForCurveName (const QString &curveName)
31 {
32  // Search for curve with matching name
33  CurveList::iterator itr;
34  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
35 
36  Curve &curve = *itr;
37  if (curveName == curve.curveName ()) {
38  return &curve;
39  }
40  }
41 
42  return 0;
43 }
44 
45 const Curve *CurvesGraphs::curveForCurveName (const QString &curveName) const
46 {
47  // Search for curve with matching name
48  CurveList::const_iterator itr;
49  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
50 
51  const Curve &curve = *itr;
52  if (curveName == curve.curveName ()) {
53  return &curve;
54  }
55  }
56 
57  return 0;
58 }
59 
60 QStringList CurvesGraphs::curvesGraphsNames () const
61 {
62  QStringList names;
63 
64  CurveList::const_iterator itr;
65  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
66 
67  const Curve &curve = *itr;
68  names << curve.curveName ();
69  }
70 
71  return names;
72 }
73 
74 int CurvesGraphs::curvesGraphsNumPoints (const QString &curveName) const
75 {
76  // Search for curve with matching name
77  CurveList::const_iterator itr;
78  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
79 
80  const Curve &curve = *itr;
81  if (curve.curveName () == curveName) {
82  return curve.numPoints ();
83  }
84  }
85 
86  return 0;
87 }
88 
89 void CurvesGraphs::iterateThroughCurvePoints (const QString &curveNameWanted,
90  const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback)
91 {
92  // Search for curve with matching name
93  CurveList::const_iterator itr;
94  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
95 
96  const Curve &curve = *itr;
97  if (curve.curveName () == curveNameWanted) {
98 
99  curve.iterateThroughCurvePoints (ftorWithCallback);
100  return;
101  }
102  }
103 
104  ENGAUGE_ASSERT (false);
105 }
106 
107 void CurvesGraphs::iterateThroughCurveSegments (const QString &curveNameWanted,
108  const Functor2wRet<const Point &, const Point &, CallbackSearchReturn> &ftorWithCallback) const
109 {
110  // Search for curve with matching name
111  CurveList::const_iterator itr;
112  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
113 
114  const Curve &curve = *itr;
115  if (curve.curveName () == curveNameWanted) {
116 
117  curve.iterateThroughCurveSegments (ftorWithCallback);
118  return;
119  }
120  }
121 
122  ENGAUGE_ASSERT (false);
123 }
124 
125 void CurvesGraphs::iterateThroughCurvesPoints (const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback)
126 {
127  CurveList::const_iterator itr;
128  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
129 
130  const Curve &curve = *itr;
131  curve.iterateThroughCurvePoints (ftorWithCallback);
132  }
133 }
134 
135 void CurvesGraphs::iterateThroughCurvesPoints (const Functor2wRet<const QString &, const Point &, CallbackSearchReturn> &ftorWithCallback) const
136 {
137  CurveList::const_iterator itr;
138  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
139 
140  const Curve &curve = *itr;
141  curve.iterateThroughCurvePoints (ftorWithCallback);
142  }
143 }
144 
145 void CurvesGraphs::loadXml(QXmlStreamReader &reader)
146 {
147  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::loadXml";
148 
149  bool success = true;
150 
151  // Read until end of this subtree
152  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
153  (reader.name() != DOCUMENT_SERIALIZE_CURVES_GRAPHS)){
154 
155  if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
156  (reader.name () == DOCUMENT_SERIALIZE_CURVE)) {
157 
158  Curve curve (reader);
159 
160  m_curvesGraphs.push_back (curve);
161 
162  } else {
163 
164  loadNextFromReader(reader);
165  if (reader.hasError()) {
166  // No need to set success flag, which raises the error, since error was already raised. Just
167  // need to exit loop immediately
168  break;
169  }
170  if (reader.atEnd()) {
171  success = false;
172  break;
173  }
174  }
175  }
176 
177  if (!success) {
178  reader.raiseError ("Cannot read graph curves data");
179  }
180 }
181 
183 {
184  return m_curvesGraphs.count ();
185 }
186 
187 void CurvesGraphs::printStream (QString indentation,
188  QTextStream &str) const
189 {
190  str << indentation << "CurvesGraphs\n";
191 
192  indentation += INDENTATION_DELTA;
193 
194  CurveList::const_iterator itr;
195  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
196 
197  const Curve &curve = *itr;
198  curve.printStream (indentation,
199  str);
200  }
201 }
202 
203 void CurvesGraphs::removePoint (const QString &pointIdentifier)
204 {
205  QString curveName = Point::curveNameFromPointIdentifier(pointIdentifier);
206 
207  Curve *curve = curveForCurveName (curveName);
208  curve->removePoint (pointIdentifier);
209 }
210 
211 void CurvesGraphs::saveXml(QXmlStreamWriter &writer) const
212 {
213  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::saveXml";
214 
215  writer.writeStartElement(DOCUMENT_SERIALIZE_CURVES_GRAPHS);
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.saveXml (writer);
222  }
223 
224  writer.writeEndElement();
225 }
226 
228 {
229  LOG4CPP_INFO_S ((*mainCat)) << "CurvesGraphs::updatePointOrdinals";
230 
231  CurveList::iterator itr;
232  for (itr = m_curvesGraphs.begin (); itr != m_curvesGraphs.end (); itr++) {
233 
234  Curve &curve = *itr;
235  curve.updatePointOrdinals (transformation);
236  }
237 }
void removePoint(const QString &identifier)
Perform the opposite of addPointAtEnd.
Definition: Curve.cpp:366
static QString curveNameFromPointIdentifier(const QString &pointIdentifier)
Parse the curve name from the specified point identifier. This does the opposite of uniqueIdentifierG...
Definition: Point.cpp:202
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 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:288
void updatePointOrdinals(const Transformation &transformation)
See CurveGraphs::updatePointOrdinals.
Definition: Curve.cpp:416
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:346
QString identifier() const
Unique identifier for a specific Point.
Definition: Point.cpp:218
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:157
int curvesGraphsNumPoints(const QString &curveName) const
Point count.
Container for one set of digitized Points.
Definition: Curve.h:24
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals to be consistent with their CurveStyle and x/theta coordinate.
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:172
void saveXml(QXmlStreamWriter &writer) const
Serialize curve.
Definition: Curve.cpp:379
QString curveName() const
Name of this Curve.
Definition: Curve.cpp:65