Engauge Digitizer 2
Loading...
Searching...
No Matches
CmdEditPointGraph.cpp
Go to the documentation of this file.
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
18const QString CMD_DESCRIPTION ("Edit curve points");
19
22 const QStringList &pointIdentifiers,
23 bool isX,
24 bool isY,
25 double x,
26 double y) :
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) :
48 cmdDescription)
49{
50 LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::CmdEditPointGraph";
51
52 QXmlStreamAttributes attributes = reader.attributes();
53
54 QStringList requiredAttributesLeaf;
55 requiredAttributesLeaf << DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X
59 leafAndBaseAttributes (attributes,
60 requiredAttributesLeaf,
61 reader);
62
63 // Boolean attributes
64 QString isX = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X).toString();
65 QString isY = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y).toString();
66
67 m_isX = (isX == DOCUMENT_SERIALIZE_BOOL_TRUE);
68 m_isY = (isY == DOCUMENT_SERIALIZE_BOOL_TRUE);
69 m_x = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_X).toDouble();
70 m_y = attributes.value(DOCUMENT_SERIALIZE_EDIT_GRAPH_Y).toDouble();
71
72 bool success = true;
73 while (loadNextFromReader (reader)) {
74
75 if (reader.atEnd() || reader.hasError ()) {
76 success = false;
77 break;
78 }
79
80 if ((reader.tokenType() == QXmlStreamReader::EndElement) &
81 (reader.name() == DOCUMENT_SERIALIZE_CMD)) {
82 break;
83 }
84
85 // Not done yet
86 if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
87 (reader.name() == DOCUMENT_SERIALIZE_POINT)) {
88
89 // This is an entry that we need to add
90 QXmlStreamAttributes attributes = reader.attributes ();
91
92 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_IDENTIFIER)) {
93
94 m_pointIdentifiers << attributes.value(DOCUMENT_SERIALIZE_IDENTIFIER).toString();
95 }
96 }
97
98 if (!success) {
99 reader.raiseError (QObject::tr ("Cannot read graph points"));
100 }
101 }
102}
103
107
109{
110 LOG4CPP_INFO_S ((*mainCat)) << "CmdEditPointGraph::cmdRedo";
111
112 restoreState ();
115 document().editPointGraph (m_isX,
116 m_isY,
117 m_x,
118 m_y,
119 m_pointIdentifiers,
120 mainWindow().transformation());
121 document().updatePointOrdinals (mainWindow().transformation());
124}
125
136
137void CmdEditPointGraph::saveXml (QXmlStreamWriter &writer) const
138{
139 writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
141 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
142 writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X, m_isX ?
145 writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y, m_isY ?
148 writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_X, QString::number (m_x));
149 writer.writeAttribute(DOCUMENT_SERIALIZE_EDIT_GRAPH_Y, QString::number (m_y));
150
151 for (int index = 0; index < m_pointIdentifiers.count(); index++) {
152
153 writer.writeStartElement (DOCUMENT_SERIALIZE_POINT);
154 writer.writeAttribute(DOCUMENT_SERIALIZE_IDENTIFIER, m_pointIdentifiers.at (index));
155 writer.writeEndElement();
156 }
157 baseAttributes (writer);
158 writer.writeEndElement();
159}
const QString CMD_DESCRIPTION("Add axis point")
const QString CMD_DESCRIPTION("Edit curve points")
const QString DOCUMENT_SERIALIZE_IDENTIFIER
const QString DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_X
const QString DOCUMENT_SERIALIZE_POINT
const QString DOCUMENT_SERIALIZE_CMD
const QString DOCUMENT_SERIALIZE_CMD_TYPE
const QString DOCUMENT_SERIALIZE_EDIT_GRAPH_X
const QString DOCUMENT_SERIALIZE_CMD_EDIT_POINT_GRAPH
const QString DOCUMENT_SERIALIZE_EDIT_GRAPH_IS_Y
const QString DOCUMENT_SERIALIZE_EDIT_GRAPH_Y
const QString DOCUMENT_SERIALIZE_CMD_DESCRIPTION
const QString DOCUMENT_SERIALIZE_BOOL_TRUE
const QString DOCUMENT_SERIALIZE_BOOL_FALSE
log4cpp::Category * mainCat
Definition Logger.cpp:14
QXmlStreamReader::TokenType loadNextFromReader(QXmlStreamReader &reader)
Load next token from xml reader.
Definition Xml.cpp:14
Document & document()
Return the Document that this command will modify during redo and undo.
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
void saveOrCheckPostCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
void restoreState()
Before any other operations associated with a Cmd class are performed, this method is called to resto...
void saveOrCheckPreCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
void leafAndBaseAttributes(const QXmlStreamAttributes &attributes, const QStringList &requiredAttributesLeaf, QXmlStreamReader &reader)
Before reading leaf class attributes, check all required attributes from leaf and this base class are...
void baseAttributes(QXmlStreamWriter &writer) const
After writing leaf class attributes, this writes the base class atributes.
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.
CmdEditPointGraph(MainWindow &mainWindow, Document &document, const QStringList &pointIdentifiers, bool isX, bool isY, double x, double y)
Constructor for normal creation.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
void saveDocumentState(const Document &document)
Save the document state for restoration by restoreDocumentState.
void restoreDocumentState(Document &document) const
Restore the document previously saved by saveDocumentState.
CmdPointChangeBase(MainWindow &mainWindow, Document &document, const QString &cmdDescription)
Single constructor.
Storage of one imported image and the data attached to that image.
Definition Document.h:44
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
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:383
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition MainWindow.h:95
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18