00001
00002
00003
00004
00005
00006
00007 #include "CmdAddPointsGraph.h"
00008 #include "Document.h"
00009 #include "DocumentSerialize.h"
00010 #include "EngaugeAssert.h"
00011 #include "Logger.h"
00012 #include "MainWindow.h"
00013 #include <qdebug.h>
00014 #include "QtToString.h"
00015 #include <QXmlStreamReader>
00016 #include <QXmlStreamWriter>
00017 #include "Xml.h"
00018
00019 const QString CMD_DESCRIPTION ("Add graph points");
00020
00021 CmdAddPointsGraph::CmdAddPointsGraph (MainWindow &mainWindow,
00022 Document &document,
00023 const QString &curveName,
00024 const QList<QPoint> &points,
00025 const QList<double> &ordinals) :
00026 CmdPointChangeBase (mainWindow,
00027 document,
00028 CMD_DESCRIPTION),
00029 m_curveName (curveName),
00030 m_points (points),
00031 m_ordinals (ordinals)
00032 {
00033 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::CmdAddPointsGraph";
00034 }
00035
00036 CmdAddPointsGraph::CmdAddPointsGraph (MainWindow &mainWindow,
00037 Document &document,
00038 const QString &cmdDescription,
00039 QXmlStreamReader &reader) :
00040 CmdPointChangeBase (mainWindow,
00041 document,
00042 cmdDescription)
00043 {
00044 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::CmdAddPointsGraph";
00045
00046 QXmlStreamAttributes attributes = reader.attributes();
00047
00048 if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_CURVE_NAME)) {
00049 xmlExitWithError (reader,
00050 QString ("%1 %2")
00051 .arg (QObject::tr ("Missing attribute"))
00052 .arg (DOCUMENT_SERIALIZE_CURVE_NAME));
00053 }
00054
00055 m_curveName = attributes.value(DOCUMENT_SERIALIZE_CURVE_NAME).toString();
00056
00057 bool success = true;
00058 while (loadNextFromReader (reader)) {
00059
00060 if (reader.atEnd() || reader.hasError ()) {
00061 success = false;
00062 break;
00063 }
00064
00065 if ((reader.tokenType() == QXmlStreamReader::EndElement) &
00066 (reader.name() == DOCUMENT_SERIALIZE_CMD)) {
00067 break;
00068 }
00069
00070
00071 if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
00072 (reader.name() == DOCUMENT_SERIALIZE_POINT)) {
00073
00074
00075 QXmlStreamAttributes attributes = reader.attributes ();
00076
00077 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER) &&
00078 attributes.hasAttribute(DOCUMENT_SERIALIZE_ORDINAL) &&
00079 attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X) &&
00080 attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y)) {
00081
00082 m_identifiersAdded << attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
00083 m_ordinals << attributes.value(DOCUMENT_SERIALIZE_ORDINAL).toDouble();
00084
00085 QPoint point (attributes.value(DOCUMENT_SERIALIZE_SCREEN_X).toInt(),
00086 attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y).toInt());
00087 m_points << point;
00088 }
00089 }
00090 }
00091
00092 if (!success) {
00093 reader.raiseError (QObject::tr ("Cannot read graph points"));
00094 }
00095 }
00096
00097 CmdAddPointsGraph::~CmdAddPointsGraph ()
00098 {
00099 }
00100
00101 void CmdAddPointsGraph::cmdRedo ()
00102 {
00103 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdRedo";
00104
00105 saveOrCheckPreCommandDocumentStateHash (document ());
00106 saveDocumentState (document ());
00107 for (int index = 0; index < m_points.count(); index++) {
00108
00109 QString identifierAdded;
00110 document().addPointGraphWithGeneratedIdentifier (m_curveName,
00111 m_points.at (index),
00112 identifierAdded,
00113 m_ordinals.at (index));
00114 m_identifiersAdded.push_back (identifierAdded);
00115 }
00116
00117 document().updatePointOrdinals (mainWindow().transformation());
00118 mainWindow().updateAfterCommand();
00119 saveOrCheckPostCommandDocumentStateHash (document ());
00120 }
00121
00122 void CmdAddPointsGraph::cmdUndo ()
00123 {
00124 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdUndo";
00125
00126 saveOrCheckPostCommandDocumentStateHash (document ());
00127 restoreDocumentState (document ());
00128 mainWindow().updateAfterCommand();
00129 saveOrCheckPreCommandDocumentStateHash (document ());
00130 }
00131
00132 void CmdAddPointsGraph::saveXml (QXmlStreamWriter &writer) const
00133 {
00134 writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
00135 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_ADD_POINTS_GRAPH);
00136 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
00137 writer.writeAttribute(DOCUMENT_SERIALIZE_CURVE_NAME, m_curveName);
00138
00139 for (int index = 0; index < m_points.count(); index++) {
00140
00141 writer.writeStartElement (DOCUMENT_SERIALIZE_POINT);
00142 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X, QString::number (m_points.at (index).x()));
00143 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y, QString::number (m_points.at (index).y()));
00144
00145 QString identifier;
00146 if (index < m_identifiersAdded.count()) {
00147 identifier = m_identifiersAdded.at (index);
00148 }
00149
00150 writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, identifier);
00151 writer.writeAttribute(DOCUMENT_SERIALIZE_ORDINAL, QString::number (m_ordinals.at (index)));
00152 writer.writeEndElement();
00153 }
00154 writer.writeEndElement();
00155 }