00001
00002
00003
00004
00005
00006
00007 #include "DocumentSerialize.h"
00008 #include "EngaugeAssert.h"
00009 #include "Logger.h"
00010 #include "Point.h"
00011 #include <QObject>
00012 #include <QStringList>
00013 #include <QTextStream>
00014 #include "QtToString.h"
00015 #include <QXmlStreamReader>
00016 #include <QXmlStreamWriter>
00017 #include "Xml.h"
00018
00019 unsigned int Point::m_identifierIndex = 0;
00020
00021 extern const QString AXIS_CURVE_NAME;
00022 extern const QString DUMMY_CURVE_NAME;
00023 const QString POINT_IDENTIFIER_DELIMITER_SAFE ("\t");
00024 const QString POINT_IDENTIFIER_DELIMITER_XML ("_");
00025
00026 const double MISSING_ORDINAL_VALUE = 0;
00027 const double MISSING_POSGRAPH_VALUE = 0;
00028
00029 Point::Point ()
00030 {
00031 }
00032
00033 Point::Point(const QString &curveName,
00034 const QPointF &posScreen) :
00035 m_isAxisPoint (curveName == AXIS_CURVE_NAME),
00036 m_identifier (uniqueIdentifierGenerator(curveName)),
00037 m_posScreen (posScreen),
00038 m_hasPosGraph (false),
00039 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
00040 m_hasOrdinal (false),
00041 m_ordinal (MISSING_ORDINAL_VALUE),
00042 m_isXOnly (false)
00043 {
00044 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point"
00045 << " curveName=" << curveName.toLatin1().data()
00046 << " identifierGenerated=" << m_identifier.toLatin1().data()
00047 << " posScreen=" << QPointFToString (posScreen).toLatin1().data();
00048
00049 ENGAUGE_ASSERT (!curveName.isEmpty ());
00050 }
00051
00052 Point::Point(const QString &curveName,
00053 const QPointF &posScreen,
00054 const QPointF &posGraph,
00055 bool isXOnly) :
00056 m_isAxisPoint (true),
00057 m_identifier (uniqueIdentifierGenerator(curveName)),
00058 m_posScreen (posScreen),
00059 m_hasPosGraph (true),
00060 m_posGraph (posGraph),
00061 m_hasOrdinal (false),
00062 m_ordinal (MISSING_ORDINAL_VALUE),
00063 m_isXOnly (isXOnly)
00064 {
00065 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME ||
00066 curveName == DUMMY_CURVE_NAME);
00067
00068 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point"
00069 << " curveName=" << curveName.toLatin1().data()
00070 << " identifierGenerated=" << m_identifier.toLatin1().data()
00071 << " posScreen=" << QPointFToString (posScreen).toLatin1().data()
00072 << " posGraph=" << QPointFToString (posGraph).toLatin1().data()
00073 << " isXOnly=" << (isXOnly ? "true" : "false");
00074
00075 ENGAUGE_ASSERT (!curveName.isEmpty ());
00076 }
00077
00078 Point::Point(const QString &curveName,
00079 const QString &identifier,
00080 const QPointF &posScreen,
00081 const QPointF &posGraph,
00082 double ordinal,
00083 bool isXOnly) :
00084 m_isAxisPoint (true),
00085 m_identifier (identifier),
00086 m_posScreen (posScreen),
00087 m_hasPosGraph (true),
00088 m_posGraph (posGraph),
00089 m_hasOrdinal (true),
00090 m_ordinal (ordinal),
00091 m_isXOnly (isXOnly)
00092 {
00093 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME);
00094
00095 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point"
00096 << " curveName=" << curveName.toLatin1().data()
00097 << " identifier=" << m_identifier.toLatin1().data()
00098 << " posScreen=" << QPointFToString (posScreen).toLatin1().data()
00099 << " posGraph=" << QPointFToString (posGraph).toLatin1().data()
00100 << " ordinal=" << ordinal
00101 << " isXOnly=" << (isXOnly ? "true" : "false");
00102
00103 ENGAUGE_ASSERT (!curveName.isEmpty ());
00104 }
00105
00106 Point::Point(const QString &curveName,
00107 const QPointF &posScreen,
00108 const QPointF &posGraph,
00109 double ordinal,
00110 bool isXOnly) :
00111 m_isAxisPoint (true),
00112 m_identifier (uniqueIdentifierGenerator(curveName)),
00113 m_posScreen (posScreen),
00114 m_hasPosGraph (true),
00115 m_posGraph (posGraph),
00116 m_hasOrdinal (true),
00117 m_ordinal (ordinal),
00118 m_isXOnly (isXOnly)
00119 {
00120 ENGAUGE_ASSERT (curveName == AXIS_CURVE_NAME);
00121
00122 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point"
00123 << " curveName=" << curveName.toLatin1().data()
00124 << " identifierGenerated=" << m_identifier.toLatin1().data()
00125 << " posScreen=" << QPointFToString (posScreen).toLatin1().data()
00126 << " posGraph=" << QPointFToString (posGraph).toLatin1().data()
00127 << " ordinal=" << ordinal
00128 << " isXOnly=" << (isXOnly ? "true" : "false");
00129
00130 ENGAUGE_ASSERT (!curveName.isEmpty ());
00131 }
00132
00133 Point::Point(const QString &curveName,
00134 const QString &identifier,
00135 const QPointF &posScreen,
00136 double ordinal) :
00137 m_isAxisPoint (false),
00138 m_identifier (identifier),
00139 m_posScreen (posScreen),
00140 m_hasPosGraph (false),
00141 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
00142 m_hasOrdinal (true),
00143 m_ordinal (ordinal),
00144 m_isXOnly (false)
00145 {
00146 ENGAUGE_ASSERT (curveName != AXIS_CURVE_NAME);
00147
00148 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point"
00149 << " curveName=" << curveName.toLatin1().data()
00150 << " identifier=" << identifier.toLatin1().data()
00151 << " posScreen=" << QPointFToString (posScreen).toLatin1().data()
00152 << " ordinal=" << ordinal;
00153
00154 ENGAUGE_ASSERT (!curveName.isEmpty ());
00155 }
00156
00157 Point::Point (const QString &curveName,
00158 const QPointF &posScreen,
00159 double ordinal) :
00160 m_isAxisPoint (false),
00161 m_identifier (uniqueIdentifierGenerator(curveName)),
00162 m_posScreen (posScreen),
00163 m_hasPosGraph (false),
00164 m_posGraph (MISSING_POSGRAPH_VALUE, MISSING_POSGRAPH_VALUE),
00165 m_hasOrdinal (true),
00166 m_ordinal (ordinal),
00167 m_isXOnly (false)
00168 {
00169 ENGAUGE_ASSERT (curveName != AXIS_CURVE_NAME);
00170
00171 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point(identifier,posScreen,posGraph,ordinal)"
00172 << " identifierGenerated=" << m_identifier.toLatin1().data()
00173 << " posScreen=" << QPointFToString (posScreen).toLatin1().data()
00174 << " ordinal=" << ordinal;
00175 }
00176
00177 Point::Point (QXmlStreamReader &reader)
00178 {
00179 loadXml(reader);
00180 }
00181
00182 Point::Point (const Point &other)
00183 {
00184 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::Point(const Point &other)"
00185 << " isAxisPoint=" << (other.isAxisPoint() ? "true" : "false")
00186 << " identifier=" << other.identifier ().toLatin1().data()
00187 << " posScreen=" << QPointFToString (other.posScreen ()).toLatin1().data()
00188 << " hasPosGraph=" << (other.hasPosGraph() ? "true" : "false")
00189 << " posGraph=" << QPointFToString (other.posGraph (SKIP_HAS_CHECK)).toLatin1().data()
00190 << " hasOrdinal=" << (other.hasOrdinal() ? "true" : "false")
00191 << " ordinal=" << other.ordinal (SKIP_HAS_CHECK)
00192 << " isXOnly=" << other.isXOnly ();
00193
00194 m_isAxisPoint = other.isAxisPoint ();
00195 m_identifier = other.identifier ();
00196 m_posScreen = other.posScreen ();
00197 m_hasPosGraph = other.hasPosGraph ();
00198 m_posGraph = other.posGraph (SKIP_HAS_CHECK);
00199 m_hasOrdinal = other.hasOrdinal ();
00200 m_ordinal = other.ordinal (SKIP_HAS_CHECK);
00201 m_isXOnly = other.isXOnly ();
00202 }
00203
00204 Point &Point::operator=(const Point &point)
00205 {
00206 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::operator="
00207 << " isAxisPoint=" << (point.isAxisPoint() ? "true" : "false")
00208 << " identifier=" << point.identifier ().toLatin1().data()
00209 << " posScreen=" << QPointFToString (point.posScreen ()).toLatin1().data()
00210 << " hasPosGraph=" << (point.hasPosGraph() ? "true" : "false")
00211 << " posGraph=" << QPointFToString (point.posGraph (SKIP_HAS_CHECK)).toLatin1().data()
00212 << " hasOrdinal=" << (point.hasOrdinal() ? "true" : "false")
00213 << " ordinal=" << point.ordinal (SKIP_HAS_CHECK);
00214
00215 m_isAxisPoint = point.isAxisPoint ();
00216 m_identifier = point.identifier ();
00217 m_posScreen = point.posScreen ();
00218 m_hasPosGraph = point.hasPosGraph ();
00219 m_posGraph = point.posGraph (SKIP_HAS_CHECK);
00220 m_hasOrdinal = point.hasOrdinal ();
00221 m_ordinal = point.ordinal (SKIP_HAS_CHECK);
00222 m_isXOnly = point.isXOnly ();
00223
00224 return *this;
00225 }
00226
00227 QString Point::curveNameFromPointIdentifier (const QString &pointIdentifier)
00228 {
00229 QStringList tokens;
00230
00231 if (pointIdentifier.contains (POINT_IDENTIFIER_DELIMITER_SAFE)) {
00232
00233 tokens = pointIdentifier.split (POINT_IDENTIFIER_DELIMITER_SAFE);
00234
00235 } else {
00236
00237
00238
00239 tokens = pointIdentifier.split (POINT_IDENTIFIER_DELIMITER_XML);
00240
00241 }
00242
00243 return tokens.value (0);
00244 }
00245
00246 bool Point::hasOrdinal () const
00247 {
00248 return m_hasOrdinal;
00249 }
00250
00251 bool Point::hasPosGraph () const
00252 {
00253 return m_hasPosGraph;
00254 }
00255
00256 QString Point::identifier() const
00257 {
00258 return m_identifier;
00259 }
00260
00261 unsigned int Point::identifierIndex ()
00262 {
00263 LOG4CPP_INFO_S ((*mainCat)) << "Point::identifierIndex"
00264 << " identifierIndex=" << m_identifierIndex;
00265
00266 return m_identifierIndex;
00267 }
00268
00269 bool Point::isAxisPoint() const
00270 {
00271 return m_isAxisPoint;
00272 }
00273
00274 bool Point::isXOnly() const
00275 {
00276 return m_isXOnly;
00277 }
00278
00279 void Point::loadXml(QXmlStreamReader &reader)
00280 {
00281 LOG4CPP_INFO_S ((*mainCat)) << "Point::loadXml";
00282
00283 bool success = true;
00284
00285 QXmlStreamAttributes attributes = reader.attributes();
00286
00287
00288
00289 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER) &&
00290 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX) &&
00291 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT)) {
00292
00293 m_hasOrdinal = attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_ORDINAL);
00294 if (m_hasOrdinal) {
00295 m_ordinal = attributes.value(DOCUMENT_SERIALIZE_POINT_ORDINAL).toDouble();
00296 } else {
00297 m_ordinal = MISSING_ORDINAL_VALUE;
00298 }
00299
00300 QString isAxisPoint = attributes.value(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT).toString();
00301 QString isXOnly;
00302 if (attributes.hasAttribute (DOCUMENT_SERIALIZE_POINT_IS_X_ONLY)) {
00303 isXOnly = attributes.value(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY).toString();
00304 }
00305
00306 m_identifier = attributes.value(DOCUMENT_SERIALIZE_POINT_IDENTIFIER).toString();
00307 m_identifierIndex = attributes.value(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX).toInt();
00308 m_isAxisPoint = (isAxisPoint == DOCUMENT_SERIALIZE_BOOL_TRUE);
00309 m_hasPosGraph = false;
00310 m_posGraph.setX (MISSING_POSGRAPH_VALUE);
00311 m_posGraph.setY (MISSING_POSGRAPH_VALUE);
00312 m_isXOnly = (isXOnly == DOCUMENT_SERIALIZE_BOOL_TRUE);
00313
00314 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
00315 (reader.name () != DOCUMENT_SERIALIZE_POINT)) {
00316
00317 loadNextFromReader(reader);
00318 if (reader.atEnd()) {
00319 success = false;
00320 break;
00321 }
00322
00323 if (reader.tokenType () == QXmlStreamReader::StartElement) {
00324
00325 if (reader.name() == DOCUMENT_SERIALIZE_POINT_POSITION_SCREEN) {
00326
00327 attributes = reader.attributes();
00328
00329 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_X) &&
00330 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_Y)) {
00331
00332 m_posScreen.setX (attributes.value(DOCUMENT_SERIALIZE_POINT_X).toDouble());
00333 m_posScreen.setY (attributes.value(DOCUMENT_SERIALIZE_POINT_Y).toDouble());
00334
00335 } else {
00336 success = false;
00337 break;
00338 }
00339 } else if (reader.name() == DOCUMENT_SERIALIZE_POINT_POSITION_GRAPH) {
00340
00341 m_hasPosGraph = true;
00342 attributes = reader.attributes();
00343
00344 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_X) &&
00345 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_Y)) {
00346
00347 m_posGraph.setX (attributes.value(DOCUMENT_SERIALIZE_POINT_X).toDouble());
00348 m_posGraph.setY (attributes.value(DOCUMENT_SERIALIZE_POINT_Y).toDouble());
00349
00350 } else {
00351 success = false;
00352 break;
00353 }
00354 }
00355 }
00356 }
00357
00358 LOG4CPP_INFO_S ((*mainCat)) << "Point::loadXml"
00359 << " identifier=" << m_identifier.toLatin1().data()
00360 << " identifierIndex=" << m_identifierIndex
00361 << " posScreen=" << QPointFToString (m_posScreen).toLatin1().data()
00362 << " posGraph=" << QPointFToString (m_posGraph).toLatin1().data()
00363 << " ordinal=" << m_ordinal;
00364
00365 } else {
00366 success = false;
00367 }
00368
00369 if (!success) {
00370 reader.raiseError(QObject::tr ("Cannot read point data"));
00371 }
00372 }
00373
00374 double Point::ordinal (ApplyHasCheck applyHasCheck) const
00375 {
00376 if (applyHasCheck == KEEP_HAS_CHECK) {
00377 ENGAUGE_ASSERT (m_hasOrdinal);
00378 }
00379
00380 return m_ordinal;
00381 }
00382
00383 QPointF Point::posGraph (ApplyHasCheck applyHasCheck) const
00384 {
00385 if (applyHasCheck == KEEP_HAS_CHECK) {
00386 ENGAUGE_ASSERT (m_hasPosGraph);
00387 }
00388
00389 return m_posGraph;
00390 }
00391
00392 QPointF Point::posScreen () const
00393 {
00394 return m_posScreen;
00395 }
00396
00397 void Point::printStream(QString indentation,
00398 QTextStream &str) const
00399 {
00400 const QString UNDEFINED ("undefined");
00401
00402 str << indentation << "Point\n";
00403
00404 indentation += INDENTATION_DELTA;
00405
00406 str << indentation << "identifier=" << m_identifier << "\n";
00407 str << indentation << "posScreen=" << QPointFToString (m_posScreen) << "\n";
00408 if (m_hasPosGraph) {
00409 str << indentation << "posGraph=" << QPointFToString (m_posGraph) << "\n";
00410 } else {
00411 str << indentation << "posGraph=" << UNDEFINED << "\n";
00412 }
00413 if (m_hasOrdinal) {
00414 str << indentation << "ordinal=" << m_ordinal << "\n";
00415 } else {
00416 str << indentation << "ordinal=" << UNDEFINED << "\n";
00417 }
00418 }
00419
00420 void Point::saveXml(QXmlStreamWriter &writer) const
00421 {
00422 LOG4CPP_INFO_S ((*mainCat)) << "Point::saveXml";
00423
00424 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT);
00425 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER, m_identifier);
00426 if (m_hasOrdinal) {
00427 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_ORDINAL, QString::number (m_ordinal));
00428 }
00429 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IS_AXIS_POINT,
00430 m_isAxisPoint ? DOCUMENT_SERIALIZE_BOOL_TRUE : DOCUMENT_SERIALIZE_BOOL_FALSE);
00431 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY,
00432 m_isXOnly ? DOCUMENT_SERIALIZE_BOOL_TRUE : DOCUMENT_SERIALIZE_BOOL_FALSE);
00433
00434
00435
00436 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IDENTIFIER_INDEX, QString::number (m_identifierIndex));
00437
00438 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_POSITION_SCREEN);
00439 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_X, QString::number (m_posScreen.x()));
00440 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_Y, QString::number (m_posScreen.y()));
00441 writer.writeEndElement();
00442
00443 if (m_hasPosGraph) {
00444 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_POSITION_GRAPH);
00445 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_X, QString::number (m_posGraph.x()));
00446 writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_Y, QString::number (m_posGraph.y()));
00447 writer.writeEndElement();
00448 }
00449
00450 writer.writeEndElement();
00451 }
00452
00453 void Point::setCurveName(const QString &curveNameNew)
00454 {
00455
00456 QString curveNameOld = Point::curveNameFromPointIdentifier (m_identifier);
00457 m_identifier = curveNameNew + m_identifier.mid (curveNameOld.length());
00458 }
00459
00460 void Point::setIdentifierIndex (unsigned int identifierIndex)
00461 {
00462 LOG4CPP_INFO_S ((*mainCat)) << "Point::setIdentifierIndex"
00463 << " identifierIndex=" << identifierIndex;
00464
00465 m_identifierIndex = identifierIndex;
00466 }
00467
00468 void Point::setOrdinal(double ordinal)
00469 {
00470 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::setOrdinal"
00471 << " identifier=" << m_identifier.toLatin1().data()
00472 << " ordinal=" << ordinal;
00473
00474 m_hasOrdinal = true;
00475 m_ordinal = ordinal;
00476 }
00477
00478 void Point::setPosGraph (const QPointF &posGraph)
00479 {
00480 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::setPosGraph"
00481 << " identifier=" << m_identifier.toLatin1().data()
00482 << " posGraph=" << QPointFToString(posGraph).toLatin1().data();
00483
00484
00485
00486 ENGAUGE_ASSERT (m_isAxisPoint);
00487
00488 m_hasPosGraph = true;
00489 m_posGraph = posGraph;
00490 }
00491
00492 void Point::setPosScreen (const QPointF &posScreen)
00493 {
00494 LOG4CPP_DEBUG_S ((*mainCat)) << "Point::setPosScreen"
00495 << " identifier=" << m_identifier.toLatin1().data()
00496 << " posScreen=" << QPointFToString(posScreen).toLatin1().data();
00497
00498 m_posScreen = posScreen;
00499 }
00500
00501 QString Point::temporaryPointIdentifier ()
00502 {
00503 return QString ("%1%2%3")
00504 .arg (AXIS_CURVE_NAME)
00505 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
00506 .arg (0);
00507 }
00508
00509 QString Point::uniqueIdentifierGenerator (const QString &curveName)
00510 {
00511 LOG4CPP_INFO_S ((*mainCat)) << "Point::uniqueIdentifierGenerator"
00512 << " curveName=" << curveName.toLatin1().data()
00513 << " identifierIndex=" << m_identifierIndex;
00514
00515 return QString ("%1%2point%3%4")
00516 .arg (curveName)
00517 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
00518 .arg (POINT_IDENTIFIER_DELIMITER_SAFE)
00519 .arg (m_identifierIndex++);
00520 }