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 "CmdAddPointsGraph.h"
8 #include "Document.h"
9 #include "DocumentSerialize.h"
10 #include "EngaugeAssert.h"
11 #include "Logger.h"
12 #include "MainWindow.h"
13 #include <qdebug.h>
14 #include "QtToString.h"
15 #include <QXmlStreamReader>
16 #include <QXmlStreamWriter>
17 #include "Xml.h"
18 
19 const QString CMD_DESCRIPTION ("Add graph points");
20 
CmdAddPointsGraph(MainWindow & mainWindow,Document & document,const QString & curveName,const QList<QPoint> & points,const QList<double> & ordinals)21 CmdAddPointsGraph::CmdAddPointsGraph (MainWindow &mainWindow,
22                                       Document &document,
23                                       const QString &curveName,
24                                       const QList<QPoint> &points,
25                                       const QList<double> &ordinals) :
26   CmdPointChangeBase (mainWindow,
27                       document,
28                       CMD_DESCRIPTION),
29   m_curveName (curveName),
30   m_points (points),
31   m_ordinals (ordinals)
32 {
33   LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::CmdAddPointsGraph";
34 }
35 
CmdAddPointsGraph(MainWindow & mainWindow,Document & document,const QString & cmdDescription,QXmlStreamReader & reader)36 CmdAddPointsGraph::CmdAddPointsGraph (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)) << "CmdAddPointsGraph::CmdAddPointsGraph";
45 
46   QXmlStreamAttributes attributes = reader.attributes();
47 
48   if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_CURVE_NAME)) {
49     xmlExitWithError (reader,
50                       QString ("%1 %2")
51                       .arg (QObject::tr ("Missing attribute"))
52                       .arg (DOCUMENT_SERIALIZE_CURVE_NAME));
53   }
54 
55   m_curveName = attributes.value(DOCUMENT_SERIALIZE_CURVE_NAME).toString();
56 
57   bool success = true;
58   while (loadNextFromReader (reader)) {
59 
60     if (reader.atEnd() || reader.hasError ()) {
61       success = false;
62       break;
63     }
64 
65     if ((reader.tokenType() == QXmlStreamReader::EndElement) &
66         (reader.name() == DOCUMENT_SERIALIZE_CMD)) {
67       break;
68     }
69 
70     // Not done yet
71     if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
72         (reader.name() == DOCUMENT_SERIALIZE_POINT)) {
73 
74       // This is an entry that we need to add
75       QXmlStreamAttributes attributes = reader.attributes ();
76 
77       if (attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER) &&
78           attributes.hasAttribute(DOCUMENT_SERIALIZE_ORDINAL) &&
79           attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X) &&
80           attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y)) {
81 
82         m_identifiersAdded << attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
83         m_ordinals << attributes.value(DOCUMENT_SERIALIZE_ORDINAL).toDouble();
84 
85         QPoint point (attributes.value(DOCUMENT_SERIALIZE_SCREEN_X).toInt(),
86                       attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y).toInt());
87         m_points << point;
88       }
89     }
90   }
91 
92   if (!success) {
93     reader.raiseError (QObject::tr ("Cannot read graph points"));
94   }
95 }
96 
~CmdAddPointsGraph()97 CmdAddPointsGraph::~CmdAddPointsGraph ()
98 {
99 }
100 
cmdRedo()101 void CmdAddPointsGraph::cmdRedo ()
102 {
103   LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdRedo";
104 
105   saveOrCheckPreCommandDocumentStateHash (document ());
106   saveDocumentState (document ());
107   for (int index = 0; index < m_points.count(); index++) {
108 
109     QString identifierAdded;
110     document().addPointGraphWithGeneratedIdentifier (m_curveName,
111                                                      m_points.at (index),
112                                                      identifierAdded,
113                                                      m_ordinals.at (index));
114     m_identifiersAdded.push_back (identifierAdded);
115   }
116 
117   document().updatePointOrdinals (mainWindow().transformation());
118   mainWindow().updateAfterCommand();
119   saveOrCheckPostCommandDocumentStateHash (document ());
120 }
121 
cmdUndo()122 void CmdAddPointsGraph::cmdUndo ()
123 {
124   LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdUndo";
125 
126   saveOrCheckPostCommandDocumentStateHash (document ());
127   restoreDocumentState (document ());
128   mainWindow().updateAfterCommand();
129   saveOrCheckPreCommandDocumentStateHash (document ());
130 }
131 
saveXml(QXmlStreamWriter & writer) const132 void CmdAddPointsGraph::saveXml (QXmlStreamWriter &writer) const
133 {
134   writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
135   writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_ADD_POINTS_GRAPH);
136   writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
137   writer.writeAttribute(DOCUMENT_SERIALIZE_CURVE_NAME, m_curveName);
138 
139   for (int index = 0; index < m_points.count(); index++) {
140 
141     writer.writeStartElement (DOCUMENT_SERIALIZE_POINT);
142     writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X, QString::number (m_points.at (index).x()));
143     writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y, QString::number (m_points.at (index).y()));
144 
145     QString identifier;
146     if (index < m_identifiersAdded.count()) {
147       identifier = m_identifiersAdded.at (index);
148     }
149 
150     writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, identifier);
151     writer.writeAttribute(DOCUMENT_SERIALIZE_ORDINAL, QString::number (m_ordinals.at (index)));
152     writer.writeEndElement();
153   }
154   writer.writeEndElement();
155 }
156