Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
60 BackupData::~BackupData(){
61 clear();
62 }
63
64 //-----------------------------------------------------------------------------
65
66 size_t BackupData::maxBackups()
67 {
68 return maxBackups_;
69 }
70
71 //-----------------------------------------------------------------------------
72
73 void BackupData::setMaxBackups(size_t _max)
74 {
75 maxBackups_ = _max;
76 }
77
78 //-----------------------------------------------------------------------------
79
80 void BackupData::storeBackup(BaseBackup* _backup){
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
103 void BackupData::undo(){
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
122 void BackupData::redo(){
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
141 QString BackupData::undoName(){
142
143 if ( undoStates_.empty() )
144 return QString();
145
146 return currentState_->name();
147 }
148
149 //-----------------------------------------------------------------------------
150
151 QString BackupData::redoName(){
152
153 if ( redoStates_.empty() )
154 return QString();
155
156 return redoStates_.back()->name();
157 }
158
159 //-----------------------------------------------------------------------------
160
161 int BackupData::undoID(){
162
163 if ( undoStates_.empty() )
164 return -1;
165
166 return undoStates_.back()->id();
167 }
168
169 //-----------------------------------------------------------------------------
170
171 int BackupData::redoID(){
172
173 if ( redoStates_.empty() )
174 return -1;
175
176 return redoStates_.back()->id();
177 }
178
179 //-----------------------------------------------------------------------------
180
181 int BackupData::currentID(){
182
183 if ( currentState_ == 0 )
184 return -1;
185 else
186 return currentState_->id();
187 }
188
189 //-----------------------------------------------------------------------------
190
191 bool BackupData::undoAvailable(){
192 return !undoStates_.empty();
193 }
194
195 //-----------------------------------------------------------------------------
196
197 bool BackupData::redoAvailable(){
198 return !redoStates_.empty();
199 }
200
201 //-----------------------------------------------------------------------------
202
203 bool BackupData::undoBlocked(){
204
205 if( !undoStates_.empty() )
206 return currentState_->blocked();
207 else
208 return false;
209 }
210
211 //-----------------------------------------------------------------------------
212
213 bool BackupData::redoBlocked(){
214
215 if( !redoStates_.empty() )
216 return redoStates_.back()->blocked();
217 else
218 return false;
219 }
220 //-----------------------------------------------------------------------------
221
222 void BackupData::clear(){
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
249 BaseBackup* BackupData::currentState(){
250 return currentState_;
251 }
252
253 //-----------------------------------------------------------------------------
BackupData::redoID
int redoID()
return the id of the next redo backup
Definition: BackupData.cc:171
BaseBackup
Class that encapsulates a backup.
Definition: BaseBackup.hh:62
BackupData::undoID
int undoID()
return the id of the next undo backup
Definition: BackupData.cc:161
BaseObjectData
Definition: BaseObjectData.hh:105
BackupData::undo
void undo()
perform an undo if possible
Definition: BackupData.cc:103
BackupData::storeBackup
void storeBackup(BaseBackup *_backup)
store a backup
Definition: BackupData.cc:80
BackupData::clear
void clear()
remove all backups
Definition: BackupData.cc:222
BackupData::redoBlocked
bool redoBlocked()
return if a redo backup is blocked
Definition: BackupData.cc:213
BackupData::BackupData
BackupData(BaseObjectData *_object=0)
Constructor.
Definition: BackupData.cc:54
BackupData::redo
void redo()
perform a redo if possible
Definition: BackupData.cc:122
BaseBackup::setLinks
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:162
BackupData::currentID
int currentID()
return the id of the current backup state
Definition: BackupData.cc:181
IdList
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:192
BaseBackup::name
QString name()
Get the backups name)
Definition: BaseBackup.cc:144
BackupData::undoBlocked
bool undoBlocked()
return if an undo backup is blocked
Definition: BackupData.cc:203
BaseBackup::id
int id()
get id of this backup
Definition: BaseBackup.cc:150
BackupData::currentState
BaseBackup * currentState()
return the current state
Definition: BackupData.cc:249
BaseBackup::blocked
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:156
BackupData::undoAvailable
bool undoAvailable()
return if an undo backup is available
Definition: BackupData.cc:191
BackupData::setLinks
void setLinks(IdList _objectIDs)
Definition: BackupData.cc:241
BackupData::maxBackups
size_t maxBackups()
return the maximum of backups which are saved
Definition: BackupData.cc:66
BaseBackup::apply
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:101
BackupData::undoName
QString undoName()
return the name of the next undo backup
Definition: BackupData.cc:141
BackupData::redoAvailable
bool redoAvailable()
return if a redo backup is available
Definition: BackupData.cc:197
BackupData::~BackupData
virtual ~BackupData()
Destructor.
Definition: BackupData.cc:60
BackupData::redoName
QString redoName()
return the name of the next redo backup
Definition: BackupData.cc:151
BackupData::setMaxBackups
void setMaxBackups(size_t _max)
set the maximum of saved backups
Definition: BackupData.cc:73