1 /******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7 #include "CmdAddPointGraph.h"
8 #include "Document.h"
9 #include "DocumentSerialize.h"
10 #include "EngaugeAssert.h"
11 #include "Logger.h"
12 #include "MainWindow.h"
13 #include "QtToString.h"
14 #include <QXmlStreamReader>
15 #include "Xml.h"
16
17 const QString CMD_DESCRIPTION ("Add graph point");
18
CmdAddPointGraph(MainWindow & mainWindow,Document & document,const QString & curveName,const QPointF & posScreen,double ordinal)19 CmdAddPointGraph::CmdAddPointGraph (MainWindow &mainWindow,
20 Document &document,
21 const QString &curveName,
22 const QPointF &posScreen,
23 double ordinal) :
24 CmdPointChangeBase (mainWindow,
25 document,
26 CMD_DESCRIPTION),
27 m_curveName (curveName),
28 m_posScreen (posScreen),
29 m_ordinal (ordinal)
30 {
31 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::CmdAddPointGraph"
32 << " posScreen=" << QPointFToString (posScreen).toLatin1 ().data ()
33 << " ordinal=" << m_ordinal;
34 }
35
CmdAddPointGraph(MainWindow & mainWindow,Document & document,const QString & cmdDescription,QXmlStreamReader & reader)36 CmdAddPointGraph::CmdAddPointGraph (MainWindow &mainWindow,
37 Document &document,
38 const QString &cmdDescription,
39 QXmlStreamReader &reader) :
40 CmdPointChangeBase (mainWindow,
41 document,
42 cmdDescription)
43 {
44 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::CmdAddPointGraph";
45
46 QXmlStreamAttributes attributes = reader.attributes();
47
48 if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X) ||
49 !attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y) ||
50 !attributes.hasAttribute(DOCUMENT_SERIALIZE_CURVE_NAME) ||
51 !attributes.hasAttribute(DOCUMENT_SERIALIZE_ORDINAL) ||
52 !attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER)) {
53 xmlExitWithError (reader,
54 QString ("Missing attribute(s) %1, %2, %3, %4 and/or %5")
55 .arg (DOCUMENT_SERIALIZE_SCREEN_X)
56 .arg (DOCUMENT_SERIALIZE_SCREEN_Y)
57 .arg (DOCUMENT_SERIALIZE_CURVE_NAME)
58 .arg (DOCUMENT_SERIALIZE_ORDINAL)
59 .arg (DOCUMENT_SERIALIZE_IDENTIFIER));
60 }
61
62 m_posScreen.setX(attributes.value(DOCUMENT_SERIALIZE_SCREEN_X).toDouble());
63 m_posScreen.setY(attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y).toDouble());
64 m_curveName = attributes.value(DOCUMENT_SERIALIZE_CURVE_NAME).toString();
65 m_identifierAdded = attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
66 m_ordinal = attributes.value(DOCUMENT_SERIALIZE_ORDINAL).toDouble();
67 }
68
~CmdAddPointGraph()69 CmdAddPointGraph::~CmdAddPointGraph ()
70 {
71 }
72
cmdRedo()73 void CmdAddPointGraph::cmdRedo ()
74 {
75 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::cmdRedo";
76
77 saveOrCheckPreCommandDocumentStateHash (document ());
78 saveDocumentState (document ());
79 document().addPointGraphWithGeneratedIdentifier (m_curveName,
80 m_posScreen,
81 m_identifierAdded,
82 m_ordinal);
83 document().updatePointOrdinals (mainWindow().transformation());
84 mainWindow().updateAfterCommand();
85 saveOrCheckPostCommandDocumentStateHash (document ());
86 }
87
cmdUndo()88 void CmdAddPointGraph::cmdUndo ()
89 {
90 LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::cmdUndo";
91
92 saveOrCheckPostCommandDocumentStateHash (document ());
93 restoreDocumentState (document ());
94 mainWindow().updateAfterCommand();
95 saveOrCheckPreCommandDocumentStateHash (document ());
96 }
97
saveXml(QXmlStreamWriter & writer) const98 void CmdAddPointGraph::saveXml (QXmlStreamWriter &writer) const
99 {
100 writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
101 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_ADD_POINT_GRAPH);
102 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
103 writer.writeAttribute(DOCUMENT_SERIALIZE_CURVE_NAME, m_curveName);
104 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X, QString::number (m_posScreen.x()));
105 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y, QString::number (m_posScreen.y()));
106 writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, m_identifierAdded);
107 writer.writeAttribute(DOCUMENT_SERIALIZE_ORDINAL, QString::number (m_ordinal));
108 writer.writeEndElement();
109 }
110