karbon

vobject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, 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#include <tqdom.h>
21
22#include "vdocument.h"
23#include "vfill.h"
24#include "vobject.h"
25#include "vobject_iface.h"
26#include "vstroke.h"
27
28#include <KoStore.h>
29#include <KoGenStyles.h>
30#include <KoStyleStack.h>
31#include <KoXmlWriter.h>
32#include <KoXmlNS.h>
33#include <KoOasisLoadingContext.h>
34#include <KoOasisStyles.h>
35
37{
38 m_stroke = 0L;
39 m_fill = 0L;
40
41 m_parent = parent;
42 m_state = state;
43
45}
46
48{
49 m_stroke = 0L;
50 m_fill = 0L;
51
52 m_parent = obj.m_parent;
53 m_state = obj.m_state;
54
56 m_dcop = 0L;
57
58 VDocument *srcDoc = obj.document();
59 if( srcDoc && !srcDoc->objectName( &obj ).isEmpty() )
60 {
61 VDocument *dstDoc = document();
62 if( dstDoc )
63 dstDoc->setObjectName( this, srcDoc->objectName( &obj ) );
64 }
65}
66
68{
69 delete( m_stroke );
70 delete( m_fill );
71 delete m_dcop;
72}
73
74DCOPObject *
76{
77 if ( !m_dcop )
78 m_dcop = new VObjectIface( this );
79
80 return m_dcop;
81}
82
83void
85{
86 if( !m_stroke )
87 m_stroke = new VStroke( this );
88
90}
91
92void
94{
95 if( !m_fill )
96 m_fill = new VFill();
97
98 *m_fill = fill;
99}
100
101void
102VObject::save( TQDomElement& element ) const
103{
104 if( m_stroke )
105 m_stroke->save( element );
106
107 if( m_fill )
108 m_fill->save( element );
109
110 VDocument *doc = document();
111 if( doc && !doc->objectName( this ).isEmpty() )
112 element.setAttribute( "ID", TQString( doc->objectName( this ) ) );
113}
114
115void
116VObject::saveOasis( KoStore *, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
117{
118 if( !name().isEmpty() )
119 docWriter->addAttribute( "draw:name", name() );
120
121 TQWMatrix mat;
122 mat.scale( 1, -1 );
123 mat.translate( 0, -document()->height() );
124
125 KoGenStyle styleobjectauto( VDocument::STYLE_GRAPHICAUTO, "graphic" );
126 saveOasisFill( mainStyles, styleobjectauto );
127 if( m_stroke )
128 {
129 // mirror stroke before saving
131 stroke.transform( mat );
132 stroke.saveOasis( styleobjectauto );
133 }
134 TQString st = mainStyles.lookup( styleobjectauto, "st" );
135 if(document())
136 docWriter->addAttribute( "draw:id", "obj" + TQString::number( index ) );
137 docWriter->addAttribute( "draw:style-name", st );
138}
139
140void
141VObject::saveOasisFill( KoGenStyles &mainStyles, KoGenStyle &stylesobjectauto ) const
142{
143 if( m_fill )
144 {
145 TQWMatrix mat;
146 mat.scale( 1, -1 );
147 mat.translate( 0, -document()->height() );
148
149 // mirror fill before saving
150 VFill fill( *m_fill );
151 fill.transform( mat );
152 fill.saveOasis( mainStyles, stylesobjectauto );
153 }
154}
155
156void
157VObject::load( const TQDomElement& element )
158{
159 if( !m_stroke )
160 m_stroke = new VStroke( this );
161
162 if( !m_fill )
163 m_fill = new VFill();
164
165
166 if( element.tagName() == "STROKE" )
167 {
168 m_stroke->load( element );
169 }
170 else if( element.tagName() == "FILL" )
171 {
172 m_fill->load( element );
173 }
174
175 VDocument *doc = document();
176 if( doc && !element.attribute( "ID" ).isEmpty() )
177 doc->setObjectName( this, element.attribute( "ID" ) );
178}
179
180bool
181VObject::loadOasis( const TQDomElement &object, KoOasisLoadingContext &context )
182{
183 if( !m_stroke )
184 m_stroke = new VStroke( this );
185
186 if( !m_fill )
187 m_fill = new VFill();
188
189 if( object.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
190 context.fillStyleStack( object, KoXmlNS::draw, "style-name", "graphic" );
191
192 KoStyleStack &styleStack = context.styleStack();
193 styleStack.setTypeProperties( "graphic" );
194 m_stroke->loadOasis( styleStack );
195 m_fill->loadOasis( object, context, this );
196
197 if( object.hasAttributeNS( KoXmlNS::draw, "name" ) )
198 setName( object.attributeNS( KoXmlNS::draw, "name", TQString() ) );
199
200 return true;
201}
202
203void
204VObject::addStyles( const TQDomElement* style, KoOasisLoadingContext & context )
205{
206 if(style)
207 {
208 // this function is necessary as parent styles can have parents themself
209 if( style->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
210 addStyles( context.oasisStyles().findStyle( style->attributeNS( KoXmlNS::style, "parent-style-name", TQString() ) ), context );
211 context.addStyles( style, "style-name" );
212 }
213}
214
215VDocument *
217{
218 VObject *obj = (VObject *)this;
219 while( obj->parent() )
220 obj = obj->parent();
221 return dynamic_cast<VDocument *>( obj );
222}
223
224TQString
226{
227 return document() ? document()->objectName( this ) : TQString();
228}
229
230void
231VObject::setName( const TQString &s )
232{
233 if( document() )
234 document()->setObjectName( this, s );
235}
236
All non-visual, static doc info is in here.
Definition vdocument.h:53
void setObjectName(const VObject *obj, const TQString name)
Sets custom name of specified object.
Definition vdocument.h:279
TQString objectName(const VObject *obj) const
Returns custom name of specified object.
Manages the fill of shapes.
Definition vfill.h:45
DCOPObject * m_dcop
the object's DCOP object
Definition vobject.h:292
VObject(VObject *parent, VState state=edit)
Constructs a new object that is child of parent and has the given state.
Definition vobject.cpp:36
VObject * parent() const
Returns pointer to current parent object.
Definition vobject.h:147
virtual void saveOasis(KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index) const
Save this object's state to OpenDocument.
Definition vobject.cpp:116
VState m_state
the object's state
Definition vobject.h:286
virtual TQString name() const
Returns the name of the object.
Definition vobject.cpp:225
virtual DCOPObject * dcopObject()
Returns pointer to internal DCOP object.
Definition vobject.cpp:75
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 addStyles(const TQDomElement *style, KoOasisLoadingContext &context)
Adds a new given style to the specified OASIS context.
Definition vobject.cpp:204
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
void setName(const TQString &s)
Sets the object's name to a given new name.
Definition vobject.cpp:231
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 ~VObject()
Destroys the object and deletes the stroke, fill and DCOP-object.
Definition vobject.cpp:67
virtual void setFill(const VFill &fill)
Sets the fill to a given new fill.
Definition vobject.cpp:93
VDocument * document() const
Return document the object belongs to.
Definition vobject.cpp:216
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
Definition vobject.cpp:181
VState state() const
Get the state the object is in.
Definition vobject.h:154
Manages stroke properties.
Definition vstroke.h:49