1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "qundogroup.h"
43
#include "qundostack.h"
44
#include "qundostack_p.h"
46
#ifndef QT_NO_UNDOGROUP
50
class QUndoGroupPrivate : public QObjectPrivate
52
Q_DECLARE_PUBLIC(QUndoGroup)
54
QUndoGroupPrivate() : active(0) {}
57
QList<QUndoStack*> stack_list;
62
\brief The QUndoGroup class is a group of QUndoStack objects.
66
For an overview of the Qt's undo framework, see the
67
\l{qundo.html}{overview}.
69
An application often has multiple undo stacks, one for each opened document. At the
70
same time, an application usually has one undo action and one redo action, which
71
triggers undo or redo in the active document.
73
QUndoGroup is a group of QUndoStack objects, one of which may be active. It has
74
an undo() and redo() slot, which calls QUndoStack::undo() and QUndoStack::redo()
75
for the active stack. It also has the functions createUndoAction() and createRedoAction().
76
The actions returned by these functions behave in the same way as those returned by
77
QUndoStack::createUndoAction() and QUndoStack::createRedoAction() of the active
80
Stacks are added to a group with addStack() and removed with removeStack(). A stack
81
is implicitly added to a group when it is created with the group as its parent
84
It is the programmer's responsibility to specify which stack is active by
85
calling QUndoStack::setActive(), usually when the associated document window receives focus.
86
The active stack may also be set with setActiveStack(), and is returned by activeStack().
88
When a stack is added to a group using addStack(), the group does not take ownership
89
of the stack. This means the stack has to be deleted separately from the group. When
90
a stack is deleted, it is automatically removed from a group. A stack may belong to
91
only one group. Adding it to another group will cause it to be removed from the previous
94
A QUndoGroup is also useful in conjunction with QUndoView. If a QUndoView is
95
set to watch a group using QUndoView::setGroup(), it will update itself to display
100
Creates an empty QUndoGroup object with parent \a parent.
105
QUndoGroup::QUndoGroup(QObject *parent)
106
: QObject(*new QUndoGroupPrivate(), parent)
111
Destroys the QUndoGroup.
113
QUndoGroup::~QUndoGroup()
115
// Ensure all QUndoStacks no longer refer to this group.
117
QList<QUndoStack *>::iterator it = d->stack_list.begin();
118
QList<QUndoStack *>::iterator end = d->stack_list.end();
120
(*it)->d_func()->group = 0;
126
Adds \a stack to this group. The group does not take ownership of the stack. Another
127
way of adding a stack to a group is by specifying the group as the stack's parent
128
QObject in QUndoStack::QUndoStack(). In this case, the stack is deleted when the
129
group is deleted, in the usual manner of QObjects.
131
\sa removeStack(), stacks(), QUndoStack::QUndoStack()
134
void QUndoGroup::addStack(QUndoStack *stack)
138
if (d->stack_list.contains(stack))
140
d->stack_list.append(stack);
142
if (QUndoGroup *other = stack->d_func()->group)
143
other->removeStack(stack);
144
stack->d_func()->group = this;
148
Removes \a stack from this group. If the stack was the active stack in the group,
149
the active stack becomes 0.
151
\sa addStack(), stacks(), QUndoStack::~QUndoStack()
154
void QUndoGroup::removeStack(QUndoStack *stack)
158
if (d->stack_list.removeAll(stack) == 0)
160
if (stack == d->active)
162
stack->d_func()->group = 0;
166
Returns a list of stacks in this group.
168
\sa addStack(), removeStack()
171
QList<QUndoStack*> QUndoGroup::stacks() const
173
Q_D(const QUndoGroup);
174
return d->stack_list;
178
Sets the active stack of this group to \a stack.
180
If the stack is not a member of this group, this function does nothing.
182
Synonymous with calling QUndoStack::setActive() on \a stack.
184
The actions returned by createUndoAction() and createRedoAction() will now behave
185
in the same way as those returned by \a stack's QUndoStack::createUndoAction()
186
and QUndoStack::createRedoAction().
188
\sa QUndoStack::setActive(), activeStack()
191
void QUndoGroup::setActiveStack(QUndoStack *stack)
194
if (d->active == stack)
197
if (d->active != 0) {
198
disconnect(d->active, SIGNAL(canUndoChanged(bool)),
199
this, SIGNAL(canUndoChanged(bool)));
200
disconnect(d->active, SIGNAL(undoTextChanged(QString)),
201
this, SIGNAL(undoTextChanged(QString)));
202
disconnect(d->active, SIGNAL(canRedoChanged(bool)),
203
this, SIGNAL(canRedoChanged(bool)));
204
disconnect(d->active, SIGNAL(redoTextChanged(QString)),
205
this, SIGNAL(redoTextChanged(QString)));
206
disconnect(d->active, SIGNAL(indexChanged(int)),
207
this, SIGNAL(indexChanged(int)));
208
disconnect(d->active, SIGNAL(cleanChanged(bool)),
209
this, SIGNAL(cleanChanged(bool)));
214
if (d->active == 0) {
215
emit canUndoChanged(false);
216
emit undoTextChanged(QString());
217
emit canRedoChanged(false);
218
emit redoTextChanged(QString());
219
emit cleanChanged(true);
220
emit indexChanged(0);
222
connect(d->active, SIGNAL(canUndoChanged(bool)),
223
this, SIGNAL(canUndoChanged(bool)));
224
connect(d->active, SIGNAL(undoTextChanged(QString)),
225
this, SIGNAL(undoTextChanged(QString)));
226
connect(d->active, SIGNAL(canRedoChanged(bool)),
227
this, SIGNAL(canRedoChanged(bool)));
228
connect(d->active, SIGNAL(redoTextChanged(QString)),
229
this, SIGNAL(redoTextChanged(QString)));
230
connect(d->active, SIGNAL(indexChanged(int)),
231
this, SIGNAL(indexChanged(int)));
232
connect(d->active, SIGNAL(cleanChanged(bool)),
233
this, SIGNAL(cleanChanged(bool)));
234
emit canUndoChanged(d->active->canUndo());
235
emit undoTextChanged(d->active->undoText());
236
emit canRedoChanged(d->active->canRedo());
237
emit redoTextChanged(d->active->redoText());
238
emit cleanChanged(d->active->isClean());
239
emit indexChanged(d->active->index());
242
emit activeStackChanged(d->active);
246
Returns the active stack of this group.
248
If none of the stacks are active, or if the group is empty, this function
251
\sa setActiveStack(), QUndoStack::setActive()
254
QUndoStack *QUndoGroup::activeStack() const
256
Q_D(const QUndoGroup);
261
Calls QUndoStack::undo() on the active stack.
263
If none of the stacks are active, or if the group is empty, this function
266
\sa redo(), canUndo(), setActiveStack()
269
void QUndoGroup::undo()
277
Calls QUndoStack::redo() on the active stack.
279
If none of the stacks are active, or if the group is empty, this function
282
\sa undo(), canRedo(), setActiveStack()
286
void QUndoGroup::redo()
294
Returns the value of the active stack's QUndoStack::canUndo().
296
If none of the stacks are active, or if the group is empty, this function
299
\sa canRedo(), setActiveStack()
302
bool QUndoGroup::canUndo() const
304
Q_D(const QUndoGroup);
305
return d->active != 0 && d->active->canUndo();
309
Returns the value of the active stack's QUndoStack::canRedo().
311
If none of the stacks are active, or if the group is empty, this function
314
\sa canUndo(), setActiveStack()
317
bool QUndoGroup::canRedo() const
319
Q_D(const QUndoGroup);
320
return d->active != 0 && d->active->canRedo();
324
Returns the value of the active stack's QUndoStack::undoText().
326
If none of the stacks are active, or if the group is empty, this function
327
returns an empty string.
329
\sa redoText(), setActiveStack()
332
QString QUndoGroup::undoText() const
334
Q_D(const QUndoGroup);
335
return d->active == 0 ? QString() : d->active->undoText();
339
Returns the value of the active stack's QUndoStack::redoText().
341
If none of the stacks are active, or if the group is empty, this function
342
returns an empty string.
344
\sa undoText(), setActiveStack()
347
QString QUndoGroup::redoText() const
349
Q_D(const QUndoGroup);
350
return d->active == 0 ? QString() : d->active->redoText();
354
Returns the value of the active stack's QUndoStack::isClean().
356
If none of the stacks are active, or if the group is empty, this function
362
bool QUndoGroup::isClean() const
364
Q_D(const QUndoGroup);
365
return d->active == 0 || d->active->isClean();
371
Creates an undo QAction object with parent \a parent.
373
Triggering this action will cause a call to QUndoStack::undo() on the active stack.
374
The text of this action will always be the text of the command which will be undone
375
in the next call to undo(), prefixed by \a prefix. If there is no command available
376
for undo, if the group is empty or if none of the stacks are active, this action will
379
If \a prefix is empty, the default template "Undo %1" is used instead of prefix.
380
Before Qt 4.8, the prefix "Undo" was used by default.
382
\sa createRedoAction(), canUndo(), QUndoCommand::text()
385
QAction *QUndoGroup::createUndoAction(QObject *parent, const QString &prefix) const
387
QUndoAction *result = new QUndoAction(prefix, parent);
388
if (prefix.isEmpty())
389
result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
391
result->setEnabled(canUndo());
392
result->setPrefixedText(undoText());
393
connect(this, SIGNAL(canUndoChanged(bool)),
394
result, SLOT(setEnabled(bool)));
395
connect(this, SIGNAL(undoTextChanged(QString)),
396
result, SLOT(setPrefixedText(QString)));
397
connect(result, SIGNAL(triggered()), this, SLOT(undo()));
402
Creates an redo QAction object with parent \a parent.
404
Triggering this action will cause a call to QUndoStack::redo() on the active stack.
405
The text of this action will always be the text of the command which will be redone
406
in the next call to redo(), prefixed by \a prefix. If there is no command available
407
for redo, if the group is empty or if none of the stacks are active, this action will
410
If \a prefix is empty, the default template "Redo %1" is used instead of prefix.
411
Before Qt 4.8, the prefix "Redo" was used by default.
413
\sa createUndoAction(), canRedo(), QUndoCommand::text()
416
QAction *QUndoGroup::createRedoAction(QObject *parent, const QString &prefix) const
418
QUndoAction *result = new QUndoAction(prefix, parent);
419
if (prefix.isEmpty())
420
result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
422
result->setEnabled(canRedo());
423
result->setPrefixedText(redoText());
424
connect(this, SIGNAL(canRedoChanged(bool)),
425
result, SLOT(setEnabled(bool)));
426
connect(this, SIGNAL(redoTextChanged(QString)),
427
result, SLOT(setPrefixedText(QString)));
428
connect(result, SIGNAL(triggered()), this, SLOT(redo()));
432
#endif // QT_NO_ACTION
434
/*! \fn void QUndoGroup::activeStackChanged(QUndoStack *stack)
436
This signal is emitted whenever the active stack of the group changes. This can happen
437
when setActiveStack() or QUndoStack::setActive() is called, or when the active stack
438
is removed form the group. \a stack is the new active stack. If no stack is active,
441
\sa setActiveStack(), QUndoStack::setActive()
444
/*! \fn void QUndoGroup::indexChanged(int idx)
446
This signal is emitted whenever the active stack emits QUndoStack::indexChanged()
447
or the active stack changes.
449
\a idx is the new current index, or 0 if the active stack is 0.
451
\sa QUndoStack::indexChanged(), setActiveStack()
454
/*! \fn void QUndoGroup::cleanChanged(bool clean)
456
This signal is emitted whenever the active stack emits QUndoStack::cleanChanged()
457
or the active stack changes.
459
\a clean is the new state, or true if the active stack is 0.
461
\sa QUndoStack::cleanChanged(), setActiveStack()
464
/*! \fn void QUndoGroup::canUndoChanged(bool canUndo)
466
This signal is emitted whenever the active stack emits QUndoStack::canUndoChanged()
467
or the active stack changes.
469
\a canUndo is the new state, or false if the active stack is 0.
471
\sa QUndoStack::canUndoChanged(), setActiveStack()
474
/*! \fn void QUndoGroup::canRedoChanged(bool canRedo)
476
This signal is emitted whenever the active stack emits QUndoStack::canRedoChanged()
477
or the active stack changes.
479
\a canRedo is the new state, or false if the active stack is 0.
481
\sa QUndoStack::canRedoChanged(), setActiveStack()
484
/*! \fn void QUndoGroup::undoTextChanged(const QString &undoText)
486
This signal is emitted whenever the active stack emits QUndoStack::undoTextChanged()
487
or the active stack changes.
489
\a undoText is the new state, or an empty string if the active stack is 0.
491
\sa QUndoStack::undoTextChanged(), setActiveStack()
494
/*! \fn void QUndoGroup::redoTextChanged(const QString &redoText)
496
This signal is emitted whenever the active stack emits QUndoStack::redoTextChanged()
497
or the active stack changes.
499
\a redoText is the new state, or an empty string if the active stack is 0.
501
\sa QUndoStack::redoTextChanged(), setActiveStack()
506
#endif // QT_NO_UNDOGROUP