1 // -*- C++ -*-
2 
3 /*
4  * GChemPaint library
5  * operation.h
6  *
7  * Copyright (C) 2002-2008 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 /*!\file*/
26 #ifndef GCHEMPAINT_OPERATION_H
27 #define GCHEMPAINT_OPERATION_H
28 
29 #include <gcu/macros.h>
30 #include <gcu/object.h>
31 
32 /*!\file*/
33 namespace gcp {
34 
35 class Document;
36 
37 /*!\enum OperationType gcp/operation.h
38 Enumeration of the different operation types See gcp::Document::GetNewOeration()
39 for its use.
40 */
41 typedef enum
42 {
43 /*!
44 Object addition operation, see the AddOperation class.
45 */
46 	GCP_ADD_OPERATION,
47 /*!
48 Object deletion operation, see the DeleteOperation class.
49 */
50 	GCP_DELETE_OPERATION,
51 /*!
52 Object modification operation, see the ModifyOperation class.
53 */
54 	GCP_MODIFY_OPERATION,
55 } OperationType;
56 
57 /*!\class Operation gcp/operation.h
58 Base operation class for the Undo/Redo framework.
59 This class is virtual since some methods are pure virtual.
60 */
61 class Operation
62 {
63 public:
64 /*!
65 @param pDoc a document.
66 @param ID a unique operation ID for the document and the session.
67 
68 Creates a new operation. Operations should always created by calls to
69 Document::GetNewOperation().
70 */
71 	Operation (Document *pDoc, unsigned long ID);
72 	virtual ~Operation ();
73 
74 /*!
75 Undo the changes represented by this operation.
76 */
77 	virtual void Undo () = 0;
78 /*!
79 Redo the changes represented by this operation.
80 */
81 	virtual void Redo () = 0;
82 /*!
83 @param pObject an Object affected by the changes.
84 @param type a number indicationg the role of the stored objects.
85 
86 The \a type argument is only significant for the gcp::ModifyOperation class
87 where 0 represent the state of the objects before the operation, and 1 the
88 state of the objects after the operation.
89 
90 Adds an object to the operation.
91 Typically, modifying an object whould need code like:
92 \code
93 	Object *obj;
94 	// Initialize the object pointer so that it points to a valid object
95 		...
96 	Document *doc = obj->GetDocument ();
97 	Operation *op = doc->GetNewOperation (GCP_MODIFY_OPERATION);
98 	op->AddObject (obj, 0);
99 	// Modify the object
100 		...
101 	op->AddObject (obj, 1);
102 	doc->FinishOperation ();
103 \endcode
104 */
105 	virtual void AddObject (gcu::Object* pObject, unsigned type = 0);
106 /*!
107 @param node an xml node related to the changes.
108 @param type a number indicationg the role of the stored objects.
109 
110 The \a type argument is only significant for the gcp::ModifyOperation class
111 where 0 represent the state of the objects before the operation, and 1 the
112 state of the objects after the operation.
113 
114 Adds the node to the document owning the operation. This might be used when
115 Objects are not available such as when editing text.
116 */
117 	virtual void AddNode (xmlNodePtr node, unsigned type = 0);
118 
119 protected:
120 /*!
121 @param type a number indicationg the role of the stored objects.
122 
123 The \a type argument is only significant for the gcp::ModifyOperation class
124 where 0 represent the state of the objects before the operation, and 1 the
125 state of the objects after the operation.
126 
127 Adds the stored objects to the document owning the operation.
128 */
129 	void Add (unsigned type = 0);
130 /*!
131 @param type a number indicationg the role of the stored objects.
132 
133 The \a type argument is only significant for the gcp::ModifyOperation class
134 where 0 represent the state of the objects before the operation, and 1 the
135 state of the objects after the operation.
136 
137 Deletes the stored objects to the document owning the operation.
138 */
139 	void Delete (unsigned type = 0);
140 
141 protected:
142 /*!
143 The xml nodes storing the changes.
144 */
145 	xmlNodePtr* m_Nodes;
146 
147 private:
148 	gcp::Document* m_pDoc;
149 
150 GCU_RO_PROP (unsigned long, ID);
151 };
152 
153 /*!\class AddOperation gcp/operation.h
154 Operation class representing objects additions.
155 */
156 class AddOperation: public Operation
157 {
158 public:
159 /*!
160 @param pDoc a document.
161 @param ID a unique operation ID for the document and the session.
162 
163 Creates a new AddOperation. Operations should always created by calls to
164 Document::GetNewOperation().
165 */
166 	AddOperation (gcp::Document *pDoc, unsigned long ID);
167 	virtual ~AddOperation ();
168 
169 /*!
170 Undo the additions represented by this operation.
171 */
172 	void Undo ();
173 /*!
174 Redo the additions represented by this operation.
175 */
176 	void Redo ();
177 };
178 
179 /*!\class DeleteOperation gcp/operation.h
180 Operation class representing objects deletions.
181 */
182 class DeleteOperation: public Operation
183 {
184 public:
185 /*!
186 @param pDoc a document.
187 @param ID a unique operation ID for the document and the session.
188 
189 Creates a new DeleteOperation. Operations should always created by calls to
190 Document::GetNewOperation().
191 */
192 	DeleteOperation (gcp::Document *pDoc, unsigned long ID);
193 	virtual ~DeleteOperation ();
194 
195 /*!
196 Undo the deletions represented by this operation.
197 */
198 	void Undo ();
199 /*!
200 Redo the deletions represented by this operation.
201 */
202 	void Redo ();
203 };
204 
205 /*!\class ModifyOperation gcp/operation.h
206 Operation class representing objects modifications.
207 */
208 class ModifyOperation: public Operation
209 {
210 public:
211 /*!
212 @param pDoc a document.
213 @param ID a unique operation ID for the document and the session.
214 
215 Creates a new ModifyOperation. Operations should always created by calls to
216 Document::GetNewOperation().
217 */
218 	ModifyOperation (gcp::Document *pDoc, unsigned long ID);
219 	virtual ~ModifyOperation ();
220 
221 /*!
222 Undo the modifications represented by this operation.
223 */
224 	void Undo ();
225 /*!
226 Redo the modifications represented by this operation.
227 */
228 	void Redo ();
229 };
230 
231 }	// namespace gcp
232 
233 #endif //GCHEMPAINT_OPERATION_H
234