Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent) | - |
6 | { | - |
7 | d = new QUndoCommandPrivate; | - |
8 | if (parent != 0) | - |
9 | parent->d->child_list.append(this); | - |
10 | setText(text); | - |
11 | } | - |
12 | QUndoCommand::QUndoCommand(QUndoCommand *parent) | - |
13 | { | - |
14 | d = new QUndoCommandPrivate; | - |
15 | if (parent != 0) | - |
16 | parent->d->child_list.append(this); | - |
17 | } | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | QUndoCommand::~QUndoCommand() | - |
26 | { | - |
27 | qDeleteAll(d->child_list); | - |
28 | delete d; | - |
29 | } | - |
30 | int QUndoCommand::id() const | - |
31 | { | - |
32 | return -1; | - |
33 | } | - |
34 | bool QUndoCommand::mergeWith(const QUndoCommand *command) | - |
35 | { | - |
36 | (void)command;; | - |
37 | return false; | - |
38 | } | - |
39 | void QUndoCommand::redo() | - |
40 | { | - |
41 | for (int i = 0; i < d->child_list.size(); ++i) | - |
42 | d->child_list.at(i)->redo(); | - |
43 | } | - |
44 | void QUndoCommand::undo() | - |
45 | { | - |
46 | for (int i = d->child_list.size() - 1; i >= 0; --i) | - |
47 | d->child_list.at(i)->undo(); | - |
48 | } | - |
49 | QString QUndoCommand::text() const | - |
50 | { | - |
51 | return d->text; | - |
52 | } | - |
53 | QString QUndoCommand::actionText() const | - |
54 | { | - |
55 | return d->actionText; | - |
56 | } | - |
57 | void QUndoCommand::setText(const QString &text) | - |
58 | { | - |
59 | int cdpos = text.indexOf(QLatin1Char('\n')); | - |
60 | if (cdpos > 0) { | - |
61 | d->text = text.left(cdpos); | - |
62 | d->actionText = text.mid(cdpos + 1); | - |
63 | } else { | - |
64 | d->text = text; | - |
65 | d->actionText = text; | - |
66 | } | - |
67 | } | - |
68 | int QUndoCommand::childCount() const | - |
69 | { | - |
70 | return d->child_list.count(); | - |
71 | } | - |
72 | const QUndoCommand *QUndoCommand::child(int index) const | - |
73 | { | - |
74 | if (index < 0 || index >= d->child_list.count()) | - |
75 | return 0; | - |
76 | return d->child_list.at(index); | - |
77 | } | - |
78 | QUndoAction::QUndoAction(const QString &prefix, QObject *parent) | - |
79 | : QAction(parent) | - |
80 | { | - |
81 | m_prefix = prefix; | - |
82 | } | - |
83 | | - |
84 | void QUndoAction::setPrefixedText(const QString &text) | - |
85 | { | - |
86 | if (m_defaultText.isEmpty()) { | - |
87 | QString s = m_prefix; | - |
88 | if (!m_prefix.isEmpty() && !text.isEmpty()) | - |
89 | s.append(QLatin1Char(' ')); | - |
90 | s.append(text); | - |
91 | setText(s); | - |
92 | } else { | - |
93 | if (text.isEmpty()) | - |
94 | setText(m_defaultText); | - |
95 | else | - |
96 | setText(m_prefix.arg(text)); | - |
97 | } | - |
98 | } | - |
99 | | - |
100 | void QUndoAction::setTextFormat(const QString &textFormat, const QString &defaultText) | - |
101 | { | - |
102 | m_prefix = textFormat; | - |
103 | m_defaultText = defaultText; | - |
104 | } | - |
105 | void QUndoStackPrivate::setIndex(int idx, bool clean) | - |
106 | { | - |
107 | QUndoStack * const q = q_func(); | - |
108 | | - |
109 | bool was_clean = index == clean_index; | - |
110 | | - |
111 | if (idx != index) { | - |
112 | index = idx; | - |
113 | q->indexChanged(index); | - |
114 | q->canUndoChanged(q->canUndo()); | - |
115 | q->undoTextChanged(q->undoText()); | - |
116 | q->canRedoChanged(q->canRedo()); | - |
117 | q->redoTextChanged(q->redoText()); | - |
118 | } | - |
119 | | - |
120 | if (clean) | - |
121 | clean_index = index; | - |
122 | | - |
123 | bool is_clean = index == clean_index; | - |
124 | if (is_clean != was_clean) | - |
125 | q->cleanChanged(is_clean); | - |
126 | } | - |
127 | bool QUndoStackPrivate::checkUndoLimit() | - |
128 | { | - |
129 | if (undo_limit <= 0 || !macro_stack.isEmpty() || undo_limit >= command_list.count()) | - |
130 | return false; | - |
131 | | - |
132 | int del_count = command_list.count() - undo_limit; | - |
133 | | - |
134 | for (int i = 0; i < del_count; ++i) | - |
135 | delete command_list.takeFirst(); | - |
136 | | - |
137 | index -= del_count; | - |
138 | if (clean_index != -1) { | - |
139 | if (clean_index < del_count) | - |
140 | clean_index = -1; | - |
141 | else | - |
142 | clean_index -= del_count; | - |
143 | } | - |
144 | | - |
145 | return true; | - |
146 | } | - |
147 | QUndoStack::QUndoStack(QObject *parent) | - |
148 | : QObject(*(new QUndoStackPrivate), parent) | - |
149 | { | - |
150 | | - |
151 | if (QUndoGroup *group = qobject_cast<QUndoGroup*>(parent)) | - |
152 | group->addStack(this); | - |
153 | | - |
154 | } | - |
155 | QUndoStack::~QUndoStack() | - |
156 | { | - |
157 | | - |
158 | QUndoStackPrivate * const d = d_func(); | - |
159 | if (d->group != 0) | - |
160 | d->group->removeStack(this); | - |
161 | | - |
162 | clear(); | - |
163 | } | - |
164 | void QUndoStack::clear() | - |
165 | { | - |
166 | QUndoStackPrivate * const d = d_func(); | - |
167 | | - |
168 | if (d->command_list.isEmpty()) | - |
169 | return; | - |
170 | | - |
171 | bool was_clean = isClean(); | - |
172 | | - |
173 | d->macro_stack.clear(); | - |
174 | qDeleteAll(d->command_list); | - |
175 | d->command_list.clear(); | - |
176 | | - |
177 | d->index = 0; | - |
178 | d->clean_index = 0; | - |
179 | | - |
180 | indexChanged(0); | - |
181 | canUndoChanged(false); | - |
182 | undoTextChanged(QString()); | - |
183 | canRedoChanged(false); | - |
184 | redoTextChanged(QString()); | - |
185 | | - |
186 | if (!was_clean) | - |
187 | cleanChanged(true); | - |
188 | } | - |
189 | void QUndoStack::push(QUndoCommand *cmd) | - |
190 | { | - |
191 | QUndoStackPrivate * const d = d_func(); | - |
192 | cmd->redo(); | - |
193 | | - |
194 | bool macro = !d->macro_stack.isEmpty(); | - |
195 | | - |
196 | QUndoCommand *cur = 0; | - |
197 | if (macroTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
198 | QUndoCommand *macro_cmd = d->macro_stack.lastconstLast(); | - |
199 | if (!macro_cmd->d->child_list.isEmpty()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
200 | cur = macro_cmd->d->child_list.lastconstLast(); never executed: cur = macro_cmd->d->child_list.constLast(); | 0 |
201 | } never executed: end of block else { | 0 |
202 | if (d->index > 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
203 | cur = d->command_list.at(d->index - 1); never executed: cur = d->command_list.at(d->index - 1); | 0 |
204 | while (d->index < d->command_list.size()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
205 | delete d->command_list.takeLast(); never executed: delete d->command_list.takeLast(); | 0 |
206 | if (d->clean_index > d->indexTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
207 | d->clean_index = -1; never executed: d->clean_index = -1; | 0 |
208 | } never executed: end of block | 0 |
209 | | - |
210 | bool try_merge = cur != 0TRUE | never evaluated | FALSE | never evaluated |
| 0 |
211 | && cur->id() != -1TRUE | never evaluated | FALSE | never evaluated |
| 0 |
212 | && cur->id() == cmd->id()TRUE | never evaluated | FALSE | never evaluated |
| 0 |
213 | && (macroTRUE | never evaluated | FALSE | never evaluated |
|| d->index != d->clean_indexTRUE | never evaluated | FALSE | never evaluated |
); | 0 |
214 | | - |
215 | if (try_mergeTRUE | never evaluated | FALSE | never evaluated |
&& cur->mergeWith(cmd)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
216 | delete cmd; | - |
217 | if (!macroTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
218 | indexChanged(d->index); | - |
219 | canUndoChanged(canUndo()); | - |
220 | undoTextChanged(undoText()); | - |
221 | canRedoChanged(canRedo()); | - |
222 | redoTextChanged(redoText()); | - |
223 | } never executed: end of block | 0 |
224 | } never executed: end of block else { | 0 |
225 | if (macroTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
226 | d->macro_stack.lastconstLast()->d->child_list.append(cmd); | - |
227 | } never executed: end of block else { | 0 |
228 | d->command_list.append(cmd); | - |
229 | d->checkUndoLimit(); | - |
230 | d->setIndex(d->index + 1, false); | - |
231 | } never executed: end of block | 0 |
232 | } | - |
233 | } | - |
234 | void QUndoStack::setClean() | - |
235 | { | - |
236 | QUndoStackPrivate * const d = d_func(); | - |
237 | if (!(__builtin_expect(!!(!TRUE | never evaluated | FALSE | never evaluated |
d->macro_stack.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
238 | QMessageLogger(__FILE__, 639645, __PRETTY_FUNCTION__).warning("QUndoStack::setClean(): cannot set clean in the middle of a macro"); | - |
239 | return; never executed: return; | 0 |
240 | } | - |
241 | | - |
242 | d->setIndex(d->index, true); | - |
243 | } never executed: end of block | 0 |
244 | | - |
245 | | - |
246 | | - |
247 | | - |
248 | | - |
249 | | - |
250 | | - |
251 | bool QUndoStack::isClean() const | - |
252 | { | - |
253 | const QUndoStackPrivate * const d = d_func(); | - |
254 | if (!d->macro_stack.isEmpty()) | - |
255 | return false; | - |
256 | return d->clean_index == d->index; | - |
257 | } | - |
258 | int QUndoStack::cleanIndex() const | - |
259 | { | - |
260 | const QUndoStackPrivate * const d = d_func(); | - |
261 | return d->clean_index; | - |
262 | } | - |
263 | void QUndoStack::undo() | - |
264 | { | - |
265 | QUndoStackPrivate * const d = d_func(); | - |
266 | if (d->index == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
267 | return; never executed: return; | 0 |
268 | | - |
269 | if (!(__builtin_expect(!!(!TRUE | never evaluated | FALSE | never evaluated |
d->macro_stack.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
270 | QMessageLogger(__FILE__, 694700, __PRETTY_FUNCTION__).warning("QUndoStack::undo(): cannot undo in the middle of a macro"); | - |
271 | return; never executed: return; | 0 |
272 | } | - |
273 | | - |
274 | int idx = d->index - 1; | - |
275 | d->command_list.at(idx)->undo(); | - |
276 | d->setIndex(idx, false); | - |
277 | } never executed: end of block | 0 |
278 | void QUndoStack::redo() | - |
279 | { | - |
280 | QUndoStackPrivate * const d = d_func(); | - |
281 | if (d->index == d->command_list.size()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
282 | return; never executed: return; | 0 |
283 | | - |
284 | if (!(__builtin_expect(!!(!TRUE | never evaluated | FALSE | never evaluated |
d->macro_stack.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
285 | QMessageLogger(__FILE__, 720726, __PRETTY_FUNCTION__).warning("QUndoStack::redo(): cannot redo in the middle of a macro"); | - |
286 | return; never executed: return; | 0 |
287 | } | - |
288 | | - |
289 | d->command_list.at(d->index)->redo(); | - |
290 | d->setIndex(d->index + 1, false); | - |
291 | } never executed: end of block | 0 |
292 | int QUndoStack::count() const | - |
293 | { | - |
294 | const QUndoStackPrivate * const d = d_func(); | - |
295 | return d->command_list.size(); | - |
296 | } | - |
297 | int QUndoStack::index() const | - |
298 | { | - |
299 | const QUndoStackPrivate * const d = d_func(); | - |
300 | return d->index; | - |
301 | } | - |
302 | void QUndoStack::setIndex(int idx) | - |
303 | { | - |
304 | QUndoStackPrivate * const d = d_func(); | - |
305 | if (!(__builtin_expect(!!(!TRUE | never evaluated | FALSE | never evaluated |
d->macro_stack.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
306 | QMessageLogger(__FILE__, 767773, __PRETTY_FUNCTION__).warning("QUndoStack::setIndex(): cannot set index in the middle of a macro"); | - |
307 | return; never executed: return; | 0 |
308 | } | - |
309 | | - |
310 | if (idx < 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
311 | idx = 0; never executed: idx = 0; | 0 |
312 | else if (idx > d->command_list.size()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
313 | idx = d->command_list.size(); never executed: idx = d->command_list.size(); | 0 |
314 | | - |
315 | int i = d->index; | - |
316 | while (i < idxTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
317 | d->command_list.at(i++)->redo(); never executed: d->command_list.at(i++)->redo(); | 0 |
318 | while (i > idxTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
319 | d->command_list.at(--i)->undo(); never executed: d->command_list.at(--i)->undo(); | 0 |
320 | | - |
321 | d->setIndex(idx, false); | - |
322 | } never executed: end of block | 0 |
323 | bool QUndoStack::canUndo() const | - |
324 | { | - |
325 | const QUndoStackPrivate * const d = d_func(); | - |
326 | if (!d->macro_stack.isEmpty()) | - |
327 | return false; | - |
328 | return d->index > 0; | - |
329 | } | - |
330 | bool QUndoStack::canRedo() const | - |
331 | { | - |
332 | const QUndoStackPrivate * const d = d_func(); | - |
333 | if (!d->macro_stack.isEmpty()) | - |
334 | return false; | - |
335 | return d->index < d->command_list.size(); | - |
336 | } | - |
337 | | - |
338 | | - |
339 | | - |
340 | | - |
341 | | - |
342 | | - |
343 | | - |
344 | QString QUndoStack::undoText() const | - |
345 | { | - |
346 | const QUndoStackPrivate * const d = d_func(); | - |
347 | if (!d->macro_stack.isEmpty()) | - |
348 | return QString(); | - |
349 | if (d->index > 0) | - |
350 | return d->command_list.at(d->index - 1)->actionText(); | - |
351 | return QString(); | - |
352 | } | - |
353 | | - |
354 | | - |
355 | | - |
356 | | - |
357 | | - |
358 | | - |
359 | | - |
360 | QString QUndoStack::redoText() const | - |
361 | { | - |
362 | const QUndoStackPrivate * const d = d_func(); | - |
363 | if (!d->macro_stack.isEmpty()) | - |
364 | return QString(); | - |
365 | if (d->index < d->command_list.size()) | - |
366 | return d->command_list.at(d->index)->actionText(); | - |
367 | return QString(); | - |
368 | } | - |
369 | QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix) const | - |
370 | { | - |
371 | QUndoAction *result = new QUndoAction(prefix, parent); | - |
372 | if (prefix.isEmpty()) | - |
373 | result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action")); | - |
374 | | - |
375 | result->setEnabled(canUndo()); | - |
376 | result->setPrefixedText(undoText()); | - |
377 | connect(this, qFlagLocation("2""canUndoChanged(bool)" "\0" __FILE__ ":" "879""885"), | - |
378 | result, qFlagLocation("1""setEnabled(bool)" "\0" __FILE__ ":" "880""886")); | - |
379 | connect(this, qFlagLocation("2""undoTextChanged(QString)" "\0" __FILE__ ":" "881""887"), | - |
380 | result, qFlagLocation("1""setPrefixedText(QString)" "\0" __FILE__ ":" "882""888")); | - |
381 | connect(result, qFlagLocation("2""triggered()" "\0" __FILE__ ":" "883""889"), this, qFlagLocation("1""undo()" "\0" __FILE__ ":" "883""889")); | - |
382 | return result; | - |
383 | } | - |
384 | QAction *QUndoStack::createRedoAction(QObject *parent, const QString &prefix) const | - |
385 | { | - |
386 | QUndoAction *result = new QUndoAction(prefix, parent); | - |
387 | if (prefix.isEmpty()) | - |
388 | result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action")); | - |
389 | | - |
390 | result->setEnabled(canRedo()); | - |
391 | result->setPrefixedText(redoText()); | - |
392 | connect(this, qFlagLocation("2""canRedoChanged(bool)" "\0" __FILE__ ":" "909""915"), | - |
393 | result, qFlagLocation("1""setEnabled(bool)" "\0" __FILE__ ":" "910""916")); | - |
394 | connect(this, qFlagLocation("2""redoTextChanged(QString)" "\0" __FILE__ ":" "911""917"), | - |
395 | result, qFlagLocation("1""setPrefixedText(QString)" "\0" __FILE__ ":" "912""918")); | - |
396 | connect(result, qFlagLocation("2""triggered()" "\0" __FILE__ ":" "913""919"), this, qFlagLocation("1""redo()" "\0" __FILE__ ":" "913""919")); | - |
397 | return result; | - |
398 | } | - |
399 | void QUndoStack::beginMacro(const QString &text) | - |
400 | { | - |
401 | QUndoStackPrivate * const d = d_func(); | - |
402 | QUndoCommand *cmd = new QUndoCommand(); | - |
403 | cmd->setText(text); | - |
404 | | - |
405 | if (d->macro_stack.isEmpty()TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
406 | while (d->index < d->command_list.size()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
407 | delete d->command_list.takeLast(); never executed: delete d->command_list.takeLast(); | 0 |
408 | if (d->clean_index > d->indexTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
409 | d->clean_index = -1; never executed: d->clean_index = -1; | 0 |
410 | d->command_list.append(cmd); | - |
411 | } never executed: end of block else { | 0 |
412 | d->macro_stack.lastconstLast()->d->child_list.append(cmd); | - |
413 | } never executed: end of block | 0 |
414 | d->macro_stack.append(cmd); | - |
415 | | - |
416 | if (d->macro_stack.count() == 1TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
417 | canUndoChanged(false); | - |
418 | undoTextChanged(QString()); | - |
419 | canRedoChanged(false); | - |
420 | redoTextChanged(QString()); | - |
421 | } never executed: end of block | 0 |
422 | } never executed: end of block | 0 |
423 | void QUndoStack::endMacro() | - |
424 | { | - |
425 | QUndoStackPrivate * const d = d_func(); | - |
426 | if (__builtin_expect(!!(TRUE | never evaluated | FALSE | never evaluated |
d->macro_stack.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
427 | QMessageLogger(__FILE__, 987993, __PRETTY_FUNCTION__).warning("QUndoStack::endMacro(): no matching beginMacro()"); | - |
428 | return; never executed: return; | 0 |
429 | } | - |
430 | | - |
431 | d->macro_stack.removeLast(); | - |
432 | | - |
433 | if (d->macro_stack.isEmpty()TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
434 | d->checkUndoLimit(); | - |
435 | d->setIndex(d->index + 1, false); | - |
436 | } never executed: end of block | 0 |
437 | } never executed: end of block | 0 |
438 | const QUndoCommand *QUndoStack::command(int index) const | - |
439 | { | - |
440 | const QUndoStackPrivate * const d = d_func(); | - |
441 | | - |
442 | if (index < 0 || index >= d->command_list.count()) | - |
443 | return 0; | - |
444 | return d->command_list.at(index); | - |
445 | } | - |
446 | | - |
447 | | - |
448 | | - |
449 | | - |
450 | | - |
451 | | - |
452 | | - |
453 | QString QUndoStack::text(int idx) const | - |
454 | { | - |
455 | const QUndoStackPrivate * const d = d_func(); | - |
456 | | - |
457 | if (idx < 0 || idx >= d->command_list.size()) | - |
458 | return QString(); | - |
459 | return d->command_list.at(idx)->text(); | - |
460 | } | - |
461 | void QUndoStack::setUndoLimit(int limit) | - |
462 | { | - |
463 | QUndoStackPrivate * const d = d_func(); | - |
464 | | - |
465 | if (!(__builtin_expect(!!(!TRUE | never evaluated | FALSE | never evaluated |
d->command_list.isEmpty())()), false)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
466 | QMessageLogger(__FILE__, 10551061, __PRETTY_FUNCTION__).warning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty"); | - |
467 | return; never executed: return; | 0 |
468 | } | - |
469 | | - |
470 | if (limit == d->undo_limitTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
471 | return; never executed: return; | 0 |
472 | d->undo_limit = limit; | - |
473 | d->checkUndoLimit(); | - |
474 | } never executed: end of block | 0 |
475 | | - |
476 | int QUndoStack::undoLimit() const | - |
477 | { | - |
478 | const QUndoStackPrivate * const d = d_func(); | - |
479 | | - |
480 | return d->undo_limit; | - |
481 | } | - |
482 | void QUndoStack::setActive(bool active) | - |
483 | { | - |
484 | | - |
485 | | - |
486 | | - |
487 | QUndoStackPrivate * const d = d_func(); | - |
488 | | - |
489 | if (d->group != 0) { | - |
490 | if (active) | - |
491 | d->group->setActiveStack(this); | - |
492 | else if (d->group->activeStack() == this) | - |
493 | d->group->setActiveStack(0); | - |
494 | } | - |
495 | | - |
496 | } | - |
497 | | - |
498 | bool QUndoStack::isActive() const | - |
499 | { | - |
500 | | - |
501 | | - |
502 | | - |
503 | const QUndoStackPrivate * const d = d_func(); | - |
504 | return d->group == 0 || d->group->activeStack() == this; | - |
505 | | - |
506 | } | - |
507 | | - |
508 | | - |
| | |