Engauge Digitizer 2
Loading...
Searching...
No Matches
CmdSettingsCurveAddRemove.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 "CmdSettingsCurveAddRemove.h"
8#include "CurveNameList.h"
9#include "Document.h"
10#include "DocumentSerialize.h"
11#include "Logger.h"
12#include "MainWindow.h"
13#include <QXmlStreamReader>
14#include "Xml.h"
15
16const QString CMD_DESCRIPTION ("Curve add/remove");
17
19 Document &document,
20 const CurveNameList &modelCurves) :
21 CmdAbstract(mainWindow,
22 document,
23 CMD_DESCRIPTION)
24{
25 LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::CmdSettingsCurveAddRemove";
26
27 m_curvesGraphsBefore = document.curvesGraphs ();
28
29 // Build the 'after' state
30 for (int row = 0; row < modelCurves.rowCount (); row++) {
31
32 QModelIndex idxCurrent = modelCurves.index (row, 0);
33 QModelIndex idxOriginal = modelCurves.index (row, 1);
34
35 QString curveNameCurrent = modelCurves.data (idxCurrent).toString ();
36 QString curveNameOriginal = modelCurves.data (idxOriginal).toString ();
37 if (!curveNameOriginal.isEmpty ()) {
38
39 // There was an original Curve
40 const Curve *curveOriginal = ((const Document&) document).curveForCurveName (curveNameOriginal);
41 Curve curveCurrent (*curveOriginal);
42 curveCurrent.setCurveName (curveNameCurrent);
43
44 m_curvesGraphsAfter.addGraphCurveAtEnd (curveCurrent); // Save Curve
45
46 } else {
47
48 // There was no original Curve
49 Curve curveCurrent (curveNameCurrent,
52 PointStyle::defaultGraphCurve(m_curvesGraphsAfter.numCurves())));
53
54 m_curvesGraphsAfter.addGraphCurveAtEnd (curveCurrent); // Save Curve
55 }
56 }
57}
58
60 Document &document,
61 const QString &cmdDescription,
62 QXmlStreamReader &reader) :
63 CmdAbstract (mainWindow,
64 document,
65 cmdDescription)
66{
67 LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::CmdSettingsCurveAddRemove";
68
69 bool success = true;
70
71 // Read until end of this subtree
72 bool isBefore = true;
73 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
74 (reader.name() != DOCUMENT_SERIALIZE_CMD)){
75 loadNextFromReader(reader);
76 if (reader.atEnd()) {
77 xmlExitWithError (reader,
78 QString ("%1 %2")
79 .arg (QObject::tr ("Reached end of file before finding end element for"))
80 .arg (DOCUMENT_SERIALIZE_CMD));
81 success = false;
82 break;
83 }
84
85 if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
86 (reader.name() == DOCUMENT_SERIALIZE_CURVES_GRAPHS)) {
87
88 if (isBefore) {
89
90 m_curvesGraphsBefore.loadXml (reader);
91 isBefore = false;
92
93 } else {
94
95 m_curvesGraphsAfter.loadXml (reader);
96
97 }
98 }
99 }
100
101 if (!success) {
102 reader.raiseError ("Cannot read curve add/remove settings");
103 }
104}
105
106CmdSettingsCurveAddRemove::~CmdSettingsCurveAddRemove ()
107{
108}
109
111{
112 LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::cmdRedo";
113
114 mainWindow().updateSettingsCurveAddRemove(m_curvesGraphsAfter);
116}
117
119{
120 LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::cmdUndo";
121
122 mainWindow().updateSettingsCurveAddRemove(m_curvesGraphsBefore);
124}
125
126void CmdSettingsCurveAddRemove::saveXml (QXmlStreamWriter &writer) const
127{
128 writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
129 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_SETTINGS_CURVE_ADD_REMOVE);
130 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
131 m_curvesGraphsBefore.saveXml(writer);
132 m_curvesGraphsAfter.saveXml(writer);
133 writer.writeEndElement();
134}
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition CmdAbstract.h:19
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.
CmdSettingsCurveAddRemove(MainWindow &mainWindow, Document &document, const CurveNameList &modelCurves)
Constructor for normal creation.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
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.
static ColorFilterSettings defaultFilter()
Initial default for any Curve.
Model for DlgSettingsCurveAddRemove and CmdSettingsCurveAddRemove.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Retrieve data from model.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
One row per curve name.
Container for LineStyle and PointStyle for one Curve.
Definition CurveStyle.h:19
Container for one set of digitized Points.
Definition Curve.h:33
void setCurveName(const QString &curveName)
Change the curve name.
Definition Curve.cpp:492
void saveXml(QXmlStreamWriter &writer) const
Serialize curves.
void addGraphCurveAtEnd(Curve curve)
Append new graph Curve to end of Curve list.
void loadXml(QXmlStreamReader &reader)
Load from serialized xml post-version 5 file.
int numCurves() const
Current number of graphs curves.
Storage of one imported image and the data attached to that image.
Definition Document.h:41
const CurvesGraphs & curvesGraphs() const
Make all Curves available, read only, for CmdAbstract classes only.
Definition Document.cpp:305
static LineStyle defaultGraphCurve(int index)
Initial default for index'th graph curve.
Definition LineStyle.cpp:84
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition MainWindow.h:78
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
void updateSettingsCurveAddRemove(const CurvesGraphs &curvesGraphs)
Update with new curves.
static PointStyle defaultGraphCurve(int index)
Initial default for index'th graph curve.