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 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "BackupData.hh"
51 
52 //-----------------------------------------------------------------------------
53 
54 BackupData::BackupData(BaseObjectData* _object) : currentState_(0), object_(_object), maxBackups_(20)
55 {
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61  clear();
62 }
63 
64 //-----------------------------------------------------------------------------
65 
67 {
68  return maxBackups_;
69 }
70 
71 //-----------------------------------------------------------------------------
72 
73 void BackupData::setMaxBackups(size_t _max)
74 {
75  maxBackups_ = _max;
76 }
77 
78 //-----------------------------------------------------------------------------
79 
81 
82  //store state
83  if ( currentState_ != 0 )
84  undoStates_.push_back( currentState_ );
85 
86  currentState_ = _backup;
87 
88  //clear redo states
89  while( !redoStates_.empty() ){
90  delete redoStates_.back();
91  redoStates_.pop_back();
92  }
93 
94  //delete undo backups if there are too many
95  while( undoStates_.size() > maxBackups_ ){
96  delete undoStates_.front();
97  undoStates_.erase( undoStates_.begin() );
98  }
99 }
100 
101 //-----------------------------------------------------------------------------
102 
104 
105  if ( undoStates_.empty() )
106  return;
107 
108  //get backup
109  BaseBackup* backup = undoStates_.back();
110 
111  //apply
112  backup->apply();
113 
114  // update current state
115  undoStates_.pop_back();
116  redoStates_.push_back( currentState_ );
117  currentState_ = backup;
118 }
119 
120 //-----------------------------------------------------------------------------
121 
123 
124  if ( redoStates_.empty() )
125  return;
126 
127  //get backup
128  BaseBackup* backup = redoStates_.back();
129 
130  //apply
131  backup->apply();
132 
133  // update current state
134  redoStates_.pop_back();
135  undoStates_.push_back( currentState_ );
136  currentState_ = backup;
137 }
138 
139 //-----------------------------------------------------------------------------
140 
142 
143  if ( undoStates_.empty() )
144  return QString();
145 
146  return currentState_->name();
147 }
148 
149 //-----------------------------------------------------------------------------
150 
152 
153  if ( redoStates_.empty() )
154  return QString();
155 
156  return redoStates_.back()->name();
157 }
158 
159 //-----------------------------------------------------------------------------
160 
162 
163  if ( undoStates_.empty() )
164  return -1;
165 
166  return undoStates_.back()->id();
167 }
168 
169 //-----------------------------------------------------------------------------
170 
172 
173  if ( redoStates_.empty() )
174  return -1;
175 
176  return redoStates_.back()->id();
177 }
178 
179 //-----------------------------------------------------------------------------
180 
182 
183  if ( currentState_ == 0 )
184  return -1;
185  else
186  return currentState_->id();
187 }
188 
189 //-----------------------------------------------------------------------------
190 
192  return !undoStates_.empty();
193 }
194 
195 //-----------------------------------------------------------------------------
196 
198  return !redoStates_.empty();
199 }
200 
201 //-----------------------------------------------------------------------------
202 
204 
205  if( !undoStates_.empty() )
206  return currentState_->blocked();
207  else
208  return false;
209 }
210 
211 //-----------------------------------------------------------------------------
212 
214 
215  if( !redoStates_.empty() )
216  return redoStates_.back()->blocked();
217  else
218  return false;
219 }
220 //-----------------------------------------------------------------------------
221 
223 
224  while( !undoStates_.empty() ){
225  delete undoStates_.back();
226  undoStates_.pop_back();
227  }
228 
229  while( !redoStates_.empty() ){
230  delete redoStates_.back();
231  redoStates_.pop_back();
232  }
233 
234  if ( currentState_ != 0 )
235  delete currentState_;
236  currentState_ = 0;
237 }
238 
239 //-----------------------------------------------------------------------------
240 
241 void BackupData::setLinks(IdList _objectIDs){
242 
243  if ( currentState_ != 0 )
244  currentState_->setLinks(_objectIDs);
245 }
246 
247 //-----------------------------------------------------------------------------
248 
250  return currentState_;
251 }
252 
253 //-----------------------------------------------------------------------------
bool redoAvailable()
return if a redo backup is available
Definition: BackupData.cc:197
void setMaxBackups(size_t _max)
set the maximum of saved backups
Definition: BackupData.cc:73
bool redoBlocked()
return if a redo backup is blocked
Definition: BackupData.cc:213
int currentID()
return the id of the current backup state
Definition: BackupData.cc:181
virtual ~BackupData()
Destructor.
Definition: BackupData.cc:60
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:156
BackupData(BaseObjectData *_object=0)
Constructor.
Definition: BackupData.cc:54
void setLinks(IdList _objectIDs)
Definition: BackupData.cc:241
Class that encapsulates a backup.
Definition: BaseBackup.hh:62
bool undoAvailable()
return if an undo backup is available
Definition: BackupData.cc:191
QString redoName()
return the name of the next redo backup
Definition: BackupData.cc:151
bool undoBlocked()
return if an undo backup is blocked
Definition: BackupData.cc:203
void storeBackup(BaseBackup *_backup)
store a backup
Definition: BackupData.cc:80
BaseBackup * currentState()
return the current state
Definition: BackupData.cc:249
int undoID()
return the id of the next undo backup
Definition: BackupData.cc:161
int id()
get id of this backup
Definition: BaseBackup.cc:150
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:192
void redo()
perform a redo if possible
Definition: BackupData.cc:122
void undo()
perform an undo if possible
Definition: BackupData.cc:103
void clear()
remove all backups
Definition: BackupData.cc:222
QString name()
Get the backups name)
Definition: BaseBackup.cc:144
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:162
QString undoName()
return the name of the next undo backup
Definition: BackupData.cc:141
int redoID()
return the id of the next redo backup
Definition: BackupData.cc:171
size_t maxBackups()
return the maximum of backups which are saved
Definition: BackupData.cc:66
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:101