Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
CmdEditPointAxis.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 "CmdEditPointAxis.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 axis point");
19 
21  Document &document,
22  const QString &pointIdentifier,
23  const QPointF &posGraphBefore,
24  const QPointF &posGraphAfter,
25  bool isXOnly) :
26  CmdAbstract (mainWindow,
27  document,
28  CMD_DESCRIPTION),
29  m_pointIdentifier (pointIdentifier),
30  m_posGraphBefore (posGraphBefore),
31  m_posGraphAfter (posGraphAfter),
32  m_isXOnly (isXOnly)
33 {
34  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointAxis::CmdEditPointAxis point="
35  << pointIdentifier.toLatin1 ().data ()
36  << " posGraphBefore=" << QPointFToString (posGraphBefore).toLatin1 ().data ()
37  << " posGraphAfter=" << QPointFToString (posGraphAfter).toLatin1 ().data ();
38 }
39 
41  Document &document,
42  const QString &cmdDescription,
43  QXmlStreamReader &reader) :
44  CmdAbstract (mainWindow,
45  document,
46  cmdDescription)
47 {
48  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointAxis::CmdEditPointAxis";
49 
50  QXmlStreamAttributes attributes = reader.attributes();
51 
52  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_X_BEFORE) ||
53  !attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_Y_BEFORE) ||
54  !attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_X_AFTER) ||
55  !attributes.hasAttribute(DOCUMENT_SERIALIZE_GRAPH_Y_AFTER) ||
56  !attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER) ||
57  !attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY)) {
58  xmlExitWithError (reader,
59  QString ("%1 %2, %3, %4, %5, %6 %7 %8")
60  .arg (QObject::tr ("Missing attribute(s)"))
61  .arg (DOCUMENT_SERIALIZE_GRAPH_X_BEFORE)
62  .arg (DOCUMENT_SERIALIZE_GRAPH_Y_BEFORE)
63  .arg (DOCUMENT_SERIALIZE_GRAPH_X_AFTER)
64  .arg (DOCUMENT_SERIALIZE_GRAPH_Y_AFTER)
65  .arg (DOCUMENT_SERIALIZE_IDENTIFIER)
66  .arg (QObject::tr ("and/or"))
67  .arg (DOCUMENT_SERIALIZE_POINT_IS_X_ONLY));
68  }
69 
70  // Boolean values
71  QString isXOnlyValue = attributes.value(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY).toString();
72 
73  m_posGraphBefore.setX(attributes.value(DOCUMENT_SERIALIZE_GRAPH_X_BEFORE).toDouble());
74  m_posGraphBefore.setY(attributes.value(DOCUMENT_SERIALIZE_GRAPH_Y_BEFORE).toDouble());
75  m_posGraphAfter.setX(attributes.value(DOCUMENT_SERIALIZE_GRAPH_X_AFTER).toDouble());
76  m_posGraphAfter.setY(attributes.value(DOCUMENT_SERIALIZE_GRAPH_Y_AFTER).toDouble());
77  m_pointIdentifier = attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
78  m_isXOnly = (isXOnlyValue == DOCUMENT_SERIALIZE_BOOL_TRUE);
79 }
80 
81 CmdEditPointAxis::~CmdEditPointAxis ()
82 {
83 }
84 
86 {
87  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointAxis::cmdRedo";
88 
89  document().editPointAxis (m_posGraphAfter,
90  m_pointIdentifier);
91  document().updatePointOrdinals (mainWindow().transformation());
93 }
94 
96 {
97  LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointAxis::cmdUndo";
98 
99  document().editPointAxis (m_posGraphBefore,
100  m_pointIdentifier);
101  document().updatePointOrdinals (mainWindow().transformation());
103 }
104 
105 void CmdEditPointAxis::saveXml (QXmlStreamWriter &writer) const
106 {
107  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
108  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_EDIT_POINT_AXIS);
109  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
110  writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, m_pointIdentifier);
111  writer.writeAttribute(DOCUMENT_SERIALIZE_GRAPH_X_BEFORE, QString::number (m_posGraphBefore.x()));
112  writer.writeAttribute(DOCUMENT_SERIALIZE_GRAPH_Y_BEFORE, QString::number (m_posGraphBefore.y()));
113  writer.writeAttribute(DOCUMENT_SERIALIZE_GRAPH_X_AFTER, QString::number (m_posGraphAfter.x()));
114  writer.writeAttribute(DOCUMENT_SERIALIZE_GRAPH_Y_AFTER, QString::number (m_posGraphAfter.y()));
115  writer.writeAttribute(DOCUMENT_SERIALIZE_POINT_IS_X_ONLY, m_isXOnly ?
116  DOCUMENT_SERIALIZE_BOOL_TRUE :
117  DOCUMENT_SERIALIZE_BOOL_FALSE);
118  writer.writeEndElement();
119 }
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:18
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
void editPointAxis(const QPointF &posGraph, const QString &identifier)
Edit the graph coordinates of a single axis point. Call this after checkAddPointAxis to guarantee suc...
Definition: Document.cpp:331
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:43
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Storage of one imported image and the data attached to that image.
Definition: Document.h:40
CmdEditPointAxis(MainWindow &mainWindow, Document &document, const QString &pointIdentifier, const QPointF &posGraphBefore, const QPointF &posGraphAfter, bool isXOnly)
Constructor for normal creation.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:33
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:77
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
Definition: Document.cpp:903