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
|
/* This file is part of the KDE project
Copyright (C) 2001, 2002, 2003 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 "vellipse.h"
#include "vtransformcmd.h"
#include <klocale.h>
#include <KoUnit.h>
#include <KoStore.h>
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <vglobal.h>
#include <vdocument.h>
#include <tqdom.h>
#include <core/vfill.h>
VEllipse::VEllipse( VObject* parent, VState state ) : VPath( parent, state )
{
}
VEllipse::VEllipse( VObject* parent,
const KoPoint& topLeft, double width, double height,
VEllipseType type, double startAngle, double endAngle )
: VPath( parent ), m_type( type ), m_startAngle( startAngle ), m_endAngle( endAngle )
{
setDrawCenterNode();
m_rx = width / 2.0;
m_ry = height / 2.0;
m_center.setX( topLeft.x() + m_rx );
m_center.setY( topLeft.y() + m_ry );
init();
}
void
VEllipse::init()
{
// to radials
int nsegs;
if( m_startAngle < m_endAngle )
nsegs = int( floor( ( m_endAngle - m_startAngle ) / 90.0 ) );
else
nsegs = 4 - int( ceil( ( m_startAngle - m_endAngle ) / 90.0 ) );
double startAngle = m_startAngle - 90.0;
if( startAngle < 0 ) startAngle += 360.0;
startAngle = VGlobal::pi_2 * ( startAngle / 90.0 );
double endAngle = m_endAngle - 90.0;
if( endAngle < 0 ) endAngle += 360.0;
endAngle = VGlobal::pi_2 * ( endAngle / 90.0 );
// Create (half-)unity circle with topLeft at (0|0):
double currentAngle = -startAngle - VGlobal::pi_2;
KoPoint start( 0.5 * sin( -startAngle ), 0.5 * cos( -startAngle ) );
moveTo( KoPoint( start.x(), start.y() ) );
double midAngle = currentAngle + VGlobal::pi_2 / 2.0;
double midAmount = 0.5 / sin( VGlobal::pi_2 / 2.0 );
for( int i = 0;i < nsegs;i++ )
{
midAngle -= VGlobal::pi_2;
arcTo( KoPoint( cos( midAngle ) * midAmount, -sin( midAngle ) * midAmount ),
KoPoint( 0.5 * sin( currentAngle ), 0.5 * cos( currentAngle ) ), 0.5 );
currentAngle -= VGlobal::pi_2;
}
double rest = ( -endAngle - VGlobal::pi_2 - currentAngle ) * 90.0 / VGlobal::pi_2;
if( rest > 0 )
rest -= 360.0;
if( rest != 0 )
{
midAngle = currentAngle - ( -rest / 360.0 ) * VGlobal::pi;
midAmount = 0.5 / cos( currentAngle - midAngle );
KoPoint end( 0.5 * sin( -endAngle ), 0.5 * cos( -endAngle ) );
arcTo( KoPoint( cos( midAngle ) * midAmount, -sin( midAngle ) * midAmount ),
KoPoint( 0.5 * sin( -endAngle ), 0.5 * cos( -endAngle ) ), 0.5 );
}
if( m_type == cut )
lineTo( KoPoint( 0.0, 0.0 ) );
if( m_type != arc )
close();
// Translate and scale:
TQWMatrix m;
m.translate( m_center.x(), m_center.y() );
m.scale( 2.0 * m_rx, 2.0 * m_ry );
// only tranform the path data
VTransformCmd cmd( 0L, m );
cmd.VVisitor::visitVPath( *this );
m_matrix.reset();
}
TQString
VEllipse::name() const
{
TQString result = VObject::name();
return !result.isEmpty() ? result : i18n( "Ellipse" );
}
void
VEllipse::save( TQDomElement& element ) const
{
VDocument *doc = document();
if( doc && doc->saveAsPath() )
{
VPath::save( element );
return;
}
if( state() != deleted )
{
TQDomElement me = element.ownerDocument().createElement( "ELLIPSE" );
element.appendChild( me );
// save fill/stroke untransformed
VPath path( *this );
VTransformCmd cmd( 0L, m_matrix.invert() );
cmd.visit( path );
path.VObject::save( me );
//VObject::save( me );
me.setAttribute( "cx", m_center.x() );
me.setAttribute( "cy", m_center.y() );
me.setAttribute( "rx", m_rx );
me.setAttribute( "ry", m_ry );
me.setAttribute( "start-angle", m_startAngle );
me.setAttribute( "end-angle", m_endAngle );
if( m_type == cut )
me.setAttribute( "kind", "cut" );
else if( m_type == section )
me.setAttribute( "kind", "section" );
else if( m_type == arc )
me.setAttribute( "kind", "arc" );
else
me.setAttribute( "kind", "full" );
TQString transform = buildSvgTransform();
if( !transform.isEmpty() )
me.setAttribute( "transform", transform );
}
}
void
VEllipse::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
{
// do not save deleted objects
if( state() == deleted )
return;
docWriter->startElement( "draw:ellipse" );
//save all into pt
docWriter->addAttributePt( "svg:cx", m_center.x() );
docWriter->addAttributePt( "svg:cy", m_center.y() );
docWriter->addAttributePt( "svg:rx", m_rx );
docWriter->addAttributePt( "svg:ry", m_ry );
if( m_type == full )
docWriter->addAttribute( "draw:kind", "full" );
else
{
// the meaning of cut and section is mixed up in karbon, so just set them so for now
// be sure to do the right thing tm for karbon 2.0
if( m_type == section )
docWriter->addAttribute( "draw:kind", "cut" );
else if( m_type == cut )
docWriter->addAttribute( "draw:kind", "section" );
else
docWriter->addAttribute( "draw:kind", "arc" );
docWriter->addAttribute( "draw:start-angle", m_startAngle );
docWriter->addAttribute( "draw:end-angle", m_endAngle );
}
VObject::saveOasis( store, docWriter, mainStyles, index );
TQWMatrix tmpMat;
tmpMat.scale( 1, -1 );
tmpMat.translate( 0, -document()->height() );
TQString transform = buildOasisTransform( m_matrix*tmpMat );
if( !transform.isEmpty() )
docWriter->addAttribute( "draw:transform", transform );
docWriter->endElement();
}
bool
VEllipse::loadOasis( const TQDomElement &element, KoOasisLoadingContext &context )
{
setState( normal );
if( element.tagName() == "ellipse" )
{
if( element.hasAttributeNS( KoXmlNS::svg, "rx" ) )
m_rx = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "rx", TQString() ) );
else
m_rx = 0.5 * KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", TQString() ) );
if( element.hasAttributeNS( KoXmlNS::svg, "ry" ) )
m_ry = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "ry", TQString() ) );
else
m_ry = 0.5 * KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", TQString() ) );
}
else if( element.tagName() == "circle" )
{
if( element.hasAttributeNS( KoXmlNS::svg, "r" ) )
m_rx = m_ry = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "r", TQString() ) );
else
m_rx = m_ry = 0.5 * KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", TQString() ) );
}
if( element.hasAttributeNS( KoXmlNS::svg, "cx" ) )
m_center.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "cx", TQString() ) ) );
else
m_center.setX( m_rx + KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", TQString() ) ) );
if( element.hasAttributeNS( KoXmlNS::svg, "cy" ) )
m_center.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "cy", TQString() ) ) );
else
m_center.setY( m_ry + KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", TQString() ) ) );
// the meaning of cut and section is mixed up in karbon, so just set them so for now
// be sure to do the right thing tm for karbon 2.0
TQString kind = element.attributeNS( KoXmlNS::draw, "kind", TQString() );
if( kind == "cut" )
m_type = section;
else if( kind == "section" )
m_type = cut;
else if( kind == "arc" )
m_type = arc;
else
m_type = full;
double startAngle = element.attributeNS( KoXmlNS::draw, "start-angle", TQString() ).toDouble();
double endAngle = element.attributeNS( KoXmlNS::draw, "end-angle", TQString() ).toDouble();
// the shape gets mirrored in y-direction, so make the angles temporary clockwise
// just for creating the path
m_startAngle = 360.0 - endAngle;
m_endAngle = 360.0 - startAngle;
init();
// now set the right angles
m_startAngle = startAngle;
m_endAngle = endAngle;
transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", TQString() ) );
TQString trafo = element.attributeNS( KoXmlNS::draw, "transform", TQString() );
if( !trafo.isEmpty() )
transformOasis( trafo );
return VObject::loadOasis( element, context );
}
void
VEllipse::load( const TQDomElement& element )
{
setState( normal );
TQDomNodeList list = element.childNodes();
for( uint i = 0; i < list.count(); ++i )
if( list.item( i ).isElement() )
VObject::load( list.item( i ).toElement() );
m_rx = KoUnit::parseValue( element.attribute( "rx" ) );
m_ry = KoUnit::parseValue( element.attribute( "ry" ) );
m_center.setX( KoUnit::parseValue( element.attribute( "cx" ) ) );
m_center.setY( KoUnit::parseValue( element.attribute( "cy" ) ) );
m_startAngle = element.attribute( "start-angle" ).toDouble();
m_endAngle = element.attribute( "end-angle" ).toDouble();
if( element.attribute( "kind" ) == "cut" )
m_type = cut;
else if( element.attribute( "kind" ) == "section" )
m_type = section;
else if( element.attribute( "kind" ) == "arc" )
m_type = arc;
else
m_type = full;
init();
TQString trafo = element.attribute( "transform" );
if( !trafo.isEmpty() )
transform( trafo );
}
VPath*
VEllipse::clone() const
{
return new VEllipse( *this );
}
|