Engauge Digitizer  2
CmdEditPointGraph.cpp
1 /******************************************************************************************************
2  * (C) 2016 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 "CmdEditPointGraph.h"
8 #include "Document.h"
9 #include "DocumentSerialize.h"
10 #include "EngaugeAssert.h"
11 #include "Logger.h"
12 #include "MainWindow.h"
13 #include <QTextStream>
14 #include "QtToString.h"
15 #include <QXmlStreamReader>
16 #include "Xml.h"
17 
18 const QString CMD_DESCRIPTION ("Edit curve points");
19 
21  Document &document,
22  const QStringList &pointIdentifiers,
23  bool isX,
24  bool isY,
25  double x,
26  double y) :
27  CmdPointChangeBase (mainWindow,
28  document,
29  CMD_DESCRIPTION),
30  m_pointIdentifiers (pointIdentifiers),
31  m_isX (isX),
32  m_isY (isY),
33  m_x (x),
34  m_y (y)
35 {
36  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::CmdEditPointGraph point="
37  << pointIdentifiers.join(" ").toLatin1 ().data ()
38  << " x=" << (m_isX ? QString::number (x).toLatin1().data() : "")
39  << " y=" << (m_isY ? QString::number (y).toLatin1().data() : "");
40 }
41 
44  const QString &cmdDescription,
45  QXmlStreamReader &reader) :
46  CmdPointChangeBase (mainWindow,
47  document,
48  cmdDescription)
49 {
50  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::CmdEditPointGraph";
51 
52  QXmlStreamAttributes attributes = reader.attributes();
53 
54  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X) ||
55  !attributes.hasAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y) ||
56  !attributes.hasAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_X) ||
57  !attributes.hasAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_Y) ) {
58  xmlExitWithError (reader,
59  QString ("%1 %2, %3, %4 %5 %6")
60  .arg (QObject::tr ("Missing attribute(s)"))
61  .arg (DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X)
62  .arg (DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y)
63  .arg (DOCUMENT_SERIALIZE_EDIT_GRAPH_X)
64  .arg (QObject::tr ("and/or"))
65  .arg (DOCUMENT_SERIALIZE_EDIT_GRAPH_Y));
66  } else {
67 
68  // Boolean attributes
69  QString isX = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X).toString();
70  QString isY = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y).toString();
71 
72  m_isX = (isX == DOCUMENT_SERIALIZE_BOOL_TRUE);
73  m_isY = (isY == DOCUMENT_SERIALIZE_BOOL_TRUE);
74  m_x = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_X).toDouble();
75  m_y = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_Y).toDouble();
76 
77  bool success = true;
78  while (loadNextFromReader (reader)) {
79 
80  if (reader.atEnd() || reader.hasError ()) {
81  success = false;
82  break;
83  }
84 
85  if ((reader.tokenType() == QXmlStreamReader::EndElement) &
86  (reader.name() == DOCUMENT_SERIALIZE_CMD)) {
87  break;
88  }
89 
90  // Not done yet
91  if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
92  (reader.name() == DOCUMENT_SERIALIZE_POINT)) {
93 
94  // This is an entry that we need to add
95  QXmlStreamAttributes attributes = reader.attributes ();
96 
97  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER)) {
98 
99  m_pointIdentifiers << attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
100  }
101  }
102  }
103 
104  if (!success) {
105  reader.raiseError (QObject::tr ("Cannot read graph points"));
106  }
107  }
108 }
109 
110 CmdEditPointGraph::~CmdEditPointGraph ()
111 {
112 }
113 
115 {
116  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::cmdRedo";
117 
120  document().editPointGraph (m_isX,
121  m_isY,
122  m_x,
123  m_y,
124  m_pointIdentifiers,
125  mainWindow().transformation());
126  document().updatePointOrdinals (mainWindow().transformation());
129 }
130 
132 {
133  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::cmdUndo";
134 
139 }
140 
141 void CmdEditPointGraph::saveXml (QXmlStreamWriter &writer) const
142 {
143  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
144  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_EDIT_POINT_GRAPH);
145  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
146  writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X, m_isX ?
147  DOCUMENT_SERIALIZE_BOOL_TRUE :
148  DOCUMENT_SERIALIZE_BOOL_FALSE);
149  writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y, m_isY ?
150  DOCUMENT_SERIALIZE_BOOL_TRUE :
151  DOCUMENT_SERIALIZE_BOOL_FALSE);
152  writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_X, QString::number (m_x));
153  writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_Y, QString::number (m_y));
154 
155  for (int index = 0; index < m_pointIdentifiers.count(); index++) {
156 
157  writer.writeStartElement (DOCUMENT_SERIALIZE_POINT);
158  writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, m_pointIdentifiers.at (index));
159  writer.writeEndElement();
160  }
161  writer.writeEndElement();
162 }
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
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.
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
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
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:35
void editPointGraph(bool isX, bool isY, double x, double y, const QStringList &identifiers, const Transformation &transformation)
Edit the graph coordinates of one or more graph points.
Definition: Document.cpp:374
CmdEditPointGraph(MainWindow &mainWindow, Document &document, const QStringList &pointIdentifiers, bool isX, bool isY, double x, double y)
Constructor for normal creation.
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.