Rev 20793 | Rev 22694 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
1111 | tsoots | 7 | /*************************************************************************** |
8 | * Copyright (C) 2005 by Riku Leino * |
||
5116 | tsoots | 9 | * riku@scribus.info * |
1111 | tsoots | 10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
||
12 | * it under the terms of the GNU General Public License as published by * |
||
13 | * the Free Software Foundation; either version 2 of the License, or * |
||
14 | * (at your option) any later version. * |
||
15 | * * |
||
16 | * This program is distributed in the hope that it will be useful, * |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
19 | * GNU General Public License for more details. * |
||
20 | * * |
||
21 | * You should have received a copy of the GNU General Public License * |
||
22 | * along with this program; if not, write to the * |
||
23 | * Free Software Foundation, Inc., * |
||
18122 | mrdocs | 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
1111 | tsoots | 25 | ***************************************************************************/ |
26 | |||
27 | #include "undostate.h" |
||
5116 | tsoots | 28 | #include "undoobject.h" |
1111 | tsoots | 29 | |
5116 | tsoots | 30 | UndoState::UndoState(const QString& name, const QString& description, QPixmap* pixmap) : |
22601 | craig | 31 | transactionCode(0), |
32 | actionName_(name), |
||
33 | actionDescription_(description), |
||
34 | actionPixmap_(pixmap), |
||
35 | undoObject_(nullptr) |
||
1111 | tsoots | 36 | { |
5116 | tsoots | 37 | |
1111 | tsoots | 38 | } |
39 | |||
40 | QString UndoState::getName() |
||
41 | { |
||
5116 | tsoots | 42 | return actionName_; |
1111 | tsoots | 43 | } |
44 | |||
1213 | tsoots | 45 | void UndoState::setName(const QString &newName) |
46 | { |
||
5116 | tsoots | 47 | actionName_ = newName; |
1213 | tsoots | 48 | } |
49 | |||
1111 | tsoots | 50 | QString UndoState::getDescription() |
51 | { |
||
5116 | tsoots | 52 | return actionDescription_; |
1111 | tsoots | 53 | } |
54 | |||
1213 | tsoots | 55 | void UndoState::setDescription(const QString &newDescription) |
56 | { |
||
5116 | tsoots | 57 | actionDescription_ = newDescription; |
1213 | tsoots | 58 | } |
59 | |||
1111 | tsoots | 60 | QPixmap* UndoState::getPixmap() |
61 | { |
||
5116 | tsoots | 62 | return actionPixmap_; |
1111 | tsoots | 63 | } |
64 | |||
1213 | tsoots | 65 | void UndoState::setPixmap(QPixmap *pixmap) |
66 | { |
||
5116 | tsoots | 67 | actionPixmap_ = pixmap; |
1213 | tsoots | 68 | } |
69 | |||
5116 | tsoots | 70 | void UndoState::undo() |
71 | { |
||
72 | if (undoObject_) // if !undoObject_ there's an error, hmmm |
||
73 | undoObject_->restore(this, true); |
||
74 | } |
||
75 | |||
76 | void UndoState::redo() |
||
77 | { |
||
78 | if (undoObject_) |
||
79 | undoObject_->restore(this, false); |
||
80 | } |
||
81 | |||
82 | void UndoState::setUndoObject(UndoObject *object) |
||
83 | { |
||
11168 | jghali | 84 | undoObject_ = object->undoObjectPtr(); |
5116 | tsoots | 85 | } |
86 | |||
87 | UndoObject* UndoState::undoObject() |
||
88 | { |
||
89 | return undoObject_; |
||
90 | } |
||
91 | |||
1111 | tsoots | 92 | UndoState::~UndoState() |
93 | { |
||
1518 | tsoots | 94 | |
1111 | tsoots | 95 | } |
96 | |||
97 | /*** SimpleState **************************************************************/ |
||
98 | |||
99 | SimpleState::SimpleState(const QString& name, const QString& description, QPixmap* pixmap) |
||
100 | : UndoState(name, description, pixmap) |
||
101 | { |
||
102 | |||
103 | } |
||
104 | |||
105 | bool SimpleState::contains(const QString& key) |
||
106 | { |
||
20793 | jghali | 107 | return m_values.contains(key); |
1111 | tsoots | 108 | } |
109 | |||
13461 | jghali | 110 | QVariant SimpleState::variant(const QString& key, const QVariant& def) |
111 | { |
||
20793 | jghali | 112 | QMap<QString, QVariant>::const_iterator it = m_values.find(key); |
113 | if (it != m_values.end()) |
||
13461 | jghali | 114 | return it.value(); |
115 | |||
20793 | jghali | 116 | m_values[key] = def; |
13461 | jghali | 117 | return def; |
118 | } |
||
119 | |||
1111 | tsoots | 120 | QString SimpleState::get(const QString& key, const QString& def) |
121 | { |
||
20793 | jghali | 122 | QMap<QString, QVariant>::const_iterator it = m_values.find(key); |
123 | if (it != m_values.end()) |
||
13461 | jghali | 124 | return it.value().toString(); |
1111 | tsoots | 125 | |
20793 | jghali | 126 | m_values[key] = def; |
13461 | jghali | 127 | return def; |
1111 | tsoots | 128 | } |
129 | |||
130 | int SimpleState::getInt(const QString& key, int def) |
||
131 | { |
||
132 | bool ok = false; |
||
13461 | jghali | 133 | QVariant retVar = variant(key, QVariant(def)); |
134 | int ret = retVar.toInt(&ok); |
||
1111 | tsoots | 135 | if (!ok) |
136 | ret = def; |
||
137 | return ret; |
||
138 | } |
||
139 | |||
1238 | tsoots | 140 | uint SimpleState::getUInt(const QString& key, uint def) |
141 | { |
||
142 | bool ok = false; |
||
13461 | jghali | 143 | QVariant retVar = variant(key, QVariant(def)); |
144 | uint ret = retVar.toUInt(&ok); |
||
1238 | tsoots | 145 | if (!ok) |
146 | ret = def; |
||
147 | return ret; |
||
148 | } |
||
149 | |||
1111 | tsoots | 150 | double SimpleState::getDouble(const QString& key, double def) |
151 | { |
||
152 | bool ok = false; |
||
13461 | jghali | 153 | QVariant retVar = variant(key, QVariant(def)); |
154 | double ret = retVar.toDouble(&ok); |
||
1111 | tsoots | 155 | if (!ok) |
156 | ret = def; |
||
157 | return ret; |
||
158 | } |
||
159 | |||
1179 | tsoots | 160 | bool SimpleState::getBool(const QString& key, bool def) |
161 | { |
||
162 | bool ok = false; |
||
13461 | jghali | 163 | QVariant retVar = variant(key, QVariant(def)); |
164 | int ret = retVar.toInt(&ok); |
||
1179 | tsoots | 165 | if (!ok) |
166 | ret = def; |
||
167 | return ret; |
||
168 | } |
||
169 | |||
20793 | jghali | 170 | void SimpleState::set(const QString& key) |
171 | { |
||
172 | m_values[key] = QVariant(); |
||
173 | } |
||
174 | |||
1111 | tsoots | 175 | void SimpleState::set(const QString& key, const QString& value) |
176 | { |
||
20793 | jghali | 177 | m_values[key] = QVariant(value); |
1111 | tsoots | 178 | } |
179 | |||
180 | void SimpleState::set(const QString& key, int value) |
||
181 | { |
||
20793 | jghali | 182 | m_values[key] = QVariant(value); |
1111 | tsoots | 183 | } |
184 | |||
1238 | tsoots | 185 | void SimpleState::set(const QString& key, uint value) |
186 | { |
||
20793 | jghali | 187 | m_values[key] = QVariant(value); |
1238 | tsoots | 188 | } |
189 | |||
1111 | tsoots | 190 | void SimpleState::set(const QString& key, double value) |
191 | { |
||
20793 | jghali | 192 | m_values[key] = QVariant(value); |
1111 | tsoots | 193 | } |
194 | |||
1179 | tsoots | 195 | void SimpleState::set(const QString& key, bool value) |
196 | { |
||
20793 | jghali | 197 | m_values[key] = QVariant(value); |
1179 | tsoots | 198 | } |
1111 | tsoots | 199 | |
1179 | tsoots | 200 | |
1518 | tsoots | 201 | SimpleState::~SimpleState() |
1111 | tsoots | 202 | { |
203 | |||
204 | } |
||
19416 | jghali | 205 | |
206 | /*** TransactionState *****************************************************/ |
||
207 | |||
208 | TransactionState::TransactionState() : UndoState("") |
||
209 | { |
||
210 | size_ = 0; |
||
211 | } |
||
212 | |||
19929 | jghali | 213 | UndoState* TransactionState::at(int index) const |
19416 | jghali | 214 | { |
215 | if (index >= 0 && static_cast<uint>(index) < sizet()) |
||
216 | return states_[index]; |
||
22601 | craig | 217 | return nullptr; |
19416 | jghali | 218 | } |
219 | |||
19929 | jghali | 220 | UndoState* TransactionState::last() const |
221 | { |
||
22601 | craig | 222 | if (!states_.empty()) |
19929 | jghali | 223 | return states_.at(size_ - 1); |
22601 | craig | 224 | return nullptr; |
19929 | jghali | 225 | } |
226 | |||
19416 | jghali | 227 | bool TransactionState::contains(int uid) const |
228 | { |
||
22601 | craig | 229 | for (int i = 0; i < states_.size(); ++i) |
19416 | jghali | 230 | { |
231 | UndoObject* undoObject = states_[i]->undoObject(); |
||
232 | if (undoObject && undoObject->getUId() == static_cast<uint>(uid)) |
||
233 | return true; |
||
234 | } |
||
235 | return false; |
||
236 | } |
||
237 | |||
238 | bool TransactionState::containsOnly(int uid) const |
||
239 | { |
||
22601 | craig | 240 | for (int i = 0; i < states_.size(); ++i) |
19416 | jghali | 241 | { |
242 | UndoObject* undoObject = states_[i]->undoObject(); |
||
243 | if (undoObject && undoObject->getUId() != static_cast<uint>(uid)) |
||
244 | return false; |
||
245 | } |
||
246 | return true; |
||
247 | } |
||
248 | |||
249 | void TransactionState::pushBack(UndoObject *target, UndoState *state) |
||
250 | { |
||
251 | if (target && state) |
||
252 | { |
||
253 | state->setUndoObject(target); |
||
254 | states_.push_back(state); |
||
255 | ++size_; |
||
256 | } |
||
257 | } |
||
258 | |||
19929 | jghali | 259 | uint TransactionState::sizet() const |
19416 | jghali | 260 | { |
261 | return size_; |
||
262 | } |
||
263 | |||
264 | void TransactionState::useActionName() |
||
265 | { |
||
266 | if (size_ > 0) |
||
267 | setName(states_[size_ - 1]->getName()); |
||
268 | } |
||
269 | |||
270 | UndoObject* TransactionState::replace(ulong uid, UndoObject *newUndoObject) |
||
271 | { |
||
22601 | craig | 272 | UndoObject *tmp = nullptr; |
273 | for (int i = 0; i < states_.size(); ++i) |
||
19416 | jghali | 274 | { |
275 | TransactionState *ts = dynamic_cast<TransactionState*>(states_[i]); |
||
276 | if (ts) // are we having a transaction_inside a transaction |
||
277 | ts->replace(uid, newUndoObject); |
||
278 | else if (states_[i]->undoObject() && states_[i]->undoObject()->getUId() == uid) |
||
279 | { |
||
280 | tmp = states_[i]->undoObject(); |
||
281 | states_[i]->setUndoObject(newUndoObject); |
||
282 | } |
||
283 | } |
||
284 | return tmp; |
||
285 | } |
||
286 | |||
287 | void TransactionState::undo() // undo all attached states |
||
288 | { |
||
289 | for (int i = sizet() - 1; i > -1; --i) |
||
290 | { |
||
291 | if ((sizet() - 1) == 0) |
||
292 | at(i)->transactionCode = 0; |
||
293 | else |
||
294 | { |
||
295 | if (i == static_cast<int>(sizet() - 1)) |
||
296 | at(i)->transactionCode = 1; |
||
297 | else if (i == 0) |
||
298 | at(i)->transactionCode = 2; |
||
299 | else |
||
300 | at(i)->transactionCode = 3; |
||
301 | } |
||
302 | if (transactionCode != 0) |
||
303 | at(i)->transactionCode = transactionCode; |
||
304 | at(i)->undo(); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | void TransactionState::redo() // redo all attached states |
||
309 | { |
||
310 | for (uint i = 0; i < sizet(); ++i) |
||
311 | { |
||
312 | if ((sizet() - 1) == 0) |
||
313 | at(i)->transactionCode = 0; |
||
314 | else |
||
315 | { |
||
316 | if (i == 0) |
||
317 | at(i)->transactionCode = 1; |
||
318 | else if (i == static_cast<uint>(sizet() - 1)) |
||
319 | at(i)->transactionCode = 2; |
||
320 | else |
||
321 | at(i)->transactionCode = 3; |
||
322 | } |
||
323 | if (transactionCode != 0) |
||
324 | at(i)->transactionCode = transactionCode; |
||
325 | at(i)->redo(); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | TransactionState::~TransactionState() |
||
330 | { |
||
22601 | craig | 331 | for (int i = 0; i < states_.size(); ++i) |
19416 | jghali | 332 | { |
333 | if (states_[i]) |
||
334 | { |
||
335 | delete states_[i]; |
||
22601 | craig | 336 | states_[i] = nullptr; |
19416 | jghali | 337 | } |
338 | } |
||
339 | } |