Engauge Digitizer  2
CmdCut.cpp
1 #include "CmdCut.h"
2 #include "DataKey.h"
3 #include "Document.h"
4 #include "DocumentSerialize.h"
5 #include "EngaugeAssert.h"
6 #include "ExportToClipboard.h"
7 #include "GraphicsItemType.h"
8 #include "GraphicsView.h"
9 #include "Logger.h"
10 #include "MainWindow.h"
11 #include "MimePoints.h"
12 #include <QApplication>
13 #include <QClipboard>
14 #include <QTextStream>
15 #include "QtToString.h"
16 #include <QXmlStreamReader>
17 
18 const QString CMD_DESCRIPTION ("Cut");
19 
21  Document &document,
22  const QStringList &selectedPointIdentifiers) :
23  CmdAbstract(mainWindow,
24  document,
25  CMD_DESCRIPTION),
26  m_transformIsDefined (mainWindow.transformIsDefined())
27 {
28  LOG4CPP_INFO_S ((*mainCat)) << "CmdCut::CmdCut"
29  << " selected=(" << selectedPointIdentifiers.join (", ").toLatin1 ().data () << ")";
30 
31  // Export to clipboard
32  ExportToClipboard exportStrategy;
33  QTextStream strCsv (&m_csv), strHtml (&m_html);
34  exportStrategy.exportToClipboard (selectedPointIdentifiers,
35  mainWindow.transformation(),
36  strCsv,
37  strHtml,
38  document.curveAxes(),
39  document.curvesGraphs(),
40  m_curvesGraphsRemoved);
41 }
42 
44  Document &document,
45  const QString &cmdDescription,
46  QXmlStreamReader &reader) :
47  CmdAbstract (mainWindow,
48  document,
49  cmdDescription)
50 {
51  LOG4CPP_INFO_S ((*mainCat)) << "CmdCut::CmdCut";
52 
53  QXmlStreamAttributes attributes = reader.attributes();
54 
55  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED) ||
56  !attributes.hasAttribute(DOCUMENT_SERIALIZE_CSV) ||
57  !attributes.hasAttribute(DOCUMENT_SERIALIZE_HTML)) {
58  ENGAUGE_ASSERT (false);
59  }
60 
61  QString defined = attributes.value(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED).toString();
62 
63  m_transformIsDefined = (defined == DOCUMENT_SERIALIZE_BOOL_TRUE);
64  m_csv = attributes.value(DOCUMENT_SERIALIZE_CSV).toString();
65  m_html = attributes.value(DOCUMENT_SERIALIZE_HTML).toString();
66  m_curvesGraphsRemoved.loadXml(reader);
67 }
68 
69 CmdCut::~CmdCut ()
70 {
71 }
72 
74 {
75  LOG4CPP_INFO_S ((*mainCat)) << "CmdCut::cmdRedo";
76 
77  MimePoints *mimePoints;
78  if (m_transformIsDefined) {
79  mimePoints = new MimePoints (m_csv,
80  m_html);
81  } else {
82  mimePoints = new MimePoints (m_csv);
83  }
84 
85  QClipboard *clipboard = QApplication::clipboard();
86  clipboard->setMimeData (mimePoints, QClipboard::Clipboard);
87 
88  document().removePointsInCurvesGraphs (m_curvesGraphsRemoved);
89 
90  document().updatePointOrdinals (mainWindow().transformation());
92 }
93 
95 {
96  LOG4CPP_INFO_S ((*mainCat)) << "CmdCut::cmdUndo";
97 
98  document().addPointsInCurvesGraphs (m_curvesGraphsRemoved);
99 
100  document().updatePointOrdinals (mainWindow().transformation());
102 }
103 
104 void CmdCut::saveXml (QXmlStreamWriter &writer) const
105 {
106  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
107  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_CUT);
108  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
109  writer.writeAttribute(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED,
110  m_transformIsDefined ? DOCUMENT_SERIALIZE_BOOL_TRUE: DOCUMENT_SERIALIZE_BOOL_FALSE);
111  writer.writeAttribute(DOCUMENT_SERIALIZE_CSV, m_csv);
112  writer.writeAttribute(DOCUMENT_SERIALIZE_HTML, m_html);
113  m_curvesGraphsRemoved.saveXml(writer);
114  writer.writeEndElement();
115 }
void saveXml(QXmlStreamWriter &writer) const
Serialize curves.
void loadXml(QXmlStreamReader &reader)
Load from serialized xml post-version 5 file.
CmdCut(MainWindow &mainWindow, Document &document, const QStringList &selectedPointIdentifiers)
Constructor for normal creation.
Definition: CmdCut.cpp:20
Transformation transformation() const
Return read-only copy of transformation.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
Definition: CmdCut.cpp:94
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:11
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Definition: CmdCut.cpp:73
const Curve & curveAxes() const
Get method for axis curve.
Definition: Document.cpp:214
Custom mime type for separate treatment of graph coordinates and, when there is no transform...
Definition: MimePoints.h:7
void exportToClipboard(const QStringList &selected, const Transformation &transformation, QTextStream &strCsv, QTextStream &strHtml, const Curve &curveAxis, const CurvesGraphs &curvesGraphsAll, CurvesGraphs &curvesGraphsSelected) const
Export, curve-by-curve, raw data points to a string that will be copied to the clipboard.
void removePointsInCurvesGraphs(CurvesGraphs &curvesGraphs)
Remove all points identified in the specified CurvesGraphs. See also addPointsInCurvesGraphs.
Definition: Document.cpp:780
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:28
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
Definition: CmdCut.cpp:104
const CurvesGraphs & curvesGraphs() const
Make all Curves available, read only, for CmdAbstract classes only.
Definition: Document.cpp:247
Strategy class for exporting to the clipboard. This strategy is external to the Document class so tha...
void addPointsInCurvesGraphs(CurvesGraphs &curvesGraphs)
Add all points identified in the specified CurvesGraphs. See also removePointsInCurvesGraphs.
Definition: Document.cpp:161
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:22
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:906