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