-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUndoRedoCommands.cpp
272 lines (222 loc) · 8.24 KB
/
UndoRedoCommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
////////////////////////////////////////////////////////////////////////
// Copyright 2009-2018 NTESS. Under the terms
// of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
// Copyright (c) 2009-2018, NTESS
// All rights reserved.
//
// Portions are copyright of other developers:
// See the file CONTRIBUTORS.TXT in the top level directory
// the distribution for more information.
//
// This file is part of the SST software package. For license
// information, see the LICENSE file in the top level directory of the
// distribution.
////////////////////////////////////////////////////////////////////////
#include "UndoRedoCommands.h"
///////////////////////////////////////////////////////////////////////////////////////
ComandAddGraphicItemComponent::ComandAddGraphicItemComponent(GraphicItemComponent* Component, WiringScene* Scene, bool PasteMode /*=false*/, QUndoCommand* parent /*=0*/)
: QUndoCommand(parent)
{
m_Component = Component;
m_WiringScene = Scene;
m_PasteMode = PasteMode;
}
ComandAddGraphicItemComponent::~ComandAddGraphicItemComponent()
{
// Delete the Component
delete m_Component;
}
void ComandAddGraphicItemComponent::undo()
{
// Delete the Object
m_WiringScene->DeleteComponentFromScene(m_Component);
UpdateCommandText();
}
void ComandAddGraphicItemComponent::redo()
{
// Add the Object
m_WiringScene->AddNewComponentItemToScene(m_Component, !m_PasteMode);
UpdateCommandText();
// m_WiringScene->RefreshAllCurrentWirePositions(); // Re-enable when Move Command is handled
// NOTE: Refreshing all wire positions will cause the wire->UpdateWireLineSegmentPositions
// to emit ItemWireSetProjectDirty() which will set the project dirty bit that will
// override the undo stack clean flag.
// Until this call above is re-enabled, a bug can exist that when redo'ing an add
// command, its possible that existing wires might not connect back up.
}
void ComandAddGraphicItemComponent::UpdateCommandText()
{
QString ModeText;
m_PasteMode ? ModeText = "Paste" : ModeText = "Add";
QString CommandText = ModeText + QString(" Component (%1)").arg(m_Component->GetComponentDisplayName());
setText(CommandText);
}
///////////////////////////////////////////////////////////////////////////////////////
ComandAddGraphicItemText::ComandAddGraphicItemText(GraphicItemText* Text, WiringScene* Scene, bool PasteMode /*=false*/, QUndoCommand* parent /*=0*/)
: QUndoCommand(parent)
{
m_Text = Text;
m_WiringScene = Scene;
m_PasteMode = PasteMode;
}
ComandAddGraphicItemText::~ComandAddGraphicItemText()
{
// Delete the Text
delete m_Text;
}
void ComandAddGraphicItemText::undo()
{
// Delete the Object
m_WiringScene->DeleteTextFromScene(m_Text);
UpdateCommandText();
}
void ComandAddGraphicItemText::redo()
{
// Add the Object
m_WiringScene->AddNewTextItemToScene(m_Text, !m_PasteMode);
UpdateCommandText();
}
void ComandAddGraphicItemText::UpdateCommandText()
{
QString CommandText;
QString ModeText;
m_PasteMode ? ModeText = "Paste" : ModeText = "Add";
if (m_Text->toPlainText().isEmpty() == true) {
CommandText = ModeText + QString(" Text");
} else {
CommandText = ModeText + QString(" Text \"%1\"").arg(m_Text->toPlainText());
}
setText(CommandText);
}
///////////////////////////////////////////////////////////////////////////////////////
ComandAddGraphicItemWire::ComandAddGraphicItemWire(GraphicItemWire* Wire, WiringScene* Scene, bool PasteMode /*=false*/, QUndoCommand* parent /*=0*/)
: QUndoCommand(parent)
{
m_Wire = Wire;
m_WiringScene = Scene;
m_PasteMode = PasteMode;
}
ComandAddGraphicItemWire::~ComandAddGraphicItemWire()
{
// Delete the Component
delete m_Wire;
}
void ComandAddGraphicItemWire::undo()
{
// Delete the Object
m_WiringScene->DeleteWireFromScene(m_Wire);
UpdateCommandText();
}
void ComandAddGraphicItemWire::redo()
{
// Add the Object
m_WiringScene->AddNewWireItemToScene(m_Wire, true);
UpdateCommandText();
}
void ComandAddGraphicItemWire::UpdateCommandText()
{
QString ModeText;
m_PasteMode ? ModeText = "Paste" : ModeText = "Add";
QString CommandText = ModeText + QString(" Wire #%1").arg(m_Wire->GetWireIndex());
setText(CommandText);
}
///////////////////////////////////////////////////////////////////////////////////////
ComandDeleteGraphicItems::ComandDeleteGraphicItems(WiringScene* Scene, QUndoCommand* parent /*=0*/)
: QUndoCommand(parent)
{
m_WiringScene = Scene;
m_Initialized = false;
}
ComandDeleteGraphicItems::~ComandDeleteGraphicItems()
{
}
void ComandDeleteGraphicItems::undo()
{
// Re-Add all Deleted Wires
foreach (GraphicItemWire* item, m_DeletedWireList) {
// Add the Object
m_WiringScene->AddNewWireItemToScene(item, true);
item->SetWireSelected(true); // Have to toggle selection on off to sync all wire segments
item->SetWireSelected(false);
}
// Re-Add all Deleted Components
foreach (GraphicItemComponent* item, m_DeletedComponentList) {
// Add the Object
m_WiringScene->AddNewComponentItemToScene(item);
item->setSelected(false);
}
// Re-Add all Deleted Text
foreach (GraphicItemText* item, m_DeletedTextList) {
// Add the Object
m_WiringScene->AddNewTextItemToScene(item);
item->setSelected(false);
}
m_WiringScene->RefreshAllCurrentWirePositions();
}
void ComandDeleteGraphicItems::redo()
{
GraphicItemWire* ptrParentWire;
GraphicItemComponent* ptrComponent;
GraphicItemText* ptrText;
int NumItemsDeleted = 0;
// Figure out if this is the first time running
if (m_Initialized == false)
{
// We need to get the lists of objects from the selections
foreach (QGraphicsItem* item, m_WiringScene->selectedItems()) {
if (item->type() == GraphicItemWireLineSegment::Type) {
// Get the items Parent (Graphic Wire item)
ptrParentWire = (GraphicItemWire*)item->parentItem();
// Ensure that only one copy of the Parent Wire is in the list
if (m_DeletedWireList.indexOf(ptrParentWire) == -1) {
// Add this Item to the list of deleted Wires
m_DeletedWireList.append(ptrParentWire);
}
}
}
foreach (QGraphicsItem* item, m_WiringScene->selectedItems()) {
if (item->type() == GraphicItemComponent::Type) {
// Get the item as a Graphic Ccomponent item
ptrComponent = (GraphicItemComponent*)item;
// Add this Item to the list of deleted Components
m_DeletedComponentList.append(ptrComponent);
}
}
foreach (QGraphicsItem* item, m_WiringScene->selectedItems()) {
if (item->type() == GraphicItemText::Type) {
// Get the item as a Graphic Text item
ptrText = (GraphicItemText*)item;
// Add this Item to the list of deleted Text
m_DeletedTextList.append(ptrText);
}
}
}
// Mark this object as Initialized
m_Initialized = true;
// Delete All Wires in the DeleteWireList
foreach (GraphicItemWire* item, m_DeletedWireList) {
// Delete the Item from the Scene
m_WiringScene->DeleteWireFromScene(item);
}
// Delete All Components in the DeleteComponentList
foreach (GraphicItemComponent* item, m_DeletedComponentList) {
// Delete the Item from the Scene
m_WiringScene->DeleteComponentFromScene(item);
}
// Delete All Text in the DeleteTextList
foreach (GraphicItemText* item, m_DeletedTextList) {
// Delete the Item from the Scene
m_WiringScene->DeleteTextFromScene(item);
}
// Figure out how many items were deleted
NumItemsDeleted += m_DeletedWireList.count();
NumItemsDeleted += m_DeletedComponentList.count();
NumItemsDeleted += m_DeletedTextList.count();
if (NumItemsDeleted > 1) {
setText(QString("Delete %1 Items").arg(NumItemsDeleted));
} else {
setText(QString("Delete %1 Item").arg(NumItemsDeleted));
}
}