/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/ ** ** This file is part of the UiHelpers playground module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "uiundogroup.h" #include "uiundostack.h" #include "uiundostack_p.h" #include "uiaction.h" #ifndef QT_NO_UNDOGROUP QT_BEGIN_NAMESPACE_UIHELPERS class UiUndoGroupPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(UiUndoGroup) public: UiUndoGroupPrivate() : active(0) {} UiUndoStack *active; QList stack_list; }; /*! \class UiUndoGroup \brief The UiUndoGroup class is a group of UiUndoStack objects. \since 4.2 \inmodule UiHelpers For an overview of the Qt's undo framework, see the \link UiUndo.html overview\endlink. An application often has multiple undo stacks, one for each opened document. At the same time, an application usually has one undo action and one redo action, which triggers undo or redo in the active document. UiUndoGroup is a group of UiUndoStack objects, one of which may be active. It has an undo() and redo() slot, which calls UiUndoStack::undo() and UiUndoStack::redo() for the active stack. It also has the functions createUndoAction() and createRedoAction(). The actions returned by these functions behave in the same way as those returned by UiUndoStack::createUndoAction() and UiUndoStack::createRedoAction() of the active stack. Stacks are added to a group with addStack() and removed with removeStack(). A stack is implicitly added to a group when it is created with the group as its parent QObject. It is the programmer's responsibility to specify which stack is active by calling UiUndoStack::setActive(), usually when the associated document window receives focus. The active stack may also be set with setActiveStack(), and is returned by activeStack(). When a stack is added to a group using addStack(), the group does not take ownership of the stack. This means the stack has to be deleted separately from the group. When a stack is deleted, it is automatically removed from a group. A stack may belong to only one group. Adding it to another group will cause it to be removed from the previous group. A UiUndoGroup is also useful in conjunction with UiUndoView. If a UiUndoView is set to watch a group using UiUndoView::setGroup(), it will update itself to display the active stack. */ /*! Creates an empty UiUndoGroup object with parent \a parent. \sa addStack() */ UiUndoGroup::UiUndoGroup(QObject *parent) : QObject(*new UiUndoGroupPrivate(), parent) { } /*! Destroys the UiUndoGroup. */ UiUndoGroup::~UiUndoGroup() { // Ensure all UiUndoStacks no longer refer to this group. Q_D(UiUndoGroup); QList::iterator it = d->stack_list.begin(); QList::iterator end = d->stack_list.end(); while (it != end) { (*it)->d_func()->group = 0; ++it; } } /*! Adds \a stack to this group. The group does not take ownership of the stack. Another way of adding a stack to a group is by specifying the group as the stack's parent QObject in UiUndoStack::UiUndoStack(). In this case, the stack is deleted when the group is deleted, in the usual manner of QObjects. \sa removeStack() stacks() UiUndoStack::UiUndoStack() */ void UiUndoGroup::addStack(UiUndoStack *stack) { Q_D(UiUndoGroup); if (d->stack_list.contains(stack)) return; d->stack_list.append(stack); if (UiUndoGroup *other = stack->d_func()->group) other->removeStack(stack); stack->d_func()->group = this; } /*! Removes \a stack from this group. If the stack was the active stack in the group, the active stack becomes 0. \sa addStack() stacks() UiUndoStack::~UiUndoStack() */ void UiUndoGroup::removeStack(UiUndoStack *stack) { Q_D(UiUndoGroup); if (d->stack_list.removeAll(stack) == 0) return; if (stack == d->active) setActiveStack(0); stack->d_func()->group = 0; } /*! Returns a list of stacks in this group. \sa addStack() removeStack() */ QList UiUndoGroup::stacks() const { Q_D(const UiUndoGroup); return d->stack_list; } /*! Sets the active stack of this group to \a stack. If the stack is not a member of this group, this function does nothing. Synonymous with calling UiUndoStack::setActive() on \a stack. The actions returned by createUndoAction() and createRedoAction() will now behave in the same way as those returned by \a stack's UiUndoStack::createUndoAction() and UiUndoStack::createRedoAction(). \sa UiUndoStack::setActive() activeStack() */ void UiUndoGroup::setActiveStack(UiUndoStack *stack) { Q_D(UiUndoGroup); if (d->active == stack) return; if (d->active != 0) { disconnect(d->active, SIGNAL(canUndoChanged(bool)), this, SIGNAL(canUndoChanged(bool))); disconnect(d->active, SIGNAL(undoTextChanged(QString)), this, SIGNAL(undoTextChanged(QString))); disconnect(d->active, SIGNAL(canRedoChanged(bool)), this, SIGNAL(canRedoChanged(bool))); disconnect(d->active, SIGNAL(redoTextChanged(QString)), this, SIGNAL(redoTextChanged(QString))); disconnect(d->active, SIGNAL(indexChanged(int)), this, SIGNAL(indexChanged(int))); disconnect(d->active, SIGNAL(cleanChanged(bool)), this, SIGNAL(cleanChanged(bool))); } d->active = stack; if (d->active == 0) { emit canUndoChanged(false); emit undoTextChanged(QString()); emit canRedoChanged(false); emit redoTextChanged(QString()); emit cleanChanged(true); emit indexChanged(0); } else { connect(d->active, SIGNAL(canUndoChanged(bool)), this, SIGNAL(canUndoChanged(bool))); connect(d->active, SIGNAL(undoTextChanged(QString)), this, SIGNAL(undoTextChanged(QString))); connect(d->active, SIGNAL(canRedoChanged(bool)), this, SIGNAL(canRedoChanged(bool))); connect(d->active, SIGNAL(redoTextChanged(QString)), this, SIGNAL(redoTextChanged(QString))); connect(d->active, SIGNAL(indexChanged(int)), this, SIGNAL(indexChanged(int))); connect(d->active, SIGNAL(cleanChanged(bool)), this, SIGNAL(cleanChanged(bool))); emit canUndoChanged(d->active->canUndo()); emit undoTextChanged(d->active->undoText()); emit canRedoChanged(d->active->canRedo()); emit redoTextChanged(d->active->redoText()); emit cleanChanged(d->active->isClean()); emit indexChanged(d->active->index()); } emit activeStackChanged(d->active); } /*! Returns the active stack of this group. If none of the stacks are active, or if the group is empty, this function returns 0. \sa setActiveStack() UiUndoStack::setActive() */ UiUndoStack *UiUndoGroup::activeStack() const { Q_D(const UiUndoGroup); return d->active; } /*! Calls UiUndoStack::undo() on the active stack. If none of the stacks are active, or if the group is empty, this function does nothing. \sa redo() canUndo() setActiveStack() */ void UiUndoGroup::undo() { Q_D(UiUndoGroup); if (d->active != 0) d->active->undo(); } /*! Calls UiUndoStack::redo() on the active stack. If none of the stacks are active, or if the group is empty, this function does nothing. \sa undo() canRedo() setActiveStack() */ void UiUndoGroup::redo() { Q_D(UiUndoGroup); if (d->active != 0) d->active->redo(); } /*! Returns the value of the active stack's UiUndoStack::canUndo(). If none of the stacks are active, or if the group is empty, this function returns false. \sa canRedo() setActiveStack() */ bool UiUndoGroup::canUndo() const { Q_D(const UiUndoGroup); return d->active != 0 && d->active->canUndo(); } /*! Returns the value of the active stack's UiUndoStack::canRedo(). If none of the stacks are active, or if the group is empty, this function returns false. \sa canUndo() setActiveStack() */ bool UiUndoGroup::canRedo() const { Q_D(const UiUndoGroup); return d->active != 0 && d->active->canRedo(); } /*! Returns the value of the active stack's UiUndoStack::undoText(). If none of the stacks are active, or if the group is empty, this function returns an empty string. \sa redoText() setActiveStack() */ QString UiUndoGroup::undoText() const { Q_D(const UiUndoGroup); return d->active == 0 ? QString() : d->active->undoText(); } /*! Returns the value of the active stack's UiUndoStack::redoText(). If none of the stacks are active, or if the group is empty, this function returns an empty string. \sa undoText() setActiveStack() */ QString UiUndoGroup::redoText() const { Q_D(const UiUndoGroup); return d->active == 0 ? QString() : d->active->redoText(); } /*! Returns the value of the active stack's UiUndoStack::isClean(). If none of the stacks are active, or if the group is empty, this function returns true. \sa setActiveStack() */ bool UiUndoGroup::isClean() const { Q_D(const UiUndoGroup); return d->active == 0 || d->active->isClean(); } #ifndef QT_NO_ACTION /*! Creates an undo UiAction object with parent \a parent. Triggering this action will cause a call to UiUndoStack::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() UiUndoCommand::text() */ UiAction *UiUndoGroup::createUndoAction(QObject *parent, const QString &prefix) const { UiUndoAction *result = new UiUndoAction(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 UiAction object with parent \a parent. Triggering this action will cause a call to UiUndoStack::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() UiUndoCommand::text() */ UiAction *UiUndoGroup::createRedoAction(QObject *parent, const QString &prefix) const { UiUndoAction *result = new UiUndoAction(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 UiUndoGroup::activeStackChanged(UiUndoStack *stack) This signal is emitted whenever the active stack of the group changes. This can happen when setActiveStack() or UiUndoStack::setActive() is called, or when the active stack is removed form the group. \a stack is the new active stack. If no stack is active, \a stack is 0. \sa setActiveStack() UiUndoStack::setActive() */ /*! \fn void UiUndoGroup::indexChanged(int idx) This signal is emitted whenever the active stack emits UiUndoStack::indexChanged() or the active stack changes. \a idx is the new current index, or 0 if the active stack is 0. \sa UiUndoStack::indexChanged() setActiveStack() */ /*! \fn void UiUndoGroup::cleanChanged(bool clean) This signal is emitted whenever the active stack emits UiUndoStack::cleanChanged() or the active stack changes. \a clean is the new state, or true if the active stack is 0. \sa UiUndoStack::cleanChanged() setActiveStack() */ /*! \fn void UiUndoGroup::canUndoChanged(bool canUndo) This signal is emitted whenever the active stack emits UiUndoStack::canUndoChanged() or the active stack changes. \a canUndo is the new state, or false if the active stack is 0. \sa UiUndoStack::canUndoChanged() setActiveStack() */ /*! \fn void UiUndoGroup::canRedoChanged(bool canRedo) This signal is emitted whenever the active stack emits UiUndoStack::canRedoChanged() or the active stack changes. \a canRedo is the new state, or false if the active stack is 0. \sa UiUndoStack::canRedoChanged() setActiveStack() */ /*! \fn void UiUndoGroup::undoTextChanged(const QString &undoText) This signal is emitted whenever the active stack emits UiUndoStack::undoTextChanged() or the active stack changes. \a undoText is the new state, or an empty string if the active stack is 0. \sa UiUndoStack::undoTextChanged() setActiveStack() */ /*! \fn void UiUndoGroup::redoTextChanged(const QString &redoText) This signal is emitted whenever the active stack emits UiUndoStack::redoTextChanged() or the active stack changes. \a redoText is the new state, or an empty string if the active stack is 0. \sa UiUndoStack::redoTextChanged() setActiveStack() */ QT_END_NAMESPACE_UIHELPERS #endif // QT_NO_UNDOGROUP