Engauge Digitizer  2
CmdMoveBy.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 "CmdMoveBy.h"
8 #include "DataKey.h"
9 #include "Document.h"
10 #include "DocumentSerialize.h"
11 #include "EngaugeAssert.h"
12 #include "GraphicsItemType.h"
13 #include "GraphicsView.h"
14 #include "Logger.h"
15 #include "MainWindow.h"
16 #include <QGraphicsItem>
17 #include <QtToString.h>
18 #include <QXmlStreamReader>
19 #include "Xml.h"
20 
22  Document &document,
23  const QPointF &deltaScreen,
24  const QString &moveText,
25  const QStringList &selectedPointIdentifiers) :
26  CmdPointChangeBase (mainWindow,
27  document,
28  moveText),
29  m_deltaScreen (deltaScreen)
30 {
31  QStringList selected; // For debug
32  QStringList::const_iterator itr;
33  for (itr = selectedPointIdentifiers.begin (); itr != selectedPointIdentifiers.end (); itr++) {
34 
35  QString selectedPointIdentifier = *itr;
36 
37  selected << selectedPointIdentifier;
38  m_movedPoints.setKeyValue (selectedPointIdentifier, true);
39  }
40 
41  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy"
42  << " deltaScreen=" << QPointFToString (deltaScreen).toLatin1 ().data ()
43  << " selected=" << selected.join (", ").toLatin1 ().data () << ")";
44 }
45 
48  const QString &cmdDescription,
49  QXmlStreamReader &reader) :
50  CmdPointChangeBase (mainWindow,
51  document,
52  cmdDescription)
53 {
54  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy";
55 
56  QXmlStreamAttributes attributes = reader.attributes();
57 
58  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA) ||
59  !attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA) ) {
60  xmlExitWithError (reader,
61  QString ("%1 %2 %3 %4")
62  .arg (QObject::tr ("Missing attribute(s)"))
63  .arg (DOCUMENT_SERIALIZE_SCREEN_X_DELTA)
64  .arg (QObject::tr ("and/or"))
65  .arg (DOCUMENT_SERIALIZE_SCREEN_Y_DELTA));
66  }
67 
68  m_deltaScreen.setX(attributes.value(DOCUMENT_SERIALIZE_SCREEN_X_DELTA).toDouble());
69  m_deltaScreen.setY(attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA).toDouble());
70  m_movedPoints.loadXml (reader);
71 }
72 
73 CmdMoveBy::~CmdMoveBy ()
74 {
75 }
76 
78 {
79  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdRedo"
80  << " deltaScreen=" << QPointFToString (m_deltaScreen).toLatin1().data()
81  << " moving=" << m_movedPoints.count ();
82 
85  moveBy (m_deltaScreen);
87  resetSelection(m_movedPoints);
89 }
90 
92 {
93  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdUndo"
94  << " deltaScreen=" << QPointFToString (-1.0 * m_deltaScreen).toLatin1().data()
95  << " moving=" << m_movedPoints.count ();
96 
100  resetSelection(m_movedPoints);
102 }
103 
104 void CmdMoveBy::moveBy (const QPointF &deltaScreen)
105 {
106  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::moveBy";
107 
108  // Move Points in the Document
109  for (int i = 0; i < m_movedPoints.count(); i++) {
110 
111  QString pointIdentifier = m_movedPoints.getKey (i);
112  document().movePoint (pointIdentifier, deltaScreen);
113 
114  }
115 
116  // Move Points in GraphicsScene, using the new positions in Document
117  QList<QGraphicsItem *> items = mainWindow().view().items();
118  QList<QGraphicsItem *>::iterator itrS;
119  for (itrS = items.begin (); itrS != items.end (); itrS++) {
120 
121  QGraphicsItem *item = *itrS;
122  if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt () == GRAPHICS_ITEM_TYPE_POINT) {
123 
124  QString pointIdentifier = item->data (DATA_KEY_IDENTIFIER).toString ();
125 
126  if (m_movedPoints.contains (pointIdentifier)) {
127 
128  // Get the new position
129  QPointF posScreen = document().positionScreen (pointIdentifier);
130 
131  if (item->pos () != posScreen) {
132 
133  // Save the new position
134  item->setPos (posScreen);
135  }
136  }
137  }
138  }
139 
140  document().updatePointOrdinals (mainWindow().transformation());
141 
142  // Update the lines attached to the points
144 }
145 
146 void CmdMoveBy::saveXml (QXmlStreamWriter &writer) const
147 {
148  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
149  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_MOVE_BY);
150  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
151  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA, QString::number (m_deltaScreen.x()));
152  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA, QString::number (m_deltaScreen.y()));
153  m_movedPoints.saveXml (writer);
154  writer.writeEndElement();
155 }
void movePoint(const QString &pointIdentifier, const QPointF &deltaScreen)
See Curve::movePoint.
Definition: Document.cpp:752
CmdMoveBy(MainWindow &mainWindow, Document &document, const QPointF &deltaScreen, const QString &moveText, const QStringList &selectedPointIdentifiers)
Constructor for normal creation.
Definition: CmdMoveBy.cpp:21
QString getKey(int i) const
Get key for index.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
Definition: CmdMoveBy.cpp:91
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Definition: CmdMoveBy.cpp:77
void restoreDocumentState(Document &document) const
Restore the document previously saved by saveDocumentState.
void setKeyValue(const QString &pointIdentifier, bool value)
Set key/value pair.
void saveXml(QXmlStreamWriter &writer) const
Serialize table to xml.
void saveOrCheckPostCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
GraphicsView & view()
View for the QImage and QGraphicsItems, without const.
void saveOrCheckPreCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
Base class for CmdBase leaf subclasses that involve point additions, deletions and/or modifications...
QPointF positionScreen(const QString &pointIdentifier) const
See Curve::positionScreen.
Definition: Document.cpp:821
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:45
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
Definition: CmdMoveBy.cpp:146
void updateAfterCommand()
See GraphicsScene::updateAfterCommand.
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
void loadXml(QXmlStreamReader &reader)
Load from serialized xml.
bool contains(const QString &pointIdentifier) const
True if specified entry exists in the table.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:35
void resetSelection(const PointIdentifiers &pointIdentifiersToSelect)
Since the set of selected points has probably changed, changed that set back to the specified set...
Definition: CmdAbstract.cpp:81
void updateGraphicsLinesToMatchGraphicsPoints()
Update the graphics lines so they follow the graphics points, after a drag, addition, removal, and such.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
Definition: Document.cpp:1060
void saveDocumentState(const Document &document)
Save the document state for restoration by restoreDocumentState.
int count() const
Number of entries.