Home

qtundo.h

This is the verbatim text of the qtundo.h include file. It is provided only for illustration; the copyright remains with Trolltech.


#ifndef QTUNDO_H
#define QTUNDO_H

#include <qobject.h>
#include <qmap.h>
#include <qptrlist.h>
#include <qlistbox.h>
#include <qstringlist.h>

class QWidget;
class QAction;
class QtUndoStack;

class QtCommand : public QObject
{
    Q_OBJECT

    friend class QtUndoStack;

    public:
	enum Type { Command, MacroBegin, MacroEnd };

	QtCommand(Type type, const QString &description = QString::null,
			bool canMerge = false);
	QtCommand(const QString &description = QString::null,
			bool canMerge = true);

	virtual void redo() {};
	virtual void undo() {};

	QString description() const
	    { return m_description; }
	void setDescription(const QString &s)
	    { m_description = s; }
	bool canMerge() const
	    { return m_can_merge; }
	void setCanMerge(bool b)
	    { m_can_merge = b; }
	Type type() const
	    { return m_type; }

	bool isMacroBegin() const
	    { return m_type == MacroBegin; }
	bool isMacroEnd() const
	    { return m_type == MacroEnd; }
	bool isCommand() const
	    { return m_type == Command; }

    protected:
	virtual bool mergeMeWith(QtCommand *other);

    private:
	void shortenStack();

	bool m_can_merge;
	QString m_description;
	Type m_type;
};

struct QtUndoState;

class QtUndoStack : public QObject, private QPtrList<QtCommand>
{
    Q_OBJECT

    friend class QtUndoManager;

    public:
	QtUndoStack(QObject *parent = 0, const char *name = 0);
	void push(QtCommand *command);
	bool canUndo() const;
	bool canRedo() const;
	QString undoDescription() const;
	QString redoDescription() const;
	QStringList undoList() const;
	QStringList redoList() const;
	bool isClean() const;

	QAction *createUndoAction(QWidget *parent) const;
	QAction *createRedoAction(QWidget *parent) const;

    public slots:
	void undo(int count = 1);
	void redo(int count = 1);
	void clear();

	void setClean();

    signals:
    	void cleanChanged(bool clean);
	void commandExecuted();

	void undoDescriptionChanged(const QString &newDescription);
	void redoDescriptionChanged(const QString &newDescription);
	void canUndoChanged(bool enabled);
	void canRedoChanged(bool enabled);

    private:
	typedef QPtrListIterator<QtCommand> CommandIter;

	void undoMacro();
	void redoMacro();
	CommandIter findMacroBegin(CommandIter it) const;
	CommandIter findMacroEnd(CommandIter it) const;

        void beforeChange(QtUndoState &state);
        void afterChange(const QtUndoState &state);

	// *m_current_iter == 0 means "one-before-first"
	CommandIter m_current_iter;
	uint m_num_commands;
	int m_macro_nest;

	bool m_have_clean_command;
	const QtCommand *m_clean_command;
};

class QtUndoManager : public QObject
{
    Q_OBJECT

    public:
	QAction *createUndoAction(QWidget *parent) const;
	QAction *createRedoAction(QWidget *parent) const;

	void associateView(QObject *obj, QtUndoStack *stack);
	void disassociateView(QObject *obj);

	bool canUndo() const;
	bool canRedo() const;
	QString undoDescription() const;
	QString redoDescription() const;
	void setUndoLimit(uint i);
	uint undoLimit() const;
	QStringList undoList() const;
	QStringList redoList() const;

	static QtUndoManager *manager();

	virtual bool eventFilter(QObject *obj, QEvent *e);

    public slots:
	void undo(int count = 1);
	void redo(int count = 1);

	void updateActions();

    signals:
	void changed();

	void undoDescriptionChanged(const QString &newDescription);
	void redoDescriptionChanged(const QString &newDescription);
	void canUndoChanged(bool enabled);
	void canRedoChanged(bool enabled);

    private slots:
	void stackDestroyed(QObject *stack);
	void viewDestroyed(QObject *view);

    private:
	typedef QMap<QObject*, QtUndoStack*> StackMap;

	QtUndoManager();
	QtUndoStack *currentStack() const;
        QtUndoStack *focusStack() const;

	StackMap m_stack_map;
	mutable QtUndoStack *m_current_stack;

	static QtUndoManager *m_manager; // singleton
	static uint m_undo_limit;

	bool m_can_undo, m_can_redo;
	QString m_undo_description, m_redo_description;
};

class QtUndoListBox : public QListBox
{
    Q_OBJECT

    public:
	QtUndoListBox(QWidget *parent = 0, const char *name = 0);

    public slots:
	void updateContents();

    private slots:
    	void undoOrRedo();

    private:
	int m_undo_idx;
	bool m_dont_update;
};

#endif

Copyright © 2003-2006 TrolltechTrademarks
Qt Solutions