7 #include "DocumentSerialize.h"
8 #include "EngaugeAssert.h"
12 #include <QStringList>
13 #include <QTextStream>
14 #include "QtToString.h"
15 #include <QXmlStreamReader>
16 #include <QXmlStreamWriter>
19 unsigned int Point::m_identifierIndex = 0;
21 extern const QString AXIS_CURVE_NAME;
22 extern const QString DUMMY_CURVE_NAME;
23 const QString POINT_IDENTIFIER_DELIMITER_SAFE (
"\t");
24 const QString POINT_IDENTIFIER_DELIMITER_XML (
"_");
26 const double MISSING_ORDINAL_VALUE = 0;
27 const double MISSING_POSGRAPH_VALUE = 0;
34 const QPointF &posScreen) :
35 m_isAxisPoint (curveName == AXIS_CURVE_NAME),
36 m_identifier (uniqueIdentifierGenerator(curveName)),
37 m_posScreen (posScreen),
38 m_hasPosGraph (false),
39 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
41 m_ordinal (MISSING_ORDINAL_VALUE),
44 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point"
45 <<
" curveName=" << curveName.toLatin1().data()
46 <<
" identifierGenerated=" << m_identifier.toLatin1().data()
47 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data();
49 ENGAUGE_ASSERT (!curveName.isEmpty ());
53 const QPointF &posScreen,
54 const QPointF &posGraph,
57 m_identifier (uniqueIdentifierGenerator(curveName)),
58 m_posScreen (posScreen),
60 m_posGraph (posGraph),
62 m_ordinal (MISSING_ORDINAL_VALUE),
65 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME ||
66 curveName == DUMMY_CURVE_NAME);
68 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point"
69 <<
" curveName=" << curveName.toLatin1().data()
70 <<
" identifierGenerated=" << m_identifier.toLatin1().data()
71 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data()
72 <<
" posGraph=" << QPointFToString (posGraph).toLatin1().data()
73 <<
" isXOnly=" << (isXOnly ?
"true" :
"false");
75 ENGAUGE_ASSERT (!curveName.isEmpty ());
79 const QString &identifier,
80 const QPointF &posScreen,
81 const QPointF &posGraph,
85 m_identifier (identifier),
86 m_posScreen (posScreen),
88 m_posGraph (posGraph),
93 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME);
95 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point"
96 <<
" curveName=" << curveName.toLatin1().data()
97 <<
" identifier=" << m_identifier.toLatin1().data()
98 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data()
99 <<
" posGraph=" << QPointFToString (posGraph).toLatin1().data()
100 <<
" ordinal=" << ordinal
101 <<
" isXOnly=" << (isXOnly ?
"true" :
"false");
103 ENGAUGE_ASSERT (!curveName.isEmpty ());
107 const QPointF &posScreen,
108 const QPointF &posGraph,
111 m_isAxisPoint (true),
112 m_identifier (uniqueIdentifierGenerator(curveName)),
113 m_posScreen (posScreen),
114 m_hasPosGraph (true),
115 m_posGraph (posGraph),
120 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME);
122 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point"
123 <<
" curveName=" << curveName.toLatin1().data()
124 <<
" identifierGenerated=" << m_identifier.toLatin1().data()
125 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data()
126 <<
" posGraph=" << QPointFToString (posGraph).toLatin1().data()
127 <<
" ordinal=" << ordinal
128 <<
" isXOnly=" << (isXOnly ?
"true" :
"false");
130 ENGAUGE_ASSERT (!curveName.isEmpty ());
134 const QString &identifier,
135 const QPointF &posScreen,
137 m_isAxisPoint (false),
138 m_identifier (identifier),
139 m_posScreen (posScreen),
140 m_hasPosGraph (false),
141 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
146 ENGAUGE_ASSERT (curveName != AXIS_CURVE_NAME);
148 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point"
149 <<
" curveName=" << curveName.toLatin1().data()
150 <<
" identifier=" << identifier.toLatin1().data()
151 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data()
154 ENGAUGE_ASSERT (!curveName.isEmpty ());
158 const QPointF &posScreen,
160 m_isAxisPoint (false),
161 m_identifier (uniqueIdentifierGenerator(curveName)),
162 m_posScreen (posScreen),
163 m_hasPosGraph (false),
164 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
169 ENGAUGE_ASSERT (curveName != AXIS_CURVE_NAME);
171 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point(identifier,posScreen,posGraph,ordinal)"
172 <<
" identifierGenerated=" << m_identifier.toLatin1().data()
173 <<
" posScreen=" << QPointFToString (posScreen).toLatin1().data()
184 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::Point(const Point &other)"
185 <<
" isAxisPoint=" << (other.
isAxisPoint() ?
"true" :
"false")
186 <<
" identifier=" << other.
identifier ().toLatin1().data()
187 <<
" posScreen=" << QPointFToString (other.
posScreen ()).toLatin1().data()
188 <<
" hasPosGraph=" << (other.
hasPosGraph() ?
"true" :
"false")
189 <<
" posGraph=" << QPointFToString (other.
posGraph (SKIP_HAS_CHECK)).toLatin1().data()
190 <<
" hasOrdinal=" << (other.
hasOrdinal() ?
"true" :
"false")
191 <<
" ordinal=" << other.
ordinal (SKIP_HAS_CHECK)
192 <<
" isXOnly=" << other.
isXOnly ();
198 m_posGraph = other.
posGraph (SKIP_HAS_CHECK);
200 m_ordinal = other.
ordinal (SKIP_HAS_CHECK);
206 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::operator="
207 <<
" isAxisPoint=" << (point.
isAxisPoint() ?
"true" :
"false")
208 <<
" identifier=" << point.
identifier ().toLatin1().data()
209 <<
" posScreen=" << QPointFToString (point.
posScreen ()).toLatin1().data()
210 <<
" hasPosGraph=" << (point.
hasPosGraph() ?
"true" :
"false")
211 <<
" posGraph=" << QPointFToString (point.
posGraph (SKIP_HAS_CHECK)).toLatin1().data()
212 <<
" hasOrdinal=" << (point.
hasOrdinal() ?
"true" :
"false")
213 <<
" ordinal=" << point.
ordinal (SKIP_HAS_CHECK);
219 m_posGraph = point.
posGraph (SKIP_HAS_CHECK);
221 m_ordinal = point.
ordinal (SKIP_HAS_CHECK);
231 if (pointIdentifier.contains (POINT_IDENTIFIER_DELIMITER_SAFE)) {
233 tokens = pointIdentifier.split (POINT_IDENTIFIER_DELIMITER_SAFE);
239 tokens = pointIdentifier.split (POINT_IDENTIFIER_DELIMITER_XML);
243 return tokens.value (0);
246 QString Point::fixUnderscores (
const QString &identifier)
const
250 if (!identifier.contains (POINT_IDENTIFIER_DELIMITER_SAFE)) {
252 rtn = mutableIdentifier.replace (
"_", POINT_IDENTIFIER_DELIMITER_SAFE);
265 return m_hasPosGraph;
275 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::identifierIndex"
276 <<
" identifierIndex=" << m_identifierIndex;
278 return m_identifierIndex;
283 return m_isAxisPoint;
291 void Point::loadXml(QXmlStreamReader &reader)
293 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::loadXml";
297 QXmlStreamAttributes attributes = reader.attributes();
301 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER) &&
302 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX) &&
303 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT)) {
305 m_hasOrdinal = attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_ORDINAL);
307 m_ordinal = attributes.value(DOCUMENT_SERIALIZE_POINT_ORDINAL).toDouble();
309 m_ordinal = MISSING_ORDINAL_VALUE;
312 QString
isAxisPoint = attributes.value(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT).toString();
314 if (attributes.hasAttribute (DOCUMENT_SERIALIZE_POINT_IS_X_ONLY)) {
315 isXOnly = attributes.value(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY).toString();
318 m_identifier = fixUnderscores (attributes.value(DOCUMENT_SERIALIZE_POINT_IDENTIFIER).toString());
319 m_identifierIndex = attributes.value(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX).toInt();
320 m_isAxisPoint = (isAxisPoint == DOCUMENT_SERIALIZE_BOOL_TRUE);
321 m_hasPosGraph =
false;
322 m_posGraph.setX (MISSING_POSGRAPH_VALUE);
323 m_posGraph.setY (MISSING_POSGRAPH_VALUE);
324 m_isXOnly = (isXOnly == DOCUMENT_SERIALIZE_BOOL_TRUE);
326 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
327 (reader.name () != DOCUMENT_SERIALIZE_POINT)) {
329 loadNextFromReader(reader);
330 if (reader.atEnd()) {
335 if (reader.tokenType () == QXmlStreamReader::StartElement) {
337 if (reader.name() == DOCUMENT_SERIALIZE_POINT_POSITION_SCREEN) {
339 attributes = reader.attributes();
341 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_X) &&
342 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_Y)) {
344 m_posScreen.setX (attributes.value(DOCUMENT_SERIALIZE_POINT_X).toDouble());
345 m_posScreen.setY (attributes.value(DOCUMENT_SERIALIZE_POINT_Y).toDouble());
351 }
else if (reader.name() == DOCUMENT_SERIALIZE_POINT_POSITION_GRAPH) {
353 m_hasPosGraph =
true;
354 attributes = reader.attributes();
356 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_X) &&
357 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_Y)) {
359 m_posGraph.setX (attributes.value(DOCUMENT_SERIALIZE_POINT_X).toDouble());
360 m_posGraph.setY (attributes.value(DOCUMENT_SERIALIZE_POINT_Y).toDouble());
370 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::loadXml"
371 <<
" identifier=" << m_identifier.toLatin1().data()
372 <<
" identifierIndex=" << m_identifierIndex
373 <<
" posScreen=" << QPointFToString (m_posScreen).toLatin1().data()
374 <<
" posGraph=" << QPointFToString (m_posGraph).toLatin1().data()
375 <<
" ordinal=" << m_ordinal;
382 reader.raiseError(QObject::tr (
"Cannot read point data"));
388 if (applyHasCheck == KEEP_HAS_CHECK) {
389 ENGAUGE_ASSERT (m_hasOrdinal);
397 if (applyHasCheck == KEEP_HAS_CHECK) {
398 ENGAUGE_ASSERT (m_hasPosGraph);
410 QTextStream &str)
const
412 const QString UNDEFINED (
"undefined");
414 str << indentation <<
"Point\n";
416 indentation += INDENTATION_DELTA;
418 str << indentation <<
"identifier=" << m_identifier <<
"\n";
419 str << indentation <<
"posScreen=" << QPointFToString (m_posScreen) <<
"\n";
421 str << indentation <<
"posGraph=" << QPointFToString (m_posGraph) <<
"\n";
423 str << indentation <<
"posGraph=" << UNDEFINED <<
"\n";
426 str << indentation <<
"ordinal=" << m_ordinal <<
"\n";
428 str << indentation <<
"ordinal=" << UNDEFINED <<
"\n";
434 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::saveXml";
436 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT);
437 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER, m_identifier);
439 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_ORDINAL, QString::number (m_ordinal));
441 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT,
442 m_isAxisPoint ? DOCUMENT_SERIALIZE_BOOL_TRUE : DOCUMENT_SERIALIZE_BOOL_FALSE);
443 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY,
444 m_isXOnly ? DOCUMENT_SERIALIZE_BOOL_TRUE : DOCUMENT_SERIALIZE_BOOL_FALSE);
448 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX, QString::number (m_identifierIndex));
450 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_POSITION_SCREEN);
451 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_X, QString::number (m_posScreen.x()));
452 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_Y, QString::number (m_posScreen.y()));
453 writer.writeEndElement();
459 const char FORMAT =
'g';
460 const int PRECISION = 16;
462 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_POSITION_GRAPH);
463 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_X, QString::number (m_posGraph.x(), FORMAT, PRECISION));
464 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_Y, QString::number (m_posGraph.y(), FORMAT, PRECISION));
465 writer.writeEndElement();
468 writer.writeEndElement();
475 m_identifier = curveNameNew + m_identifier.mid (curveNameOld.length());
480 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::setIdentifierIndex"
488 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::setOrdinal"
489 <<
" identifier=" << m_identifier.toLatin1().data()
498 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::setPosGraph"
499 <<
" identifier=" << m_identifier.toLatin1().data()
500 <<
" posGraph=" << QPointFToString(posGraph).toLatin1().data();
504 ENGAUGE_ASSERT (m_isAxisPoint);
506 m_hasPosGraph =
true;
512 LOG4CPP_DEBUG_S ((*mainCat)) <<
"Point::setPosScreen"
513 <<
" identifier=" << m_identifier.toLatin1().data()
514 <<
" posScreen=" << QPointFToString(posScreen).toLatin1().data();
521 return QString (
"%1%2%3")
522 .arg (AXIS_CURVE_NAME)
523 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
527 QString Point::uniqueIdentifierGenerator (
const QString &curveName)
529 LOG4CPP_INFO_S ((*mainCat)) <<
"Point::uniqueIdentifierGenerator"
530 <<
" curveName=" << curveName.toLatin1().data()
531 <<
" identifierIndex=" << m_identifierIndex;
533 return QString (
"%1%2point%3%4")
535 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
536 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
537 .arg (m_identifierIndex++);
QPointF posGraph(ApplyHasCheck applyHasCheck=KEEP_HAS_CHECK) const
Accessor for graph position. Skip check if copying one instance to another.
static QString curveNameFromPointIdentifier(const QString &pointIdentifier)
Parse the curve name from the specified point identifier. This does the opposite of uniqueIdentifierG...
bool isAxisPoint() const
True if point is an axis point. This is used only for sanity checks.
static void setIdentifierIndex(unsigned int identifierIndex)
Reset the current index while performing a Redo.
static unsigned int identifierIndex()
Return the current index for storage in case we need to reset it later while performing a Redo...
void saveXml(QXmlStreamWriter &writer) const
Serialize to stream.
Class that represents one digitized point. The screen-to-graph coordinate transformation is always ex...
QPointF posScreen() const
Accessor for screen position.
Point & operator=(const Point &point)
Assignment constructor.
void setPosGraph(const QPointF &posGraph)
Set method for position in graph coordinates.
static QString temporaryPointIdentifier()
Point identifier for temporary point that is used by DigitzeStateAxis.
QString identifier() const
Unique identifier for a specific Point.
bool hasPosGraph() const
True if graph position is defined.
bool isXOnly() const
In DOCUMENT_AXES_POINTS_REQUIRED_4 modes, this is true/false if y/x coordinate is undefined...
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
void setPosScreen(const QPointF &posScreen)
Set method for position in screen coordinates.
Point()
Default constructor so this class can be used inside a container.
bool hasOrdinal() const
True if ordinal is defined.
double ordinal(ApplyHasCheck applyHasCheck=KEEP_HAS_CHECK) const
Get method for ordinal. Skip check if copying one instance to another.
void setOrdinal(double ordinal)
Set the ordinal used for ordering Points.
void setCurveName(const QString &curveName)
Update the point identifer to match the specified curve name.