summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmmemento.h
blob: 2041c0e15566dc292932775379960a4888ebd46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2002 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/


#ifndef PMOBJECTMEMENTO_H
#define PMOBJECTMEMENTO_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pmvariant.h"
#include "pmcommand.h"
#include "pmmetaobject.h"
#include <tqstring.h>
#include <tqptrlist.h>

/**
 * Class used by @ref PMObjectMemento to store one value.
 *
 * Each piece of data has two IDs: The type of the povray class that stored
 * the data and a unique ID within this class.
 *
 * @see PMMementoDataIterator
 */

class PMMementoData : public PMVariant
{
public:
   /**
    * Stores an integer
    */
   PMMementoData( PMMetaObject* classType, int vID, int data );
   /**
    * Stores an unsigned integer
    */
   PMMementoData( PMMetaObject* classType, int vID, unsigned int data );
   /**
    * Stores double
    */
   PMMementoData( PMMetaObject* classType, int vID, double data );
   /**
    * Stores a boolean
    */
   PMMementoData( PMMetaObject* classType, int vID, bool data );
   /**
    * Stores a @ref PMThreeState
    */
   PMMementoData( PMMetaObject* classType, int vID, PMThreeState data );
   /**
    * Stores a string
    */
   PMMementoData( PMMetaObject* classType, int vID, const TQString& data );
   /**
    * Stores a @ref PMVector
    */
   PMMementoData( PMMetaObject* classType, int vID, const PMVector& data );
   /**
    * Stores a @ref PMColor
    */
   PMMementoData( PMMetaObject* classType, int vID, const PMColor& data );
   /**
    * Stores a pointer to a PMObject
    */
   PMMementoData( PMMetaObject* classType, int vID, PMObject* obj );

   /**
    * Returns the object type of the povray class that stored the data
    */
   PMMetaObject* objectType( ) const { return m_objectType; }
   /**
    * Returns the ID of the stored value
    */
   int valueID( ) const { return m_valueID; }
   
private:
   /**
    * Class that stored the information
    */
   PMMetaObject* m_objectType;
   /**
    * The unique ID within the m_objectType
    */
   int m_valueID;
};

typedef TQPtrList<PMMementoData> PMMementoDataList;


/**
 * Helper class to store information about changed objects in @ref PMMemento
 */
class PMObjectChange
{
public:
   /**
    * Creates change information for the object obj.
    *
    * mode is a bitwise combination of @ref PMChange constants.
    */
   PMObjectChange( PMObject* obj, int mode )
   {
      m_pObject = obj;
      m_mode = mode;
   }
   PMObject* object( ) const { return m_pObject; }
   int mode( ) const { return m_mode; }
   void addMode( int mode ) { m_mode |= mode; }
private:
   PMObject* m_pObject;
   int m_mode;
};

typedef TQPtrList<PMObjectChange> PMObjectChangeList;
typedef TQPtrListIterator<PMObjectChange> PMObjectChangeListIterator;

/**
 * Class that stores the data of povray objects for undo/redo information
 *
 * This class should provide enough functionality for most objects. If not,
 * subclass this class.
 *
 * All objects have to use the memento class of its base class or an inherited
 * one.
 *
 * Only the changed attributes of an object are saved.
 *
 * See class @ref PMMementoData to see how values are stored.
 */
class PMMemento
{
   friend class PMMementoDataIterator;
public:
   /**
    * Creates a memento for the object originator
    */
   PMMemento( PMObject* originator );
   /**
    * Deletes the memento
    */
   virtual ~PMMemento( );

   /**
    * Returns a pointer to the originator
    */
   PMObject* originator( ) const { return m_pOriginator; }
   /**
    * Returns a pointer to the memento data or 0 if this value is not
    * stored
    */
   PMMementoData* findData( PMMetaObject* classType, int vID ) const;
   /**
    * Adds the data object to the list of stored data. The menento may not
    * contain this information (objType and vID).
    *
    * The memento becomes the owner of this object.*/
   void addData( PMMementoData* data );

   /**
    * Adds an integer data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const int data );

   /**
    * Adds an unsigned data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const unsigned int data );

   /**
    * Adds an double data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const double data );

   /**
    * Adds an bool data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const bool data );

   /**
    * Adds an @ref PMThreeState data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const PMThreeState data );

   /**
    * Adds an @ref TQString data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const TQString& data );

   /**
    * Adds an @ref PMVector data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const PMVector& data );

   /**
    * Adds an @ref PMColor data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, const PMColor& data );

   /**
    * Adds an PMObject pointer data object. Will be ignored if the memento
    * already contains this data
    */
   void addData( PMMetaObject* classType, int vID, PMObject* obj );
   
   /**
    * Call this to store an id change
    */
   void addIDChange( PMMetaObject* classType, int vID, const TQString& data );

   /**
    * Returns true if the memento contains changed data
    */
   bool containsChanges( ) const { return ( m_changedObjects.count( ) > 0 ); }
   
   /**
    * Call this function, if you stored some data that changed
    * the view structure of the originator
    */
   void setViewStructureChanged( ) { addChange( PMCViewStructure ); }
   /**
    * Call this function, if you stored some data that changed
    * the name or pixmap of the originator
    */
   void setDescriptionChanged( ) { addChange( PMCDescription ); }
   
   /**
    * Returns true if the id of a @ref PMDeclare was changed
    */
   bool idChanged( ) const { return m_pIDData != 0; }
   /**
    * Returns the old id
    */
   TQString oldID( ) const;
   /**
    * If one object is changed, other objects can be changed as well.
    *
    * For example, if the linked declare of an object link is changed,
    * the old and new declare are changed as well (Remove the old link,
    * add the new link).
    *
    * This function returns an iterator to the list of all changed objects.
    * The first object is the originator.
    */
   PMObjectChangeListIterator changedObjects( )
   { return PMObjectChangeListIterator( m_changedObjects ); }
   /**
    * Adds the object to the list of changed objects.
    * Note that the originator is added automatically
    */
   void addChangedObject( PMObject* obj, int mode );
protected:
   /**
    * Adds the change to the originator change object
    */
   void addChange( int mode );
private:   
   /**
    * The stored values
    */
   PMMementoDataList m_data;
   /**
    * Additional pointer to the memento data for id changes of
    * @ref PMDeclare objects
    */
   PMMementoData* m_pIDData;
   /**
    * List of changes
    */
   TQPtrList<PMObjectChange> m_changedObjects;
   PMObjectChange* m_pOriginatorChange;
   PMObject* m_pOriginator;
};


/**
 * Iterator for memento data
 */
class PMMementoDataIterator : public TQPtrListIterator<PMMementoData>
{
public:
   PMMementoDataIterator( const PMMemento& m )
         : TQPtrListIterator<PMMementoData>( m.m_data )
   {
   }
   PMMementoDataIterator( const PMMemento* m )
         : TQPtrListIterator<PMMementoData>( m->m_data )
   {
   }
   PMMementoDataIterator( const PMMementoDataList& l )
         : TQPtrListIterator<PMMementoData>( l )
   {
   }
};

#endif