Engauge Digitizer  2
CmdCopy.cpp
1 #include "CmdCopy.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 ("Copy");
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)) << "CmdCopy::CmdCopy"
29  << " selected=" << selectedPointIdentifiers.join (", ").toLatin1 ().data () << ")";
30 
31  ExportToClipboard exportStrategy;
32  QTextStream strCsv (&m_csv), strHtml (&m_html);
33  exportStrategy.exportToClipboard (selectedPointIdentifiers,
34  mainWindow.transformation(),
35  strCsv,
36  strHtml,
37  document.curveAxes(),
38  document.curvesGraphs(),
39  m_curvesGraphs);
40 }
41 
43  Document &document,
44  const QString &cmdDescription,
45  QXmlStreamReader &reader) :
46  CmdAbstract (mainWindow,
47  document,
48  cmdDescription)
49 {
50  LOG4CPP_INFO_S ((*mainCat)) << "CmdCopy::CmdCopy";
51 
52  QXmlStreamAttributes attributes = reader.attributes();
53 
54  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED) ||
55  !attributes.hasAttribute(DOCUMENT_SERIALIZE_CSV) ||
56  !attributes.hasAttribute(DOCUMENT_SERIALIZE_HTML)) {
57  ENGAUGE_ASSERT (false);
58  }
59 
60  QString defined = attributes.value(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED).toString();
61 
62  m_transformIsDefined = (defined == DOCUMENT_SERIALIZE_BOOL_TRUE);
63  m_csv = attributes.value(DOCUMENT_SERIALIZE_CSV).toString();
64  m_html = attributes.value(DOCUMENT_SERIALIZE_HTML).toString();
65  m_curvesGraphs.loadXml(reader);
66 }
67 
68 CmdCopy::~CmdCopy ()
69 {
70 }
71 
73 {
74  LOG4CPP_INFO_S ((*mainCat)) << "CmdCopy::cmdRedo";
75 
76  MimePoints *mimePoints;
77  if (m_transformIsDefined) {
78  mimePoints = new MimePoints (m_csv,
79  m_html);
80  } else {
81  mimePoints = new MimePoints (m_csv);
82  }
83 
84  QClipboard *clipboard = QApplication::clipboard();
85  clipboard->setMimeData (mimePoints, QClipboard::Clipboard);
86 
87  document().updatePointOrdinals (mainWindow().transformation());
89 }
90 
92 {
93  LOG4CPP_INFO_S ((*mainCat)) << "CmdCopy::cmdUndo";
94 
95  document().updatePointOrdinals (mainWindow().transformation());
97 }
98 
99 void CmdCopy::saveXml (QXmlStreamWriter &writer) const
100 {
101  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
102  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_COPY);
103  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
104  writer.writeAttribute(DOCUMENT_SERIALIZE_TRANSFORM_DEFINED,
105  m_transformIsDefined ? DOCUMENT_SERIALIZE_BOOL_TRUE : DOCUMENT_SERIALIZE_BOOL_FALSE);
106  writer.writeAttribute(DOCUMENT_SERIALIZE_CSV, m_csv);
107  writer.writeAttribute(DOCUMENT_SERIALIZE_HTML, m_html);
108  m_curvesGraphs.saveXml(writer);
109  writer.writeEndElement();
110 }
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
Definition: CmdCopy.cpp:91
void saveXml(QXmlStreamWriter &writer) const
Serialize curves.
CmdCopy(MainWindow &mainWindow, Document &document, const QStringList &selectedPointIdentifiers)
Constructor for normal creation.
Definition: CmdCopy.cpp:20
void loadXml(QXmlStreamReader &reader)
Load from serialized file.
Transformation transformation() const
Return read-only copy of transformation.
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:11
const Curve & curveAxes() const
Get method for axis curve.
Definition: Document.cpp:279
Custom mime type for separate treatment of graph coordinates and, when there is no transform...
Definition: MimePoints.h:7
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Definition: CmdCopy.cpp:72
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.
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:32
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
Definition: CmdCopy.cpp:99
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Storage of one imported image and the data attached to that image.
Definition: Document.h:28
const CurvesGraphs & curvesGraphs() const
Make all Curves available, read only, for CmdAbstract classes only.
Definition: Document.cpp:312
Strategy class for exporting to the clipboard. This strategy is external to the Document class so tha...
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:722