karbon

vgroup.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <tqdom.h>
25
26#include <KoStore.h>
27#include <KoXmlWriter.h>
28#include <KoOasisLoadingContext.h>
29#include <KoStyleStack.h>
30#include <KoXmlNS.h>
31
32#include "vcomposite.h"
33#include "shapes/vellipse.h"
34#include "shapes/vrectangle.h"
35#include "shapes/vsinus.h"
36#include "shapes/vspiral.h"
37#include "shapes/vstar.h"
38#include "shapes/vpolyline.h"
39#include "shapes/vpolygon.h"
40#include "vfill.h"
41#include "vgroup.h"
42#include "vlayer.h"
43#include "vimage.h"
44#include "vstroke.h"
45#include "vvisitor.h"
46#include "vclipgroup.h"
47#ifdef HAVE_KARBONTEXT
48#include "vtext.h"
49#endif
50
51#include <kdebug.h>
52
53
54VGroup::VGroup( VObject* parent, VState state )
55 : VObject( parent, state )
56{
57 m_stroke = new VStroke( this );
58 m_fill = new VFill();
59}
60
61VGroup::VGroup( const VGroup& group )
62 : VObject( group )
63{
64 m_stroke = new VStroke( *group.m_stroke );
65 m_stroke->setParent( this );
66 m_fill = new VFill( *group.m_fill );
67
68 VObjectListIterator itr = group.m_objects;
69 for ( ; itr.current() ; ++itr )
70 append( itr.current()->clone() );
71}
72
74{
75 VObjectListIterator itr = m_objects;
76 for ( ; itr.current(); ++itr )
77 {
78 delete( itr.current() );
79 }
80}
81
82void
83VGroup::draw( VPainter* painter, const KoRect* rect ) const
84{
85 if(
86 state() == deleted ||
87 state() == hidden ||
88 state() == hidden_locked )
89 {
90 return;
91 }
92
93 VObjectListIterator itr = m_objects;
94
95 for ( ; itr.current(); ++itr )
96 itr.current()->draw( painter, rect );
97}
98
99const KoRect&
101{
103 {
104 // clear:
105 m_boundingBox = KoRect();
106
107 VObjectListIterator itr = m_objects;
108 for( ; itr.current(); ++itr )
109 {
110 m_boundingBox |= itr.current()->boundingBox();
111 }
112
114 }
115
116 return m_boundingBox;
117}
118
119VGroup*
121{
122 return new VGroup( *this );
123}
124
125void
126VGroup::setFill( const VFill& fill )
127{
128 VObjectListIterator itr = m_objects;
129
130 for ( ; itr.current() ; ++itr )
131 itr.current()->setFill( fill );
132
134}
135
136void
138{
139 VObjectListIterator itr = m_objects;
140
141 for ( ; itr.current() ; ++itr )
142 itr.current()->setStroke( stroke );
143
145}
146
147void
149{
150 VObjectListIterator itr = m_objects;
151
152 for ( ; itr.current() ; ++itr )
153 if( m_state == VObject::deleted || itr.current()->state() != VObject::deleted )
154 itr.current()->setState( state );
155
157}
158
159void
160VGroup::save( TQDomElement& element ) const
161{
162 if( state() != deleted )
163 {
164 TQDomElement me = element.ownerDocument().createElement( "GROUP" );
165 element.appendChild( me );
166
167 // save objects:
168 VObjectListIterator itr = m_objects;
169
170 for ( ; itr.current(); ++itr )
171 itr.current()->save( me );
172
173 VObject::save( me );
174 }
175}
176
177void
178VGroup::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
179{
180 // do not save deleted objects
181 if( state() == deleted )
182 return;
183
184 docWriter->startElement( "draw:g" );
185
186 // save objects:
187 VObjectListIterator itr = m_objects;
188
189 for ( ; itr.current(); ++itr )
190 itr.current()->saveOasis( store, docWriter, mainStyles, ++index );
191
192 docWriter->endElement();
193}
194
195bool
196VGroup::loadOasis( const TQDomElement &element, KoOasisLoadingContext &context )
197{
198 m_objects.setAutoDelete( true );
199 m_objects.clear();
200 m_objects.setAutoDelete( false );
201
202 TQDomNodeList list = element.childNodes();
203 for( uint i = 0; i < list.count(); ++i )
204 {
205 if( list.item( i ).isElement() )
206 {
207 TQDomElement e = list.item( i ).toElement();
208
209 kdDebug(38000) << "VGroup::loadOasis: e.tagName() = " << e.tagName() << endl;
210 kdDebug(38000) << "VGroup::loadOasis: e.namespaceURI() = " << e.namespaceURI() << endl;
211 kdDebug(38000) << "VGroup::loadOasis: e.localName() = " << e.localName() << endl;
212
213 if( e.namespaceURI() != KoXmlNS::draw )
214 continue;
215
216 context.styleStack().save();
217
218 if( e.localName() == "path" || e.localName() == "custom-shape" )
219 {
220 VPath* composite = new VPath( this );
221 composite->loadOasis( e, context );
222 append( composite );
223 }
224 else if( e.localName() == "circle" || e.localName() == "ellipse" )
225 {
226 VEllipse* ellipse = new VEllipse( this );
227 ellipse->loadOasis( e, context );
228 append( ellipse );
229 }
230 else if( e.localName() == "rect" )
231 {
232 VRectangle* rectangle = new VRectangle( this );
233 rectangle->loadOasis( e, context );
234 append( rectangle );
235 }
236 else if( e.localName() == "g" )
237 {
238 VGroup* group = new VGroup( this );
239 group->loadOasis( e, context );
240 append( group );
241 }
242 else if( e.localName() == "polyline" || e.localName() == "line" )
243 {
244 VPolyline* polyline = new VPolyline( this );
245 polyline->loadOasis( e, context );
246 append( polyline );
247 }
248 else if( e.localName() == "polygon" )
249 {
250 VPolygon* polygon = new VPolygon( this );
251 polygon->loadOasis( e, context );
252 append( polygon );
253 }
254
255 context.styleStack().restore();
256 }
257 }
258
259 return true;
260}
261
262void
263VGroup::load( const TQDomElement& element )
264{
265 m_objects.setAutoDelete( true );
266 m_objects.clear();
267 m_objects.setAutoDelete( false );
268
269 VObject::load( element );
270
271 TQDomNodeList list = element.childNodes();
272 for( uint i = 0; i < list.count(); ++i )
273 {
274 if( list.item( i ).isElement() )
275 {
276 TQDomElement e = list.item( i ).toElement();
277
278 if( e.tagName() == "COMPOSITE" || e.tagName() == "PATH" ) // TODO : remove COMPOSITE later
279 {
280 VPath* composite = new VPath( this );
281 composite->load( e );
282 append( composite );
283 }
284 else if( e.tagName() == "ELLIPSE" )
285 {
286 VEllipse* ellipse = new VEllipse( this );
287 ellipse->load( e );
288 append( ellipse );
289 }
290 else if( e.tagName() == "RECT" )
291 {
292 VRectangle* rectangle = new VRectangle( this );
293 rectangle->load( e );
294 append( rectangle );
295 }
296 else if( e.tagName() == "POLYLINE" )
297 {
298 VPolyline* polyline = new VPolyline( this );
299 polyline->load( e );
300 append( polyline );
301 }
302 else if( e.tagName() == "POLYGON" )
303 {
304 VPolygon* polygon = new VPolygon( this );
305 polygon->load( e );
306 append( polygon );
307 }
308 else if( e.tagName() == "SINUS" )
309 {
310 VSinus* sinus = new VSinus( this );
311 sinus->load( e );
312 append( sinus );
313 }
314 else if( e.tagName() == "SPIRAL" )
315 {
316 VSpiral* spiral = new VSpiral( this );
317 spiral->load( e );
318 append( spiral );
319 }
320 else if( e.tagName() == "STAR" )
321 {
322 VStar* star = new VStar( this );
323 star->load( e );
324 append( star );
325 }
326 else if( e.tagName() == "GROUP" )
327 {
328 VGroup* group = new VGroup( this );
329 group->load( e );
330 append( group );
331 }
332 else if( e.tagName() == "CLIP" )
333 {
334 VClipGroup* grp = new VClipGroup( this );
335 grp->load( e );
336 append( grp );
337 }
338 else if( e.tagName() == "IMAGE" )
339 {
340 VImage* img = new VImage( this );
341 img->load( e );
342 append( img );
343 }
344 else if( e.tagName() == "TEXT" )
345 {
346#ifdef HAVE_KARBONTEXT
347 VText *text = new VText( this );
348 text->load( e );
349 append( text );
350#endif
351 }
352 }
353 }
354}
355
356void
358{
359 visitor.visitVGroup( *this );
360}
361
362
363void
364VGroup::take( const VObject& object )
365{
366 m_objects.removeRef( &object );
367
369}
370
371void
373{
374 object->setParent( this );
375
376 m_objects.append( object );
377
379}
380
381void
382VGroup::insertInfrontOf( VObject* newObject, VObject* oldObject )
383{
384 newObject->setParent( this );
385
386 m_objects.insert( m_objects.find( oldObject ), newObject );
387
389}
390
391void
393{
394 m_objects.clear();
395
397}
398
Base class for clipping conglomerates the first child element is used for clipping.
Definition vclipgroup.h:34
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Manages the fill of shapes.
Definition vfill.h:45
Base class for all sort of VObject conglomerats.
Definition vgroup.h:35
void clear()
Clears the group, without destroying the grouped objects.
Definition vgroup.cpp:392
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vgroup.cpp:148
virtual VGroup * clone() const
Create an exact copy of this object.
Definition vgroup.cpp:120
virtual void insertInfrontOf(VObject *newObject, VObject *oldObject)
This function is important for undo/redo.
Definition vgroup.cpp:382
virtual void setStroke(const VStroke &stroke)
Sets the stroke to a given new stroke.
Definition vgroup.cpp:137
virtual void accept(VVisitor &visitor)
Accept a VVisitor.
Definition vgroup.cpp:357
virtual void saveOasis(KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index) const
Save this object's state to OpenDocument.
Definition vgroup.cpp:178
virtual void setFill(const VFill &fill)
Sets the fill to a given new fill.
Definition vgroup.cpp:126
void take(const VObject &object)
Removes the reference to child object, not the object itself.
Definition vgroup.cpp:364
void append(VObject *object)
Appends a new child object.
Definition vgroup.cpp:372
VGroup(VObject *parent, VState state=normal)
Constructs a new group object that is child of parent and has the given state.
Definition vgroup.cpp:54
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
Definition vgroup.cpp:100
virtual void draw(VPainter *painter, const KoRect *rect=0L) const
Draw the object to a painting device.
Definition vgroup.cpp:83
VObjectList m_objects
the list of grouped objects
Definition vgroup.h:113
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vgroup.cpp:263
virtual ~VGroup()
Destroys the group and all of the child objects.
Definition vgroup.cpp:73
virtual void save(TQDomElement &element) const
Save this object's state to xml.
Definition vgroup.cpp:160
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
Definition vgroup.cpp:196
The base class for all karbon objects.
Definition vobject.h:50
bool m_boundingBoxIsInvalid
the flag stating if the bounding box is valid
Definition vobject.h:287
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vobject.h:163
@ hidden_locked
hidden and locked (r/o)
Definition vobject.h:57
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ hidden
hidden
Definition vobject.h:56
VState m_state
the object's state
Definition vobject.h:286
VStroke * m_stroke
the object's stroke
Definition vobject.h:289
virtual void setStroke(const VStroke &stroke)
Sets the stroke to a given new stroke.
Definition vobject.cpp:84
void setParent(VObject *parent)
Sets a new parent object.
Definition vobject.h:140
VFill * m_fill
the object's fill
Definition vobject.h:290
void invalidateBoundingBox()
Invalidates the bounding box, so it has to be recalculated.
Definition vobject.h:127
virtual VFill * fill() const
Gets the object's actual fill.
Definition vobject.h:177
KoRect m_boundingBox
the object's bounding box
Definition vobject.h:285
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vobject.cpp:157
virtual VStroke * stroke() const
Gets the object's actual stroke.
Definition vobject.h:170
virtual void save(TQDomElement &element) const
Save this object's state to xml.
Definition vobject.cpp:102
virtual void setFill(const VFill &fill)
Sets the fill to a given new fill.
Definition vobject.cpp:93
VState state() const
Get the state the object is in.
Definition vobject.h:154
A composite path consists of one or many subpaths.
Definition vcomposite.h:48
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
This shape offers star-like shapes with a lot of parameters :
Definition vstar.h:48
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vstar.cpp:314
Manages stroke properties.
Definition vstroke.h:49
The abstract visitor class.
Definition vvisitor.h:51
virtual void visitVGroup(VGroup &group)
Visit method for a VGroup.
Definition vvisitor.cpp:52