summaryrefslogtreecommitdiffstats
path: root/karbon/core/vobject.cpp
blob: 897652c373016745cc3af25d90be3c963027489c (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
/* This file is part of the KDE project
   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.
*/

#include <tqdom.h>

#include "vdocument.h"
#include "vfill.h"
#include "vobject.h"
#include "vobject_iface.h"
#include "vstroke.h"

#include <KoStore.h>
#include <KoGenStyles.h>
#include <KoStyleStack.h>
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <KoOasisLoadingContext.h>
#include <KoOasisStyles.h>

VObject::VObject( VObject* parent, VState state ) : m_dcop( 0L )
{
	m_stroke = 0L;
	m_fill = 0L;
	
	m_parent = parent;
	m_state = state;

	invalidateBoundingBox();
}

VObject::VObject( const VObject& obj )
{
	m_stroke = 0L;
	m_fill = 0L;

	m_parent = obj.m_parent;
	m_state = obj.m_state;

	invalidateBoundingBox();
	m_dcop = 0L;

	VDocument *srcDoc = obj.document();
	if( srcDoc && !srcDoc->objectName( &obj ).isEmpty() )
	{
		VDocument *dstDoc = document();
		if( dstDoc )
			dstDoc->setObjectName( this, srcDoc->objectName( &obj ) );
	}
}

VObject::~VObject()
{
	delete( m_stroke );
	delete( m_fill );
	delete m_dcop;
}

DCOPObject *
VObject::dcopObject()
{
    if ( !m_dcop )
		m_dcop = new VObjectIface( this );

    return m_dcop;
}

void
VObject::setStroke( const VStroke& stroke )
{
	if( !m_stroke )
		m_stroke = new VStroke( this );

	*m_stroke = stroke;
}

void
VObject::setFill( const VFill& fill )
{
	if( !m_fill )
		m_fill = new VFill();

	*m_fill = fill;
}

void
VObject::save( TQDomElement& element ) const
{
	if( m_stroke )
		m_stroke->save( element );

	if( m_fill )
		m_fill->save( element );

	VDocument *doc = document();
	if( doc && !doc->objectName( this ).isEmpty() )
		element.setAttribute( "ID", TQString( doc->objectName( this ) ) );
}

void
VObject::saveOasis( KoStore *, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
{
	if( !name().isEmpty() )
		docWriter->addAttribute( "draw:name", name() );

	TQWMatrix mat;
	mat.scale( 1, -1 );
	mat.translate( 0, -document()->height() );

	KoGenStyle styleobjectauto( VDocument::STYLE_GRAPHICAUTO, "graphic" );
	saveOasisFill( mainStyles, styleobjectauto );
	if( m_stroke )
	{
		// mirror stroke before saving
		VStroke stroke( *m_stroke );
		stroke.transform( mat );
		stroke.saveOasis( styleobjectauto );
	}
	TQString st = mainStyles.lookup( styleobjectauto, "st" );
	if(document())
	        docWriter->addAttribute( "draw:id",  "obj" + TQString::number( index ) );
	docWriter->addAttribute( "draw:style-name", st );
}

void
VObject::saveOasisFill( KoGenStyles &mainStyles, KoGenStyle &stylesobjectauto ) const
{
	if( m_fill )
	{
		TQWMatrix mat;
		mat.scale( 1, -1 );
		mat.translate( 0, -document()->height() );

		// mirror fill before saving
		VFill fill( *m_fill );
		fill.transform( mat );
		fill.saveOasis( mainStyles, stylesobjectauto );
	}
}

void
VObject::load( const TQDomElement& element )
{
	if( !m_stroke )
		m_stroke = new VStroke( this );

	if( !m_fill )
		m_fill = new VFill();


	if( element.tagName() == "STROKE" )
	{
		m_stroke->load( element );
	}
	else if( element.tagName() == "FILL" )
	{
		m_fill->load( element );
	}

	VDocument *doc = document();
	if( doc && !element.attribute( "ID" ).isEmpty() )
		doc->setObjectName( this, element.attribute( "ID" ) );
}

bool
VObject::loadOasis( const TQDomElement &object, KoOasisLoadingContext &context )
{
	if( !m_stroke )
		m_stroke = new VStroke( this );

	if( !m_fill )
		m_fill = new VFill();

	if( object.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
		context.fillStyleStack( object, KoXmlNS::draw, "style-name", "graphic" );

	KoStyleStack &styleStack = context.styleStack();
	styleStack.setTypeProperties( "graphic" );
	m_stroke->loadOasis( styleStack );
	m_fill->loadOasis( object, context, this );

	if( object.hasAttributeNS( KoXmlNS::draw, "name" ) )
		setName( object.attributeNS( KoXmlNS::draw, "name", TQString() ) );

	return true;
}

void
VObject::addStyles( const TQDomElement* style, KoOasisLoadingContext & context )
{
	if(style)
	{
		// this function is necessary as parent styles can have parents themself
		if( style->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
			addStyles( context.oasisStyles().findStyle( style->attributeNS( KoXmlNS::style, "parent-style-name", TQString() ) ), context );
		context.addStyles( style, "style-name" );
	}
}

VDocument *
VObject::document() const
{
	VObject *obj = (VObject *)this;
	while( obj->parent() )
		obj = obj->parent();
	return dynamic_cast<VDocument *>( obj );
}

TQString
VObject::name() const
{
	return document() ? document()->objectName( this ) : TQString();
}

void
VObject::setName( const TQString &s )
{
	if( document() )
		document()->setObjectName( this, s );
}