Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
CmdAddPointsGraph.cpp
1 #include "CmdAddPointsGraph.h"
2 #include "Document.h"
3 #include "DocumentSerialize.h"
4 #include "EngaugeAssert.h"
5 #include "Logger.h"
6 #include "MainWindow.h"
7 #include "QtToString.h"
8 #include <QXmlStreamReader>
9 #include <QXmlStreamWriter>
10 #include "Xml.h"
11 
12 const QString CMD_DESCRIPTION ("Add graph points");
13 
15  Document &document,
16  const QString &curveName,
17  const QList<QPoint> &points,
18  const QList<double> &ordinals) :
19  CmdAbstract (mainWindow,
20  document,
21  CMD_DESCRIPTION),
22  m_curveName (curveName),
23  m_points (points),
24  m_ordinals (ordinals)
25 {
26  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::CmdAddPointsGraph";
27 }
28 
30  Document &document,
31  const QString &cmdDescription,
32  QXmlStreamReader &reader) :
33  CmdAbstract (mainWindow,
34  document,
35  cmdDescription)
36 {
37  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::CmdAddPointsGraph";
38 
39  QXmlStreamAttributes attributes = reader.attributes();
40 
41  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_CURVE_NAME)) {
42  ENGAUGE_ASSERT (false);
43  }
44 
45  m_curveName = attributes.value(DOCUMENT_SERIALIZE_CURVE_NAME).toString();
46 
47  bool success = true;
48  while (loadNextFromReader (reader)) {
49 
50  if (reader.atEnd() || reader.hasError ()) {
51  success = false;
52  break;
53  }
54 
55  if ((reader.tokenType() == QXmlStreamReader::EndElement) &
56  (reader.name() == DOCUMENT_SERIALIZE_POINTS)) {
57  break;
58  }
59 
60  // Not done yet
61  if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
62  (reader.name() == DOCUMENT_SERIALIZE_POINT)) {
63 
64  // This is an entry that we need to add
65  QXmlStreamAttributes attributes = reader.attributes ();
66 
67  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER) &&
68  attributes.hasAttribute(DOCUMENT_SERIALIZE_ORDINAL) &&
69  attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_X) &&
70  attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_Y)) {
71 
72  m_identifiersAdded << attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
73  m_ordinals << attributes.value(DOCUMENT_SERIALIZE_ORDINAL).toDouble();
74 
75  QPoint point (attributes.value(DOCUMENT_SERIALIZE_GRAPH_X).toInt(),
76  attributes.value(DOCUMENT_SERIALIZE_GRAPH_Y).toInt());
77  m_points << point;
78  }
79  }
80  }
81 
82  if (!success) {
83  reader.raiseError ("Cannot read points");
84  }
85 }
86 
87 CmdAddPointsGraph::~CmdAddPointsGraph ()
88 {
89 }
90 
92 {
93  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdRedo";
94 
95  for (int index = 0; index < m_points.count(); index++) {
96 
97  QString identifierAdded;
99  m_points.at (index),
100  identifierAdded,
101  m_ordinals.at (index));
102  m_identifiersAdded.push_back (identifierAdded);
103  }
104 
105  document().updatePointOrdinals (mainWindow().transformation());
107 }
108 
110 {
111  LOG4CPP_INFO_S ((*mainCat)) << "CmdAddPointsGraph::cmdUndo";
112 
113  for (int index = 0; index < m_points.count(); index++) {
114  document().removePointGraph (m_identifiersAdded [index]);
115  }
116  document().updatePointOrdinals (mainWindow().transformation());
118 }
119 
120 void CmdAddPointsGraph::saveXml (QXmlStreamWriter &writer) const
121 {
122  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
123  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_ADD_POINTS_GRAPH);
124  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
125  writer.writeAttribute(DOCUMENT_SERIALIZE_CURVE_NAME, m_curveName);
126 
127  for (int index = 0; index < m_points.count(); index++) {
128 
129  writer.writeStartElement (DOCUMENT_SERIALIZE_POINT);
130  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X, QString::number (m_points.at (index).x()));
131  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y, QString::number (m_points.at (index).y()));
132 
133  QString identifier;
134  if (index < m_identifiersAdded.count()) {
135  identifier = m_identifiersAdded.at (index);
136  }
137 
138  writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, identifier);
139  writer.writeAttribute(DOCUMENT_SERIALIZE_ORDINAL, QString::number (m_ordinals.at (index)));
140  writer.writeEndElement();
141  }
142  writer.writeEndElement();
143 }
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:155
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:11
void removePointGraph(const QString &identifier)
Perform the opposite of addPointGraph.
Definition: Document.cpp:793
CmdAddPointsGraph(MainWindow &mainWindow, Document &document, const QString &curveName, const QList< QPoint > &points, const QList< double > &ordinals)
Constructor for normal creation.
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:32
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Storage of one imported image and the data attached to that image.
Definition: Document.h:29
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:22
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:60
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
Definition: Document.cpp:926