Developer Documentation
BackupData.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 #include "BackupData.hh"
43 
44 //-----------------------------------------------------------------------------
45 
46 BackupData::BackupData(BaseObjectData* _object) : currentState_(0), object_(_object), maxBackups_(20)
47 {
48 }
49 
50 //-----------------------------------------------------------------------------
51 
53  clear();
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59 {
60  return maxBackups_;
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 void BackupData::setMaxBackups(size_t _max)
66 {
67  maxBackups_ = _max;
68 }
69 
70 //-----------------------------------------------------------------------------
71 
73 
74  //store state
75  if ( currentState_ != 0 )
76  undoStates_.push_back( currentState_ );
77 
78  currentState_ = _backup;
79 
80  //clear redo states
81  while( !redoStates_.empty() ){
82  delete redoStates_.back();
83  redoStates_.pop_back();
84  }
85 
86  //delete undo backups if there are too many
87  while( undoStates_.size() > maxBackups_ ){
88  delete undoStates_.front();
89  undoStates_.erase( undoStates_.begin() );
90  }
91 }
92 
93 //-----------------------------------------------------------------------------
94 
96 
97  if ( undoStates_.empty() )
98  return;
99 
100  //get backup
101  BaseBackup* backup = undoStates_.back();
102 
103  //apply
104  backup->apply();
105 
106  // update current state
107  undoStates_.pop_back();
108  redoStates_.push_back( currentState_ );
109  currentState_ = backup;
110 }
111 
112 //-----------------------------------------------------------------------------
113 
115 
116  if ( redoStates_.empty() )
117  return;
118 
119  //get backup
120  BaseBackup* backup = redoStates_.back();
121 
122  //apply
123  backup->apply();
124 
125  // update current state
126  redoStates_.pop_back();
127  undoStates_.push_back( currentState_ );
128  currentState_ = backup;
129 }
130 
131 //-----------------------------------------------------------------------------
132 
134 
135  if ( undoStates_.empty() )
136  return QString();
137 
138  return currentState_->name();
139 }
140 
141 //-----------------------------------------------------------------------------
142 
144 
145  if ( redoStates_.empty() )
146  return QString();
147 
148  return redoStates_.back()->name();
149 }
150 
151 //-----------------------------------------------------------------------------
152 
154 
155  if ( undoStates_.empty() )
156  return -1;
157 
158  return undoStates_.back()->id();
159 }
160 
161 //-----------------------------------------------------------------------------
162 
164 
165  if ( redoStates_.empty() )
166  return -1;
167 
168  return redoStates_.back()->id();
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174 
175  if ( currentState_ == 0 )
176  return -1;
177  else
178  return currentState_->id();
179 }
180 
181 //-----------------------------------------------------------------------------
182 
184  return !undoStates_.empty();
185 }
186 
187 //-----------------------------------------------------------------------------
188 
190  return !redoStates_.empty();
191 }
192 
193 //-----------------------------------------------------------------------------
194 
196 
197  if( !undoStates_.empty() )
198  return currentState_->blocked();
199  else
200  return false;
201 }
202 
203 //-----------------------------------------------------------------------------
204 
206 
207  if( !redoStates_.empty() )
208  return redoStates_.back()->blocked();
209  else
210  return false;
211 }
212 //-----------------------------------------------------------------------------
213 
215 
216  while( !undoStates_.empty() ){
217  delete undoStates_.back();
218  undoStates_.pop_back();
219  }
220 
221  while( !redoStates_.empty() ){
222  delete redoStates_.back();
223  redoStates_.pop_back();
224  }
225 
226  if ( currentState_ != 0 )
227  delete currentState_;
228  currentState_ = 0;
229 }
230 
231 //-----------------------------------------------------------------------------
232 
233 void BackupData::setLinks(IdList _objectIDs){
234 
235  if ( currentState_ != 0 )
236  currentState_->setLinks(_objectIDs);
237 }
238 
239 //-----------------------------------------------------------------------------
240 
242  return currentState_;
243 }
244 
245 //-----------------------------------------------------------------------------
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:93
void redo()
perform a redo if possible
Definition: BackupData.cc:114
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:148
bool redoAvailable()
return if a redo backup is available
Definition: BackupData.cc:189
bool undoBlocked()
return if an undo backup is blocked
Definition: BackupData.cc:195
int id()
get id of this backup
Definition: BaseBackup.cc:142
void setLinks(IdList _objectIDs)
Definition: BackupData.cc:233
void storeBackup(BaseBackup *_backup)
store a backup
Definition: BackupData.cc:72
QString redoName()
return the name of the next redo backup
Definition: BackupData.cc:143
int redoID()
return the id of the next redo backup
Definition: BackupData.cc:163
QString name()
Get the backups name)
Definition: BaseBackup.cc:136
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
size_t maxBackups()
return the maximum of backups which are saved
Definition: BackupData.cc:58
void setMaxBackups(size_t _max)
set the maximum of saved backups
Definition: BackupData.cc:65
int undoID()
return the id of the next undo backup
Definition: BackupData.cc:153
bool redoBlocked()
return if a redo backup is blocked
Definition: BackupData.cc:205
int currentID()
return the id of the current backup state
Definition: BackupData.cc:173
BaseBackup * currentState()
return the current state
Definition: BackupData.cc:241
virtual ~BackupData()
Destructor.
Definition: BackupData.cc:52
BackupData(BaseObjectData *_object=0)
Constructor.
Definition: BackupData.cc:46
void clear()
remove all backups
Definition: BackupData.cc:214
Class that encapsulates a backup.
Definition: BaseBackup.hh:53
QString undoName()
return the name of the next undo backup
Definition: BackupData.cc:133
bool undoAvailable()
return if an undo backup is available
Definition: BackupData.cc:183
void undo()
perform an undo if possible
Definition: BackupData.cc:95
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:154