Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qundogroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qundogroup.h"
5#include "qundostack.h"
6#include "qundostack_p.h"
7
9
11{
12 Q_DECLARE_PUBLIC(QUndoGroup)
13public:
15
16 QUndoStack *active;
17 QList<QUndoStack*> stack_list;
18};
19
66 : QObject(*new QUndoGroupPrivate(), parent)
67{
68}
69
74{
75 // Ensure all QUndoStacks no longer refer to this group.
76 Q_D(QUndoGroup);
78 QList<QUndoStack *>::iterator end = d->stack_list.end();
79 while (it != end) {
80 (*it)->d_func()->group = nullptr;
81 ++it;
82 }
83}
84
94void QUndoGroup::addStack(QUndoStack *stack)
95{
96 Q_D(QUndoGroup);
97
98 if (d->stack_list.contains(stack))
99 return;
100 d->stack_list.append(stack);
101
102 if (QUndoGroup *other = stack->d_func()->group)
103 other->removeStack(stack);
104 stack->d_func()->group = this;
105}
106
114void QUndoGroup::removeStack(QUndoStack *stack)
115{
116 Q_D(QUndoGroup);
117
118 if (d->stack_list.removeAll(stack) == 0)
119 return;
120 if (stack == d->active)
121 setActiveStack(nullptr);
122 stack->d_func()->group = nullptr;
123}
124
131QList<QUndoStack*> QUndoGroup::stacks() const
132{
133 Q_D(const QUndoGroup);
134 return d->stack_list;
135}
136
151void QUndoGroup::setActiveStack(QUndoStack *stack)
152{
153 Q_D(QUndoGroup);
154 if (d->active == stack)
155 return;
156
157 if (d->active != nullptr) {
158 disconnect(d->active, SIGNAL(canUndoChanged(bool)),
159 this, SIGNAL(canUndoChanged(bool)));
162 disconnect(d->active, SIGNAL(canRedoChanged(bool)),
163 this, SIGNAL(canRedoChanged(bool)));
166 disconnect(d->active, SIGNAL(indexChanged(int)),
167 this, SIGNAL(indexChanged(int)));
168 disconnect(d->active, SIGNAL(cleanChanged(bool)),
169 this, SIGNAL(cleanChanged(bool)));
170 }
171
172 d->active = stack;
173
174 if (d->active == nullptr) {
175 emit canUndoChanged(false);
177 emit canRedoChanged(false);
179 emit cleanChanged(true);
181 } else {
182 connect(d->active, SIGNAL(canUndoChanged(bool)),
183 this, SIGNAL(canUndoChanged(bool)));
186 connect(d->active, SIGNAL(canRedoChanged(bool)),
187 this, SIGNAL(canRedoChanged(bool)));
190 connect(d->active, SIGNAL(indexChanged(int)),
191 this, SIGNAL(indexChanged(int)));
192 connect(d->active, SIGNAL(cleanChanged(bool)),
193 this, SIGNAL(cleanChanged(bool)));
194 emit canUndoChanged(d->active->canUndo());
195 emit undoTextChanged(d->active->undoText());
196 emit canRedoChanged(d->active->canRedo());
197 emit redoTextChanged(d->active->redoText());
198 emit cleanChanged(d->active->isClean());
199 emit indexChanged(d->active->index());
200 }
201
202 emit activeStackChanged(d->active);
203}
204
214QUndoStack *QUndoGroup::activeStack() const
215{
216 Q_D(const QUndoGroup);
217 return d->active;
218}
219
220#ifndef QT_NO_ACTION
221
238{
239 QAction *action = new QAction(parent);
240 action->setEnabled(canUndo());
241
242 QString effectivePrefix = prefix;
243 QString defaultText;
244 if (prefix.isEmpty()) {
245 effectivePrefix = tr("Undo %1");
246 defaultText = tr("Undo", "Default text for undo action");
247 }
248
249 QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, undoText());
250
252 connect(this, &QUndoGroup::undoTextChanged, action, [=](const QString &text) {
253 QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, text);
254 });
256
257 return action;
258}
259
276{
277 QAction *action = new QAction(parent);
278 action->setEnabled(canRedo());
279
280 QString effectivePrefix = prefix;
281 QString defaultText;
282 if (prefix.isEmpty()) {
283 effectivePrefix = tr("Redo %1");
284 defaultText = tr("Redo", "Default text for redo action");
285 }
286
287 QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, redoText());
288
290 connect(this, &QUndoGroup::redoTextChanged, action, [=](const QString &text) {
291 QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, text);
292 });
294 return action;
295}
296
297#endif // QT_NO_ACTION
298
309{
310 Q_D(QUndoGroup);
311 if (d->active != nullptr)
312 d->active->undo();
313}
314
326{
327 Q_D(QUndoGroup);
328 if (d->active != nullptr)
329 d->active->redo();
330}
331
342{
343 Q_D(const QUndoGroup);
344 return d->active != nullptr && d->active->canUndo();
345}
346
357{
358 Q_D(const QUndoGroup);
359 return d->active != nullptr && d->active->canRedo();
360}
361
372{
373 Q_D(const QUndoGroup);
374 return d->active == nullptr ? QString() : d->active->undoText();
375}
376
387{
388 Q_D(const QUndoGroup);
389 return d->active == nullptr ? QString() : d->active->redoText();
390}
391
402{
403 Q_D(const QUndoGroup);
404 return d->active == nullptr || d->active->isClean();
405}
406
478
479#include "moc_qundogroup.cpp"
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
void setEnabled(bool)
Definition qaction.cpp:927
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
iterator begin()
Definition qset.h:136
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QList< QUndoStack * > stack_list
QUndoStack * active
The QUndoGroup class is a group of QUndoStack objects.
Definition qundogroup.h:20
void canUndoChanged(bool canUndo)
This signal is emitted whenever the active stack emits QUndoStack::canUndoChanged() or the active sta...
void addStack(QUndoStack *stack)
Adds stack to this group.
void cleanChanged(bool clean)
This signal is emitted whenever the active stack emits QUndoStack::cleanChanged() or the active stack...
QUndoStack * activeStack() const
Returns the active stack of this group.
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the active stack emits QUndoStack::redoTextChanged() or the active st...
~QUndoGroup()
Destroys the QUndoGroup.
QUndoGroup(QObject *parent=nullptr)
Creates an empty QUndoGroup object with parent parent.
bool isClean() const
Returns the value of the active stack's QUndoStack::isClean().
QAction * createRedoAction(QObject *parent, const QString &prefix=QString()) const
Creates an redo QAction object with parent parent.
void removeStack(QUndoStack *stack)
Removes stack from this group.
bool canRedo() const
Returns the value of the active stack's QUndoStack::canRedo().
QList< QUndoStack * > stacks() const
Returns a list of stacks in this group.
QString undoText() const
Returns the value of the active stack's QUndoStack::undoText().
void canRedoChanged(bool canRedo)
This signal is emitted whenever the active stack emits QUndoStack::canRedoChanged() or the active sta...
QAction * createUndoAction(QObject *parent, const QString &prefix=QString()) const
Creates an undo QAction object with parent parent.
void undo()
Calls QUndoStack::undo() on the active stack.
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the active stack emits QUndoStack::undoTextChanged() or the active st...
void setActiveStack(QUndoStack *stack)
Sets the active stack of this group to stack.
void activeStackChanged(QUndoStack *stack)
This signal is emitted whenever the active stack of the group changes.
bool canUndo() const
Returns the value of the active stack's QUndoStack::canUndo().
QString redoText() const
Returns the value of the active stack's QUndoStack::redoText().
void indexChanged(int idx)
This signal is emitted whenever the active stack emits QUndoStack::indexChanged() or the active stack...
void redo()
Calls QUndoStack::redo() on the active stack.
QString text
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint GLuint end
#define tr(X)
#define emit
QObject::connect nullptr
myObject disconnect()
[26]
QSharedPointer< T > other(t)
[5]