ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkUndoSet.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkUndoSet.cxx
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkUndoSet.h"
16 
17 #include "vtkCollection.h"
18 #include "vtkCollectionIterator.h"
19 #include "vtkObjectFactory.h"
20 #include "vtkPVXMLElement.h"
21 #include "vtkUndoElement.h"
22 
24 //-----------------------------------------------------------------------------
26 {
27  this->Collection = vtkCollection::New();
28  this->TmpWorkingCollection = vtkCollection::New();
29 }
30 
31 //-----------------------------------------------------------------------------
33 {
34  this->Collection->Delete();
35  this->TmpWorkingCollection->Delete();
36 }
37 
38 //-----------------------------------------------------------------------------
40 {
41  int num_elements = this->Collection->GetNumberOfItems();
42  if (elem->GetMergeable() && num_elements > 0)
43  {
44  vtkUndoElement* prev =
45  vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(num_elements - 1));
46  if (prev && prev->GetMergeable())
47  {
48  if (prev->Merge(elem))
49  {
50  // merge was successful, return index of the merge.
51  return (num_elements - 1);
52  }
53  }
54  }
55 
56  this->Collection->AddItem(elem);
57  return num_elements;
58 }
59 
60 //-----------------------------------------------------------------------------
62 {
63  this->Collection->RemoveItem(index);
64 }
65 
66 //-----------------------------------------------------------------------------
68 {
69  return vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(index));
70 }
71 
72 //-----------------------------------------------------------------------------
74 {
75  this->Collection->RemoveAllItems();
76 }
77 
78 //-----------------------------------------------------------------------------
80 {
81  return this->Collection->GetNumberOfItems();
82 }
83 
84 //-----------------------------------------------------------------------------
86 {
87  int max = this->Collection->GetNumberOfItems();
88  for (int cc = 0; cc < max; cc++)
89  {
90  vtkUndoElement* elem = vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(cc));
91 
92  // Init working context
94  if (!elem->Redo())
95  {
96  vtkDebugMacro("Redo Action is failing. Start redoing the actions.");
97  // redo failed, undo the half redone operations.
98  for (int rr = cc - 1; rr >= 0; --rr)
99  {
100  vtkUndoElement* elemU = vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(rr));
101  elemU->SetUndoSetWorkingContext(this->TmpWorkingCollection); // Init
102  elemU->Undo();
103  elemU->SetUndoSetWorkingContext(0); // Clear Working context
104  }
105  // Release ref of tmp objects
106  this->TmpWorkingCollection->RemoveAllItems();
107  return 0;
108  }
109  elem->SetUndoSetWorkingContext(0); // Clear Working context
110  }
111  // Release ref of tmp objects
112  this->TmpWorkingCollection->RemoveAllItems();
113  return 1;
114 }
115 
116 //-----------------------------------------------------------------------------
118 {
119  int max = this->Collection->GetNumberOfItems();
120  for (int cc = max - 1; cc >= 0; --cc)
121  {
122  vtkUndoElement* elem = vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(cc));
123 
124  // Init working context
126  if (!elem->Undo())
127  {
128  vtkDebugMacro("Undo Action is failing. Start redoing the actions.");
129  // undo failed, redo the half undone operations.
130  for (int rr = 0; rr < cc; ++rr)
131  {
132  vtkUndoElement* elemR = vtkUndoElement::SafeDownCast(this->Collection->GetItemAsObject(rr));
133  elemR->SetUndoSetWorkingContext(this->TmpWorkingCollection); // Init
134  elemR->Redo();
135  elemR->SetUndoSetWorkingContext(0); // Clear Working context
136  }
137  // Release ref of tmp objects
138  this->TmpWorkingCollection->RemoveAllItems();
139  return 0;
140  }
141  elem->SetUndoSetWorkingContext(0); // Clear Working context
142  }
143  // Release ref of tmp objects
144  this->TmpWorkingCollection->RemoveAllItems();
145  return 1;
146 }
147 
148 //-----------------------------------------------------------------------------
149 void vtkUndoSet::PrintSelf(ostream& os, vtkIndent indent)
150 {
151  this->Superclass::PrintSelf(os, indent);
152 }
virtual int Redo()=0
virtual int Undo()=0
virtual void SetUndoSetWorkingContext(vtkCollection *workCTX)
virtual bool Merge(vtkUndoElement *vtkNotUsed(new_element))
void RemoveElement(int index)
Definition: vtkUndoSet.cxx:61
vtkCollection * Collection
Definition: vtkUndoSet.h:67
vtkCollection * TmpWorkingCollection
Definition: vtkUndoSet.h:68
virtual int Undo()
Definition: vtkUndoSet.cxx:117
void PrintSelf(ostream &os, vtkIndent indent) override
Definition: vtkUndoSet.cxx:149
int AddElement(vtkUndoElement *elem)
Definition: vtkUndoSet.cxx:39
void RemoveAllElements()
Definition: vtkUndoSet.cxx:73
~vtkUndoSet() override
Definition: vtkUndoSet.cxx:32
virtual int Redo()
Definition: vtkUndoSet.cxx:85
vtkUndoElement * GetElement(int index)
Definition: vtkUndoSet.cxx:67
int GetNumberOfElements()
Definition: vtkUndoSet.cxx:79
vtkStandardNewMacro(vtkUndoSet)