Warning, file /office/calligra/libs/kundo2/kundo2group.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
0004 ** All rights reserved.
0005 ** Contact: Nokia Corporation (qt-info@nokia.com)
0006 **
0007 ** This file is part of the QtGui module of the Qt Toolkit.
0008 **
0009 ** $QT_BEGIN_LICENSE:LGPL$
0010 ** No Commercial Usage
0011 ** This file contains pre-release code and may not be distributed.
0012 ** You may use this file in accordance with the terms and conditions
0013 ** contained in the Technology Preview License Agreement accompanying
0014 ** this package.
0015 **
0016 ** GNU Lesser General Public License Usage
0017 ** Alternatively, this file may be used under the terms of the GNU Lesser
0018 ** General Public License version 2.1 as published by the Free Software
0019 ** Foundation and appearing in the file LICENSE.LGPL included in the
0020 ** packaging of this file.  Please review the following information to
0021 ** ensure the GNU Lesser General Public License version 2.1 requirements
0022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0023 **
0024 ** In addition, as a special exception, Nokia gives you certain additional
0025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
0026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
0027 **
0028 ** If you have questions regarding the use of this file, please contact
0029 ** Nokia at qt-info@nokia.com.
0030 **
0031 **
0032 **
0033 **
0034 **
0035 **
0036 **
0037 **
0038 ** $QT_END_LICENSE$
0039 **
0040 ****************************************************************************/
0041 
0042 #include "kundo2group.h"
0043 #include "kundo2stack.h"
0044 #include "kundo2stack_p.h"
0045 #include <klocalizedstring.h>
0046 
0047 #ifndef QT_NO_UNDOGROUP
0048 
0049 /*!
0050     \class KUndo2Group
0051     \brief The KUndo2Group class is a group of KUndo2QStack objects.
0052     \since 4.2
0053 
0054     For an overview of the Qt's undo framework, see the
0055     \link qundo.html overview\endlink.
0056 
0057     An application often has multiple undo stacks, one for each opened document. At the
0058     same time, an application usually has one undo action and one redo action, which
0059     triggers undo or redo in the active document.
0060 
0061     KUndo2Group is a group of KUndo2QStack objects, one of which may be active. It has
0062     an undo() and redo() slot, which calls KUndo2QStack::undo() and KUndo2QStack::redo()
0063     for the active stack. It also has the functions createUndoAction() and createRedoAction().
0064     The actions returned by these functions behave in the same way as those returned by
0065     KUndo2QStack::createUndoAction() and KUndo2QStack::createRedoAction() of the active
0066     stack.
0067 
0068     Stacks are added to a group with addStack() and removed with removeStack(). A stack
0069     is implicitly added to a group when it is created with the group as its parent
0070     QObject.
0071 
0072     It is the programmer's responsibility to specify which stack is active by
0073     calling KUndo2QStack::setActive(), usually when the associated document window receives focus.
0074     The active stack may also be set with setActiveStack(), and is returned by activeStack().
0075 
0076     When a stack is added to a group using addStack(), the group does not take ownership
0077     of the stack. This means the stack has to be deleted separately from the group. When
0078     a stack is deleted, it is automatically removed from a group. A stack may belong to
0079     only one group. Adding it to another group will cause it to be removed from the previous
0080     group.
0081 
0082     A KUndo2Group is also useful in conjunction with KUndo2View. If a KUndo2View is
0083     set to watch a group using KUndo2View::setGroup(), it will update itself to display
0084     the active stack.
0085 */
0086 
0087 /*!
0088     Creates an empty KUndo2Group object with parent \a parent.
0089 
0090     \sa addStack()
0091 */
0092 
0093 KUndo2Group::KUndo2Group(QObject *parent)
0094     : QObject(parent), m_active(0)
0095 {
0096 }
0097 
0098 /*!
0099     Destroys the KUndo2Group.
0100 */
0101 KUndo2Group::~KUndo2Group()
0102 {
0103     // Ensure all KUndo2Stacks no longer refer to this group.
0104     QList<KUndo2QStack *>::iterator it = m_stack_list.begin();
0105     QList<KUndo2QStack *>::iterator end = m_stack_list.end();
0106     while (it != end) {
0107         (*it)->m_group = 0;
0108         ++it;
0109     }
0110 }
0111 
0112 /*!
0113     Adds \a stack to this group. The group does not take ownership of the stack. Another
0114     way of adding a stack to a group is by specifying the group as the stack's parent
0115     QObject in KUndo2QStack::KUndo2QStack(). In this case, the stack is deleted when the
0116     group is deleted, in the usual manner of QObjects.
0117 
0118     \sa removeStack() stacks() KUndo2QStack::KUndo2QStack()
0119 */
0120 
0121 void KUndo2Group::addStack(KUndo2QStack *stack)
0122 {
0123     if (m_stack_list.contains(stack))
0124         return;
0125     m_stack_list.append(stack);
0126 
0127     if (KUndo2Group *other = stack->m_group)
0128         other->removeStack(stack);
0129     stack->m_group = this;
0130 }
0131 
0132 /*!
0133     Removes \a stack from this group. If the stack was the active stack in the group,
0134     the active stack becomes 0.
0135 
0136     \sa addStack() stacks() KUndo2QStack::~KUndo2QStack()
0137 */
0138 
0139 void KUndo2Group::removeStack(KUndo2QStack *stack)
0140 {
0141     if (m_stack_list.removeAll(stack) == 0)
0142         return;
0143     if (stack == m_active)
0144         setActiveStack(0);
0145     stack->m_group = 0;
0146 }
0147 
0148 /*!
0149     Returns a list of stacks in this group.
0150 
0151     \sa addStack() removeStack()
0152 */
0153 
0154 QList<KUndo2QStack*> KUndo2Group::stacks() const
0155 {
0156     return m_stack_list;
0157 }
0158 
0159 /*!
0160     Sets the active stack of this group to \a stack.
0161 
0162     If the stack is not a member of this group, this function does nothing.
0163 
0164     Synonymous with calling KUndo2QStack::setActive() on \a stack.
0165 
0166     The actions returned by createUndoAction() and createRedoAction() will now behave
0167     in the same way as those returned by \a stack's KUndo2QStack::createUndoAction()
0168     and KUndo2QStack::createRedoAction().
0169 
0170     \sa KUndo2QStack::setActive() activeStack()
0171 */
0172 
0173 void KUndo2Group::setActiveStack(KUndo2QStack *stack)
0174 {
0175     if (m_active == stack)
0176         return;
0177 
0178     if (m_active != 0) {
0179         disconnect(m_active, SIGNAL(canUndoChanged(bool)),
0180                    this, SIGNAL(canUndoChanged(bool)));
0181         disconnect(m_active, SIGNAL(undoTextChanged(QString)),
0182                    this, SIGNAL(undoTextChanged(QString)));
0183         disconnect(m_active, SIGNAL(canRedoChanged(bool)),
0184                    this, SIGNAL(canRedoChanged(bool)));
0185         disconnect(m_active, SIGNAL(redoTextChanged(QString)),
0186                    this, SIGNAL(redoTextChanged(QString)));
0187         disconnect(m_active, SIGNAL(indexChanged(int)),
0188                    this, SIGNAL(indexChanged(int)));
0189         disconnect(m_active, SIGNAL(cleanChanged(bool)),
0190                    this, SIGNAL(cleanChanged(bool)));
0191     }
0192 
0193     m_active = stack;
0194 
0195     if (m_active == 0) {
0196         emit canUndoChanged(false);
0197         emit undoTextChanged(QString());
0198         emit canRedoChanged(false);
0199         emit redoTextChanged(QString());
0200         emit cleanChanged(true);
0201         emit indexChanged(0);
0202     } else {
0203         connect(m_active, SIGNAL(canUndoChanged(bool)),
0204                 this, SIGNAL(canUndoChanged(bool)));
0205         connect(m_active, SIGNAL(undoTextChanged(QString)),
0206                 this, SIGNAL(undoTextChanged(QString)));
0207         connect(m_active, SIGNAL(canRedoChanged(bool)),
0208                 this, SIGNAL(canRedoChanged(bool)));
0209         connect(m_active, SIGNAL(redoTextChanged(QString)),
0210                 this, SIGNAL(redoTextChanged(QString)));
0211         connect(m_active, SIGNAL(indexChanged(int)),
0212                 this, SIGNAL(indexChanged(int)));
0213         connect(m_active, SIGNAL(cleanChanged(bool)),
0214                 this, SIGNAL(cleanChanged(bool)));
0215         emit canUndoChanged(m_active->canUndo());
0216         emit undoTextChanged(m_active->undoText());
0217         emit canRedoChanged(m_active->canRedo());
0218         emit redoTextChanged(m_active->redoText());
0219         emit cleanChanged(m_active->isClean());
0220         emit indexChanged(m_active->index());
0221     }
0222 
0223     emit activeStackChanged(m_active);
0224 }
0225 
0226 /*!
0227     Returns the active stack of this group.
0228 
0229     If none of the stacks are active, or if the group is empty, this function
0230     returns 0.
0231 
0232     \sa setActiveStack() KUndo2QStack::setActive()
0233 */
0234 
0235 KUndo2QStack *KUndo2Group::activeStack() const
0236 {
0237     return m_active;
0238 }
0239 
0240 /*!
0241     Calls KUndo2QStack::undo() on the active stack.
0242 
0243     If none of the stacks are active, or if the group is empty, this function
0244     does nothing.
0245 
0246     \sa redo() canUndo() setActiveStack()
0247 */
0248 
0249 void KUndo2Group::undo()
0250 {
0251     if (m_active != 0)
0252         m_active->undo();
0253 }
0254 
0255 /*!
0256     Calls KUndo2QStack::redo() on the active stack.
0257 
0258     If none of the stacks are active, or if the group is empty, this function
0259     does nothing.
0260 
0261     \sa undo() canRedo() setActiveStack()
0262 */
0263 
0264 
0265 void KUndo2Group::redo()
0266 {
0267     if (m_active != 0)
0268         m_active->redo();
0269 }
0270 
0271 /*!
0272     Returns the value of the active stack's KUndo2QStack::canUndo().
0273 
0274     If none of the stacks are active, or if the group is empty, this function
0275     returns false.
0276 
0277     \sa canRedo() setActiveStack()
0278 */
0279 
0280 bool KUndo2Group::canUndo() const
0281 {
0282     return m_active != 0 && m_active->canUndo();
0283 }
0284 
0285 /*!
0286     Returns the value of the active stack's KUndo2QStack::canRedo().
0287 
0288     If none of the stacks are active, or if the group is empty, this function
0289     returns false.
0290 
0291     \sa canUndo() setActiveStack()
0292 */
0293 
0294 bool KUndo2Group::canRedo() const
0295 {
0296     return m_active != 0 && m_active->canRedo();
0297 }
0298 
0299 /*!
0300     Returns the value of the active stack's KUndo2QStack::undoActionText().
0301 
0302     If none of the stacks are active, or if the group is empty, this function
0303     returns an empty string.
0304 
0305     \sa undoItemText() redoActionText() setActiveStack()
0306 */
0307 
0308 QString KUndo2Group::undoText() const
0309 {
0310     return m_active == 0 ? QString() : m_active->undoText();
0311 }
0312 
0313 /*!
0314     Returns the value of the active stack's KUndo2QStack::redoActionText().
0315 
0316     If none of the stacks are active, or if the group is empty, this function
0317     returns an empty string.
0318 
0319     \sa redoItemText() undoActionText() setActiveStack()
0320 */
0321 
0322 QString KUndo2Group::redoText() const
0323 {
0324     return m_active == 0 ? QString() : m_active->redoText();
0325 }
0326 
0327 /*!
0328     Returns the value of the active stack's KUndo2QStack::isClean().
0329 
0330     If none of the stacks are active, or if the group is empty, this function
0331     returns true.
0332 
0333     \sa setActiveStack()
0334 */
0335 
0336 bool KUndo2Group::isClean() const
0337 {
0338     return m_active == 0 || m_active->isClean();
0339 }
0340 
0341 #ifndef QT_NO_ACTION
0342 
0343 /*!
0344     Creates an undo QAction object with parent \a parent.
0345 
0346     Triggering this action will cause a call to KUndo2QStack::undo() on the active stack.
0347     The text of this action will always be the text of the command which will be undone
0348     in the next call to undo(), prefixed by \a prefix. If there is no command available
0349     for undo, if the group is empty or if none of the stacks are active, this action will
0350     be disabled.
0351 
0352     If \a prefix is empty, the default prefix "Undo" is used.
0353 
0354     \sa createRedoAction() canUndo() KUndo2Command::text()
0355 */
0356 
0357 QAction *KUndo2Group::createUndoAction(QObject *parent) const
0358 {
0359     KUndo2Action *result = new KUndo2Action(i18n("Undo %1"), i18nc("Default text for undo action", "Undo"), parent);
0360     result->setEnabled(canUndo());
0361     result->setPrefixedText(undoText());
0362     connect(this, SIGNAL(canUndoChanged(bool)),
0363             result, SLOT(setEnabled(bool)));
0364     connect(this, SIGNAL(undoTextChanged(QString)),
0365             result, SLOT(setPrefixedText(QString)));
0366     connect(result, SIGNAL(triggered()), this, SLOT(undo()));
0367     return result;
0368 }
0369 
0370 /*!
0371     Creates an redo QAction object with parent \a parent.
0372 
0373     Triggering this action will cause a call to KUndo2QStack::redo() on the active stack.
0374     The text of this action will always be the text of the command which will be redone
0375     in the next call to redo(), prefixed by \a prefix. If there is no command available
0376     for redo, if the group is empty or if none of the stacks are active, this action will
0377     be disabled.
0378 
0379     If \a prefix is empty, the default prefix "Undo" is used.
0380 
0381     \sa createUndoAction() canRedo() KUndo2Command::text()
0382 */
0383 
0384 QAction *KUndo2Group::createRedoAction(QObject *parent) const
0385 {
0386     KUndo2Action *result = new KUndo2Action(i18n("Redo %1"), i18nc("Default text for redo action", "Redo"), parent);
0387     result->setEnabled(canRedo());
0388     result->setPrefixedText(redoText());
0389     connect(this, SIGNAL(canRedoChanged(bool)),
0390             result, SLOT(setEnabled(bool)));
0391     connect(this, SIGNAL(redoTextChanged(QString)),
0392             result, SLOT(setPrefixedText(QString)));
0393     connect(result, SIGNAL(triggered()), this, SLOT(redo()));
0394     return result;
0395 }
0396 
0397 #endif // QT_NO_ACTION
0398 
0399 /*! \fn void KUndo2Group::activeStackChanged(KUndo2QStack *stack)
0400 
0401     This signal is emitted whenever the active stack of the group changes. This can happen
0402     when setActiveStack() or KUndo2QStack::setActive() is called, or when the active stack
0403     is removed form the group. \a stack is the new active stack. If no stack is active,
0404     \a stack is 0.
0405 
0406     \sa setActiveStack() KUndo2QStack::setActive()
0407 */
0408 
0409 /*! \fn void KUndo2Group::indexChanged(int idx)
0410 
0411     This signal is emitted whenever the active stack emits KUndo2QStack::indexChanged()
0412     or the active stack changes.
0413 
0414     \a idx is the new current index, or 0 if the active stack is 0.
0415 
0416     \sa KUndo2QStack::indexChanged() setActiveStack()
0417 */
0418 
0419 /*! \fn void KUndo2Group::cleanChanged(bool clean)
0420 
0421     This signal is emitted whenever the active stack emits KUndo2QStack::cleanChanged()
0422     or the active stack changes.
0423 
0424     \a clean is the new state, or true if the active stack is 0.
0425 
0426     \sa KUndo2QStack::cleanChanged() setActiveStack()
0427 */
0428 
0429 /*! \fn void KUndo2Group::canUndoChanged(bool canUndo)
0430 
0431     This signal is emitted whenever the active stack emits KUndo2QStack::canUndoChanged()
0432     or the active stack changes.
0433 
0434     \a canUndo is the new state, or false if the active stack is 0.
0435 
0436     \sa KUndo2QStack::canUndoChanged() setActiveStack()
0437 */
0438 
0439 /*! \fn void KUndo2Group::canRedoChanged(bool canRedo)
0440 
0441     This signal is emitted whenever the active stack emits KUndo2QStack::canRedoChanged()
0442     or the active stack changes.
0443 
0444     \a canRedo is the new state, or false if the active stack is 0.
0445 
0446     \sa KUndo2QStack::canRedoChanged() setActiveStack()
0447 */
0448 
0449 /*! \fn void KUndo2Group::undoTextChanged(const QString &undoText)
0450 
0451     This signal is emitted whenever the active stack emits KUndo2QStack::undoTextChanged()
0452     or the active stack changes.
0453 
0454     \a undoText is the new state, or an empty string if the active stack is 0.
0455 
0456     \sa KUndo2QStack::undoTextChanged() setActiveStack()
0457 */
0458 
0459 /*! \fn void KUndo2Group::redoTextChanged(const QString &redoText)
0460 
0461     This signal is emitted whenever the active stack emits KUndo2QStack::redoTextChanged()
0462     or the active stack changes.
0463 
0464     \a redoText is the new state, or an empty string if the active stack is 0.
0465 
0466     \sa KUndo2QStack::redoTextChanged() setActiveStack()
0467 */
0468 
0469 #endif // QT_NO_UNDOGROUP