Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
Toshihiro Shimizu 890ddd
#ifndef T_UNDO_INCLUDED
Toshihiro Shimizu 890ddd
#define T_UNDO_INCLUDED
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qobject></qobject>
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
//    TUndo  class
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TUndo {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  bool m_isLastInBlock;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TUndo() {}
Shinya Kitaoka 120a6e
  virtual ~TUndo() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual int getSize() const = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual void undo() const = 0;
Shinya Kitaoka 120a6e
  virtual void redo() const = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual void onAdd() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // for history viewer panel
Shinya Kitaoka 120a6e
  virtual QString getHistoryString() {
Shinya Kitaoka 120a6e
    return QObject::tr("Unidentified Action");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  virtual int getHistoryType() { return 0; }  // HistoryType::Unidentified
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Noncopyable
Shinya Kitaoka 120a6e
  TUndo(const TUndo &undo);
Shinya Kitaoka 120a6e
  TUndo &operator=(const TUndo &undo);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
//    TUndoManager  class
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class DVAPI TUndoManager final : public QObject {
Shinya Kitaoka 120a6e
  Q_OBJECT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TUndoManager();
Shinya Kitaoka 120a6e
  ~TUndoManager();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static TUndoManager *manager();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void beginBlock();
Shinya Kitaoka 120a6e
  void endBlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void add(TUndo *undo);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool undo();
Shinya Kitaoka 120a6e
  bool redo();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void reset();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*!If forward = false remove n elements in the stack before m_imp->m_current;
Shinya Kitaoka 120a6e
else if forward = true remove n elements in the stack after m_imp->m_current.
Shinya Kitaoka 120a6e
If n = -1 remove all elements before or after m_imp->m_current.*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void popUndo(int n = -1, bool forward = false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //! undo() and redo() methods call TUndoManager::manager()->skip() if
Shinya Kitaoka 120a6e
  //! they want another undo/redo performed
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void skip();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //! Set the size of memory used for undo redo.
Shinya Kitaoka 120a6e
  //! \b memorySyze is expressed in MB
Shinya Kitaoka 120a6e
  void setUndoMemorySize(int memorySyze);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // for history viewer pane
Shinya Kitaoka 120a6e
  int getHistoryCount();
Shinya Kitaoka 120a6e
  int getCurrentHistoryIndex();
Shinya Kitaoka 120a6e
  TUndo *getUndoItem(int index);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Q_SIGNALS:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void historyChanged();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void somethingChanged();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  struct TUndoManagerImp;
Shinya Kitaoka 120a6e
  std::unique_ptr<tundomanagerimp> m_imp;</tundomanagerimp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Noncopyable
Shinya Kitaoka 120a6e
  TUndoManager(const TUndoManager &);
Shinya Kitaoka 120a6e
  TUndoManager &operator=(const TUndoManager &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
//    TUndoScopedBlock  class
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct TUndoScopedBlock {
Shinya Kitaoka 120a6e
  TUndoScopedBlock() { TUndoManager::manager()->beginBlock(); }
Shinya Kitaoka 120a6e
  ~TUndoScopedBlock() { TUndoManager::manager()->endBlock(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Noncopyable
Shinya Kitaoka 120a6e
  TUndoScopedBlock(const TUndoScopedBlock &);
Shinya Kitaoka 120a6e
  TUndoScopedBlock &operator=(const TUndoScopedBlock &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // T_UNDO_INCLUDED