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
52 BackupData::~BackupData(){
53 clear();
54 }
55
56 //-----------------------------------------------------------------------------
57
58 size_t BackupData::maxBackups()
59 {
60 return maxBackups_;
61 }
62
63 //-----------------------------------------------------------------------------
64
65 void BackupData::setMaxBackups(size_t _max)
66 {
67 maxBackups_ = _max;
68 }
69
70 //-----------------------------------------------------------------------------
71
72 void BackupData::storeBackup(BaseBackup* _backup){
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
95 void BackupData::undo(){
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
114 void BackupData::redo(){
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
133 QString BackupData::undoName(){
134
135 if ( undoStates_.empty() )
136 return QString();
137
138 return currentState_->name();
139 }
140
141 //-----------------------------------------------------------------------------
142
143 QString BackupData::redoName(){
144
145 if ( redoStates_.empty() )
146 return QString();
147
148 return redoStates_.back()->name();
149 }
150
151 //-----------------------------------------------------------------------------
152
153 int BackupData::undoID(){
154
155 if ( undoStates_.empty() )
156 return -1;
157
158 return undoStates_.back()->id();
159 }
160
161 //-----------------------------------------------------------------------------
162
163 int BackupData::redoID(){
164
165 if ( redoStates_.empty() )
166 return -1;
167
168 return redoStates_.back()->id();
169 }
170
171 //-----------------------------------------------------------------------------
172
173 int BackupData::currentID(){
174
175 if ( currentState_ == 0 )
176 return -1;
177 else
178 return currentState_->id();
179 }
180
181 //-----------------------------------------------------------------------------
182
183 bool BackupData::undoAvailable(){
184 return !undoStates_.empty();
185 }
186
187 //-----------------------------------------------------------------------------
188
189 bool BackupData::redoAvailable(){
190 return !redoStates_.empty();
191 }
192
193 //-----------------------------------------------------------------------------
194
195 bool BackupData::undoBlocked(){
196
197 if( !undoStates_.empty() )
198 return currentState_->blocked();
199 else
200 return false;
201 }
202
203 //-----------------------------------------------------------------------------
204
205 bool BackupData::redoBlocked(){
206
207 if( !redoStates_.empty() )
208 return redoStates_.back()->blocked();
209 else
210 return false;
211 }
212 //-----------------------------------------------------------------------------
213
214 void BackupData::clear(){
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
241 BaseBackup* BackupData::currentState(){
242 return currentState_;
243 }
244
245 //-----------------------------------------------------------------------------
BackupData::redoBlocked
bool redoBlocked()
return if a redo backup is blocked
Definition: BackupData.cc:205
BackupData::undo
void undo()
perform an undo if possible
Definition: BackupData.cc:95
BackupData::maxBackups
size_t maxBackups()
return the maximum of backups which are saved
Definition: BackupData.cc:58
BaseBackup::id
int id()
get id of this backup
Definition: BaseBackup.cc:142
BackupData::currentID
int currentID()
return the id of the current backup state
Definition: BackupData.cc:173
BaseBackup::name
QString name()
Get the backups name)
Definition: BaseBackup.cc:136
BackupData::undoID
int undoID()
return the id of the next undo backup
Definition: BackupData.cc:153
BackupData::setLinks
void setLinks(IdList _objectIDs)
Definition: BackupData.cc:233
BaseBackup::blocked
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:148
BaseBackup
Class that encapsulates a backup.
Definition: BaseBackup.hh:53
BackupData::currentState
BaseBackup * currentState()
return the current state
Definition: BackupData.cc:241
BackupData::undoName
QString undoName()
return the name of the next undo backup
Definition: BackupData.cc:133
BackupData::clear
void clear()
remove all backups
Definition: BackupData.cc:214
IdList
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
BaseBackup::apply
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:93
BackupData::storeBackup
void storeBackup(BaseBackup *_backup)
store a backup
Definition: BackupData.cc:72
BackupData::setMaxBackups
void setMaxBackups(size_t _max)
set the maximum of saved backups
Definition: BackupData.cc:65
BackupData::redoID
int redoID()
return the id of the next redo backup
Definition: BackupData.cc:163
BackupData::redoName
QString redoName()
return the name of the next redo backup
Definition: BackupData.cc:143
BackupData::redoAvailable
bool redoAvailable()
return if a redo backup is available
Definition: BackupData.cc:189
BackupData::~BackupData
virtual ~BackupData()
Destructor.
Definition: BackupData.cc:52
BackupData::undoAvailable
bool undoAvailable()
return if an undo backup is available
Definition: BackupData.cc:183
BaseObjectData
Definition: BaseObjectData.hh:102
BackupData::redo
void redo()
perform a redo if possible
Definition: BackupData.cc:114
BaseBackup::setLinks
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:154
BackupData::undoBlocked
bool undoBlocked()
return if an undo backup is blocked
Definition: BackupData.cc:195
BackupData::BackupData
BackupData(BaseObjectData *_object=0)
Constructor.
Definition: BackupData.cc:46