Engauge Digitizer  2
CmdAddPointGraph.cpp
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 
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 
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 
69 CmdAddPointGraph::~CmdAddPointGraph ()
70 {
71 }
72 
74 {
75  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::cmdRedo";
76 
80  m_posScreen,
81  m_identifierAdded,
82  m_ordinal);
83  document().updatePointOrdinals (mainWindow().transformation());
86 }
87 
89 {
90  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointGraph::cmdUndo";
91 
96 }
97 
98 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 }
void addPointGraphWithGeneratedIdentifier(const QString &curveName, const QPointF &posScreen, QString &generatedIentifier, double ordinal)
Add a single graph point with a generated point identifier.
Definition: Document.cpp:192
void restoreDocumentState(Document &document) const
Restore the document previously saved by saveDocumentState.
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
void saveOrCheckPostCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
void saveOrCheckPreCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
Base class for CmdBase leaf subclasses that involve point additions, deletions and/or modifications...
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:45
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
CmdAddPointGraph(MainWindow &mainWindow, Document &document, const QString &curveName, const QPointF &posScreen, double ordinal)
Constructor for normal creation.
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:35
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
Definition: Document.cpp:1060
void saveDocumentState(const Document &document)
Save the document state for restoration by restoreDocumentState.