2
* Copyright (c) 2014 Dmitry Kazakov <dimula73@gmail.com>
3
* Copyright (c) 2014 Mohit Goyal <mohit.bits2011@gmail.com>
5
* This library is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Lesser General Public License as published by
7
* the Free Software Foundation; either version 2.1 of the License, or
8
* (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
/****************************************************************************
21
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
22
** All rights reserved.
23
** Contact: Nokia Corporation (qt-info@nokia.com)
25
** This file is part of the QtGui module of the Qt Toolkit.
27
** $QT_BEGIN_LICENSE:LGPL$
28
** No Commercial Usage
29
** This file contains pre-release code and may not be distributed.
30
** You may use this file in accordance with the terms and conditions
31
** contained in the Technology Preview License Agreement accompanying
34
** GNU Lesser General Public License Usage
35
** Alternatively, this file may be used under the terms of the GNU Lesser
36
** General Public License version 2.1 as published by the Free Software
37
** Foundation and appearing in the file LICENSE.LGPL included in the
38
** packaging of this file. Please review the following information to
39
** ensure the GNU Lesser General Public License version 2.1 requirements
40
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
42
** In addition, as a special exception, Nokia gives you certain additional
43
** rights. These rights are described in the Nokia Qt LGPL Exception
44
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
46
** If you have questions regarding the use of this file, please contact
47
** Nokia at qt-info@nokia.com.
58
****************************************************************************/
61
#include <klocalizedstring.h>
62
#include <kstandardaction.h>
63
#include <kactioncollection.h>
64
#include "kundo2stack.h"
65
#include "kundo2stack_p.h"
66
#include "kundo2group.h"
68
#include <core/kexi.h>
69
#include <kexiutils/KexiIcon.h>
74
#ifndef QT_NO_UNDOCOMMAND
78
\brief The KUndo2Command class is the base class of all commands stored on a KUndo2QStack.
81
For an overview of Qt's Undo Framework, see the
82
\l{Overview of Qt's Undo Framework}{overview document}.
84
A KUndo2Command represents a single editing action on a document; for example,
85
inserting or deleting a block of text in a text editor. KUndo2Command can apply
86
a change to the document with redo() and undo the change with undo(). The
87
implementations for these functions must be provided in a derived class.
89
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 0
91
A KUndo2Command has an associated text(). This is a short string
92
describing what the command does. It is used to update the text
93
properties of the stack's undo and redo actions; see
94
KUndo2QStack::createUndoAction() and KUndo2QStack::createRedoAction().
96
KUndo2Command objects are owned by the stack they were pushed on.
97
KUndo2QStack deletes a command if it has been undone and a new command is pushed. For example:
99
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 1
101
In effect, when a command is pushed, it becomes the top-most command
104
To support command compression, KUndo2Command has an id() and the virtual function
105
mergeWith(). These functions are used by KUndo2QStack::push().
107
To support command macros, a KUndo2Command object can have any number of child
108
commands. Undoing or redoing the parent command will cause the child
109
commands to be undone or redone. A command can be assigned
110
to a parent explicitly in the constructor. In this case, the command
111
will be owned by the parent.
113
The parent in this case is usually an empty command, in that it doesn't
114
provide its own implementation of undo() and redo(). Instead, it uses
115
the base implementations of these functions, which simply call undo() or
116
redo() on all its children. The parent should, however, have a meaningful
119
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 2
121
Another way to create macros is to use the convenience functions
122
KUndo2QStack::beginMacro() and KUndo2QStack::endMacro().
128
Constructs a KUndo2Command object with the given \a parent and \a text.
130
If \a parent is not 0, this command is appended to parent's child list.
131
The parent command then owns this command and will delete it in its
137
KUndo2Command::KUndo2Command(const KUndo2MagicString &text, KUndo2Command *parent):
138
m_hasParent(parent != 0),
140
m_endOfCommand(QTime::currentTime())
142
d = new KUndo2CommandPrivate;
144
parent->d->child_list.append(this);
151
Constructs a KUndo2Command object with parent \a parent.
153
If \a parent is not 0, this command is appended to parent's child list.
154
The parent command then owns this command and will delete it in its
160
KUndo2Command::KUndo2Command(KUndo2Command *parent):
161
m_hasParent(parent != 0),m_timedID(0)
163
d = new KUndo2CommandPrivate;
165
parent->d->child_list.append(this);
170
Destroys the KUndo2Command object and all child commands.
175
KUndo2Command::~KUndo2Command()
177
qDeleteAll(d->child_list);
182
Returns the ID of this command.
184
A command ID is used in command compression. It must be an integer unique to
185
this command's class, or -1 if the command doesn't support compression.
187
If the command supports compression this function must be overridden in the
188
derived class to return the correct ID. The base implementation returns -1.
190
KUndo2QStack::push() will only try to merge two commands if they have the
191
same ID, and the ID is not -1.
193
\sa mergeWith(), KUndo2QStack::push()
196
int KUndo2Command::id() const
202
Attempts to merge this command with \a command. Returns true on
203
success; otherwise returns false.
205
If this function returns true, calling this command's redo() must have the same
206
effect as redoing both this command and \a command.
207
Similarly, calling this command's undo() must have the same effect as undoing
208
\a command and this command.
210
KUndo2QStack will only try to merge two commands if they have the same id, and
213
The default implementation returns false.
215
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 3
217
\sa id() KUndo2QStack::push()
220
bool KUndo2Command::mergeWith(const KUndo2Command *command)
227
Applies a change to the document. This function must be implemented in
228
the derived class. Calling KUndo2QStack::push(),
229
KUndo2QStack::undo() or KUndo2QStack::redo() from this function leads to
232
The default implementation calls redo() on all child commands.
237
void KUndo2Command::redo()
239
for (int i = 0; i < d->child_list.size(); ++i)
240
d->child_list.at(i)->redo();
244
Reverts a change to the document. After undo() is called, the state of
245
the document should be the same as before redo() was called. This function must
246
be implemented in the derived class. Calling KUndo2QStack::push(),
247
KUndo2QStack::undo() or KUndo2QStack::redo() from this function leads to
250
The default implementation calls undo() on all child commands in reverse order.
255
void KUndo2Command::undo()
257
for (int i = d->child_list.size() - 1; i >= 0; --i)
258
d->child_list.at(i)->undo();
262
Returns a short text string describing what this command does; for example,
265
The text is used when the text properties of the stack's undo and redo
268
\sa setText(), KUndo2QStack::createUndoAction(), KUndo2QStack::createRedoAction()
271
QString KUndo2Command::actionText() const
273
if(d->actionText!=NULL)
274
return d->actionText;
280
Returns a short text string describing what this command does; for example,
283
The text is used when the text properties of the stack's undo and redo
286
\sa setText(), KUndo2QStack::createUndoAction(), KUndo2QStack::createRedoAction()
289
KUndo2MagicString KUndo2Command::text() const
295
Sets the command's text to be the \a text specified.
297
The specified text should be a short user-readable string describing what this
300
\sa text() KUndo2QStack::createUndoAction() KUndo2QStack::createRedoAction()
303
void KUndo2Command::setText(const KUndo2MagicString &undoText)
306
d->actionText = undoText.toSecondaryString();
312
Returns the number of child commands in this command.
317
int KUndo2Command::childCount() const
319
return d->child_list.count();
325
Returns the child command at \a index.
327
\sa childCount(), KUndo2QStack::command()
330
const KUndo2Command *KUndo2Command::child(int index) const
332
if (index < 0 || index >= d->child_list.count())
334
return d->child_list.at(index);
337
bool KUndo2Command::hasParent()
341
int KUndo2Command::timedId()
345
void KUndo2Command::setTimedID(int value)
350
bool KUndo2Command::timedMergeWith(KUndo2Command *other)
352
if(other->timedId() == this->timedId() && other->timedId()!=-1 )
353
m_mergeCommandsVector.append(other);
358
void KUndo2Command::setTime()
360
m_timeOfCreation = QTime::currentTime();
362
QTime KUndo2Command::time()
364
return m_timeOfCreation;
366
void KUndo2Command::setEndTime()
368
m_endOfCommand = QTime::currentTime();
370
QTime KUndo2Command::endTime()
372
return m_endOfCommand;
375
void KUndo2Command::undoMergedCommands()
379
if (!mergeCommandsVector().isEmpty()) {
380
QVectorIterator<KUndo2Command*> it(mergeCommandsVector());
382
while (it.hasNext()) {
383
KUndo2Command* cmd = it.next();
384
cmd->undoMergedCommands();
389
void KUndo2Command::redoMergedCommands()
391
if (!mergeCommandsVector().isEmpty()) {
393
QVectorIterator<KUndo2Command*> it(mergeCommandsVector());
395
while (it.hasPrevious()) {
396
KUndo2Command* cmd = it.previous();
397
cmd->redoMergedCommands();
402
QVector<KUndo2Command*> KUndo2Command::mergeCommandsVector()
404
return m_mergeCommandsVector;
406
bool KUndo2Command::isMerged()
408
return !m_mergeCommandsVector.isEmpty();
411
KUndo2CommandExtraData* KUndo2Command::extraData() const
413
return d->extraData.data();
416
void KUndo2Command::setExtraData(KUndo2CommandExtraData *data)
418
d->extraData.reset(data);
422
#endif // QT_NO_UNDOCOMMAND
424
#ifndef QT_NO_UNDOSTACK
428
\brief The KUndo2QStack class is a stack of KUndo2Command objects.
431
For an overview of Qt's Undo Framework, see the
432
\l{Overview of Qt's Undo Framework}{overview document}.
434
An undo stack maintains a stack of commands that have been applied to a
437
New commands are pushed on the stack using push(). Commands can be
438
undone and redone using undo() and redo(), or by triggering the
439
actions returned by createUndoAction() and createRedoAction().
441
KUndo2QStack keeps track of the \a current command. This is the command
442
which will be executed by the next call to redo(). The index of this
443
command is returned by index(). The state of the edited object can be
444
rolled forward or back using setIndex(). If the top-most command on the
445
stack has already been redone, index() is equal to count().
447
KUndo2QStack provides support for undo and redo actions, command
448
compression, command macros, and supports the concept of a
451
\section1 Undo and Redo Actions
453
KUndo2QStack provides convenient undo and redo QAction objects, which
454
can be inserted into a menu or a toolbar. When commands are undone or
455
redone, KUndo2QStack updates the text properties of these actions
456
to reflect what change they will trigger. The actions are also disabled
457
when no command is available for undo or redo. These actions
458
are returned by KUndo2QStack::createUndoAction() and KUndo2QStack::createRedoAction().
460
\section1 Command Compression and Macros
462
Command compression is useful when several commands can be compressed
463
into a single command that can be undone and redone in a single operation.
464
For example, when a user types a character in a text editor, a new command
465
is created. This command inserts the character into the document at the
466
cursor position. However, it is more convenient for the user to be able
467
to undo or redo typing of whole words, sentences, or paragraphs.
468
Command compression allows these single-character commands to be merged
469
into a single command which inserts or deletes sections of text.
470
For more information, see KUndo2Command::mergeWith() and push().
472
A command macro is a sequence of commands, all of which are undone and
473
redone in one go. Command macros are created by giving a command a list
475
Undoing or redoing the parent command will cause the child commands to
476
be undone or redone. Command macros may be created explicitly
477
by specifying a parent in the KUndo2Command constructor, or by using the
478
convenience functions beginMacro() and endMacro().
480
Although command compression and macros appear to have the same effect to the
481
user, they often have different uses in an application. Commands that
482
perform small changes to a document may be usefully compressed if there is
483
no need to individually record them, and if only larger changes are relevant
485
However, for commands that need to be recorded individually, or those that
486
cannot be compressed, it is useful to use macros to provide a more convenient
487
user experience while maintaining a record of each command.
489
\section1 Clean State
491
KUndo2QStack supports the concept of a clean state. When the
492
document is saved to disk, the stack can be marked as clean using
493
setClean(). Whenever the stack returns to this state through undoing and
494
redoing commands, it emits the signal cleanChanged(). This signal
495
is also emitted when the stack leaves the clean state. This signal is
496
usually used to enable and disable the save actions in the application,
497
and to update the document's title to reflect that it contains unsaved
500
\sa KUndo2Command, KUndo2View
505
KUndo2Action::KUndo2Action(const QString &textTemplate, const QString &defaultText, QObject *parent)
508
m_textTemplate = textTemplate;
509
m_defaultText = defaultText;
513
void KUndo2Action::setPrefixedText(const QString &text)
516
setText(m_defaultText);
518
setText(m_textTemplate.arg(text));
521
#endif // QT_NO_ACTION
524
Sets the current index to \a idx, emitting appropriate signals. If \a clean is true,
525
makes \a idx the clean index as well.
528
void KUndo2QStack::setIndex(int idx, bool clean)
530
bool was_clean = m_index == m_clean_index;
531
if (m_lastMergedIndex <= idx) {
532
m_lastMergedSetCount = idx - m_lastMergedIndex;
535
m_lastMergedSetCount = 1;
536
m_lastMergedIndex = idx-1;
539
m_lastMergedSetCount = 0;
540
m_lastMergedIndex = 0;
542
if (idx != m_index) {
544
emit indexChanged(m_index);
545
emit canUndoChanged(canUndo());
546
emit undoTextChanged(undoText());
547
emit canRedoChanged(canRedo());
548
emit redoTextChanged(redoText());
552
m_clean_index = m_index;
554
bool is_clean = m_index == m_clean_index;
555
if (is_clean != was_clean)
556
emit cleanChanged(is_clean);
559
void KUndo2QStack::purgeRedoState()
561
bool macro = !m_macro_stack.isEmpty();
564
bool redoStateChanged = false;
565
bool cleanStateChanged = false;
567
while (m_index < m_command_list.size()) {
568
delete m_command_list.takeLast();
569
redoStateChanged = true;
572
if (m_clean_index > m_index) {
573
m_clean_index = -1; // we've deleted the clean state
574
cleanStateChanged = true;
577
if (redoStateChanged) {
578
emit canRedoChanged(canRedo());
579
emit redoTextChanged(redoText());
582
if (cleanStateChanged) {
583
emit cleanChanged(isClean());
588
If the number of commands on the stack exceedes the undo limit, deletes commands from
589
the bottom of the stack.
591
Returns true if commands were deleted.
594
bool KUndo2QStack::checkUndoLimit()
596
if (m_undo_limit <= 0 || !m_macro_stack.isEmpty() || m_undo_limit >= m_command_list.count())
599
int del_count = m_command_list.count() - m_undo_limit;
601
for (int i = 0; i < del_count; ++i)
602
delete m_command_list.takeFirst();
604
m_index -= del_count;
605
if (m_clean_index != -1) {
606
if (m_clean_index < del_count)
607
m_clean_index = -1; // we've deleted the clean command
609
m_clean_index -= del_count;
615
Constructs an empty undo stack with the parent \a parent. The
616
stack will initially be in the clean state. If \a parent is a
617
KUndo2Group object, the stack is automatically added to the group.
622
KUndo2QStack::KUndo2QStack(QObject *parent)
623
: QObject(parent), m_index(0), m_clean_index(0), m_group(0), m_undo_limit(0), m_useCumulativeUndoRedo(false), m_lastMergedSetCount(0), m_lastMergedIndex(0)
628
#ifndef QT_NO_UNDOGROUP
629
if (KUndo2Group *group = qobject_cast<KUndo2Group*>(parent))
630
group->addStack(this);
635
Destroys the undo stack, deleting any commands that are on it. If the
636
stack is in a KUndo2Group, the stack is automatically removed from the group.
641
KUndo2QStack::~KUndo2QStack()
643
#ifndef QT_NO_UNDOGROUP
645
m_group->removeStack(this);
651
Clears the command stack by deleting all commands on it, and returns the stack
656
Commands are not undone or redone; the state of the edited object remains
659
This function is usually used when the contents of the document are
665
void KUndo2QStack::clear()
667
if (m_command_list.isEmpty())
670
bool was_clean = isClean();
672
m_macro_stack.clear();
673
qDeleteAll(m_command_list);
674
m_command_list.clear();
679
emit indexChanged(0);
680
emit canUndoChanged(false);
681
emit undoTextChanged(QString());
682
emit canRedoChanged(false);
683
emit redoTextChanged(QString());
686
emit cleanChanged(true);
690
Pushes \a cmd on the stack or merges it with the most recently executed command.
691
In either case, executes \a cmd by calling its redo() function.
693
If \a cmd's id is not -1, and if the id is the same as that of the
694
most recently executed command, KUndo2QStack will attempt to merge the two
695
commands by calling KUndo2Command::mergeWith() on the most recently executed
696
command. If KUndo2Command::mergeWith() returns true, \a cmd is deleted and false
699
In all other cases \a cmd is simply pushed on the stack and true is returned.
701
If commands were undone before \a cmd was pushed, the current command and
702
all commands above it are deleted. Hence \a cmd always ends up being the
703
top-most on the stack.
705
Once a command is pushed, the stack takes ownership of it. There
706
are no getters to return the command, since modifying it after it has
707
been executed will almost always lead to corruption of the document's
710
\sa KUndo2Command::id() KUndo2Command::mergeWith()
713
bool KUndo2QStack::push(KUndo2Command *cmd)
715
cmd->redoMergedCommands();
718
bool macro = !m_macro_stack.isEmpty();
720
KUndo2Command *cur = 0;
722
KUndo2Command *macro_cmd = m_macro_stack.last();
723
if (!macro_cmd->d->child_list.isEmpty())
724
cur = macro_cmd->d->child_list.last();
727
cur = m_command_list.at(m_index - 1);
728
while (m_index < m_command_list.size())
729
delete m_command_list.takeLast();
730
if (m_clean_index > m_index)
731
m_clean_index = -1; // we've deleted the clean state
734
bool try_merge = cur != 0
736
&& cur->id() == cmd->id()
737
&& (macro || m_index != m_clean_index);
740
*Here we are going to try to merge several commands together using the QVector field in the commands using
741
*3 parameters. N : Number of commands that should remain individual at the top of the stack. T1 : Time lapsed between current command and previously merged command -- signal to
742
*merge throughout the stack. T2 : Time lapsed between two commands signalling both commands belong to the same set
743
*Whenever a KUndo2Command is initialized -- it consists of a start-time and when it is pushed --an end time.
744
*Every time a command is pushed -- it checks whether the command pushed was pushed after T1 seconds of the last merged command
745
*Then the merging begins with each group depending on the time in between each command (T2).
747
*@TODO : Currently it is not able to merge two merged commands together.
749
if (!macro && m_command_list.size() > 1 && cmd->timedId() != -1 && m_useCumulativeUndoRedo) {
750
KUndo2Command* lastcmd = m_command_list.last();
751
if (qAbs(cmd->time().msecsTo(lastcmd->endTime())) < m_timeT2 * 1000) {
752
m_lastMergedSetCount++;
754
m_lastMergedSetCount = 0;
755
m_lastMergedIndex = m_index-1;
757
if (lastcmd->timedId() == -1){
758
m_lastMergedSetCount = 0;
759
m_lastMergedIndex = m_index;
761
if (m_lastMergedSetCount > m_strokesN) {
762
KUndo2Command* toMerge = m_command_list.at(m_lastMergedIndex);
763
if (toMerge && m_command_list.size() >= m_lastMergedIndex + 1 && m_command_list.at(m_lastMergedIndex + 1)) {
764
if(toMerge->timedMergeWith(m_command_list.at(m_lastMergedIndex + 1))){
765
m_command_list.removeAt(m_lastMergedIndex + 1);
767
m_lastMergedSetCount--;
768
m_lastMergedIndex = m_command_list.indexOf(toMerge);
772
m_index = m_command_list.size();
773
if(m_lastMergedIndex<m_index){
774
if (cmd->time().msecsTo(m_command_list.at(m_lastMergedIndex)->endTime()) < -m_timeT1 * 1000) { //T1 time elapsed
775
QListIterator<KUndo2Command*> it(m_command_list);
777
m_lastMergedSetCount = 1;
779
while (it.hasPrevious()) {
780
KUndo2Command* curr = it.previous();
781
KUndo2Command* lastCmdInCurrent = curr;
783
if (!lastcmd->mergeCommandsVector().isEmpty()) {
784
if (qAbs(lastcmd->mergeCommandsVector().last()->time().msecsTo(lastCmdInCurrent->endTime())) < int(m_timeT2 * 1000) && lastcmd != lastCmdInCurrent && lastcmd != curr) {
785
if(lastcmd->timedMergeWith(curr)){
786
if (m_command_list.contains(curr)) {
787
m_command_list.removeOne(curr);
791
lastcmd = curr; //end of a merge set
794
if (qAbs(lastcmd->time().msecsTo(lastCmdInCurrent->endTime())) < int(m_timeT2 * 1000) && lastcmd != lastCmdInCurrent &&lastcmd!=curr) {
795
if(lastcmd->timedMergeWith(curr)){
796
if (m_command_list.contains(curr)){
797
m_command_list.removeOne(curr);
801
lastcmd = curr; //end of a merge set
805
m_lastMergedIndex = m_command_list.size()-1;
808
m_index = m_command_list.size();
810
if (try_merge && cur->mergeWith(cmd)) {
814
emit indexChanged(m_index);
815
emit canUndoChanged(canUndo());
816
emit undoTextChanged(undoText());
817
emit canRedoChanged(canRedo());
818
emit redoTextChanged(redoText());
822
m_macro_stack.last()->d->child_list.append(cmd);
824
m_command_list.append(cmd);
827
m_lastMergedIndex = m_index - m_strokesN;
829
setIndex(m_index + 1, false);
836
Marks the stack as clean and emits cleanChanged() if the stack was
839
Whenever the stack returns to this state through the use of undo/redo
840
commands, it emits the signal cleanChanged(). This signal is also
841
emitted when the stack leaves the clean state.
843
\sa isClean(), cleanIndex()
846
void KUndo2QStack::setClean()
848
if (!m_macro_stack.isEmpty()) {
849
qWarning("KUndo2QStack::setClean(): cannot set clean in the middle of a macro");
853
setIndex(m_index, true);
857
If the stack is in the clean state, returns true; otherwise returns false.
859
\sa setClean() cleanIndex()
862
bool KUndo2QStack::isClean() const
864
if (!m_macro_stack.isEmpty())
866
return m_clean_index == m_index;
870
Returns the clean index. This is the index at which setClean() was called.
872
A stack may not have a clean index. This happens if a document is saved,
873
some commands are undone, then a new command is pushed. Since
874
push() deletes all the undone commands before pushing the new command, the stack
875
can't return to the clean state again. In this case, this function returns -1.
877
\sa isClean() setClean()
880
int KUndo2QStack::cleanIndex() const
882
return m_clean_index;
886
Undoes the command below the current command by calling KUndo2Command::undo().
887
Decrements the current command index.
889
If the stack is empty, or if the bottom command on the stack has already been
890
undone, this function does nothing.
895
void KUndo2QStack::undo()
900
if (!m_macro_stack.isEmpty()) {
901
qWarning("KUndo2QStack::undo(): cannot undo in the middle of a macro");
905
int idx = m_index - 1;
906
m_command_list.at(idx)->undoMergedCommands();
907
setIndex(idx, false);
911
Redoes the current command by calling KUndo2Command::redo(). Increments the current
914
If the stack is empty, or if the top command on the stack has already been
915
redone, this function does nothing.
920
void KUndo2QStack::redo()
922
if (m_index == m_command_list.size())
925
if (!m_macro_stack.isEmpty()) {
926
qWarning("KUndo2QStack::redo(): cannot redo in the middle of a macro");
930
m_command_list.at(m_index)->redoMergedCommands();
931
setIndex(m_index + 1, false);
935
Returns the number of commands on the stack. Macro commands are counted as
938
\sa index() setIndex() command()
941
int KUndo2QStack::count() const
943
return m_command_list.size();
947
Returns the index of the current command. This is the command that will be
948
executed on the next call to redo(). It is not always the top-most command
949
on the stack, since a number of commands may have been undone.
951
\sa undo() redo() count()
954
int KUndo2QStack::index() const
960
Repeatedly calls undo() or redo() until the current command index reaches
961
\a idx. This function can be used to roll the state of the document forwards
962
of backwards. indexChanged() is emitted only once.
964
\sa index() count() undo() redo()
967
void KUndo2QStack::setIndex(int idx)
969
if (!m_macro_stack.isEmpty()) {
970
qWarning("KUndo2QStack::setIndex(): cannot set index in the middle of a macro");
976
else if (idx > m_command_list.size())
977
idx = m_command_list.size();
981
m_command_list.at(i++)->redoMergedCommands();
982
notifySetIndexChangedOneCommand();
985
m_command_list.at(--i)->undoMergedCommands();
986
notifySetIndexChangedOneCommand();
989
setIndex(idx, false);
994
* Called by setIndex after every command execution. It is needed by
995
* Krita to insert barriers between different kind of commands
997
void KUndo2QStack::notifySetIndexChangedOneCommand()
1002
Returns true if there is a command available for undo; otherwise returns false.
1004
This function returns false if the stack is empty, or if the bottom command
1005
on the stack has already been undone.
1007
Synonymous with index() == 0.
1009
\sa index() canRedo()
1012
bool KUndo2QStack::canUndo() const
1014
if (!m_macro_stack.isEmpty())
1020
Returns true if there is a command available for redo; otherwise returns false.
1022
This function returns false if the stack is empty or if the top command
1023
on the stack has already been redone.
1025
Synonymous with index() == count().
1027
\sa index() canUndo()
1030
bool KUndo2QStack::canRedo() const
1032
if (!m_macro_stack.isEmpty())
1034
return m_index < m_command_list.size();
1038
Returns the text of the command which will be undone in the next call to undo().
1040
\sa KUndo2Command::text() redoActionText() undoItemText()
1043
QString KUndo2QStack::undoText() const
1045
if (!m_macro_stack.isEmpty())
1047
if (m_index > 0 && m_command_list.at(m_index-1)!=NULL)
1049
return m_command_list.at(m_index - 1)->actionText();
1054
Returns the text of the command which will be redone in the next call to redo().
1056
\sa KUndo2Command::text() undoActionText() redoItemText()
1059
QString KUndo2QStack::redoText() const
1061
if (!m_macro_stack.isEmpty())
1063
if (m_index < m_command_list.size())
1064
return m_command_list.at(m_index)->actionText();
1068
#ifndef QT_NO_ACTION
1071
Creates an undo QAction object with the given \a parent.
1073
Triggering this action will cause a call to undo(). The text of this action
1074
is the text of the command which will be undone in the next call to undo(),
1075
prefixed by the specified \a prefix. If there is no command available for undo,
1076
this action will be disabled.
1078
If \a prefix is empty, the default prefix "Undo" is used.
1080
\sa createRedoAction(), canUndo(), KUndo2Command::text()
1083
QAction *KUndo2QStack::createUndoAction(QObject *parent) const
1085
KUndo2Action *result = new KUndo2Action(i18n("Undo %1"), i18nc("Default text for undo action", "Undo"), parent);
1086
result->setEnabled(canUndo());
1087
result->setPrefixedText(undoText());
1088
connect(this, SIGNAL(canUndoChanged(bool)),
1089
result, SLOT(setEnabled(bool)));
1090
connect(this, SIGNAL(undoTextChanged(QString)),
1091
result, SLOT(setPrefixedText(QString)));
1092
connect(result, SIGNAL(triggered()), this, SLOT(undo()));
1097
Creates an redo QAction object with the given \a parent.
1099
Triggering this action will cause a call to redo(). The text of this action
1100
is the text of the command which will be redone in the next call to redo(),
1101
prefixed by the specified \a prefix. If there is no command available for redo,
1102
this action will be disabled.
1104
If \a prefix is empty, the default prefix "Redo" is used.
1106
\sa createUndoAction(), canRedo(), KUndo2Command::text()
1109
QAction *KUndo2QStack::createRedoAction(QObject *parent) const
1111
KUndo2Action *result = new KUndo2Action(i18n("Redo %1"), i18nc("Default text for redo action", "Redo"), parent);
1112
result->setEnabled(canRedo());
1113
result->setPrefixedText(redoText());
1114
connect(this, SIGNAL(canRedoChanged(bool)),
1115
result, SLOT(setEnabled(bool)));
1116
connect(this, SIGNAL(redoTextChanged(QString)),
1117
result, SLOT(setPrefixedText(QString)));
1118
connect(result, SIGNAL(triggered()), this, SLOT(redo()));
1122
#endif // QT_NO_ACTION
1125
Begins composition of a macro command with the given \a text description.
1127
An empty command described by the specified \a text is pushed on the stack.
1128
Any subsequent commands pushed on the stack will be appended to the empty
1129
command's children until endMacro() is called.
1131
Calls to beginMacro() and endMacro() may be nested, but every call to
1132
beginMacro() must have a matching call to endMacro().
1134
While a macro is composed, the stack is disabled. This means that:
1136
\i indexChanged() and cleanChanged() are not emitted,
1137
\i canUndo() and canRedo() return false,
1138
\i calling undo() or redo() has no effect,
1139
\i the undo/redo actions are disabled.
1142
The stack becomes enabled and appropriate signals are emitted when endMacro()
1143
is called for the outermost macro.
1145
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 4
1147
This code is equivalent to:
1149
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 5
1154
void KUndo2QStack::beginMacro(const KUndo2MagicString &text)
1156
KUndo2Command *cmd = new KUndo2Command();
1159
if (m_macro_stack.isEmpty()) {
1160
while (m_index < m_command_list.size())
1161
delete m_command_list.takeLast();
1162
if (m_clean_index > m_index)
1163
m_clean_index = -1; // we've deleted the clean state
1164
m_command_list.append(cmd);
1166
m_macro_stack.last()->d->child_list.append(cmd);
1168
m_macro_stack.append(cmd);
1170
if (m_macro_stack.count() == 1) {
1171
emit canUndoChanged(false);
1172
emit undoTextChanged(QString());
1173
emit canRedoChanged(false);
1174
emit redoTextChanged(QString());
1179
Ends composition of a macro command.
1181
If this is the outermost macro in a set nested macros, this function emits
1182
indexChanged() once for the entire macro command.
1187
void KUndo2QStack::endMacro()
1189
if (m_macro_stack.isEmpty()) {
1190
qWarning("KUndo2QStack::endMacro(): no matching beginMacro()");
1194
m_macro_stack.removeLast();
1196
if (m_macro_stack.isEmpty()) {
1198
setIndex(m_index + 1, false);
1205
Returns a const pointer to the command at \a index.
1207
This function returns a const pointer, because modifying a command,
1208
once it has been pushed onto the stack and executed, almost always
1209
causes corruption of the state of the document, if the command is
1210
later undone or redone.
1212
\sa KUndo2Command::child()
1214
const KUndo2Command *KUndo2QStack::command(int index) const
1216
if (index < 0 || index >= m_command_list.count())
1218
return m_command_list.at(index);
1222
Returns the text of the command at index \a idx.
1227
QString KUndo2QStack::text(int idx) const
1229
if (idx < 0 || idx >= m_command_list.size())
1231
return m_command_list.at(idx)->text().toString();
1235
\property KUndo2QStack::undoLimit
1236
\brief the maximum number of commands on this stack.
1239
When the number of commands on a stack exceedes the stack's undoLimit, commands are
1240
deleted from the bottom of the stack. Macro commands (commands with child commands)
1241
are treated as one command. The default value is 0, which means that there is no
1244
This property may only be set when the undo stack is empty, since setting it on a
1245
non-empty stack might delete the command at the current index. Calling setUndoLimit()
1246
on a non-empty stack prints a warning and does nothing.
1249
void KUndo2QStack::setUndoLimit(int limit)
1251
if (!m_command_list.isEmpty()) {
1252
qWarning("KUndo2QStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
1256
if (limit == m_undo_limit)
1258
m_undo_limit = limit;
1262
int KUndo2QStack::undoLimit() const
1264
return m_undo_limit;
1268
\property KUndo2QStack::active
1269
\brief the active status of this stack.
1271
An application often has multiple undo stacks, one for each opened document. The active
1272
stack is the one associated with the currently active document. If the stack belongs
1273
to a KUndo2Group, calls to KUndo2Group::undo() or KUndo2Group::redo() will be forwarded
1274
to this stack when it is active. If the KUndo2Group is watched by a KUndo2View, the view
1275
will display the contents of this stack when it is active. If the stack does not belong to
1276
a KUndo2Group, making it active has no effect.
1278
It is the programmer's responsibility to specify which stack is active by
1279
calling setActive(), usually when the associated document window receives focus.
1284
void KUndo2QStack::setActive(bool active)
1286
#ifdef QT_NO_UNDOGROUP
1291
m_group->setActiveStack(this);
1292
else if (m_group->activeStack() == this)
1293
m_group->setActiveStack(0);
1298
bool KUndo2QStack::isActive() const
1300
#ifdef QT_NO_UNDOGROUP
1303
return m_group == 0 || m_group->activeStack() == this;
1306
void KUndo2QStack::setUseCumulativeUndoRedo(bool value)
1308
m_useCumulativeUndoRedo = value;
1311
bool KUndo2QStack::useCumulativeUndoRedo()
1313
return m_useCumulativeUndoRedo;
1315
void KUndo2QStack::setTimeT1(double value)
1320
double KUndo2QStack::timeT1()
1325
void KUndo2QStack::setTimeT2(double value)
1330
double KUndo2QStack::timeT2()
1334
int KUndo2QStack::strokesN()
1338
void KUndo2QStack::setStrokesN(int value)
1345
QAction* KUndo2Stack::createRedoAction(KActionCollection* actionCollection, const QString& actionName)
1347
QAction* action = KUndo2QStack::createRedoAction(actionCollection);
1349
if (actionName.isEmpty()) {
1350
action->setObjectName(KStandardAction::name(KStandardAction::Redo));
1352
action->setObjectName(actionName);
1355
action->setIcon(KexiIcon("edit-redo"));
1356
action->setIconText(i18n("Redo"));
1357
action->setShortcuts(KStandardShortcut::redo());
1359
actionCollection->addAction(action->objectName(), action);
1364
QAction* KUndo2Stack::createUndoAction(KActionCollection* actionCollection, const QString& actionName)
1366
QAction* action = KUndo2QStack::createUndoAction(actionCollection);
1368
if (actionName.isEmpty()) {
1369
action->setObjectName(KStandardAction::name(KStandardAction::Undo));
1371
action->setObjectName(actionName);
1374
action->setIcon(koIcon("edit-undo"));
1375
action->setIconText(i18n("Undo"));
1376
action->setShortcuts(KStandardShortcut::undo());
1378
actionCollection->addAction(action->objectName(), action);
1384
\fn void KUndo2QStack::indexChanged(int idx)
1386
This signal is emitted whenever a command modifies the state of the document.
1387
This happens when a command is undone or redone. When a macro
1388
command is undone or redone, or setIndex() is called, this signal
1389
is emitted only once.
1391
\a idx specifies the index of the current command, ie. the command which will be
1392
executed on the next call to redo().
1394
\sa index() setIndex()
1398
\fn void KUndo2QStack::cleanChanged(bool clean)
1400
This signal is emitted whenever the stack enters or leaves the clean state.
1401
If \a clean is true, the stack is in a clean state; otherwise this signal
1402
indicates that it has left the clean state.
1404
\sa isClean() setClean()
1408
\fn void KUndo2QStack::undoTextChanged(const QString &undoText)
1410
This signal is emitted whenever the value of undoText() changes. It is
1411
used to update the text property of the undo action returned by createUndoAction().
1412
\a undoText specifies the new text.
1416
\fn void KUndo2QStack::canUndoChanged(bool canUndo)
1418
This signal is emitted whenever the value of canUndo() changes. It is
1419
used to enable or disable the undo action returned by createUndoAction().
1420
\a canUndo specifies the new value.
1424
\fn void KUndo2QStack::redoTextChanged(const QString &redoText)
1426
This signal is emitted whenever the value of redoText() changes. It is
1427
used to update the text property of the redo action returned by createRedoAction().
1428
\a redoText specifies the new text.
1432
\fn void KUndo2QStack::canRedoChanged(bool canRedo)
1434
This signal is emitted whenever the value of canRedo() changes. It is
1435
used to enable or disable the redo action returned by createRedoAction().
1436
\a canRedo specifies the new value.
1439
KUndo2Stack::KUndo2Stack(QObject *parent):
1440
KUndo2QStack(parent)
1444
#endif // QT_NO_UNDOSTACK