Qt 4.8
qundogroup.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qundogroup.h"
43 #include "qundostack.h"
44 #include "qundostack_p.h"
45 
46 #ifndef QT_NO_UNDOGROUP
47 
49 
51 {
53 public:
55 
58 };
59 
107  : QObject(*new QUndoGroupPrivate(), parent)
108 {
109 }
110 
115 {
116  // Ensure all QUndoStacks no longer refer to this group.
117  Q_D(QUndoGroup);
118  QList<QUndoStack *>::iterator it = d->stack_list.begin();
119  QList<QUndoStack *>::iterator end = d->stack_list.end();
120  while (it != end) {
121  (*it)->d_func()->group = 0;
122  ++it;
123  }
124 }
125 
136 {
137  Q_D(QUndoGroup);
138 
139  if (d->stack_list.contains(stack))
140  return;
141  d->stack_list.append(stack);
142 
143  if (QUndoGroup *other = stack->d_func()->group)
144  other->removeStack(stack);
145  stack->d_func()->group = this;
146 }
147 
156 {
157  Q_D(QUndoGroup);
158 
159  if (d->stack_list.removeAll(stack) == 0)
160  return;
161  if (stack == d->active)
162  setActiveStack(0);
163  stack->d_func()->group = 0;
164 }
165 
173 {
174  Q_D(const QUndoGroup);
175  return d->stack_list;
176 }
177 
193 {
194  Q_D(QUndoGroup);
195  if (d->active == stack)
196  return;
197 
198  if (d->active != 0) {
199  disconnect(d->active, SIGNAL(canUndoChanged(bool)),
200  this, SIGNAL(canUndoChanged(bool)));
202  this, SIGNAL(undoTextChanged(QString)));
203  disconnect(d->active, SIGNAL(canRedoChanged(bool)),
204  this, SIGNAL(canRedoChanged(bool)));
206  this, SIGNAL(redoTextChanged(QString)));
207  disconnect(d->active, SIGNAL(indexChanged(int)),
208  this, SIGNAL(indexChanged(int)));
209  disconnect(d->active, SIGNAL(cleanChanged(bool)),
210  this, SIGNAL(cleanChanged(bool)));
211  }
212 
213  d->active = stack;
214 
215  if (d->active == 0) {
216  emit canUndoChanged(false);
218  emit canRedoChanged(false);
220  emit cleanChanged(true);
221  emit indexChanged(0);
222  } else {
223  connect(d->active, SIGNAL(canUndoChanged(bool)),
224  this, SIGNAL(canUndoChanged(bool)));
226  this, SIGNAL(undoTextChanged(QString)));
227  connect(d->active, SIGNAL(canRedoChanged(bool)),
228  this, SIGNAL(canRedoChanged(bool)));
230  this, SIGNAL(redoTextChanged(QString)));
231  connect(d->active, SIGNAL(indexChanged(int)),
232  this, SIGNAL(indexChanged(int)));
233  connect(d->active, SIGNAL(cleanChanged(bool)),
234  this, SIGNAL(cleanChanged(bool)));
235  emit canUndoChanged(d->active->canUndo());
236  emit undoTextChanged(d->active->undoText());
237  emit canRedoChanged(d->active->canRedo());
238  emit redoTextChanged(d->active->redoText());
239  emit cleanChanged(d->active->isClean());
240  emit indexChanged(d->active->index());
241  }
242 
243  emit activeStackChanged(d->active);
244 }
245 
256 {
257  Q_D(const QUndoGroup);
258  return d->active;
259 }
260 
271 {
272  Q_D(QUndoGroup);
273  if (d->active != 0)
274  d->active->undo();
275 }
276 
288 {
289  Q_D(QUndoGroup);
290  if (d->active != 0)
291  d->active->redo();
292 }
293 
304 {
305  Q_D(const QUndoGroup);
306  return d->active != 0 && d->active->canUndo();
307 }
308 
319 {
320  Q_D(const QUndoGroup);
321  return d->active != 0 && d->active->canRedo();
322 }
323 
334 {
335  Q_D(const QUndoGroup);
336  return d->active == 0 ? QString() : d->active->undoText();
337 }
338 
349 {
350  Q_D(const QUndoGroup);
351  return d->active == 0 ? QString() : d->active->redoText();
352 }
353 
364 {
365  Q_D(const QUndoGroup);
366  return d->active == 0 || d->active->isClean();
367 }
368 
369 #ifndef QT_NO_ACTION
370 
387 {
388  QUndoAction *result = new QUndoAction(prefix, parent);
389  if (prefix.isEmpty())
390  result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
391 
392  result->setEnabled(canUndo());
393  result->setPrefixedText(undoText());
394  connect(this, SIGNAL(canUndoChanged(bool)),
395  result, SLOT(setEnabled(bool)));
397  result, SLOT(setPrefixedText(QString)));
398  connect(result, SIGNAL(triggered()), this, SLOT(undo()));
399  return result;
400 }
401 
418 {
419  QUndoAction *result = new QUndoAction(prefix, parent);
420  if (prefix.isEmpty())
421  result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
422 
423  result->setEnabled(canRedo());
424  result->setPrefixedText(redoText());
425  connect(this, SIGNAL(canRedoChanged(bool)),
426  result, SLOT(setEnabled(bool)));
428  result, SLOT(setPrefixedText(QString)));
429  connect(result, SIGNAL(triggered()), this, SLOT(redo()));
430  return result;
431 }
432 
433 #endif // QT_NO_ACTION
434 
524 
525 #endif // QT_NO_UNDOGROUP
double d
Definition: qnumeric_p.h:62
void undo()
Calls QUndoStack::undo() on the active stack.
Definition: qundogroup.cpp:270
QUndoGroup(QObject *parent=0)
Creates an empty QUndoGroup object with parent parent.
Definition: qundogroup.cpp:106
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define it(className, varName)
#define SLOT(a)
Definition: qobjectdefs.h:226
void canUndoChanged(bool canUndo)
This signal is emitted whenever the active stack emits QUndoStack::canUndoChanged() or the active sta...
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void addStack(QUndoStack *stack)
Adds stack to this group.
Definition: qundogroup.cpp:135
void indexChanged(int idx)
This signal is emitted whenever the active stack emits QUndoStack::indexChanged() or the active stack...
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
QList< QUndoStack * > stack_list
Definition: qundogroup.cpp:57
void setEnabled(bool)
Definition: qaction.cpp:1192
void setTextFormat(const QString &textFormat, const QString &defaultText)
Definition: qundostack.cpp:435
The QUndoGroup class is a group of QUndoStack objects.
Definition: qundogroup.h:60
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QAction * createUndoAction(QObject *parent, const QString &prefix=QString()) const
Creates an undo QAction object with parent parent.
Definition: qundogroup.cpp:386
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QUndoStack * activeStack() const
Returns the active stack of this group.
Definition: qundogroup.cpp:255
#define emit
Definition: qobjectdefs.h:76
void setActiveStack(QUndoStack *stack)
Sets the active stack of this group to stack.
Definition: qundogroup.cpp:192
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
void activeStackChanged(QUndoStack *stack)
This signal is emitted whenever the active stack of the group changes.
void cleanChanged(bool clean)
This signal is emitted whenever the active stack emits QUndoStack::cleanChanged() or the active stack...
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
QString redoText() const
Returns the value of the active stack&#39;s QUndoStack::redoText().
Definition: qundogroup.cpp:348
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the active stack emits QUndoStack::redoTextChanged() or the active st...
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QAction * createRedoAction(QObject *parent, const QString &prefix=QString()) const
Creates an redo QAction object with parent parent.
Definition: qundogroup.cpp:417
void removeStack(QUndoStack *stack)
Removes stack from this group.
Definition: qundogroup.cpp:155
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the active stack emits QUndoStack::undoTextChanged() or the active st...
bool isClean() const
Returns the value of the active stack&#39;s QUndoStack::isClean().
Definition: qundogroup.cpp:363
~QUndoGroup()
Destroys the QUndoGroup.
Definition: qundogroup.cpp:114
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void canRedoChanged(bool canRedo)
This signal is emitted whenever the active stack emits QUndoStack::canRedoChanged() or the active sta...
bool canUndo() const
Returns the value of the active stack&#39;s QUndoStack::canUndo().
Definition: qundogroup.cpp:303
void setPrefixedText(const QString &text)
Definition: qundostack.cpp:419
QObject * parent
Definition: qobject.h:92
QUndoStack * active
Definition: qundogroup.cpp:56
QString undoText() const
Returns the value of the active stack&#39;s QUndoStack::undoText().
Definition: qundogroup.cpp:333
void redo()
Calls QUndoStack::redo() on the active stack.
Definition: qundogroup.cpp:287
QList< QUndoStack * > stacks() const
Returns a list of stacks in this group.
Definition: qundogroup.cpp:172
static const KeyPair *const end
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
bool canRedo() const
Returns the value of the active stack&#39;s QUndoStack::canRedo().
Definition: qundogroup.cpp:318