tahoma2d/toonz/sources/include/tundo.h

133 lines
3 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef T_UNDO_INCLUDED
#define T_UNDO_INCLUDED
2016-04-14 22:15:09 +12:00
#include <memory>
2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tsmartpointer.h"
// Qt includes
#include <QObject>
#include <QString>
#undef DVAPI
#undef DVVAR
#ifdef TNZCORE_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
//************************************************************************
// TUndo class
//************************************************************************
2016-06-15 18:43:10 +12:00
class DVAPI TUndo {
2016-03-19 06:57:51 +13:00
public:
2021-01-15 12:17:57 +13:00
// To be called in the last of the block when undo
2016-06-15 18:43:10 +12:00
bool m_isLastInBlock;
2021-01-15 12:17:57 +13:00
// To be called in the last of the block when redo
bool m_isLastInRedoBlock;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TUndo() {}
virtual ~TUndo() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual int getSize() const = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void undo() const = 0;
virtual void redo() const = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void onAdd() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// for history viewer panel
virtual QString getHistoryString() {
return QObject::tr("Unidentified Action");
}
virtual int getHistoryType() { return 0; } // HistoryType::Unidentified
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// Noncopyable
TUndo(const TUndo &undo);
TUndo &operator=(const TUndo &undo);
2016-03-19 06:57:51 +13:00
};
//************************************************************************
// TUndoManager class
//************************************************************************
class DVAPI TUndoManager final : public QObject {
2016-06-15 18:43:10 +12:00
Q_OBJECT
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TUndoManager();
~TUndoManager();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
static TUndoManager *manager();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void beginBlock();
void endBlock();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void add(TUndo *undo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool undo();
bool redo();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void reset();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*!If forward = false remove n elements in the stack before m_imp->m_current;
else if forward = true remove n elements in the stack after m_imp->m_current.
If n = -1 remove all elements before or after m_imp->m_current.*/
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void popUndo(int n = -1, bool forward = false);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! undo() and redo() methods call TUndoManager::manager()->skip() if
//! they want another undo/redo performed
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void skip();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! Set the size of memory used for undo redo.
//! \b memorySyze is expressed in MB
void setUndoMemorySize(int memorySyze);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// for history viewer pane
int getHistoryCount();
int getCurrentHistoryIndex();
TUndo *getUndoItem(int index);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
Q_SIGNALS:
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void historyChanged();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void somethingChanged();
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
struct TUndoManagerImp;
std::unique_ptr<TUndoManagerImp> m_imp;
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// Noncopyable
TUndoManager(const TUndoManager &);
TUndoManager &operator=(const TUndoManager &);
2016-03-19 06:57:51 +13:00
};
//************************************************************************
// TUndoScopedBlock class
//************************************************************************
struct TUndoScopedBlock {
2016-06-15 18:43:10 +12:00
TUndoScopedBlock() { TUndoManager::manager()->beginBlock(); }
~TUndoScopedBlock() { TUndoManager::manager()->endBlock(); }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// Noncopyable
TUndoScopedBlock(const TUndoScopedBlock &);
TUndoScopedBlock &operator=(const TUndoScopedBlock &);
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
#endif // T_UNDO_INCLUDED