Vuo  2.0.3
VuoCommandMove.cc
Go to the documentation of this file.
1 
10 #include <sstream>
11 
12 #include "VuoCommandCommon.hh"
13 #include "VuoCompilerComment.hh"
14 #include "VuoCompilerNode.hh"
15 #include "VuoCommandMove.hh"
16 #include "VuoEditorComposition.hh"
17 #include "VuoEditorWindow.hh"
18 #include "VuoComment.hh"
19 #include "VuoRendererComment.hh"
20 #include "VuoRendererNode.hh"
21 #include "VuoStringUtilities.hh"
22 
26 VuoCommandMove::VuoCommandMove(set<VuoRendererNode *> movedNodes,
27  set<VuoRendererComment *> movedComments,
28  qreal dx,
29  qreal dy,
30  VuoEditorWindow *window,
31  bool movedByDragging)
32  : VuoCommandCommon(window)
33 {
34  setText(QApplication::translate("VuoEditorWindow", "Move"));
35  this->window = window;
36 
37  this->dx = dx;
38  this->dy = dy;
39  this->movedByDragging = movedByDragging;
40 
41  foreach (VuoRendererNode *rn, movedNodes)
42  {
43  if (rn->getBase()->hasCompiler())
44  movedItemIDs.insert(rn->getBase()->getCompiler()->getGraphvizIdentifier());
45  }
46 
47  foreach (VuoRendererComment *rc, movedComments)
48  {
49  if (rc->getBase()->hasCompiler())
50  movedItemIDs.insert(rc->getBase()->getCompiler()->getGraphvizIdentifier());
51  }
52 
53  bool moveAlreadyMade = movedByDragging;
54  if (moveAlreadyMade)
55  {
56  // If the items' positions have already been updated, reconstruct their original positions
57  // for the composition's "Before" snapshot.
58  for (set<VuoRendererNode *>::iterator node = movedNodes.begin(); node != movedNodes.end(); ++node)
59  {
60  (*node)->getBase()->setX((*node)->getBase()->getX() - dx);
61  (*node)->getBase()->setY((*node)->getBase()->getY() - dy);
62  }
63 
64  for (set<VuoRendererComment *>::iterator comment = movedComments.begin(); comment != movedComments.end(); ++comment)
65  {
66  (*comment)->getBase()->setX((*comment)->getBase()->getX() - dx);
67  (*comment)->getBase()->setY((*comment)->getBase()->getY() - dy);
68  }
69 
70  this->revertedSnapshot = window->getComposition()->takeSnapshot();
71 
72  // Now put them back.
73  for (set<VuoRendererNode *>::iterator node = movedNodes.begin(); node != movedNodes.end(); ++node)
74  {
75  (*node)->getBase()->setX((*node)->getBase()->getX() + dx);
76  (*node)->getBase()->setY((*node)->getBase()->getY() + dy);
77  }
78 
79  for (set<VuoRendererComment *>::iterator comment = movedComments.begin(); comment != movedComments.end(); ++comment)
80  {
81  (*comment)->getBase()->setX((*comment)->getBase()->getX() + dx);
82  (*comment)->getBase()->setY((*comment)->getBase()->getY() + dy);
83  }
84  }
85  else
86  this->revertedSnapshot = window->getComposition()->takeSnapshot();
87 
88 
89  // Start of command content.
90  {
91  if (!moveAlreadyMade)
92  {
93  for (set<VuoRendererNode *>::iterator node = movedNodes.begin(); node != movedNodes.end(); ++node)
94  {
95  (*node)->getBase()->setX((*node)->getBase()->getX() + dx);
96  (*node)->getBase()->setY((*node)->getBase()->getY() + dy);
97  (*node)->moveBy(dx, dy);
98 
99  (*node)->layoutConnectedInputDrawers();
100  }
101 
102  for (set<VuoRendererComment *>::iterator comment = movedComments.begin(); comment != movedComments.end(); ++comment)
103  {
104  (*comment)->getBase()->setX((*comment)->getBase()->getX() + dx);
105  (*comment)->getBase()->setY((*comment)->getBase()->getY() + dy);
106  (*comment)->moveBy(dx, dy);
107  }
108  }
109  }
110  // End of command content.
111 
112  this->updatedSnapshot = window->getComposition()->takeSnapshot();
113 
114  if (dx || dy)
115  setDescription("Move %s by %g,%g%s", VuoStringUtilities::join(movedItemIDs, ", ").c_str(), dx, dy, movedByDragging ? " (drag)" : "");
116 }
117 
122 {
124 }
125 
130 {
132 
133  window->resetCompositionWithSnapshot(revertedSnapshot);
134 }
135 
140 {
142 
143  window->resetCompositionWithSnapshot(updatedSnapshot);
144 }
145 
149 bool VuoCommandMove::mergeWith(const QUndoCommand *command)
150 {
151  const VuoCommandMove *otherMoveCommand = dynamic_cast<const VuoCommandMove *>(command);
152  if (! otherMoveCommand)
153  return false;
154 
155  // Merge consecutive moves of the same set of components by keypress.
156  // However, assume that moves by dragging are fully aggregated (per mouse press/release) ahead of time.
157  if ((movedItemIDs == otherMoveCommand->movedItemIDs) && !this->movedByDragging && !otherMoveCommand->movedByDragging)
158  {
159  this->updatedSnapshot = otherMoveCommand->updatedSnapshot;
160  return true;
161  }
162 
163  return false;
164 }