/***********************************************************************
*
* Copyright (c) 2012-2016 Barbara Geller
* Copyright (c) 2012-2016 Ansel Sermersheim
* Copyright (c) 2012-2014 Digia Plc and/or its subsidiary(-ies).
* Copyright (c) 2008-2012 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
*
* This file is part of CopperSpice.
*
* CopperSpice is free software: you can redistribute it and/or 
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* CopperSpice is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with CopperSpice.  If not, see 
* <http://www.gnu.org/licenses/>.
*
***********************************************************************/

#ifndef QUNDOSTACK_H
#define QUNDOSTACK_H

#include <QtCore/qobject.h>
#include <QtCore/qstring.h>
#include <QScopedPointer>

QT_BEGIN_NAMESPACE

class QAction;
class QUndoCommandPrivate;
class QUndoStackPrivate;

#ifndef QT_NO_UNDOCOMMAND

class Q_GUI_EXPORT QUndoCommand
{
   QUndoCommandPrivate *d;

 public:
   explicit QUndoCommand(QUndoCommand *parent = 0);
   explicit QUndoCommand(const QString &text, QUndoCommand *parent = 0);
   virtual ~QUndoCommand();

   virtual void undo();
   virtual void redo();

   QString text() const;
   QString actionText() const;
   void setText(const QString &text);

   virtual int id() const;
   virtual bool mergeWith(const QUndoCommand *other);

   int childCount() const;
   const QUndoCommand *child(int index) const;

 private:
   Q_DISABLE_COPY(QUndoCommand)
   friend class QUndoStack;
};

#endif // QT_NO_UNDOCOMMAND

#ifndef QT_NO_UNDOSTACK

class Q_GUI_EXPORT QUndoStack : public QObject
{
   GUI_CS_OBJECT(QUndoStack)
   Q_DECLARE_PRIVATE(QUndoStack)

   GUI_CS_PROPERTY_READ(active, isActive)
   GUI_CS_PROPERTY_WRITE(active, setActive)
   GUI_CS_PROPERTY_READ(undoLimit, undoLimit)
   GUI_CS_PROPERTY_WRITE(undoLimit, setUndoLimit)

 public:
   explicit QUndoStack(QObject *parent = 0);
   ~QUndoStack();
   void clear();

   void push(QUndoCommand *cmd);

   bool canUndo() const;
   bool canRedo() const;
   QString undoText() const;
   QString redoText() const;

   int count() const;
   int index() const;
   QString text(int idx) const;

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

   bool isActive() const;
   bool isClean() const;
   int cleanIndex() const;

   void beginMacro(const QString &text);
   void endMacro();

   void setUndoLimit(int limit);
   int undoLimit() const;

   const QUndoCommand *command(int index) const;

   GUI_CS_SLOT_1(Public, void setClean())
   GUI_CS_SLOT_2(setClean)
   GUI_CS_SLOT_1(Public, void setIndex(int idx))
   GUI_CS_SLOT_2(setIndex)
   GUI_CS_SLOT_1(Public, void undo())
   GUI_CS_SLOT_2(undo)
   GUI_CS_SLOT_1(Public, void redo())
   GUI_CS_SLOT_2(redo)
   GUI_CS_SLOT_1(Public, void setActive(bool active = true))
   GUI_CS_SLOT_2(setActive)

   GUI_CS_SIGNAL_1(Public, void indexChanged(int idx))
   GUI_CS_SIGNAL_2(indexChanged, idx)
   GUI_CS_SIGNAL_1(Public, void cleanChanged(bool clean))
   GUI_CS_SIGNAL_2(cleanChanged, clean)
   GUI_CS_SIGNAL_1(Public, void canUndoChanged(bool canUndo))
   GUI_CS_SIGNAL_2(canUndoChanged, canUndo)
   GUI_CS_SIGNAL_1(Public, void canRedoChanged(bool canRedo))
   GUI_CS_SIGNAL_2(canRedoChanged, canRedo)
   GUI_CS_SIGNAL_1(Public, void undoTextChanged(const QString &undoText))
   GUI_CS_SIGNAL_2(undoTextChanged, undoText)
   GUI_CS_SIGNAL_1(Public, void redoTextChanged(const QString &redoText))
   GUI_CS_SIGNAL_2(redoTextChanged, redoText)

 protected:
   QScopedPointer<QUndoStackPrivate> d_ptr;

 private:
   Q_DISABLE_COPY(QUndoStack)
   friend class QUndoGroup;

};

#endif // QT_NO_UNDOSTACK

QT_END_NAMESPACE

#endif // QUNDOSTACK_H
