Engauge Digitizer  2
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 
16 const 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, CURVE_NAME_LIST_COLUMN_CURRENT);
33 
34  QString curveNameCurrent = modelCurves.data (idxCurrent).toString ();
35  QString curveNameOriginal = modelCurves.currentCurveToOriginalCurve (curveNameCurrent);
36  if (!curveNameOriginal.isEmpty ()) {
37 
38  // There was an original Curve
39  const Curve *curveOriginal = ((const Document&) document).curveForCurveName (curveNameOriginal);
40  Curve curveCurrent (*curveOriginal);
41  curveCurrent.setCurveName (curveNameCurrent);
42 
43  m_curvesGraphsAfter.addGraphCurveAtEnd (curveCurrent); // Save Curve
44 
45  } else {
46 
47  // There was no original Curve
48  Curve curveCurrent (curveNameCurrent,
50  CurveStyle (LineStyle::defaultGraphCurve(m_curvesGraphsAfter.numCurves()),
51  PointStyle::defaultGraphCurve(m_curvesGraphsAfter.numCurves())));
52 
53  m_curvesGraphsAfter.addGraphCurveAtEnd (curveCurrent); // Save Curve
54  }
55  }
56 }
57 
60  const QString &cmdDescription,
61  QXmlStreamReader &reader) :
62  CmdAbstract (mainWindow,
63  document,
64  cmdDescription)
65 {
66  LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::CmdSettingsCurveAddRemove";
67 
68  bool success = true;
69 
70  // Read until end of this subtree
71  bool isBefore = true;
72  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
73  (reader.name() != DOCUMENT_SERIALIZE_CMD)){
74  loadNextFromReader(reader);
75  if (reader.atEnd()) {
76  xmlExitWithError (reader,
77  QString ("%1 %2")
78  .arg (QObject::tr ("Reached end of file before finding end element for"))
79  .arg (DOCUMENT_SERIALIZE_CMD));
80  success = false;
81  break;
82  }
83 
84  if ((reader.tokenType() == QXmlStreamReader::StartElement) &&
85  (reader.name() == DOCUMENT_SERIALIZE_CURVES_GRAPHS)) {
86 
87  if (isBefore) {
88 
89  m_curvesGraphsBefore.loadXml (reader);
90  isBefore = false;
91 
92  } else {
93 
94  m_curvesGraphsAfter.loadXml (reader);
95 
96  }
97  }
98  }
99 
100  if (!success) {
101  reader.raiseError ("Cannot read curve add/remove settings");
102  }
103 }
104 
105 CmdSettingsCurveAddRemove::~CmdSettingsCurveAddRemove ()
106 {
107 }
108 
110 {
111  LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::cmdRedo";
112 
114  mainWindow().updateSettingsCurveAddRemove(m_curvesGraphsAfter);
117 }
118 
120 {
121  LOG4CPP_INFO_S ((*mainCat)) << "CmdSettingsCurveAddRemove::cmdUndo";
122 
124  mainWindow().updateSettingsCurveAddRemove(m_curvesGraphsBefore);
127 }
128 
129 void CmdSettingsCurveAddRemove::saveXml (QXmlStreamWriter &writer) const
130 {
131  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
132  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_SETTINGS_CURVE_ADD_REMOVE);
133  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
134  m_curvesGraphsBefore.saveXml(writer);
135  m_curvesGraphsAfter.saveXml(writer);
136  writer.writeEndElement();
137 }
static LineStyle defaultGraphCurve(int index)
Initial default for index&#39;th graph curve.
Definition: LineStyle.cpp:84
void loadXml(QXmlStreamReader &reader)
Load from serialized xml post-version 5 file.
void updateSettingsCurveAddRemove(const CurvesGraphs &curvesGraphs)
Update with new curves.
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:19
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
void addGraphCurveAtEnd(Curve curve)
Append new graph Curve to end of Curve list.
void saveOrCheckPostCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
static ColorFilterSettings defaultFilter()
Initial default for any Curve.
void saveXml(QXmlStreamWriter &writer) const
Serialize curves.
QString currentCurveToOriginalCurve(const QString &currentCurve) const
Return the original curve for the specified current curve.
void saveOrCheckPreCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
CmdSettingsCurveAddRemove(MainWindow &mainWindow, Document &document, const CurveNameList &modelCurves)
Constructor for normal creation.
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:45
int numCurves() const
Current number of graphs curves.
void setCurveName(const QString &curveName)
Change the curve name.
Definition: Curve.cpp:551
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Container for LineStyle and PointStyle for one Curve.
Definition: CurveStyle.h:18
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
Container for one set of digitized Points.
Definition: Curve.h:33
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
One row per curve name.
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:35
const CurvesGraphs & curvesGraphs() const
Make all Curves available, read only, for CmdAbstract classes only.
Definition: Document.cpp:339
static PointStyle defaultGraphCurve(int index)
Initial default for index&#39;th graph curve.
Definition: PointStyle.cpp:83
Model for DlgSettingsCurveAddRemove and CmdSettingsCurveAddRemove.
Definition: CurveNameList.h:27
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89