util/qundostack.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include <QtCore/qdebug.h> -
43#include "qundostack.h" -
44#include "qundogroup.h" -
45#include "qundostack_p.h" -
46 -
47#ifndef QT_NO_UNDOCOMMAND -
48 -
49QT_BEGIN_NAMESPACE -
50 -
51/*! -
52 \class QUndoCommand -
53 \brief The QUndoCommand class is the base class of all commands stored on a QUndoStack. -
54 \since 4.2 -
55 -
56 \inmodule QtWidgets -
57 -
58 For an overview of Qt's Undo Framework, see the -
59 \l{Overview of Qt's Undo Framework}{overview document}. -
60 -
61 A QUndoCommand represents a single editing action on a document; for example, -
62 inserting or deleting a block of text in a text editor. QUndoCommand can apply -
63 a change to the document with redo() and undo the change with undo(). The -
64 implementations for these functions must be provided in a derived class. -
65 -
66 \snippet code/src_gui_util_qundostack.cpp 0 -
67 -
68 A QUndoCommand has an associated text(). This is a short string -
69 describing what the command does. It is used to update the text -
70 properties of the stack's undo and redo actions; see -
71 QUndoStack::createUndoAction() and QUndoStack::createRedoAction(). -
72 -
73 QUndoCommand objects are owned by the stack they were pushed on. -
74 QUndoStack deletes a command if it has been undone and a new command is pushed. For example: -
75 -
76\snippet code/src_gui_util_qundostack.cpp 1 -
77 -
78 In effect, when a command is pushed, it becomes the top-most command -
79 on the stack. -
80 -
81 To support command compression, QUndoCommand has an id() and the virtual function -
82 mergeWith(). These functions are used by QUndoStack::push(). -
83 -
84 To support command macros, a QUndoCommand object can have any number of child -
85 commands. Undoing or redoing the parent command will cause the child -
86 commands to be undone or redone. A command can be assigned -
87 to a parent explicitly in the constructor. In this case, the command -
88 will be owned by the parent. -
89 -
90 The parent in this case is usually an empty command, in that it doesn't -
91 provide its own implementation of undo() and redo(). Instead, it uses -
92 the base implementations of these functions, which simply call undo() or -
93 redo() on all its children. The parent should, however, have a meaningful -
94 text(). -
95 -
96 \snippet code/src_gui_util_qundostack.cpp 2 -
97 -
98 Another way to create macros is to use the convenience functions -
99 QUndoStack::beginMacro() and QUndoStack::endMacro(). -
100 -
101 \sa QUndoStack -
102*/ -
103 -
104/*! -
105 Constructs a QUndoCommand object with the given \a parent and \a text. -
106 -
107 If \a parent is not 0, this command is appended to parent's child list. -
108 The parent command then owns this command and will delete it in its -
109 destructor. -
110 -
111 \sa ~QUndoCommand() -
112*/ -
113 -
114QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent) -
115{ -
116 d = new QUndoCommandPrivate;
never executed (the execution status of this line is deduced): d = new QUndoCommandPrivate;
-
117 if (parent != 0)
never evaluated: parent != 0
0
118 parent->d->child_list.append(this);
never executed: parent->d->child_list.append(this);
0
119 setText(text);
never executed (the execution status of this line is deduced): setText(text);
-
120}
never executed: }
0
121 -
122/*! -
123 Constructs a QUndoCommand object with parent \a parent. -
124 -
125 If \a parent is not 0, this command is appended to parent's child list. -
126 The parent command then owns this command and will delete it in its -
127 destructor. -
128 -
129 \sa ~QUndoCommand() -
130*/ -
131 -
132QUndoCommand::QUndoCommand(QUndoCommand *parent) -
133{ -
134 d = new QUndoCommandPrivate;
executed (the execution status of this line is deduced): d = new QUndoCommandPrivate;
-
135 if (parent != 0)
evaluated: parent != 0
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:50
2-50
136 parent->d->child_list.append(this);
executed: parent->d->child_list.append(this);
Execution Count:2
2
137}
executed: }
Execution Count:52
52
138 -
139/*! -
140 Destroys the QUndoCommand object and all child commands. -
141 -
142 \sa QUndoCommand() -
143*/ -
144 -
145QUndoCommand::~QUndoCommand() -
146{ -
147 qDeleteAll(d->child_list);
executed (the execution status of this line is deduced): qDeleteAll(d->child_list);
-
148 delete d;
executed (the execution status of this line is deduced): delete d;
-
149}
executed: }
Execution Count:52
52
150 -
151/*! -
152 Returns the ID of this command. -
153 -
154 A command ID is used in command compression. It must be an integer unique to -
155 this command's class, or -1 if the command doesn't support compression. -
156 -
157 If the command supports compression this function must be overridden in the -
158 derived class to return the correct ID. The base implementation returns -1. -
159 -
160 QUndoStack::push() will only try to merge two commands if they have the -
161 same ID, and the ID is not -1. -
162 -
163 \sa mergeWith(), QUndoStack::push() -
164*/ -
165 -
166int QUndoCommand::id() const -
167{ -
168 return -1;
executed: return -1;
Execution Count:15
15
169} -
170 -
171/*! -
172 Attempts to merge this command with \a command. Returns true on -
173 success; otherwise returns false. -
174 -
175 If this function returns true, calling this command's redo() must have the same -
176 effect as redoing both this command and \a command. -
177 Similarly, calling this command's undo() must have the same effect as undoing -
178 \a command and this command. -
179 -
180 QUndoStack will only try to merge two commands if they have the same id, and -
181 the id is not -1. -
182 -
183 The default implementation returns false. -
184 -
185 \snippet code/src_gui_util_qundostack.cpp 3 -
186 -
187 \sa id(), QUndoStack::push() -
188*/ -
189 -
190bool QUndoCommand::mergeWith(const QUndoCommand *command) -
191{ -
192 Q_UNUSED(command);
never executed (the execution status of this line is deduced): (void)command;;
-
193 return false;
never executed: return false;
0
194} -
195 -
196/*! -
197 Applies a change to the document. This function must be implemented in -
198 the derived class. Calling QUndoStack::push(), -
199 QUndoStack::undo() or QUndoStack::redo() from this function leads to -
200 undefined beahavior. -
201 -
202 The default implementation calls redo() on all child commands. -
203 -
204 \sa undo() -
205*/ -
206 -
207void QUndoCommand::redo() -
208{ -
209 for (int i = 0; i < d->child_list.size(); ++i)
evaluated: i < d->child_list.size()
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:5
5-10
210 d->child_list.at(i)->redo();
executed: d->child_list.at(i)->redo();
Execution Count:10
10
211}
executed: }
Execution Count:5
5
212 -
213/*! -
214 Reverts a change to the document. After undo() is called, the state of -
215 the document should be the same as before redo() was called. This function must -
216 be implemented in the derived class. Calling QUndoStack::push(), -
217 QUndoStack::undo() or QUndoStack::redo() from this function leads to -
218 undefined beahavior. -
219 -
220 The default implementation calls undo() on all child commands in reverse order. -
221 -
222 \sa redo() -
223*/ -
224 -
225void QUndoCommand::undo() -
226{ -
227 for (int i = d->child_list.size() - 1; i >= 0; --i)
evaluated: i >= 0
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:7
7-14
228 d->child_list.at(i)->undo();
executed: d->child_list.at(i)->undo();
Execution Count:14
14
229}
executed: }
Execution Count:7
7
230 -
231/*! -
232 Returns a short text string describing what this command does; for example, -
233 "insert text". -
234 -
235 The text is used for names of items in QUndoView. -
236 -
237 \sa actionText(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction() -
238*/ -
239 -
240QString QUndoCommand::text() const -
241{ -
242 return d->text;
executed: return d->text;
Execution Count:4
4
243} -
244 -
245/*! -
246 \since 4.8 -
247 -
248 Returns a short text string describing what this command does; for example, -
249 "insert text". -
250 -
251 The text is used when the text properties of the stack's undo and redo -
252 actions are updated. -
253 -
254 \sa text(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction() -
255*/ -
256 -
257QString QUndoCommand::actionText() const -
258{ -
259 return d->actionText;
executed: return d->actionText;
Execution Count:220
220
260} -
261 -
262/*! -
263 Sets the command's text to be the \a text specified. -
264 -
265 The specified text should be a short user-readable string describing what this -
266 command does. -
267 -
268 If you need to have two different strings for text() and actionText(), separate -
269 them with "\\n" and pass into this function. Even if you do not use this feature -
270 for English strings during development, you can still let translators use two -
271 different strings in order to match specific languages' needs. -
272 The described feature and the function actionText() are available since Qt 4.8. -
273 -
274 \sa text(), actionText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction() -
275*/ -
276 -
277void QUndoCommand::setText(const QString &text) -
278{ -
279 int cdpos = text.indexOf(QLatin1Char('\n'));
executed (the execution status of this line is deduced): int cdpos = text.indexOf(QLatin1Char('\n'));
-
280 if (cdpos > 0) {
evaluated: cdpos > 0
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:51
3-51
281 d->text = text.left(cdpos);
executed (the execution status of this line is deduced): d->text = text.left(cdpos);
-
282 d->actionText = text.mid(cdpos + 1);
executed (the execution status of this line is deduced): d->actionText = text.mid(cdpos + 1);
-
283 } else {
executed: }
Execution Count:3
3
284 d->text = text;
executed (the execution status of this line is deduced): d->text = text;
-
285 d->actionText = text;
executed (the execution status of this line is deduced): d->actionText = text;
-
286 }
executed: }
Execution Count:51
51
287} -
288 -
289/*! -
290 \since 4.4 -
291 -
292 Returns the number of child commands in this command. -
293 -
294 \sa child() -
295*/ -
296 -
297int QUndoCommand::childCount() const -
298{ -
299 return d->child_list.count();
never executed: return d->child_list.count();
0
300} -
301 -
302/*! -
303 \since 4.4 -
304 -
305 Returns the child command at \a index. -
306 -
307 \sa childCount(), QUndoStack::command() -
308*/ -
309 -
310const QUndoCommand *QUndoCommand::child(int index) const -
311{ -
312 if (index < 0 || index >= d->child_list.count())
never evaluated: index < 0
never evaluated: index >= d->child_list.count()
0
313 return 0;
never executed: return 0;
0
314 return d->child_list.at(index);
never executed: return d->child_list.at(index);
0
315} -
316 -
317#endif // QT_NO_UNDOCOMMAND -
318 -
319#ifndef QT_NO_UNDOSTACK -
320 -
321/*! -
322 \class QUndoStack -
323 \brief The QUndoStack class is a stack of QUndoCommand objects. -
324 \since 4.2 -
325 -
326 \inmodule QtWidgets -
327 -
328 For an overview of Qt's Undo Framework, see the -
329 \l{Overview of Qt's Undo Framework}{overview document}. -
330 -
331 An undo stack maintains a stack of commands that have been applied to a -
332 document. -
333 -
334 New commands are pushed on the stack using push(). Commands can be -
335 undone and redone using undo() and redo(), or by triggering the -
336 actions returned by createUndoAction() and createRedoAction(). -
337 -
338 QUndoStack keeps track of the \a current command. This is the command -
339 which will be executed by the next call to redo(). The index of this -
340 command is returned by index(). The state of the edited object can be -
341 rolled forward or back using setIndex(). If the top-most command on the -
342 stack has already been redone, index() is equal to count(). -
343 -
344 QUndoStack provides support for undo and redo actions, command -
345 compression, command macros, and supports the concept of a -
346 \e{clean state}. -
347 -
348 \section1 Undo and Redo Actions -
349 -
350 QUndoStack provides convenient undo and redo QAction objects, which -
351 can be inserted into a menu or a toolbar. When commands are undone or -
352 redone, QUndoStack updates the text properties of these actions -
353 to reflect what change they will trigger. The actions are also disabled -
354 when no command is available for undo or redo. These actions -
355 are returned by QUndoStack::createUndoAction() and QUndoStack::createRedoAction(). -
356 -
357 \section1 Command Compression and Macros -
358 -
359 Command compression is useful when several commands can be compressed -
360 into a single command that can be undone and redone in a single operation. -
361 For example, when a user types a character in a text editor, a new command -
362 is created. This command inserts the character into the document at the -
363 cursor position. However, it is more convenient for the user to be able -
364 to undo or redo typing of whole words, sentences, or paragraphs. -
365 Command compression allows these single-character commands to be merged -
366 into a single command which inserts or deletes sections of text. -
367 For more information, see QUndoCommand::mergeWith() and push(). -
368 -
369 A command macro is a sequence of commands, all of which are undone and -
370 redone in one go. Command macros are created by giving a command a list -
371 of child commands. -
372 Undoing or redoing the parent command will cause the child commands to -
373 be undone or redone. Command macros may be created explicitly -
374 by specifying a parent in the QUndoCommand constructor, or by using the -
375 convenience functions beginMacro() and endMacro(). -
376 -
377 Although command compression and macros appear to have the same effect to the -
378 user, they often have different uses in an application. Commands that -
379 perform small changes to a document may be usefully compressed if there is -
380 no need to individually record them, and if only larger changes are relevant -
381 to the user. -
382 However, for commands that need to be recorded individually, or those that -
383 cannot be compressed, it is useful to use macros to provide a more convenient -
384 user experience while maintaining a record of each command. -
385 -
386 \section1 Clean State -
387 -
388 QUndoStack supports the concept of a clean state. When the -
389 document is saved to disk, the stack can be marked as clean using -
390 setClean(). Whenever the stack returns to this state through undoing and -
391 redoing commands, it emits the signal cleanChanged(). This signal -
392 is also emitted when the stack leaves the clean state. This signal is -
393 usually used to enable and disable the save actions in the application, -
394 and to update the document's title to reflect that it contains unsaved -
395 changes. -
396 -
397 \sa QUndoCommand, QUndoView -
398*/ -
399 -
400#ifndef QT_NO_ACTION -
401 -
402QUndoAction::QUndoAction(const QString &prefix, QObject *parent) -
403 : QAction(parent) -
404{ -
405 m_prefix = prefix;
executed (the execution status of this line is deduced): m_prefix = prefix;
-
406}
executed: }
Execution Count:20
20
407 -
408void QUndoAction::setPrefixedText(const QString &text) -
409{ -
410 if (m_defaultText.isEmpty()) {
evaluated: m_defaultText.isEmpty()
TRUEFALSE
yes
Evaluation Count:202
yes
Evaluation Count:10
10-202
411 QString s = m_prefix;
executed (the execution status of this line is deduced): QString s = m_prefix;
-
412 if (!m_prefix.isEmpty() && !text.isEmpty())
partially evaluated: !m_prefix.isEmpty()
TRUEFALSE
yes
Evaluation Count:202
no
Evaluation Count:0
evaluated: !text.isEmpty()
TRUEFALSE
yes
Evaluation Count:101
yes
Evaluation Count:101
0-202
413 s.append(QLatin1Char(' '));
executed: s.append(QLatin1Char(' '));
Execution Count:101
101
414 s.append(text);
executed (the execution status of this line is deduced): s.append(text);
-
415 setText(s);
executed (the execution status of this line is deduced): setText(s);
-
416 } else {
executed: }
Execution Count:202
202
417 if (text.isEmpty())
evaluated: text.isEmpty()
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:4
4-6
418 setText(m_defaultText);
executed: setText(m_defaultText);
Execution Count:6
6
419 else -
420 setText(m_prefix.arg(text));
executed: setText(m_prefix.arg(text));
Execution Count:4
4
421 } -
422} -
423 -
424void QUndoAction::setTextFormat(const QString &textFormat, const QString &defaultText) -
425{ -
426 m_prefix = textFormat;
executed (the execution status of this line is deduced): m_prefix = textFormat;
-
427 m_defaultText = defaultText;
executed (the execution status of this line is deduced): m_defaultText = defaultText;
-
428}
executed: }
Execution Count:2
2
429 -
430#endif // QT_NO_ACTION -
431 -
432/*! \internal -
433 Sets the current index to \a idx, emitting appropriate signals. If \a clean is true, -
434 makes \a idx the clean index as well. -
435*/ -
436 -
437void QUndoStackPrivate::setIndex(int idx, bool clean) -
438{ -
439 Q_Q(QUndoStack);
executed (the execution status of this line is deduced): QUndoStack * const q = q_func();
-
440 -
441 bool was_clean = index == clean_index;
executed (the execution status of this line is deduced): bool was_clean = index == clean_index;
-
442 -
443 if (idx != index) {
evaluated: idx != index
TRUEFALSE
yes
Evaluation Count:79
yes
Evaluation Count:9
9-79
444 index = idx;
executed (the execution status of this line is deduced): index = idx;
-
445 emit q->indexChanged(index);
executed (the execution status of this line is deduced): q->indexChanged(index);
-
446 emit q->canUndoChanged(q->canUndo());
executed (the execution status of this line is deduced): q->canUndoChanged(q->canUndo());
-
447 emit q->undoTextChanged(q->undoText());
executed (the execution status of this line is deduced): q->undoTextChanged(q->undoText());
-
448 emit q->canRedoChanged(q->canRedo());
executed (the execution status of this line is deduced): q->canRedoChanged(q->canRedo());
-
449 emit q->redoTextChanged(q->redoText());
executed (the execution status of this line is deduced): q->redoTextChanged(q->redoText());
-
450 }
executed: }
Execution Count:79
79
451 -
452 if (clean)
evaluated: clean
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:83
5-83
453 clean_index = index;
executed: clean_index = index;
Execution Count:5
5
454 -
455 bool is_clean = index == clean_index;
executed (the execution status of this line is deduced): bool is_clean = index == clean_index;
-
456 if (is_clean != was_clean)
evaluated: is_clean != was_clean
TRUEFALSE
yes
Evaluation Count:40
yes
Evaluation Count:48
40-48
457 emit q->cleanChanged(is_clean);
executed: q->cleanChanged(is_clean);
Execution Count:40
40
458}
executed: }
Execution Count:88
88
459 -
460/*! \internal -
461 If the number of commands on the stack exceedes the undo limit, deletes commands from -
462 the bottom of the stack. -
463 -
464 Returns true if commands were deleted. -
465*/ -
466 -
467bool QUndoStackPrivate::checkUndoLimit() -
468{ -
469 if (undo_limit <= 0 || !macro_stack.isEmpty() || undo_limit >= command_list.count())
evaluated: undo_limit <= 0
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:11
partially evaluated: !macro_stack.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:11
evaluated: undo_limit >= command_list.count()
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:4
0-29
470 return false;
executed: return false;
Execution Count:36
36
471 -
472 int del_count = command_list.count() - undo_limit;
executed (the execution status of this line is deduced): int del_count = command_list.count() - undo_limit;
-
473 -
474 for (int i = 0; i < del_count; ++i)
evaluated: i < del_count
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:4
4
475 delete command_list.takeFirst();
executed: delete command_list.takeFirst();
Execution Count:4
4
476 -
477 index -= del_count;
executed (the execution status of this line is deduced): index -= del_count;
-
478 if (clean_index != -1) {
evaluated: clean_index != -1
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:1
1-3
479 if (clean_index < del_count)
evaluated: clean_index < del_count
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:2
1-2
480 clean_index = -1; // we've deleted the clean command
executed: clean_index = -1;
Execution Count:1
1
481 else -
482 clean_index -= del_count;
executed: clean_index -= del_count;
Execution Count:2
2
483 } -
484 -
485 return true;
executed: return true;
Execution Count:4
4
486} -
487 -
488/*! -
489 Constructs an empty undo stack with the parent \a parent. The -
490 stack will initially be in the clean state. If \a parent is a -
491 QUndoGroup object, the stack is automatically added to the group. -
492 -
493 \sa push() -
494*/ -
495 -
496QUndoStack::QUndoStack(QObject *parent) -
497 : QObject(*(new QUndoStackPrivate), parent) -
498{ -
499#ifndef QT_NO_UNDOGROUP -
500 if (QUndoGroup *group = qobject_cast<QUndoGroup*>(parent))
evaluated: QUndoGroup *group = qobject_cast<QUndoGroup*>(parent)
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:12
8-12
501 group->addStack(this);
executed: group->addStack(this);
Execution Count:8
8
502#endif -
503}
executed: }
Execution Count:20
20
504 -
505/*! -
506 Destroys the undo stack, deleting any commands that are on it. If the -
507 stack is in a QUndoGroup, the stack is automatically removed from the group. -
508 -
509 \sa QUndoStack() -
510*/ -
511 -
512QUndoStack::~QUndoStack() -
513{ -
514#ifndef QT_NO_UNDOGROUP -
515 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
516 if (d->group != 0)
evaluated: d->group != 0
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:15
5-15
517 d->group->removeStack(this);
executed: d->group->removeStack(this);
Execution Count:5
5
518#endif -
519 clear();
executed (the execution status of this line is deduced): clear();
-
520}
executed: }
Execution Count:20
20
521 -
522/*! -
523 Clears the command stack by deleting all commands on it, and returns the stack -
524 to the clean state. -
525 -
526 Commands are not undone or redone; the state of the edited object remains -
527 unchanged. -
528 -
529 This function is usually used when the contents of the document are -
530 abandoned. -
531 -
532 \sa QUndoStack() -
533*/ -
534 -
535void QUndoStack::clear() -
536{ -
537 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
538 -
539 if (d->command_list.isEmpty())
evaluated: d->command_list.isEmpty()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:11
11-12
540 return;
executed: return;
Execution Count:12
12
541 -
542 bool was_clean = isClean();
executed (the execution status of this line is deduced): bool was_clean = isClean();
-
543 -
544 d->macro_stack.clear();
executed (the execution status of this line is deduced): d->macro_stack.clear();
-
545 qDeleteAll(d->command_list);
executed (the execution status of this line is deduced): qDeleteAll(d->command_list);
-
546 d->command_list.clear();
executed (the execution status of this line is deduced): d->command_list.clear();
-
547 -
548 d->index = 0;
executed (the execution status of this line is deduced): d->index = 0;
-
549 d->clean_index = 0;
executed (the execution status of this line is deduced): d->clean_index = 0;
-
550 -
551 emit indexChanged(0);
executed (the execution status of this line is deduced): indexChanged(0);
-
552 emit canUndoChanged(false);
executed (the execution status of this line is deduced): canUndoChanged(false);
-
553 emit undoTextChanged(QString());
executed (the execution status of this line is deduced): undoTextChanged(QString());
-
554 emit canRedoChanged(false);
executed (the execution status of this line is deduced): canRedoChanged(false);
-
555 emit redoTextChanged(QString());
executed (the execution status of this line is deduced): redoTextChanged(QString());
-
556 -
557 if (!was_clean)
evaluated: !was_clean
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:1
1-10
558 emit cleanChanged(true);
executed: cleanChanged(true);
Execution Count:10
10
559}
executed: }
Execution Count:11
11
560 -
561/*! -
562 Pushes \a cmd on the stack or merges it with the most recently executed command. -
563 In either case, executes \a cmd by calling its redo() function. -
564 -
565 If \a cmd's id is not -1, and if the id is the same as that of the -
566 most recently executed command, QUndoStack will attempt to merge the two -
567 commands by calling QUndoCommand::mergeWith() on the most recently executed -
568 command. If QUndoCommand::mergeWith() returns true, \a cmd is deleted. -
569 -
570 In all other cases \a cmd is simply pushed on the stack. -
571 -
572 If commands were undone before \a cmd was pushed, the current command and -
573 all commands above it are deleted. Hence \a cmd always ends up being the -
574 top-most on the stack. -
575 -
576 Once a command is pushed, the stack takes ownership of it. There -
577 are no getters to return the command, since modifying it after it has -
578 been executed will almost always lead to corruption of the document's -
579 state. -
580 -
581 \sa QUndoCommand::id(), QUndoCommand::mergeWith() -
582*/ -
583 -
584void QUndoStack::push(QUndoCommand *cmd) -
585{ -
586 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
587 cmd->redo();
executed (the execution status of this line is deduced): cmd->redo();
-
588 -
589 bool macro = !d->macro_stack.isEmpty();
executed (the execution status of this line is deduced): bool macro = !d->macro_stack.isEmpty();
-
590 -
591 QUndoCommand *cur = 0;
executed (the execution status of this line is deduced): QUndoCommand *cur = 0;
-
592 if (macro) {
evaluated: macro
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:37
8-37
593 QUndoCommand *macro_cmd = d->macro_stack.last();
executed (the execution status of this line is deduced): QUndoCommand *macro_cmd = d->macro_stack.last();
-
594 if (!macro_cmd->d->child_list.isEmpty())
evaluated: !macro_cmd->d->child_list.isEmpty()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:4
4
595 cur = macro_cmd->d->child_list.last();
executed: cur = macro_cmd->d->child_list.last();
Execution Count:4
4
596 } else {
executed: }
Execution Count:8
8
597 if (d->index > 0)
evaluated: d->index > 0
TRUEFALSE
yes
Evaluation Count:23
yes
Evaluation Count:14
14-23
598 cur = d->command_list.at(d->index - 1);
executed: cur = d->command_list.at(d->index - 1);
Execution Count:23
23
599 while (d->index < d->command_list.size())
evaluated: d->index < d->command_list.size()
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:37
11-37
600 delete d->command_list.takeLast();
executed: delete d->command_list.takeLast();
Execution Count:11
11
601 if (d->clean_index > d->index)
evaluated: d->clean_index > d->index
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:35
2-35
602 d->clean_index = -1; // we've deleted the clean state
executed: d->clean_index = -1;
Execution Count:2
2
603 }
executed: }
Execution Count:37
37
604 -
605 bool try_merge = cur != 0
evaluated: cur != 0
TRUEFALSE
yes
Evaluation Count:27
yes
Evaluation Count:18
18-27
606 && cur->id() != -1
evaluated: cur->id() != -1
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:13
13-14
607 && cur->id() == cmd->id()
evaluated: cur->id() == cmd->id()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:2
2-12
608 && (macro || d->index != d->clean_index);
evaluated: macro
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:10
evaluated: d->index != d->clean_index
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:2
2-10
609 -
610 if (try_merge && cur->mergeWith(cmd)) {
evaluated: try_merge
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:35
evaluated: cur->mergeWith(cmd)
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:7
3-35
611 delete cmd;
executed (the execution status of this line is deduced): delete cmd;
-
612 if (!macro) {
evaluated: !macro
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:1
1-2
613 emit indexChanged(d->index);
executed (the execution status of this line is deduced): indexChanged(d->index);
-
614 emit canUndoChanged(canUndo());
executed (the execution status of this line is deduced): canUndoChanged(canUndo());
-
615 emit undoTextChanged(undoText());
executed (the execution status of this line is deduced): undoTextChanged(undoText());
-
616 emit canRedoChanged(canRedo());
executed (the execution status of this line is deduced): canRedoChanged(canRedo());
-
617 emit redoTextChanged(redoText());
executed (the execution status of this line is deduced): redoTextChanged(redoText());
-
618 }
executed: }
Execution Count:2
2
619 } else {
executed: }
Execution Count:3
3
620 if (macro) {
evaluated: macro
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:35
7-35
621 d->macro_stack.last()->d->child_list.append(cmd);
executed (the execution status of this line is deduced): d->macro_stack.last()->d->child_list.append(cmd);
-
622 } else {
executed: }
Execution Count:7
7
623 d->command_list.append(cmd);
executed (the execution status of this line is deduced): d->command_list.append(cmd);
-
624 d->checkUndoLimit();
executed (the execution status of this line is deduced): d->checkUndoLimit();
-
625 d->setIndex(d->index + 1, false);
executed (the execution status of this line is deduced): d->setIndex(d->index + 1, false);
-
626 }
executed: }
Execution Count:35
35
627 } -
628} -
629 -
630/*! -
631 Marks the stack as clean and emits cleanChanged() if the stack was -
632 not already clean. -
633 -
634 Whenever the stack returns to this state through the use of undo/redo -
635 commands, it emits the signal cleanChanged(). This signal is also -
636 emitted when the stack leaves the clean state. -
637 -
638 \sa isClean(), cleanIndex() -
639*/ -
640 -
641void QUndoStack::setClean() -
642{ -
643 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
644 if (!d->macro_stack.isEmpty()) {
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:5
1-5
645 qWarning("QUndoStack::setClean(): cannot set clean in the middle of a macro");
executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 645, __PRETTY_FUNCTION__).warning("QUndoStack::setClean(): cannot set clean in the middle of a macro");
-
646 return;
executed: return;
Execution Count:1
1
647 } -
648 -
649 d->setIndex(d->index, true);
executed (the execution status of this line is deduced): d->setIndex(d->index, true);
-
650}
executed: }
Execution Count:5
5
651 -
652/*! -
653 If the stack is in the clean state, returns true; otherwise returns false. -
654 -
655 \sa setClean(), cleanIndex() -
656*/ -
657 -
658bool QUndoStack::isClean() const -
659{ -
660 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
661 if (!d->macro_stack.isEmpty())
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:116
18-116
662 return false;
executed: return false;
Execution Count:18
18
663 return d->clean_index == d->index;
executed: return d->clean_index == d->index;
Execution Count:116
116
664} -
665 -
666/*! -
667 Returns the clean index. This is the index at which setClean() was called. -
668 -
669 A stack may not have a clean index. This happens if a document is saved, -
670 some commands are undone, then a new command is pushed. Since -
671 push() deletes all the undone commands before pushing the new command, the stack -
672 can't return to the clean state again. In this case, this function returns -1. -
673 -
674 \sa isClean(), setClean() -
675*/ -
676 -
677int QUndoStack::cleanIndex() const -
678{ -
679 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
680 return d->clean_index;
executed: return d->clean_index;
Execution Count:11
11
681} -
682 -
683/*! -
684 Undoes the command below the current command by calling QUndoCommand::undo(). -
685 Decrements the current command index. -
686 -
687 If the stack is empty, or if the bottom command on the stack has already been -
688 undone, this function does nothing. -
689 -
690 \sa redo(), index() -
691*/ -
692 -
693void QUndoStack::undo() -
694{ -
695 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
696 if (d->index == 0)
evaluated: d->index == 0
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:25
5-25
697 return;
executed: return;
Execution Count:5
5
698 -
699 if (!d->macro_stack.isEmpty()) {
partially evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:25
0-25
700 qWarning("QUndoStack::undo(): cannot undo in the middle of a macro");
never executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 700, __PRETTY_FUNCTION__).warning("QUndoStack::undo(): cannot undo in the middle of a macro");
-
701 return;
never executed: return;
0
702 } -
703 -
704 int idx = d->index - 1;
executed (the execution status of this line is deduced): int idx = d->index - 1;
-
705 d->command_list.at(idx)->undo();
executed (the execution status of this line is deduced): d->command_list.at(idx)->undo();
-
706 d->setIndex(idx, false);
executed (the execution status of this line is deduced): d->setIndex(idx, false);
-
707}
executed: }
Execution Count:25
25
708 -
709/*! -
710 Redoes the current command by calling QUndoCommand::redo(). Increments the current -
711 command index. -
712 -
713 If the stack is empty, or if the top command on the stack has already been -
714 redone, this function does nothing. -
715 -
716 \sa undo(), index() -
717*/ -
718 -
719void QUndoStack::redo() -
720{ -
721 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
722 if (d->index == d->command_list.size())
evaluated: d->index == d->command_list.size()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:7
1-7
723 return;
executed: return;
Execution Count:1
1
724 -
725 if (!d->macro_stack.isEmpty()) {
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:6
1-6
726 qWarning("QUndoStack::redo(): cannot redo in the middle of a macro");
executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 726, __PRETTY_FUNCTION__).warning("QUndoStack::redo(): cannot redo in the middle of a macro");
-
727 return;
executed: return;
Execution Count:1
1
728 } -
729 -
730 d->command_list.at(d->index)->redo();
executed (the execution status of this line is deduced): d->command_list.at(d->index)->redo();
-
731 d->setIndex(d->index + 1, false);
executed (the execution status of this line is deduced): d->setIndex(d->index + 1, false);
-
732}
executed: }
Execution Count:6
6
733 -
734/*! -
735 Returns the number of commands on the stack. Macro commands are counted as -
736 one command. -
737 -
738 \sa index(), setIndex(), command() -
739*/ -
740 -
741int QUndoStack::count() const -
742{ -
743 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
744 return d->command_list.size();
executed: return d->command_list.size();
Execution Count:107
107
745} -
746 -
747/*! -
748 Returns the index of the current command. This is the command that will be -
749 executed on the next call to redo(). It is not always the top-most command -
750 on the stack, since a number of commands may have been undone. -
751 -
752 \sa undo(), redo(), count() -
753*/ -
754 -
755int QUndoStack::index() const -
756{ -
757 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
758 return d->index;
executed: return d->index;
Execution Count:114
114
759} -
760 -
761/*! -
762 Repeatedly calls undo() or redo() until the current command index reaches -
763 \a idx. This function can be used to roll the state of the document forwards -
764 of backwards. indexChanged() is emitted only once. -
765 -
766 \sa index(), count(), undo(), redo() -
767*/ -
768 -
769void QUndoStack::setIndex(int idx) -
770{ -
771 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
772 if (!d->macro_stack.isEmpty()) {
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:13
1-13
773 qWarning("QUndoStack::setIndex(): cannot set index in the middle of a macro");
executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 773, __PRETTY_FUNCTION__).warning("QUndoStack::setIndex(): cannot set index in the middle of a macro");
-
774 return;
executed: return;
Execution Count:1
1
775 } -
776 -
777 if (idx < 0)
evaluated: idx < 0
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:11
2-11
778 idx = 0;
executed: idx = 0;
Execution Count:2
2
779 else if (idx > d->command_list.size())
evaluated: idx > d->command_list.size()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:9
2-9
780 idx = d->command_list.size();
executed: idx = d->command_list.size();
Execution Count:2
2
781 -
782 int i = d->index;
executed (the execution status of this line is deduced): int i = d->index;
-
783 while (i < idx)
evaluated: i < idx
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:13
6-13
784 d->command_list.at(i++)->redo();
executed: d->command_list.at(i++)->redo();
Execution Count:6
6
785 while (i > idx)
evaluated: i > idx
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:13
9-13
786 d->command_list.at(--i)->undo();
executed: d->command_list.at(--i)->undo();
Execution Count:9
9
787 -
788 d->setIndex(idx, false);
executed (the execution status of this line is deduced): d->setIndex(idx, false);
-
789}
executed: }
Execution Count:13
13
790 -
791/*! -
792 Returns true if there is a command available for undo; otherwise returns false. -
793 -
794 This function returns false if the stack is empty, or if the bottom command -
795 on the stack has already been undone. -
796 -
797 Synonymous with index() == 0. -
798 -
799 \sa index(), canRedo() -
800*/ -
801 -
802bool QUndoStack::canUndo() const -
803{ -
804 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
805 if (!d->macro_stack.isEmpty())
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:195
18-195
806 return false;
executed: return false;
Execution Count:18
18
807 return d->index > 0;
executed: return d->index > 0;
Execution Count:195
195
808} -
809 -
810/*! -
811 Returns true if there is a command available for redo; otherwise returns false. -
812 -
813 This function returns false if the stack is empty or if the top command -
814 on the stack has already been redone. -
815 -
816 Synonymous with index() == count(). -
817 -
818 \sa index(), canUndo() -
819*/ -
820 -
821bool QUndoStack::canRedo() const -
822{ -
823 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
824 if (!d->macro_stack.isEmpty())
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:195
18-195
825 return false;
executed: return false;
Execution Count:18
18
826 return d->index < d->command_list.size();
executed: return d->index < d->command_list.size();
Execution Count:195
195
827} -
828 -
829/*! -
830 Returns the text of the command which will be undone in the next call to undo(). -
831 -
832 \sa QUndoCommand::actionText(), redoText() -
833*/ -
834 -
835QString QUndoStack::undoText() const -
836{ -
837 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
838 if (!d->macro_stack.isEmpty())
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:195
18-195
839 return QString();
executed: return QString();
Execution Count:18
18
840 if (d->index > 0)
evaluated: d->index > 0
TRUEFALSE
yes
Evaluation Count:144
yes
Evaluation Count:51
51-144
841 return d->command_list.at(d->index - 1)->actionText();
executed: return d->command_list.at(d->index - 1)->actionText();
Execution Count:144
144
842 return QString();
executed: return QString();
Execution Count:51
51
843} -
844 -
845/*! -
846 Returns the text of the command which will be redone in the next call to redo(). -
847 -
848 \sa QUndoCommand::actionText(), undoText() -
849*/ -
850 -
851QString QUndoStack::redoText() const -
852{ -
853 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
854 if (!d->macro_stack.isEmpty())
evaluated: !d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:195
18-195
855 return QString();
executed: return QString();
Execution Count:18
18
856 if (d->index < d->command_list.size())
evaluated: d->index < d->command_list.size()
TRUEFALSE
yes
Evaluation Count:73
yes
Evaluation Count:122
73-122
857 return d->command_list.at(d->index)->actionText();
executed: return d->command_list.at(d->index)->actionText();
Execution Count:73
73
858 return QString();
executed: return QString();
Execution Count:122
122
859} -
860 -
861#ifndef QT_NO_ACTION -
862 -
863/*! -
864 Creates an undo QAction object with the given \a parent. -
865 -
866 Triggering this action will cause a call to undo(). The text of this action -
867 is the text of the command which will be undone in the next call to undo(), -
868 prefixed by the specified \a prefix. If there is no command available for undo, -
869 this action will be disabled. -
870 -
871 If \a prefix is empty, the default template "Undo %1" is used instead of prefix. -
872 Before Qt 4.8, the prefix "Undo" was used by default. -
873 -
874 \sa createRedoAction(), canUndo(), QUndoCommand::text() -
875*/ -
876 -
877QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix) const -
878{ -
879 QUndoAction *result = new QUndoAction(prefix, parent);
executed (the execution status of this line is deduced): QUndoAction *result = new QUndoAction(prefix, parent);
-
880 if (prefix.isEmpty())
evaluated: prefix.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:8
1-8
881 result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
executed: result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
Execution Count:1
1
882 -
883 result->setEnabled(canUndo());
executed (the execution status of this line is deduced): result->setEnabled(canUndo());
-
884 result->setPrefixedText(undoText());
executed (the execution status of this line is deduced): result->setPrefixedText(undoText());
-
885 connect(this, SIGNAL(canUndoChanged(bool)),
executed (the execution status of this line is deduced): connect(this, "2""canUndoChanged(bool)",
-
886 result, SLOT(setEnabled(bool)));
executed (the execution status of this line is deduced): result, "1""setEnabled(bool)");
-
887 connect(this, SIGNAL(undoTextChanged(QString)),
executed (the execution status of this line is deduced): connect(this, "2""undoTextChanged(QString)",
-
888 result, SLOT(setPrefixedText(QString)));
executed (the execution status of this line is deduced): result, "1""setPrefixedText(QString)");
-
889 connect(result, SIGNAL(triggered()), this, SLOT(undo()));
executed (the execution status of this line is deduced): connect(result, "2""triggered()", this, "1""undo()");
-
890 return result;
executed: return result;
Execution Count:9
9
891} -
892 -
893/*! -
894 Creates an redo QAction object with the given \a parent. -
895 -
896 Triggering this action will cause a call to redo(). The text of this action -
897 is the text of the command which will be redone in the next call to redo(), -
898 prefixed by the specified \a prefix. If there is no command available for redo, -
899 this action will be disabled. -
900 -
901 If \a prefix is empty, the default template "Redo %1" is used instead of prefix. -
902 Before Qt 4.8, the prefix "Redo" was used by default. -
903 -
904 \sa createUndoAction(), canRedo(), QUndoCommand::text() -
905*/ -
906 -
907QAction *QUndoStack::createRedoAction(QObject *parent, const QString &prefix) const -
908{ -
909 QUndoAction *result = new QUndoAction(prefix, parent);
executed (the execution status of this line is deduced): QUndoAction *result = new QUndoAction(prefix, parent);
-
910 if (prefix.isEmpty())
evaluated: prefix.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:8
1-8
911 result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
executed: result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
Execution Count:1
1
912 -
913 result->setEnabled(canRedo());
executed (the execution status of this line is deduced): result->setEnabled(canRedo());
-
914 result->setPrefixedText(redoText());
executed (the execution status of this line is deduced): result->setPrefixedText(redoText());
-
915 connect(this, SIGNAL(canRedoChanged(bool)),
executed (the execution status of this line is deduced): connect(this, "2""canRedoChanged(bool)",
-
916 result, SLOT(setEnabled(bool)));
executed (the execution status of this line is deduced): result, "1""setEnabled(bool)");
-
917 connect(this, SIGNAL(redoTextChanged(QString)),
executed (the execution status of this line is deduced): connect(this, "2""redoTextChanged(QString)",
-
918 result, SLOT(setPrefixedText(QString)));
executed (the execution status of this line is deduced): result, "1""setPrefixedText(QString)");
-
919 connect(result, SIGNAL(triggered()), this, SLOT(redo()));
executed (the execution status of this line is deduced): connect(result, "2""triggered()", this, "1""redo()");
-
920 return result;
executed: return result;
Execution Count:9
9
921} -
922 -
923#endif // QT_NO_ACTION -
924 -
925/*! -
926 Begins composition of a macro command with the given \a text description. -
927 -
928 An empty command described by the specified \a text is pushed on the stack. -
929 Any subsequent commands pushed on the stack will be appended to the empty -
930 command's children until endMacro() is called. -
931 -
932 Calls to beginMacro() and endMacro() may be nested, but every call to -
933 beginMacro() must have a matching call to endMacro(). -
934 -
935 While a macro is composed, the stack is disabled. This means that: -
936 \list -
937 \li indexChanged() and cleanChanged() are not emitted, -
938 \li canUndo() and canRedo() return false, -
939 \li calling undo() or redo() has no effect, -
940 \li the undo/redo actions are disabled. -
941 \endlist -
942 -
943 The stack becomes enabled and appropriate signals are emitted when endMacro() -
944 is called for the outermost macro. -
945 -
946 \snippet code/src_gui_util_qundostack.cpp 4 -
947 -
948 This code is equivalent to: -
949 -
950 \snippet code/src_gui_util_qundostack.cpp 5 -
951 -
952 \sa endMacro() -
953*/ -
954 -
955void QUndoStack::beginMacro(const QString &text) -
956{ -
957 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
958 QUndoCommand *cmd = new QUndoCommand();
executed (the execution status of this line is deduced): QUndoCommand *cmd = new QUndoCommand();
-
959 cmd->setText(text);
executed (the execution status of this line is deduced): cmd->setText(text);
-
960 -
961 if (d->macro_stack.isEmpty()) {
evaluated: d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:1
1-4
962 while (d->index < d->command_list.size())
evaluated: d->index < d->command_list.size()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:4
2-4
963 delete d->command_list.takeLast();
executed: delete d->command_list.takeLast();
Execution Count:2
2
964 if (d->clean_index > d->index)
partially evaluated: d->clean_index > d->index
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
965 d->clean_index = -1; // we've deleted the clean state
never executed: d->clean_index = -1;
0
966 d->command_list.append(cmd);
executed (the execution status of this line is deduced): d->command_list.append(cmd);
-
967 } else {
executed: }
Execution Count:4
4
968 d->macro_stack.last()->d->child_list.append(cmd);
executed (the execution status of this line is deduced): d->macro_stack.last()->d->child_list.append(cmd);
-
969 }
executed: }
Execution Count:1
1
970 d->macro_stack.append(cmd);
executed (the execution status of this line is deduced): d->macro_stack.append(cmd);
-
971 -
972 if (d->macro_stack.count() == 1) {
evaluated: d->macro_stack.count() == 1
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:1
1-4
973 emit canUndoChanged(false);
executed (the execution status of this line is deduced): canUndoChanged(false);
-
974 emit undoTextChanged(QString());
executed (the execution status of this line is deduced): undoTextChanged(QString());
-
975 emit canRedoChanged(false);
executed (the execution status of this line is deduced): canRedoChanged(false);
-
976 emit redoTextChanged(QString());
executed (the execution status of this line is deduced): redoTextChanged(QString());
-
977 }
executed: }
Execution Count:4
4
978}
executed: }
Execution Count:5
5
979 -
980/*! -
981 Ends composition of a macro command. -
982 -
983 If this is the outermost macro in a set nested macros, this function emits -
984 indexChanged() once for the entire macro command. -
985 -
986 \sa beginMacro() -
987*/ -
988 -
989void QUndoStack::endMacro() -
990{ -
991 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
992 if (d->macro_stack.isEmpty()) {
partially evaluated: d->macro_stack.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
993 qWarning("QUndoStack::endMacro(): no matching beginMacro()");
never executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 993, __PRETTY_FUNCTION__).warning("QUndoStack::endMacro(): no matching beginMacro()");
-
994 return;
never executed: return;
0
995 } -
996 -
997 d->macro_stack.removeLast();
executed (the execution status of this line is deduced): d->macro_stack.removeLast();
-
998 -
999 if (d->macro_stack.isEmpty()) {
evaluated: d->macro_stack.isEmpty()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:1
1-4
1000 d->checkUndoLimit();
executed (the execution status of this line is deduced): d->checkUndoLimit();
-
1001 d->setIndex(d->index + 1, false);
executed (the execution status of this line is deduced): d->setIndex(d->index + 1, false);
-
1002 }
executed: }
Execution Count:4
4
1003}
executed: }
Execution Count:5
5
1004 -
1005/*! -
1006 \since 4.4 -
1007 -
1008 Returns a const pointer to the command at \a index. -
1009 -
1010 This function returns a const pointer, because modifying a command, -
1011 once it has been pushed onto the stack and executed, almost always -
1012 causes corruption of the state of the document, if the command is -
1013 later undone or redone. -
1014 -
1015 \sa QUndoCommand::child() -
1016*/ -
1017const QUndoCommand *QUndoStack::command(int index) const -
1018{ -
1019 Q_D(const QUndoStack);
never executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
1020 -
1021 if (index < 0 || index >= d->command_list.count())
never evaluated: index < 0
never evaluated: index >= d->command_list.count()
0
1022 return 0;
never executed: return 0;
0
1023 return d->command_list.at(index);
never executed: return d->command_list.at(index);
0
1024} -
1025 -
1026/*! -
1027 Returns the text of the command at index \a idx. -
1028 -
1029 \sa beginMacro() -
1030*/ -
1031 -
1032QString QUndoStack::text(int idx) const -
1033{ -
1034 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
1035 -
1036 if (idx < 0 || idx >= d->command_list.size())
partially evaluated: idx < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
partially evaluated: idx >= d->command_list.size()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
1037 return QString();
never executed: return QString();
0
1038 return d->command_list.at(idx)->text();
executed: return d->command_list.at(idx)->text();
Execution Count:1
1
1039} -
1040 -
1041/*! -
1042 \property QUndoStack::undoLimit -
1043 \brief the maximum number of commands on this stack. -
1044 \since 4.3 -
1045 -
1046 When the number of commands on a stack exceedes the stack's undoLimit, commands are -
1047 deleted from the bottom of the stack. Macro commands (commands with child commands) -
1048 are treated as one command. The default value is 0, which means that there is no -
1049 limit. -
1050 -
1051 This property may only be set when the undo stack is empty, since setting it on a -
1052 non-empty stack might delete the command at the current index. Calling setUndoLimit() -
1053 on a non-empty stack prints a warning and does nothing. -
1054*/ -
1055 -
1056void QUndoStack::setUndoLimit(int limit) -
1057{ -
1058 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
1059 -
1060 if (!d->command_list.isEmpty()) {
partially evaluated: !d->command_list.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
1061 qWarning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
never executed (the execution status of this line is deduced): QMessageLogger("util/qundostack.cpp", 1061, __PRETTY_FUNCTION__).warning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
-
1062 return;
never executed: return;
0
1063 } -
1064 -
1065 if (limit == d->undo_limit)
partially evaluated: limit == d->undo_limit
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
1066 return;
never executed: return;
0
1067 d->undo_limit = limit;
executed (the execution status of this line is deduced): d->undo_limit = limit;
-
1068 d->checkUndoLimit();
executed (the execution status of this line is deduced): d->checkUndoLimit();
-
1069}
executed: }
Execution Count:1
1
1070 -
1071int QUndoStack::undoLimit() const -
1072{ -
1073 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
1074 -
1075 return d->undo_limit;
executed: return d->undo_limit;
Execution Count:2
2
1076} -
1077 -
1078/*! -
1079 \property QUndoStack::active -
1080 \brief the active status of this stack. -
1081 -
1082 An application often has multiple undo stacks, one for each opened document. The active -
1083 stack is the one associated with the currently active document. If the stack belongs -
1084 to a QUndoGroup, calls to QUndoGroup::undo() or QUndoGroup::redo() will be forwarded -
1085 to this stack when it is active. If the QUndoGroup is watched by a QUndoView, the view -
1086 will display the contents of this stack when it is active. If the stack does not belong to -
1087 a QUndoGroup, making it active has no effect. -
1088 -
1089 It is the programmer's responsibility to specify which stack is active by -
1090 calling setActive(), usually when the associated document window receives focus. -
1091 -
1092 \sa QUndoGroup -
1093*/ -
1094 -
1095void QUndoStack::setActive(bool active) -
1096{ -
1097#ifdef QT_NO_UNDOGROUP -
1098 Q_UNUSED(active); -
1099#else -
1100 Q_D(QUndoStack);
executed (the execution status of this line is deduced): QUndoStackPrivate * const d = d_func();
-
1101 -
1102 if (d->group != 0) {
evaluated: d->group != 0
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:1
1-8
1103 if (active)
evaluated: active
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:2
2-6
1104 d->group->setActiveStack(this);
executed: d->group->setActiveStack(this);
Execution Count:6
6
1105 else if (d->group->activeStack() == this)
evaluated: d->group->activeStack() == this
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
1106 d->group->setActiveStack(0);
executed: d->group->setActiveStack(0);
Execution Count:1
1
1107 } -
1108#endif -
1109}
executed: }
Execution Count:9
9
1110 -
1111bool QUndoStack::isActive() const -
1112{ -
1113#ifdef QT_NO_UNDOGROUP -
1114 return true; -
1115#else -
1116 Q_D(const QUndoStack);
executed (the execution status of this line is deduced): const QUndoStackPrivate * const d = d_func();
-
1117 return d->group == 0 || d->group->activeStack() == this;
executed: return d->group == 0 || d->group->activeStack() == this;
Execution Count:16
16
1118#endif -
1119} -
1120 -
1121/*! -
1122 \fn void QUndoStack::indexChanged(int idx) -
1123 -
1124 This signal is emitted whenever a command modifies the state of the document. -
1125 This happens when a command is undone or redone. When a macro -
1126 command is undone or redone, or setIndex() is called, this signal -
1127 is emitted only once. -
1128 -
1129 \a idx specifies the index of the current command, ie. the command which will be -
1130 executed on the next call to redo(). -
1131 -
1132 \sa index(), setIndex() -
1133*/ -
1134 -
1135/*! -
1136 \fn void QUndoStack::cleanChanged(bool clean) -
1137 -
1138 This signal is emitted whenever the stack enters or leaves the clean state. -
1139 If \a clean is true, the stack is in a clean state; otherwise this signal -
1140 indicates that it has left the clean state. -
1141 -
1142 \sa isClean(), setClean() -
1143*/ -
1144 -
1145/*! -
1146 \fn void QUndoStack::undoTextChanged(const QString &undoText) -
1147 -
1148 This signal is emitted whenever the value of undoText() changes. It is -
1149 used to update the text property of the undo action returned by createUndoAction(). -
1150 \a undoText specifies the new text. -
1151*/ -
1152 -
1153/*! -
1154 \fn void QUndoStack::canUndoChanged(bool canUndo) -
1155 -
1156 This signal is emitted whenever the value of canUndo() changes. It is -
1157 used to enable or disable the undo action returned by createUndoAction(). -
1158 \a canUndo specifies the new value. -
1159*/ -
1160 -
1161/*! -
1162 \fn void QUndoStack::redoTextChanged(const QString &redoText) -
1163 -
1164 This signal is emitted whenever the value of redoText() changes. It is -
1165 used to update the text property of the redo action returned by createRedoAction(). -
1166 \a redoText specifies the new text. -
1167*/ -
1168 -
1169/*! -
1170 \fn void QUndoStack::canRedoChanged(bool canRedo) -
1171 -
1172 This signal is emitted whenever the value of canRedo() changes. It is -
1173 used to enable or disable the redo action returned by createRedoAction(). -
1174 \a canRedo specifies the new value. -
1175*/ -
1176 -
1177QT_END_NAMESPACE -
1178 -
1179#endif // QT_NO_UNDOSTACK -
1180 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial