Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
CmdMoveBy.cpp
1 #include "CmdMoveBy.h"
2 #include "DataKey.h"
3 #include "Document.h"
4 #include "DocumentSerialize.h"
5 #include "EngaugeAssert.h"
6 #include "GraphicsItemType.h"
7 #include "GraphicsScene.h"
8 #include "GraphicsView.h"
9 #include "Logger.h"
10 #include "MainWindow.h"
11 #include <QGraphicsItem>
12 #include <QtToString.h>
13 #include <QXmlStreamReader>
14 
16  Document &document,
17  const QPointF &deltaScreen,
18  const QString &moveText,
19  const QStringList &selectedPointIdentifiers) :
20  CmdAbstract(mainWindow,
21  document,
22  moveText),
23  m_deltaScreen (deltaScreen)
24 {
25  QStringList selected; // For debug
26  QStringList::const_iterator itr;
27  for (itr = selectedPointIdentifiers.begin (); itr != selectedPointIdentifiers.end (); itr++) {
28 
29  QString selectedPointIdentifier = *itr;
30 
31  selected << selectedPointIdentifier;
32  m_movedPoints.setKeyValue (selectedPointIdentifier, true);
33  }
34 
35  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy"
36  << " deltaScreen=" << QPointFToString (deltaScreen).toLatin1 ().data ()
37  << " selected=" << selected.join (", ").toLatin1 ().data () << ")";
38 }
39 
41  Document &document,
42  const QString &cmdDescription,
43  QXmlStreamReader &reader) :
44  CmdAbstract (mainWindow,
45  document,
46  cmdDescription)
47 {
48  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy";
49 
50  QXmlStreamAttributes attributes = reader.attributes();
51 
52  if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA) ||
53  !attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA) ) {
54  ENGAUGE_ASSERT (false);
55  }
56 
57  m_deltaScreen.setX(attributes.value(DOCUMENT_SERIALIZE_SCREEN_X_DELTA).toDouble());
58  m_deltaScreen.setY(attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA).toDouble());
59  m_movedPoints.loadXml (reader);
60 }
61 
62 CmdMoveBy::~CmdMoveBy ()
63 {
64 }
65 
67 {
68  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdRedo"
69  << " deltaScreen=" << QPointFToString (m_deltaScreen).toLatin1().data()
70  << " moving=" << m_movedPoints.count ();
71 
72  moveBy (m_deltaScreen);
74 }
75 
77 {
78  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdUndo"
79  << " deltaScreen=" << QPointFToString (-1.0 * m_deltaScreen).toLatin1().data()
80  << " moving=" << m_movedPoints.count ();
81 
82  moveBy (-1.0 * m_deltaScreen);
84 }
85 
86 void CmdMoveBy::moveBy (const QPointF &deltaScreen)
87 {
88  LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::moveBy";
89 
90  // Move Points in the Document
91  for (int i = 0; i < m_movedPoints.count(); i++) {
92 
93  QString pointIdentifier = m_movedPoints.getKey (i);
94  document().movePoint (pointIdentifier, deltaScreen);
95 
96  }
97 
98  // Move Points in GraphicsScene, using the new positions in Document
99  QList<QGraphicsItem *> items = mainWindow().view().items();
100  QList<QGraphicsItem *>::iterator itrS;
101  for (itrS = items.begin (); itrS != items.end (); itrS++) {
102 
103  QGraphicsItem *item = *itrS;
104  if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt () == GRAPHICS_ITEM_TYPE_POINT) {
105 
106  QString pointIdentifier = item->data (DATA_KEY_IDENTIFIER).toString ();
107 
108  if (m_movedPoints.contains (pointIdentifier)) {
109 
110  // Get the new position
111  QPointF posScreen = document().positionScreen (pointIdentifier);
112 
113  if (item->pos () != posScreen) {
114 
115  // Save the new position
116  item->setPos (posScreen);
117  }
118  }
119  }
120  }
121 
122  document().updatePointOrdinals (mainWindow().transformation());
123 
124  // Update the lines attached to the points
126 }
127 
128 void CmdMoveBy::saveXml (QXmlStreamWriter &writer) const
129 {
130  writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
131  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_MOVE_BY);
132  writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
133  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA, QString::number (m_deltaScreen.x()));
134  writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA, QString::number (m_deltaScreen.y()));
135  m_movedPoints.saveXml (writer);
136  writer.writeEndElement();
137 }
QPointF positionScreen(const QString &pointIdentifier) const
See Curve::positionScreen.
Definition: Document.cpp:728
void movePoint(const QString &pointIdentifier, const QPointF &deltaScreen)
See Curve::movePoint.
Definition: Document.cpp:689
CmdMoveBy(MainWindow &mainWindow, Document &document, const QPointF &deltaScreen, const QString &moveText, const QStringList &selectedPointIdentifiers)
Constructor for normal creation.
Definition: CmdMoveBy.cpp:15
void saveXml(QXmlStreamWriter &writer) const
Serialize table to xml.
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:11
QString getKey(int i) const
Get key for index.
bool contains(const QString &pointIdentifier) const
True if specified entry exists in the table.
virtual void cmdUndo()
Undo method that is called when QUndoStack is moved one command backward.
Definition: CmdMoveBy.cpp:76
virtual void cmdRedo()
Redo method that is called when QUndoStack is moved one command forward.
Definition: CmdMoveBy.cpp:66
void setKeyValue(const QString &pointIdentifier, bool value)
Set key/value pair.
virtual void saveXml(QXmlStreamWriter &writer) const
Save commands as xml for later uploading.
Definition: CmdMoveBy.cpp:128
GraphicsView & view()
View for the QImage and QGraphicsItems, without const.
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:29
void loadXml(QXmlStreamReader &reader)
Load from serialized xml.
int count() const
Number of entries.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:22
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:60
void updatePointOrdinals(const Transformation &transformation)
Update point ordinals after point addition/removal or dragging.
Definition: Document.cpp:926