|         | 
     1 /* | 
|         | 
     2  *  LDForge: LDraw parts authoring CAD | 
|         | 
     3  *  Copyright (C) 2013, 2014 Santeri Piippo | 
|         | 
     4  * | 
|         | 
     5  *  This program is free software: you can redistribute it and/or modify | 
|         | 
     6  *  it under the terms of the GNU General Public License as published by | 
|         | 
     7  *  the Free Software Foundation, either version 3 of the License, or | 
|         | 
     8  *  (at your option) any later version. | 
|         | 
     9  * | 
|         | 
    10  *  This program is distributed in the hope that it will be useful, | 
|         | 
    11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|         | 
    12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|         | 
    13  *  GNU General Public License for more details. | 
|         | 
    14  * | 
|         | 
    15  *  You should have received a copy of the GNU General Public License | 
|         | 
    16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|         | 
    17  */ | 
|         | 
    18  | 
|         | 
    19 #pragma once | 
|         | 
    20 #include "main.h" | 
|         | 
    21 #include "ldObject.h" | 
|         | 
    22  | 
|         | 
    23 #define IMPLEMENT_HISTORY_TYPE(N)							\ | 
|         | 
    24 	virtual ~N##History() {}								\ | 
|         | 
    25 	virtual void undo() const override;						\ | 
|         | 
    26 	virtual void redo() const override;						\ | 
|         | 
    27 															\ | 
|         | 
    28 	virtual History::EHistoryType getType() const override	\ | 
|         | 
    29 	{														\ | 
|         | 
    30 		return History::E##N##History;						\ | 
|         | 
    31 	}														\ | 
|         | 
    32 															\ | 
|         | 
    33 	virtual QString getTypeName() const						\ | 
|         | 
    34 	{														\ | 
|         | 
    35 		return #N;											\ | 
|         | 
    36 	} | 
|         | 
    37  | 
|         | 
    38 class AbstractHistoryEntry; | 
|         | 
    39  | 
|         | 
    40 // ============================================================================= | 
|         | 
    41 class History | 
|         | 
    42 { | 
|         | 
    43 	PROPERTY (private,	int,			position,	setPosition,	STOCK_WRITE) | 
|         | 
    44 	PROPERTY (public,	LDDocument*,	document,	setDocument,	STOCK_WRITE) | 
|         | 
    45 	PROPERTY (public,	bool,			isIgnoring,	setIgnoring,	STOCK_WRITE) | 
|         | 
    46  | 
|         | 
    47 	public: | 
|         | 
    48 		typedef QList<AbstractHistoryEntry*> Changeset; | 
|         | 
    49  | 
|         | 
    50 		enum EHistoryType | 
|         | 
    51 		{ | 
|         | 
    52 			EDelHistory, | 
|         | 
    53 			EEditHistory, | 
|         | 
    54 			EAddHistory, | 
|         | 
    55 			EMoveHistory, | 
|         | 
    56 			ESwapHistory, | 
|         | 
    57 		}; | 
|         | 
    58  | 
|         | 
    59 		History(); | 
|         | 
    60 		void undo(); | 
|         | 
    61 		void redo(); | 
|         | 
    62 		void clear(); | 
|         | 
    63  | 
|         | 
    64 		void addStep(); | 
|         | 
    65 		void add (AbstractHistoryEntry* entry); | 
|         | 
    66  | 
|         | 
    67 		inline long getSize() const | 
|         | 
    68 		{ | 
|         | 
    69 			return m_changesets.size(); | 
|         | 
    70 		} | 
|         | 
    71  | 
|         | 
    72 		inline History& operator<< (AbstractHistoryEntry* entry) | 
|         | 
    73 		{ | 
|         | 
    74 			add (entry); | 
|         | 
    75 			return *this; | 
|         | 
    76 		} | 
|         | 
    77  | 
|         | 
    78 		inline const Changeset& getChangeset (long pos) const | 
|         | 
    79 		{ | 
|         | 
    80 			return m_changesets[pos]; | 
|         | 
    81 		} | 
|         | 
    82  | 
|         | 
    83 	private: | 
|         | 
    84 		Changeset m_currentChangeset; | 
|         | 
    85 		QList<Changeset> m_changesets; | 
|         | 
    86 }; | 
|         | 
    87  | 
|         | 
    88 // ============================================================================= | 
|         | 
    89 // | 
|         | 
    90 class AbstractHistoryEntry | 
|         | 
    91 { | 
|         | 
    92 	PROPERTY (public,	History*,	parent,	setParent,	STOCK_WRITE) | 
|         | 
    93  | 
|         | 
    94 	public: | 
|         | 
    95 		virtual ~AbstractHistoryEntry() {} | 
|         | 
    96 		virtual void undo() const = 0; | 
|         | 
    97 		virtual void redo() const = 0; | 
|         | 
    98 		virtual History::EHistoryType getType() const = 0; | 
|         | 
    99 		virtual QString getTypeName() const = 0; | 
|         | 
   100 }; | 
|         | 
   101  | 
|         | 
   102 // ============================================================================= | 
|         | 
   103 // | 
|         | 
   104 class DelHistory : public AbstractHistoryEntry | 
|         | 
   105 { | 
|         | 
   106 	PROPERTY (private,	int,		index,	setIndex,	STOCK_WRITE) | 
|         | 
   107 	PROPERTY (private,	QString,	code,	setCode,	STOCK_WRITE) | 
|         | 
   108  | 
|         | 
   109 	public: | 
|         | 
   110 		IMPLEMENT_HISTORY_TYPE (Del) | 
|         | 
   111 		DelHistory (int idx, LDObject* obj); | 
|         | 
   112 }; | 
|         | 
   113  | 
|         | 
   114 // ============================================================================= | 
|         | 
   115 // | 
|         | 
   116 class EditHistory : public AbstractHistoryEntry | 
|         | 
   117 { | 
|         | 
   118 	PROPERTY (private,	int, 		index,		setIndex,	STOCK_WRITE) | 
|         | 
   119 	PROPERTY (private,	QString,	oldCode,	setOldCode,	STOCK_WRITE) | 
|         | 
   120 	PROPERTY (private,	QString,	newCode,	setNewCode,	STOCK_WRITE) | 
|         | 
   121  | 
|         | 
   122 	public: | 
|         | 
   123 		IMPLEMENT_HISTORY_TYPE (Edit) | 
|         | 
   124  | 
|         | 
   125 		EditHistory (int idx, QString oldCode, QString newCode) : | 
|         | 
   126 			m_index (idx), | 
|         | 
   127 			m_oldCode (oldCode), | 
|         | 
   128 			m_newCode (newCode) {} | 
|         | 
   129 }; | 
|         | 
   130  | 
|         | 
   131 // ============================================================================= | 
|         | 
   132 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 
|         | 
   133 // ============================================================================= | 
|         | 
   134 class AddHistory : public AbstractHistoryEntry | 
|         | 
   135 { | 
|         | 
   136 	PROPERTY (private,	int,		index,	setIndex,	STOCK_WRITE) | 
|         | 
   137 	PROPERTY (private,	QString,	code,	setCode,	STOCK_WRITE) | 
|         | 
   138  | 
|         | 
   139 	public: | 
|         | 
   140 		IMPLEMENT_HISTORY_TYPE (Add) | 
|         | 
   141  | 
|         | 
   142 		AddHistory (int idx, LDObject* obj) : | 
|         | 
   143 			m_index (idx), | 
|         | 
   144 			m_code (obj->asText()) {} | 
|         | 
   145 }; | 
|         | 
   146  | 
|         | 
   147 // ============================================================================= | 
|         | 
   148 // | 
|         | 
   149 class MoveHistory : public AbstractHistoryEntry | 
|         | 
   150 { | 
|         | 
   151 	public: | 
|         | 
   152 		IMPLEMENT_HISTORY_TYPE (Move) | 
|         | 
   153  | 
|         | 
   154 		QList<int> indices; | 
|         | 
   155 		Vertex dest; | 
|         | 
   156  | 
|         | 
   157 		MoveHistory (QList<int> indices, Vertex dest) : | 
|         | 
   158 				indices (indices), | 
|         | 
   159 				dest (dest) {} | 
|         | 
   160 }; | 
|         | 
   161  | 
|         | 
   162 // ============================================================================= | 
|         | 
   163 // | 
|         | 
   164 class SwapHistory : public AbstractHistoryEntry | 
|         | 
   165 { | 
|         | 
   166 	public: | 
|         | 
   167 		IMPLEMENT_HISTORY_TYPE (Swap) | 
|         | 
   168  | 
|         | 
   169 		SwapHistory (int a, int b) : | 
|         | 
   170 			a (a), | 
|         | 
   171 			b (b) {} | 
|         | 
   172  | 
|         | 
   173 	private: | 
|         | 
   174 		int a, b; | 
|         | 
   175 }; |