summaryrefslogtreecommitdiffstats
path: root/karbon/core/vobject.h
blob: eccc6180b2862e4702833a7514acde02e8b3adc1 (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
/* This file is part of the KDE project
   Copyright (C) 2001, The Karbon Developers
   Copyright (C) 2002, The Karbon Developers

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef __VOBJECT_H__
#define __VOBJECT_H__


#include <KoRect.h>
#include <dcopobject.h>
#include <koffice_export.h>

class TQDomElement;
class VDocument;
class VFill;
class VPainter;
class VStroke;
class VVisitor;
class DCOPObject;
class KoStore;
class KoXmlWriter;
class KoOasisLoadingContext;
class KoGenStyles;
class KoGenStyle;

/**
 * The base class for all karbon objects. Every object should
 * have the ability to draw itself using a painter, perform
 * hit detection, transform on demand, clone and load/save itself.
 * Also each object manages its own bounding box and keeps track of its
 * parent object.
 */
class KARBONBASE_EXPORT VObject
{
public:
	enum VState
	{
		normal        = 0,	/**< visible, not active */
		normal_locked = 1,	/**< visible, but locked (r/o) */
		hidden        = 2,	/**< hidden */
		hidden_locked = 3,	/**< hidden and locked (r/o) */
		deleted       = 4,	/**< deleted, nearly dead */

		// shape specific states:
		selected      = 5,	/**< visible, active and can be manipulated by tools */
		edit          = 6	/**< visible, active and is currently manipulated by a tool */
	};

	/**
	 * Constructs a new object that is child of parent and has the given state.
	 *
	 * @param parent the new object's parent
	 * @param state the new object's state
	 */
	VObject( VObject* parent, VState state = edit );
	
	/**
	 * Copy constructor.
	 * Copies parent, state and name of given object.
	 *
	 * @param obj the object to copy properties from
	 */
	VObject( const VObject& obj );

	/** 
	 * Destroys the object and deletes the stroke, fill and DCOP-object.
	 */
	virtual ~VObject();

	/**
	 * Returns pointer to internal DCOP object.
	 *
	 * If no internal DCOP object exist yet, it is created.
	 */
	virtual DCOPObject* dcopObject();

	/**
	 * Draw the object to a painting device.
	 *
	 * @param painter abstraction that is used to render to a painting device.
	 * @param rect represents the visible rectangular area. If this object doesn't
	 *             intersect with this area it is not drawn.
	 */
	virtual void draw( VPainter* painter, const KoRect* rect = 0L ) const 
	{ 
		Q_UNUSED( painter );
		Q_UNUSED( rect );
	}

	/**
	 * Calculates the tightest bounding box around the object.
	 *
	 * @return the bounding box.
	 */
	virtual const KoRect& boundingBox() const
		{ return m_boundingBox; }

	/**
	 * Checks if the bounding box is invalid and needs to be recalculated.
	 *
	 * @return true if bounding box is invalid.
	 */
	bool boundingBoxIsInvalid() const
		{ return m_boundingBoxIsInvalid; }

	/**
	 * Invalidates the bounding box, so it has to be recalculated.
	 * This function is public so visitors can access it themself at the right
	 * time when they manipulate many VSegments.
	 */
	void invalidateBoundingBox()
	{
		m_boundingBoxIsInvalid = true;

		if( m_parent )
			m_parent->invalidateBoundingBox();
	}

	/**
	 * Sets a new parent object.
	 *
	 * @param parent the new parent object
	 */
	void setParent( VObject* parent ) { m_parent = parent; }

	/**
	 * Returns pointer to current parent object.
	 *
	 * @return pointer to current parent object or 0 if no parent object is set
	 */
	VObject* parent() const { return m_parent; }

	/**
	 * Get the state the object is in.
	 *
	 * @return the object state at time of calling.
	 */
	VState state() const { return m_state; }

	/**
	 * Sets the state to a specified new state.
	 * Note that this will not have any visual effect until draw() is
	 * called on this object.
	 *
	 * @param state the new state.
	 */
	virtual void setState( const VState state ) { m_state = state; }

	/**
	 * Gets the object's actual stroke.
	 *
	 * @return pointer to the object's stroke
	 */
	virtual VStroke* stroke() const { return m_stroke; }

	/**
	 * Gets the object's actual fill.
	 *
	 * @return pointer to the object's fill
	 */
	virtual VFill* fill() const { return m_fill; }

	/**
	 * Sets the stroke to a given new stroke.
	 *
	 * @param stroke the new stroke
	 */
	virtual void setStroke( const VStroke& stroke );

	/**
	 * Sets the fill to a given new fill.
	 *
	 * @param fill the new fill
	 */
	virtual void setFill( const VFill& fill );

	/**
	 * Save this object's state to xml.
	 * 
	 * @param element the DOM element to which the attributes are saved
	 */
	virtual void save( TQDomElement& element ) const;

	/**
	 * Save this object's state to OpenDocument.
	 *
	 * @param store FIXME
	 * @param docWriter FIXME
	 * @param mainStyles FIXME
	 */
	virtual void saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const;

	/**
	 * Load this object's state from xml and initialize
	 * this object accordingly.
	 *
	 * @param element the DOM element from which the attributes are read
	 */
	virtual void load( const TQDomElement& element );

	/**
	 * Load this object's state from OpenDocument and initialize
	 * this object accordingly.
	 *
	 * @param element the DOM element to read attributes from
	 * @param context FIXME
	 */
	virtual bool loadOasis( const TQDomElement &element, KoOasisLoadingContext &context );

	/**
	 * Create an exact copy of this object.
	 *
	 * @return the exact object copy
	 */
	virtual VObject* clone() const = 0;

	/** 
	 * Accept a VVisitor.
	 */
	virtual void accept( VVisitor& /*visitor*/ ) 
		{ }

	/**
	 * This function is important for undo/redo. It inserts newObject in front
	 * of oldObject.
	 *
	 * @param newObject the new object to insert
	 * @param oldObject the old object the new object is inserted in front of
	 */
	virtual void insertInfrontOf( VObject* newObject, VObject* oldObject )
	{ 
		Q_UNUSED( newObject );
		Q_UNUSED( oldObject );
	}

	/**
	 * Returns the name of the object.
	 *
	 * @return the object's name
	 */
	virtual TQString name() const;
	
	/**
	 * Sets the object's name to a given new name.
	 *
	 * @param s the new object name 
	 */
	void setName( const TQString &s );

	/**
	 * Return document the object belongs to.
	 *
	 * @return pointer to parent document or 0 if object does not belong to a document
	 */
	VDocument *document() const;

protected:
	/**
	 * Adds a new given style to the specified OASIS context
	 *
	 * @param style FIXME
	 * @param context FIXME
	 */
	void addStyles( const TQDomElement* style, KoOasisLoadingContext & context );

	virtual void saveOasisFill( KoGenStyles &mainStyles, KoGenStyle &stylesojectauto ) const;

protected:
	mutable KoRect m_boundingBox; /**< the object's bounding box */
	mutable VState m_state				: 8; /**< the object's state */
	mutable bool m_boundingBoxIsInvalid : 1; /**< the flag stating if the bounding box is valid */

	VStroke* m_stroke; /**< the object's stroke */
	VFill* m_fill; /**< the object's fill */

	DCOPObject *m_dcop; /**< the object's DCOP object */

private:
	VObject* m_parent;
};

#endif