Skip to content

Commit

Permalink
Move undo framework out of Qt Widgets
Browse files Browse the repository at this point in the history
- Moves QUndo* classes (except QUndoView) from src/widgets/utils to src/gui/utils
- Moves related auto tests from widgets to gui
- Replaces QUndoAction with lambdas that do text prefixing

[ChangeLog][Undo Framework] QUndo* classes (except QUndoView) were moved from Qt
Widgets to Qt GUI.

Done-with: [email protected]
Fixes: QTBUG-40040
Change-Id: I3bd8d4d32c64f8dee548f62159a1df2126da89d8
Reviewed-by: Lars Knoll <[email protected]>
Reviewed-by: Volker Hilsheimer <[email protected]>
  • Loading branch information
mitchcurtis authored and vohi committed Mar 30, 2020
1 parent adc1be3 commit 3f73995
Show file tree
Hide file tree
Showing 30 changed files with 263 additions and 265 deletions.
2 changes: 1 addition & 1 deletion examples/widgets/doc/src/undoframework.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
The \c createActions() function sets up all the examples actions
in the manner shown above. The
\l{QUndoStack::}{createUndoAction()} and
\l{QUndoStack::}{createRedoAction()} helps us crate actions that
\l{QUndoStack::}{createRedoAction()} methods help us create actions that
are disabled and enabled based on the state of the stack. Also,
the text of the action will be updated automatically based on the
\l{QUndoCommand::}{text()} of the undo commands. For the other
Expand Down
9 changes: 9 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,15 @@ qt_extend_target(Gui CONDITION NOT GCC OR NOT QT_COMPILER_VERSION_MAJOR STREQUAL
"painting/qdrawhelper.cpp"
)

qt_extend_target(Gui CONDITION QT_FEATURE_undocommand
SOURCES
util/qundostack.cpp util/qundostack.h util/qundostack_p.h
)

qt_extend_target(Gui CONDITION QT_FEATURE_undogroup
SOURCES
util/qundogroup.cpp util/qundogroup.h
)

qt_create_tracepoints(Gui qtgui.tracepoints)
qt_add_docs(Gui
Expand Down
20 changes: 20 additions & 0 deletions src/gui/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,26 @@ qt_feature("multiprocess" PRIVATE
PURPOSE "Provides support for detecting the desktop environment, launching external processes and opening URLs."
CONDITION NOT INTEGRITY AND NOT rtems
)
qt_feature("undocommand" PUBLIC
SECTION "Utilities"
LABEL "QUndoCommand"
PURPOSE "Applies (redo or) undo of a single change in a document."
)
qt_feature_definition("undocommand" "QT_NO_UNDOCOMMAND" NEGATE VALUE "1")
qt_feature("undostack" PUBLIC
SECTION "Utilities"
LABEL "QUndoStack"
PURPOSE "Provides the ability to (redo or) undo a list of changes in a document."
CONDITION QT_FEATURE_undocommand
)
qt_feature_definition("undostack" "QT_NO_UNDOSTACK" NEGATE VALUE "1")
qt_feature("undogroup" PUBLIC
SECTION "Utilities"
LABEL "QUndoGroup"
PURPOSE "Provides the ability to cluster QUndoCommands."
CONDITION QT_FEATURE_undostack
)
qt_feature_definition("undogroup" "QT_NO_UNDOGROUP" NEGATE VALUE "1")
qt_feature("whatsthis" PUBLIC
SECTION "Widget Support"
LABEL "QWhatsThis"
Expand Down
20 changes: 20 additions & 0 deletions src/gui/configure.json
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,26 @@
"purpose": "Internal painting support for 64 bit (16 bpc) rasterization.",
"section": "Painting",
"output": [ "privateFeature" ]
},
"undocommand": {
"label": "QUndoCommand",
"purpose": "Applies (redo or) undo of a single change in a document.",
"section": "Utilities",
"output": [ "publicFeature", "feature" ]
},
"undostack": {
"label": "QUndoStack",
"purpose": "Provides the ability to (redo or) undo a list of changes in a document.",
"section": "Utilities",
"condition": "features.undocommand",
"output": [ "publicFeature", "feature" ]
},
"undogroup": {
"label": "QUndoGroup",
"purpose": "Provides the ability to cluster QUndoCommands.",
"section": "Utilities",
"condition": "features.undostack",
"output": [ "publicFeature", "feature" ]
}
},

Expand Down
147 changes: 80 additions & 67 deletions src/widgets/util/qundogroup.cpp → src/gui/util/qundogroup.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
Expand Down Expand Up @@ -253,6 +253,85 @@ QUndoStack *QUndoGroup::activeStack() const
return d->active;
}

#ifndef QT_NO_ACTION

/*!
Creates an undo QAction object with parent \a parent.
Triggering this action will cause a call to QUndoStack::undo() on the active stack.
The text of this action will always be the text of the command which will be undone
in the next call to undo(), prefixed by \a prefix. If there is no command available
for undo, if the group is empty or if none of the stacks are active, this action will
be disabled.
If \a prefix is empty, the default template "Undo %1" is used instead of prefix.
Before Qt 4.8, the prefix "Undo" was used by default.
\sa createRedoAction(), canUndo(), QUndoCommand::text()
*/

QAction *QUndoGroup::createUndoAction(QObject *parent, const QString &prefix) const
{
QAction *action = new QAction(parent);
action->setEnabled(canUndo());

QString effectivePrefix = prefix;
QString defaultText;
if (prefix.isEmpty()) {
effectivePrefix = tr("Undo %1");
defaultText = tr("Undo", "Default text for undo action");
}

QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, undoText());

connect(this, &QUndoGroup::canUndoChanged, action, &QAction::setEnabled);
connect(this, &QUndoGroup::undoTextChanged, action, [=](const QString &text) {
QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, text);
});
connect(action, &QAction::triggered, this, &QUndoGroup::undo);

return action;
}

/*!
Creates an redo QAction object with parent \a parent.
Triggering this action will cause a call to QUndoStack::redo() on the active stack.
The text of this action will always be the text of the command which will be redone
in the next call to redo(), prefixed by \a prefix. If there is no command available
for redo, if the group is empty or if none of the stacks are active, this action will
be disabled.
If \a prefix is empty, the default template "Redo %1" is used instead of prefix.
Before Qt 4.8, the prefix "Redo" was used by default.
\sa createUndoAction(), canRedo(), QUndoCommand::text()
*/

QAction *QUndoGroup::createRedoAction(QObject *parent, const QString &prefix) const
{
QAction *action = new QAction(parent);
action->setEnabled(canRedo());

QString effectivePrefix = prefix;
QString defaultText;
if (prefix.isEmpty()) {
effectivePrefix = tr("Redo %1");
defaultText = tr("Redo", "Default text for redo action");
}

QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, redoText());

connect(this, &QUndoGroup::canRedoChanged, action, &QAction::setEnabled);
connect(this, &QUndoGroup::redoTextChanged, action, [=](const QString &text) {
QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, text);
});
connect(action, &QAction::triggered, this, &QUndoGroup::redo);
return action;
}

#endif // QT_NO_ACTION

/*!
Calls QUndoStack::undo() on the active stack.
Expand Down Expand Up @@ -361,72 +440,6 @@ bool QUndoGroup::isClean() const
return d->active == nullptr || d->active->isClean();
}

#ifndef QT_NO_ACTION

/*!
Creates an undo QAction object with parent \a parent.
Triggering this action will cause a call to QUndoStack::undo() on the active stack.
The text of this action will always be the text of the command which will be undone
in the next call to undo(), prefixed by \a prefix. If there is no command available
for undo, if the group is empty or if none of the stacks are active, this action will
be disabled.
If \a prefix is empty, the default template "Undo %1" is used instead of prefix.
Before Qt 4.8, the prefix "Undo" was used by default.
\sa createRedoAction(), canUndo(), QUndoCommand::text()
*/

QAction *QUndoGroup::createUndoAction(QObject *parent, const QString &prefix) const
{
QUndoAction *result = new QUndoAction(prefix, parent);
if (prefix.isEmpty())
result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));

result->setEnabled(canUndo());
result->setPrefixedText(undoText());
connect(this, SIGNAL(canUndoChanged(bool)),
result, SLOT(setEnabled(bool)));
connect(this, SIGNAL(undoTextChanged(QString)),
result, SLOT(setPrefixedText(QString)));
connect(result, SIGNAL(triggered()), this, SLOT(undo()));
return result;
}

/*!
Creates an redo QAction object with parent \a parent.
Triggering this action will cause a call to QUndoStack::redo() on the active stack.
The text of this action will always be the text of the command which will be redone
in the next call to redo(), prefixed by \a prefix. If there is no command available
for redo, if the group is empty or if none of the stacks are active, this action will
be disabled.
If \a prefix is empty, the default template "Redo %1" is used instead of prefix.
Before Qt 4.8, the prefix "Redo" was used by default.
\sa createUndoAction(), canRedo(), QUndoCommand::text()
*/

QAction *QUndoGroup::createRedoAction(QObject *parent, const QString &prefix) const
{
QUndoAction *result = new QUndoAction(prefix, parent);
if (prefix.isEmpty())
result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));

result->setEnabled(canRedo());
result->setPrefixedText(redoText());
connect(this, SIGNAL(canRedoChanged(bool)),
result, SLOT(setEnabled(bool)));
connect(this, SIGNAL(redoTextChanged(QString)),
result, SLOT(setPrefixedText(QString)));
connect(result, SIGNAL(triggered()), this, SLOT(redo()));
return result;
}

#endif // QT_NO_ACTION

/*! \fn void QUndoGroup::activeStackChanged(QUndoStack *stack)
This signal is emitted whenever the active stack of the group changes. This can happen
Expand Down
13 changes: 6 additions & 7 deletions src/widgets/util/qundogroup.h → src/gui/util/qundogroup.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
Expand Down Expand Up @@ -40,7 +40,7 @@
#ifndef QUNDOGROUP_H
#define QUNDOGROUP_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qtguiglobal.h>
#include <QtCore/qobject.h>
#include <QtCore/qstring.h>

Expand All @@ -52,7 +52,7 @@ class QUndoGroupPrivate;
class QUndoStack;
class QAction;

class Q_WIDGETS_EXPORT QUndoGroup : public QObject
class Q_GUI_EXPORT QUndoGroup : public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(QUndoGroup)
Expand All @@ -67,11 +67,10 @@ class Q_WIDGETS_EXPORT QUndoGroup : public QObject
QUndoStack *activeStack() const;

#ifndef QT_NO_ACTION
QAction *createUndoAction(QObject *parent,
const QString &prefix = QString()) const;
QAction *createRedoAction(QObject *parent,
const QString &prefix = QString()) const;
QAction *createUndoAction(QObject *parent, const QString &prefix = QString()) const;
QAction *createRedoAction(QObject *parent, const QString &prefix = QString()) const;
#endif // QT_NO_ACTION

bool canUndo() const;
bool canRedo() const;
QString undoText() const;
Expand Down
Loading

0 comments on commit 3f73995

Please sign in to comment.