~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/widget/undo/kundo2stack.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2014 Dmitry Kazakov <dimula73@gmail.com>
 
3
 *  Copyright (c) 2014 Mohit Goyal <mohit.bits2011@gmail.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
/****************************************************************************
 
20
**
 
21
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 
22
** All rights reserved.
 
23
** Contact: Nokia Corporation (qt-info@nokia.com)
 
24
**
 
25
** This file is part of the QtGui module of the Qt Toolkit.
 
26
**
 
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
 
32
** this package.
 
33
**
 
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.
 
41
**
 
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.
 
45
**
 
46
** If you have questions regarding the use of this file, please contact
 
47
** Nokia at qt-info@nokia.com.
 
48
**
 
49
**
 
50
**
 
51
**
 
52
**
 
53
**
 
54
**
 
55
**
 
56
** $QT_END_LICENSE$
 
57
**
 
58
****************************************************************************/
 
59
 
 
60
#include <QDebug>
 
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"
 
67
 
 
68
#include <core/kexi.h>
 
69
#include <kexiutils/KexiIcon.h>
 
70
 
 
71
#include <QtGlobal>
 
72
 
 
73
 
 
74
#ifndef QT_NO_UNDOCOMMAND
 
75
 
 
76
/*!
 
77
    \class KUndo2Command
 
78
    \brief The KUndo2Command class is the base class of all commands stored on a KUndo2QStack.
 
79
    \since 4.2
 
80
 
 
81
    For an overview of Qt's Undo Framework, see the
 
82
    \l{Overview of Qt's Undo Framework}{overview document}.
 
83
 
 
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.
 
88
 
 
89
    \snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 0
 
90
 
 
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().
 
95
 
 
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:
 
98
 
 
99
\snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 1
 
100
 
 
101
    In effect, when a command is pushed, it becomes the top-most command
 
102
    on the stack.
 
103
 
 
104
    To support command compression, KUndo2Command has an id() and the virtual function
 
105
    mergeWith(). These functions are used by KUndo2QStack::push().
 
106
 
 
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.
 
112
 
 
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
 
117
    text().
 
118
 
 
119
    \snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 2
 
120
 
 
121
    Another way to create macros is to use the convenience functions
 
122
    KUndo2QStack::beginMacro() and KUndo2QStack::endMacro().
 
123
 
 
124
    \sa KUndo2QStack
 
125
*/
 
126
 
 
127
/*!
 
128
    Constructs a KUndo2Command object with the given \a parent and \a text.
 
129
 
 
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
 
132
    destructor.
 
133
 
 
134
    \sa ~KUndo2Command()
 
135
*/
 
136
 
 
137
KUndo2Command::KUndo2Command(const KUndo2MagicString &text, KUndo2Command *parent):
 
138
    m_hasParent(parent != 0),
 
139
    m_timedID(0),
 
140
    m_endOfCommand(QTime::currentTime())
 
141
{
 
142
    d = new KUndo2CommandPrivate;
 
143
    if (parent != 0) {
 
144
        parent->d->child_list.append(this);
 
145
    }
 
146
    setText(text);
 
147
    setTime();
 
148
}
 
149
 
 
150
/*!
 
151
    Constructs a KUndo2Command object with parent \a parent.
 
152
 
 
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
 
155
    destructor.
 
156
 
 
157
    \sa ~KUndo2Command()
 
158
*/
 
159
 
 
160
KUndo2Command::KUndo2Command(KUndo2Command *parent):
 
161
    m_hasParent(parent != 0),m_timedID(0)
 
162
{
 
163
    d = new KUndo2CommandPrivate;
 
164
    if (parent != 0)
 
165
        parent->d->child_list.append(this);
 
166
    setTime();
 
167
}
 
168
 
 
169
/*!
 
170
    Destroys the KUndo2Command object and all child commands.
 
171
 
 
172
    \sa KUndo2Command()
 
173
*/
 
174
 
 
175
KUndo2Command::~KUndo2Command()
 
176
{
 
177
    qDeleteAll(d->child_list);
 
178
    delete d;
 
179
}
 
180
 
 
181
/*!
 
182
    Returns the ID of this command.
 
183
 
 
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.
 
186
 
 
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.
 
189
 
 
190
    KUndo2QStack::push() will only try to merge two commands if they have the
 
191
    same ID, and the ID is not -1.
 
192
 
 
193
    \sa mergeWith(), KUndo2QStack::push()
 
194
*/
 
195
 
 
196
int KUndo2Command::id() const
 
197
{
 
198
    return -1;
 
199
}
 
200
 
 
201
/*!
 
202
    Attempts to merge this command with \a command. Returns true on
 
203
    success; otherwise returns false.
 
204
 
 
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.
 
209
 
 
210
    KUndo2QStack will only try to merge two commands if they have the same id, and
 
211
    the id is not -1.
 
212
 
 
213
    The default implementation returns false.
 
214
 
 
215
    \snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 3
 
216
 
 
217
    \sa id() KUndo2QStack::push()
 
218
*/
 
219
 
 
220
bool KUndo2Command::mergeWith(const KUndo2Command *command)
 
221
{
 
222
    Q_UNUSED(command);
 
223
    return false;
 
224
}
 
225
 
 
226
/*!
 
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
 
230
    undefined beahavior.
 
231
 
 
232
    The default implementation calls redo() on all child commands.
 
233
 
 
234
    \sa undo()
 
235
*/
 
236
 
 
237
void KUndo2Command::redo()
 
238
{
 
239
    for (int i = 0; i < d->child_list.size(); ++i)
 
240
        d->child_list.at(i)->redo();
 
241
}
 
242
 
 
243
/*!
 
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
 
248
    undefined beahavior.
 
249
 
 
250
    The default implementation calls undo() on all child commands in reverse order.
 
251
 
 
252
    \sa redo()
 
253
*/
 
254
 
 
255
void KUndo2Command::undo()
 
256
{
 
257
    for (int i = d->child_list.size() - 1; i >= 0; --i)
 
258
        d->child_list.at(i)->undo();
 
259
}
 
260
 
 
261
/*!
 
262
    Returns a short text string describing what this command does; for example,
 
263
    "insert text".
 
264
 
 
265
    The text is used when the text properties of the stack's undo and redo
 
266
    actions are updated.
 
267
 
 
268
    \sa setText(), KUndo2QStack::createUndoAction(), KUndo2QStack::createRedoAction()
 
269
*/
 
270
 
 
271
QString KUndo2Command::actionText() const
 
272
{
 
273
    if(d->actionText!=NULL)
 
274
        return d->actionText;
 
275
    else
 
276
        return QString();
 
277
}
 
278
 
 
279
/*!
 
280
    Returns a short text string describing what this command does; for example,
 
281
    "insert text".
 
282
 
 
283
    The text is used when the text properties of the stack's undo and redo
 
284
    actions are updated.
 
285
 
 
286
    \sa setText(), KUndo2QStack::createUndoAction(), KUndo2QStack::createRedoAction()
 
287
*/
 
288
 
 
289
KUndo2MagicString KUndo2Command::text() const
 
290
{
 
291
    return d->text;
 
292
}
 
293
 
 
294
/*!
 
295
    Sets the command's text to be the \a text specified.
 
296
 
 
297
    The specified text should be a short user-readable string describing what this
 
298
    command does.
 
299
 
 
300
    \sa text() KUndo2QStack::createUndoAction() KUndo2QStack::createRedoAction()
 
301
*/
 
302
 
 
303
void KUndo2Command::setText(const KUndo2MagicString &undoText)
 
304
{
 
305
    d->text = undoText;
 
306
    d->actionText = undoText.toSecondaryString();
 
307
}
 
308
 
 
309
/*!
 
310
    \since 4.4
 
311
 
 
312
    Returns the number of child commands in this command.
 
313
 
 
314
    \sa child()
 
315
*/
 
316
 
 
317
int KUndo2Command::childCount() const
 
318
{
 
319
    return d->child_list.count();
 
320
}
 
321
 
 
322
/*!
 
323
    \since 4.4
 
324
 
 
325
    Returns the child command at \a index.
 
326
 
 
327
    \sa childCount(), KUndo2QStack::command()
 
328
*/
 
329
 
 
330
const KUndo2Command *KUndo2Command::child(int index) const
 
331
{
 
332
    if (index < 0 || index >= d->child_list.count())
 
333
        return 0;
 
334
    return d->child_list.at(index);
 
335
}
 
336
 
 
337
bool KUndo2Command::hasParent()
 
338
{
 
339
    return m_hasParent;
 
340
}
 
341
int KUndo2Command::timedId()
 
342
{
 
343
    return m_timedID;
 
344
}
 
345
void KUndo2Command::setTimedID(int value)
 
346
{
 
347
    m_timedID = value;
 
348
}
 
349
 
 
350
bool KUndo2Command::timedMergeWith(KUndo2Command *other)
 
351
{
 
352
    if(other->timedId() == this->timedId() && other->timedId()!=-1 )
 
353
        m_mergeCommandsVector.append(other);
 
354
    else
 
355
        return false;
 
356
    return true;
 
357
}
 
358
void KUndo2Command::setTime()
 
359
{
 
360
    m_timeOfCreation = QTime::currentTime();
 
361
}
 
362
QTime KUndo2Command::time()
 
363
{
 
364
    return m_timeOfCreation;
 
365
}
 
366
void KUndo2Command::setEndTime()
 
367
{
 
368
    m_endOfCommand  = QTime::currentTime();
 
369
}
 
370
QTime KUndo2Command::endTime()
 
371
{
 
372
    return m_endOfCommand;
 
373
}
 
374
 
 
375
void KUndo2Command::undoMergedCommands()
 
376
{
 
377
 
 
378
    undo();
 
379
    if (!mergeCommandsVector().isEmpty()) {
 
380
        QVectorIterator<KUndo2Command*> it(mergeCommandsVector());
 
381
        it.toFront();
 
382
        while (it.hasNext()) {
 
383
            KUndo2Command* cmd = it.next();
 
384
            cmd->undoMergedCommands();
 
385
        }
 
386
    }
 
387
}
 
388
 
 
389
void KUndo2Command::redoMergedCommands()
 
390
{
 
391
    if (!mergeCommandsVector().isEmpty()) {
 
392
 
 
393
        QVectorIterator<KUndo2Command*> it(mergeCommandsVector());
 
394
        it.toBack();
 
395
        while (it.hasPrevious()) {
 
396
            KUndo2Command* cmd = it.previous();
 
397
            cmd->redoMergedCommands();
 
398
        }
 
399
    }
 
400
    redo();
 
401
}
 
402
QVector<KUndo2Command*> KUndo2Command::mergeCommandsVector()
 
403
{
 
404
    return m_mergeCommandsVector;
 
405
}
 
406
bool KUndo2Command::isMerged()
 
407
{
 
408
    return !m_mergeCommandsVector.isEmpty();
 
409
}
 
410
 
 
411
KUndo2CommandExtraData* KUndo2Command::extraData() const
 
412
{
 
413
    return d->extraData.data();
 
414
}
 
415
 
 
416
void KUndo2Command::setExtraData(KUndo2CommandExtraData *data)
 
417
{
 
418
    d->extraData.reset(data);
 
419
}
 
420
 
 
421
 
 
422
#endif // QT_NO_UNDOCOMMAND
 
423
 
 
424
#ifndef QT_NO_UNDOSTACK
 
425
 
 
426
/*!
 
427
    \class KUndo2QStack
 
428
    \brief The KUndo2QStack class is a stack of KUndo2Command objects.
 
429
    \since 4.2
 
430
 
 
431
    For an overview of Qt's Undo Framework, see the
 
432
    \l{Overview of Qt's Undo Framework}{overview document}.
 
433
 
 
434
    An undo stack maintains a stack of commands that have been applied to a
 
435
    document.
 
436
 
 
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().
 
440
 
 
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().
 
446
 
 
447
    KUndo2QStack provides support for undo and redo actions, command
 
448
    compression, command macros, and supports the concept of a
 
449
    \e{clean state}.
 
450
 
 
451
    \section1 Undo and Redo Actions
 
452
 
 
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().
 
459
 
 
460
    \section1 Command Compression and Macros
 
461
 
 
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().
 
471
 
 
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
 
474
    of child commands.
 
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().
 
479
 
 
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
 
484
    to the user.
 
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.
 
488
 
 
489
    \section1 Clean State
 
490
 
 
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
 
498
    changes.
 
499
 
 
500
    \sa KUndo2Command, KUndo2View
 
501
*/
 
502
 
 
503
#ifndef QT_NO_ACTION
 
504
 
 
505
KUndo2Action::KUndo2Action(const QString &textTemplate, const QString &defaultText, QObject *parent)
 
506
    : QAction(parent)
 
507
{
 
508
    m_textTemplate = textTemplate;
 
509
    m_defaultText = defaultText;
 
510
 
 
511
}
 
512
 
 
513
void KUndo2Action::setPrefixedText(const QString &text)
 
514
{
 
515
    if (text.isEmpty())
 
516
        setText(m_defaultText);
 
517
    else
 
518
        setText(m_textTemplate.arg(text));
 
519
}
 
520
 
 
521
#endif // QT_NO_ACTION
 
522
 
 
523
/*! \internal
 
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.
 
526
*/
 
527
 
 
528
void KUndo2QStack::setIndex(int idx, bool clean)
 
529
{
 
530
    bool was_clean = m_index == m_clean_index;
 
531
    if (m_lastMergedIndex <= idx) {
 
532
        m_lastMergedSetCount = idx - m_lastMergedIndex;
 
533
 
 
534
    } else {
 
535
        m_lastMergedSetCount = 1;
 
536
        m_lastMergedIndex = idx-1;
 
537
    }
 
538
    if(idx == 0){
 
539
        m_lastMergedSetCount = 0;
 
540
        m_lastMergedIndex = 0;
 
541
    }
 
542
    if (idx != m_index) {
 
543
        m_index = idx;
 
544
        emit indexChanged(m_index);
 
545
        emit canUndoChanged(canUndo());
 
546
        emit undoTextChanged(undoText());
 
547
        emit canRedoChanged(canRedo());
 
548
        emit redoTextChanged(redoText());
 
549
    }
 
550
 
 
551
    if (clean)
 
552
        m_clean_index = m_index;
 
553
 
 
554
    bool is_clean = m_index == m_clean_index;
 
555
    if (is_clean != was_clean)
 
556
        emit cleanChanged(is_clean);
 
557
}
 
558
 
 
559
void KUndo2QStack::purgeRedoState()
 
560
{
 
561
    bool macro = !m_macro_stack.isEmpty();
 
562
    if (macro) return;
 
563
 
 
564
    bool redoStateChanged = false;
 
565
    bool cleanStateChanged = false;
 
566
 
 
567
    while (m_index < m_command_list.size()) {
 
568
        delete m_command_list.takeLast();
 
569
        redoStateChanged = true;
 
570
    }
 
571
 
 
572
    if (m_clean_index > m_index) {
 
573
        m_clean_index = -1; // we've deleted the clean state
 
574
        cleanStateChanged = true;
 
575
    }
 
576
 
 
577
    if (redoStateChanged) {
 
578
        emit canRedoChanged(canRedo());
 
579
        emit redoTextChanged(redoText());
 
580
    }
 
581
 
 
582
    if (cleanStateChanged) {
 
583
        emit cleanChanged(isClean());
 
584
    }
 
585
}
 
586
 
 
587
/*! \internal
 
588
    If the number of commands on the stack exceedes the undo limit, deletes commands from
 
589
    the bottom of the stack.
 
590
 
 
591
    Returns true if commands were deleted.
 
592
*/
 
593
 
 
594
bool KUndo2QStack::checkUndoLimit()
 
595
{
 
596
    if (m_undo_limit <= 0 || !m_macro_stack.isEmpty() || m_undo_limit >= m_command_list.count())
 
597
        return false;
 
598
 
 
599
    int del_count = m_command_list.count() - m_undo_limit;
 
600
 
 
601
    for (int i = 0; i < del_count; ++i)
 
602
        delete m_command_list.takeFirst();
 
603
 
 
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
 
608
        else
 
609
            m_clean_index -= del_count;
 
610
    }
 
611
    return true;
 
612
}
 
613
 
 
614
/*!
 
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.
 
618
 
 
619
    \sa push()
 
620
*/
 
621
 
 
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)
 
624
{
 
625
    setTimeT1(5);
 
626
    setTimeT2(1);
 
627
    setStrokesN(2);
 
628
#ifndef QT_NO_UNDOGROUP
 
629
    if (KUndo2Group *group = qobject_cast<KUndo2Group*>(parent))
 
630
        group->addStack(this);
 
631
#endif
 
632
}
 
633
 
 
634
/*!
 
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.
 
637
 
 
638
    \sa KUndo2QStack()
 
639
*/
 
640
 
 
641
KUndo2QStack::~KUndo2QStack()
 
642
{
 
643
#ifndef QT_NO_UNDOGROUP
 
644
    if (m_group != 0)
 
645
        m_group->removeStack(this);
 
646
#endif
 
647
    clear();
 
648
}
 
649
 
 
650
/*!
 
651
    Clears the command stack by deleting all commands on it, and returns the stack
 
652
    to the clean state.{
 
653
 
 
654
            }
 
655
 
 
656
    Commands are not undone or redone; the state of the edited object remains
 
657
    unchanged.
 
658
 
 
659
    This function is usually used when the contents of the document are
 
660
    abandoned.
 
661
 
 
662
    \sa KUndo2QStack()
 
663
*/
 
664
 
 
665
void KUndo2QStack::clear()
 
666
{
 
667
    if (m_command_list.isEmpty())
 
668
        return;
 
669
 
 
670
    bool was_clean = isClean();
 
671
 
 
672
    m_macro_stack.clear();
 
673
    qDeleteAll(m_command_list);
 
674
    m_command_list.clear();
 
675
 
 
676
    m_index = 0;
 
677
    m_clean_index = 0;
 
678
 
 
679
    emit indexChanged(0);
 
680
    emit canUndoChanged(false);
 
681
    emit undoTextChanged(QString());
 
682
    emit canRedoChanged(false);
 
683
    emit redoTextChanged(QString());
 
684
 
 
685
    if (!was_clean)
 
686
        emit cleanChanged(true);
 
687
}
 
688
 
 
689
/*!
 
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.
 
692
 
 
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
 
697
    is returned.
 
698
 
 
699
    In all other cases \a cmd is simply pushed on the stack and true is returned.
 
700
 
 
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.
 
704
 
 
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
 
708
    state.
 
709
 
 
710
    \sa KUndo2Command::id() KUndo2Command::mergeWith()
 
711
*/
 
712
 
 
713
bool KUndo2QStack::push(KUndo2Command *cmd)
 
714
{
 
715
    cmd->redoMergedCommands();
 
716
    cmd->setEndTime();
 
717
 
 
718
    bool macro = !m_macro_stack.isEmpty();
 
719
 
 
720
    KUndo2Command *cur = 0;
 
721
    if (macro) {
 
722
        KUndo2Command *macro_cmd = m_macro_stack.last();
 
723
        if (!macro_cmd->d->child_list.isEmpty())
 
724
            cur = macro_cmd->d->child_list.last();
 
725
    } else {
 
726
        if (m_index > 0)
 
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
 
732
    }
 
733
 
 
734
    bool try_merge = cur != 0
 
735
                     && cur->id() != -1
 
736
                     && cur->id() == cmd->id()
 
737
                     && (macro || m_index != m_clean_index);
 
738
 
 
739
    /*!
 
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).
 
746
     *
 
747
     *@TODO : Currently it is not able to merge two merged commands together.
 
748
    */
 
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++;
 
753
        } else {
 
754
            m_lastMergedSetCount = 0;
 
755
            m_lastMergedIndex = m_index-1;
 
756
        }
 
757
        if (lastcmd->timedId() == -1){
 
758
            m_lastMergedSetCount = 0;
 
759
            m_lastMergedIndex = m_index;
 
760
        }
 
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);
 
766
                }
 
767
                m_lastMergedSetCount--;
 
768
                m_lastMergedIndex = m_command_list.indexOf(toMerge);       
 
769
            }
 
770
 
 
771
        }
 
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);
 
776
                it.toBack();
 
777
                m_lastMergedSetCount = 1;
 
778
 
 
779
                while (it.hasPrevious()) {
 
780
                    KUndo2Command* curr = it.previous();
 
781
                    KUndo2Command* lastCmdInCurrent = curr;
 
782
 
 
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);
 
788
                                }
 
789
                             }
 
790
                        } else {
 
791
                            lastcmd = curr; //end of a merge set
 
792
                        }
 
793
                    } else {
 
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);
 
798
                                }
 
799
                            }
 
800
                        } else {
 
801
                            lastcmd = curr; //end of a merge set
 
802
                        }
 
803
                    }
 
804
                }
 
805
                m_lastMergedIndex = m_command_list.size()-1;
 
806
            }
 
807
        }
 
808
        m_index = m_command_list.size();
 
809
    }   
 
810
    if (try_merge && cur->mergeWith(cmd)) {
 
811
        delete cmd;
 
812
        cmd = 0;
 
813
        if (!macro) {
 
814
            emit indexChanged(m_index);
 
815
            emit canUndoChanged(canUndo());
 
816
            emit undoTextChanged(undoText());
 
817
            emit canRedoChanged(canRedo());
 
818
            emit redoTextChanged(redoText());
 
819
        }
 
820
    } else {
 
821
        if (macro) {
 
822
            m_macro_stack.last()->d->child_list.append(cmd);
 
823
        } else {
 
824
            m_command_list.append(cmd);
 
825
            if(checkUndoLimit())
 
826
            {
 
827
                m_lastMergedIndex = m_index - m_strokesN;
 
828
            }
 
829
            setIndex(m_index + 1, false);
 
830
        }
 
831
    }
 
832
    return cmd;
 
833
}
 
834
 
 
835
/*!
 
836
    Marks the stack as clean and emits cleanChanged() if the stack was
 
837
    not already clean.
 
838
 
 
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.
 
842
 
 
843
    \sa isClean(), cleanIndex()
 
844
*/
 
845
 
 
846
void KUndo2QStack::setClean()
 
847
{
 
848
    if (!m_macro_stack.isEmpty()) {
 
849
        qWarning("KUndo2QStack::setClean(): cannot set clean in the middle of a macro");
 
850
        return;
 
851
    }
 
852
 
 
853
    setIndex(m_index, true);
 
854
}
 
855
 
 
856
/*!
 
857
    If the stack is in the clean state, returns true; otherwise returns false.
 
858
 
 
859
    \sa setClean() cleanIndex()
 
860
*/
 
861
 
 
862
bool KUndo2QStack::isClean() const
 
863
{
 
864
    if (!m_macro_stack.isEmpty())
 
865
        return false;
 
866
    return m_clean_index == m_index;
 
867
}
 
868
 
 
869
/*!
 
870
    Returns the clean index. This is the index at which setClean() was called.
 
871
 
 
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.
 
876
 
 
877
    \sa isClean() setClean()
 
878
*/
 
879
 
 
880
int KUndo2QStack::cleanIndex() const
 
881
{
 
882
    return m_clean_index;
 
883
}
 
884
 
 
885
/*!
 
886
    Undoes the command below the current command by calling KUndo2Command::undo().
 
887
    Decrements the current command index.
 
888
 
 
889
    If the stack is empty, or if the bottom command on the stack has already been
 
890
    undone, this function does nothing.
 
891
 
 
892
    \sa redo() index()
 
893
*/
 
894
 
 
895
void KUndo2QStack::undo()
 
896
{
 
897
    if (m_index == 0)
 
898
        return;
 
899
 
 
900
    if (!m_macro_stack.isEmpty()) {
 
901
        qWarning("KUndo2QStack::undo(): cannot undo in the middle of a macro");
 
902
        return;
 
903
    }
 
904
 
 
905
    int idx = m_index - 1;
 
906
    m_command_list.at(idx)->undoMergedCommands();
 
907
    setIndex(idx, false);
 
908
}
 
909
 
 
910
/*!
 
911
    Redoes the current command by calling KUndo2Command::redo(). Increments the current
 
912
    command index.
 
913
 
 
914
    If the stack is empty, or if the top command on the stack has already been
 
915
    redone, this function does nothing.
 
916
 
 
917
    \sa undo() index()
 
918
*/
 
919
 
 
920
void KUndo2QStack::redo()
 
921
{
 
922
    if (m_index == m_command_list.size())
 
923
        return;
 
924
 
 
925
    if (!m_macro_stack.isEmpty()) {
 
926
        qWarning("KUndo2QStack::redo(): cannot redo in the middle of a macro");
 
927
        return;
 
928
    }
 
929
 
 
930
    m_command_list.at(m_index)->redoMergedCommands();
 
931
    setIndex(m_index + 1, false);
 
932
}
 
933
 
 
934
/*!
 
935
    Returns the number of commands on the stack. Macro commands are counted as
 
936
    one command.
 
937
 
 
938
    \sa index() setIndex() command()
 
939
*/
 
940
 
 
941
int KUndo2QStack::count() const
 
942
{
 
943
    return m_command_list.size();
 
944
}
 
945
 
 
946
/*!
 
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.
 
950
 
 
951
    \sa undo() redo() count()
 
952
*/
 
953
 
 
954
int KUndo2QStack::index() const
 
955
{
 
956
    return m_index;
 
957
}
 
958
 
 
959
/*!
 
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.
 
963
 
 
964
    \sa index() count() undo() redo()
 
965
*/
 
966
 
 
967
void KUndo2QStack::setIndex(int idx)
 
968
{
 
969
    if (!m_macro_stack.isEmpty()) {
 
970
        qWarning("KUndo2QStack::setIndex(): cannot set index in the middle of a macro");
 
971
        return;
 
972
    }
 
973
 
 
974
    if (idx < 0)
 
975
        idx = 0;
 
976
    else if (idx > m_command_list.size())
 
977
        idx = m_command_list.size();
 
978
 
 
979
    int i = m_index;
 
980
    while (i < idx) {
 
981
        m_command_list.at(i++)->redoMergedCommands();
 
982
        notifySetIndexChangedOneCommand();
 
983
    }
 
984
    while (i > idx) {
 
985
        m_command_list.at(--i)->undoMergedCommands();
 
986
        notifySetIndexChangedOneCommand();
 
987
    }
 
988
 
 
989
    setIndex(idx, false);
 
990
}
 
991
 
 
992
 
 
993
/**
 
994
 * Called by setIndex after every command execution.  It is needed by
 
995
 * Krita to insert barriers between different kind of commands
 
996
 */
 
997
void KUndo2QStack::notifySetIndexChangedOneCommand()
 
998
{
 
999
}
 
1000
 
 
1001
/*!
 
1002
    Returns true if there is a command available for undo; otherwise returns false.
 
1003
 
 
1004
    This function returns false if the stack is empty, or if the bottom command
 
1005
    on the stack has already been undone.
 
1006
 
 
1007
    Synonymous with index() == 0.
 
1008
 
 
1009
    \sa index() canRedo()
 
1010
*/
 
1011
 
 
1012
bool KUndo2QStack::canUndo() const
 
1013
{
 
1014
    if (!m_macro_stack.isEmpty())
 
1015
        return false;
 
1016
    return m_index > 0;
 
1017
}
 
1018
 
 
1019
/*!
 
1020
    Returns true if there is a command available for redo; otherwise returns false.
 
1021
 
 
1022
    This function returns false if the stack is empty or if the top command
 
1023
    on the stack has already been redone.
 
1024
 
 
1025
    Synonymous with index() == count().
 
1026
 
 
1027
    \sa index() canUndo()
 
1028
*/
 
1029
 
 
1030
bool KUndo2QStack::canRedo() const
 
1031
{
 
1032
    if (!m_macro_stack.isEmpty())
 
1033
        return false;
 
1034
    return m_index < m_command_list.size();
 
1035
}
 
1036
 
 
1037
/*!
 
1038
    Returns the text of the command which will be undone in the next call to undo().
 
1039
 
 
1040
    \sa KUndo2Command::text() redoActionText() undoItemText()
 
1041
*/
 
1042
 
 
1043
QString KUndo2QStack::undoText() const
 
1044
{
 
1045
    if (!m_macro_stack.isEmpty())
 
1046
        return QString();
 
1047
    if (m_index > 0 && m_command_list.at(m_index-1)!=NULL)
 
1048
 
 
1049
        return m_command_list.at(m_index - 1)->actionText();
 
1050
    return QString();
 
1051
}
 
1052
 
 
1053
/*!
 
1054
    Returns the text of the command which will be redone in the next call to redo().
 
1055
 
 
1056
    \sa KUndo2Command::text() undoActionText() redoItemText()
 
1057
*/
 
1058
 
 
1059
QString KUndo2QStack::redoText() const
 
1060
{
 
1061
    if (!m_macro_stack.isEmpty())
 
1062
        return QString();
 
1063
    if (m_index < m_command_list.size())
 
1064
        return m_command_list.at(m_index)->actionText();
 
1065
    return QString();
 
1066
}
 
1067
 
 
1068
#ifndef QT_NO_ACTION
 
1069
 
 
1070
/*!
 
1071
    Creates an undo QAction object with the given \a parent.
 
1072
 
 
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.
 
1077
 
 
1078
    If \a prefix is empty, the default prefix "Undo" is used.
 
1079
 
 
1080
    \sa createRedoAction(), canUndo(), KUndo2Command::text()
 
1081
*/
 
1082
 
 
1083
QAction *KUndo2QStack::createUndoAction(QObject *parent) const
 
1084
{
 
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()));
 
1093
    return result;
 
1094
}
 
1095
 
 
1096
/*!
 
1097
    Creates an redo QAction object with the given \a parent.
 
1098
 
 
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.
 
1103
 
 
1104
    If \a prefix is empty, the default prefix "Redo" is used.
 
1105
 
 
1106
    \sa createUndoAction(), canRedo(), KUndo2Command::text()
 
1107
*/
 
1108
 
 
1109
QAction *KUndo2QStack::createRedoAction(QObject *parent) const
 
1110
{
 
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()));
 
1119
    return result;
 
1120
}
 
1121
 
 
1122
#endif // QT_NO_ACTION
 
1123
 
 
1124
/*!
 
1125
    Begins composition of a macro command with the given \a text description.
 
1126
 
 
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.
 
1130
 
 
1131
    Calls to beginMacro() and endMacro() may be nested, but every call to
 
1132
    beginMacro() must have a matching call to endMacro().
 
1133
 
 
1134
    While a macro is composed, the stack is disabled. This means that:
 
1135
    \list
 
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.
 
1140
    \endlist
 
1141
 
 
1142
    The stack becomes enabled and appropriate signals are emitted when endMacro()
 
1143
    is called for the outermost macro.
 
1144
 
 
1145
    \snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 4
 
1146
 
 
1147
    This code is equivalent to:
 
1148
 
 
1149
    \snippet doc/src/snippets/code/src_gui_util_qundostack.cpp 5
 
1150
 
 
1151
    \sa endMacro()
 
1152
*/
 
1153
 
 
1154
void KUndo2QStack::beginMacro(const KUndo2MagicString &text)
 
1155
{
 
1156
    KUndo2Command *cmd = new KUndo2Command();
 
1157
    cmd->setText(text);
 
1158
 
 
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);
 
1165
    } else {
 
1166
        m_macro_stack.last()->d->child_list.append(cmd);
 
1167
    }
 
1168
    m_macro_stack.append(cmd);
 
1169
 
 
1170
    if (m_macro_stack.count() == 1) {
 
1171
        emit canUndoChanged(false);
 
1172
        emit undoTextChanged(QString());
 
1173
        emit canRedoChanged(false);
 
1174
        emit redoTextChanged(QString());
 
1175
    }
 
1176
}
 
1177
 
 
1178
/*!
 
1179
    Ends composition of a macro command.
 
1180
 
 
1181
    If this is the outermost macro in a set nested macros, this function emits
 
1182
    indexChanged() once for the entire macro command.
 
1183
 
 
1184
    \sa beginMacro()
 
1185
*/
 
1186
 
 
1187
void KUndo2QStack::endMacro()
 
1188
{
 
1189
    if (m_macro_stack.isEmpty()) {
 
1190
        qWarning("KUndo2QStack::endMacro(): no matching beginMacro()");
 
1191
        return;
 
1192
    }
 
1193
 
 
1194
    m_macro_stack.removeLast();
 
1195
 
 
1196
    if (m_macro_stack.isEmpty()) {
 
1197
        checkUndoLimit();
 
1198
        setIndex(m_index + 1, false);
 
1199
    }
 
1200
}
 
1201
 
 
1202
/*!
 
1203
  \since 4.4
 
1204
 
 
1205
  Returns a const pointer to the command at \a index.
 
1206
 
 
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.
 
1211
 
 
1212
  \sa KUndo2Command::child()
 
1213
*/
 
1214
const KUndo2Command *KUndo2QStack::command(int index) const
 
1215
{
 
1216
    if (index < 0 || index >= m_command_list.count())
 
1217
        return 0;
 
1218
    return m_command_list.at(index);
 
1219
}
 
1220
 
 
1221
/*!
 
1222
    Returns the text of the command at index \a idx.
 
1223
 
 
1224
    \sa beginMacro()
 
1225
*/
 
1226
 
 
1227
QString KUndo2QStack::text(int idx) const
 
1228
{
 
1229
    if (idx < 0 || idx >= m_command_list.size())
 
1230
        return QString();
 
1231
    return m_command_list.at(idx)->text().toString();
 
1232
}
 
1233
 
 
1234
/*!
 
1235
    \property KUndo2QStack::undoLimit
 
1236
    \brief the maximum number of commands on this stack.
 
1237
    \since 4.3
 
1238
 
 
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
 
1242
    limit.
 
1243
 
 
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.
 
1247
*/
 
1248
 
 
1249
void KUndo2QStack::setUndoLimit(int limit)
 
1250
{
 
1251
    if (!m_command_list.isEmpty()) {
 
1252
        qWarning("KUndo2QStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
 
1253
        return;
 
1254
    }
 
1255
 
 
1256
    if (limit == m_undo_limit)
 
1257
        return;
 
1258
    m_undo_limit = limit;
 
1259
    checkUndoLimit();
 
1260
}
 
1261
 
 
1262
int KUndo2QStack::undoLimit() const
 
1263
{
 
1264
    return m_undo_limit;
 
1265
}
 
1266
 
 
1267
/*!
 
1268
    \property KUndo2QStack::active
 
1269
    \brief the active status of this stack.
 
1270
 
 
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.
 
1277
 
 
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.
 
1280
 
 
1281
    \sa KUndo2Group
 
1282
*/
 
1283
 
 
1284
void KUndo2QStack::setActive(bool active)
 
1285
{
 
1286
#ifdef QT_NO_UNDOGROUP
 
1287
    Q_UNUSED(active);
 
1288
#else
 
1289
    if (m_group != 0) {
 
1290
        if (active)
 
1291
            m_group->setActiveStack(this);
 
1292
        else if (m_group->activeStack() == this)
 
1293
            m_group->setActiveStack(0);
 
1294
    }
 
1295
#endif
 
1296
}
 
1297
 
 
1298
bool KUndo2QStack::isActive() const
 
1299
{
 
1300
#ifdef QT_NO_UNDOGROUP
 
1301
    return true;
 
1302
#else
 
1303
    return m_group == 0 || m_group->activeStack() == this;
 
1304
#endif
 
1305
}
 
1306
void KUndo2QStack::setUseCumulativeUndoRedo(bool value)
 
1307
{
 
1308
    m_useCumulativeUndoRedo = value;
 
1309
}
 
1310
 
 
1311
bool KUndo2QStack::useCumulativeUndoRedo()
 
1312
{
 
1313
    return m_useCumulativeUndoRedo;
 
1314
}
 
1315
void KUndo2QStack::setTimeT1(double value)
 
1316
{
 
1317
    m_timeT1 = value;
 
1318
}
 
1319
 
 
1320
double KUndo2QStack::timeT1()
 
1321
{
 
1322
    return m_timeT1;
 
1323
}
 
1324
 
 
1325
void KUndo2QStack::setTimeT2(double value)
 
1326
{
 
1327
    m_timeT2 = value;
 
1328
}
 
1329
 
 
1330
double KUndo2QStack::timeT2()
 
1331
{
 
1332
    return m_timeT2;
 
1333
}
 
1334
int KUndo2QStack::strokesN()
 
1335
{
 
1336
    return m_strokesN;
 
1337
}
 
1338
void KUndo2QStack::setStrokesN(int value)
 
1339
{
 
1340
    m_strokesN  = value;
 
1341
}
 
1342
 
 
1343
 
 
1344
 
 
1345
QAction* KUndo2Stack::createRedoAction(KActionCollection* actionCollection, const QString& actionName)
 
1346
{
 
1347
    QAction* action = KUndo2QStack::createRedoAction(actionCollection);
 
1348
 
 
1349
    if (actionName.isEmpty()) {
 
1350
        action->setObjectName(KStandardAction::name(KStandardAction::Redo));
 
1351
    } else {
 
1352
        action->setObjectName(actionName);
 
1353
    }
 
1354
 
 
1355
    action->setIcon(KexiIcon("edit-redo"));
 
1356
    action->setIconText(i18n("Redo"));
 
1357
    action->setShortcuts(KStandardShortcut::redo());
 
1358
 
 
1359
    actionCollection->addAction(action->objectName(), action);
 
1360
 
 
1361
    return action;
 
1362
}
 
1363
 
 
1364
QAction* KUndo2Stack::createUndoAction(KActionCollection* actionCollection, const QString& actionName)
 
1365
{
 
1366
    QAction* action = KUndo2QStack::createUndoAction(actionCollection);
 
1367
 
 
1368
    if (actionName.isEmpty()) {
 
1369
        action->setObjectName(KStandardAction::name(KStandardAction::Undo));
 
1370
    } else {
 
1371
        action->setObjectName(actionName);
 
1372
    }
 
1373
 
 
1374
    action->setIcon(koIcon("edit-undo"));
 
1375
    action->setIconText(i18n("Undo"));
 
1376
    action->setShortcuts(KStandardShortcut::undo());
 
1377
 
 
1378
    actionCollection->addAction(action->objectName(), action);
 
1379
 
 
1380
    return action;
 
1381
}
 
1382
 
 
1383
/*!
 
1384
    \fn void KUndo2QStack::indexChanged(int idx)
 
1385
 
 
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.
 
1390
 
 
1391
    \a idx specifies the index of the current command, ie. the command which will be
 
1392
    executed on the next call to redo().
 
1393
 
 
1394
    \sa index() setIndex()
 
1395
*/
 
1396
 
 
1397
/*!
 
1398
    \fn void KUndo2QStack::cleanChanged(bool clean)
 
1399
 
 
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.
 
1403
 
 
1404
    \sa isClean() setClean()
 
1405
*/
 
1406
 
 
1407
/*!
 
1408
    \fn void KUndo2QStack::undoTextChanged(const QString &undoText)
 
1409
 
 
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.
 
1413
*/
 
1414
 
 
1415
/*!
 
1416
    \fn void KUndo2QStack::canUndoChanged(bool canUndo)
 
1417
 
 
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.
 
1421
*/
 
1422
 
 
1423
/*!
 
1424
    \fn void KUndo2QStack::redoTextChanged(const QString &redoText)
 
1425
 
 
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.
 
1429
*/
 
1430
 
 
1431
/*!
 
1432
    \fn void KUndo2QStack::canRedoChanged(bool canRedo)
 
1433
 
 
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.
 
1437
*/
 
1438
 
 
1439
KUndo2Stack::KUndo2Stack(QObject *parent):
 
1440
    KUndo2QStack(parent)
 
1441
{
 
1442
}
 
1443
 
 
1444
#endif // QT_NO_UNDOSTACK