karbon

vpolygon.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
21#include <tqdom.h>
22
23#include "vglobal.h"
24#include "vpolygon.h"
25#include "vtransformcmd.h"
26#include <tdelocale.h>
27#include <KoUnit.h>
28#include <KoStore.h>
29#include <KoXmlWriter.h>
30#include <KoXmlNS.h>
31#include <vdocument.h>
32
33VPolygon::VPolygon( VObject* parent, VState state )
34 : VPath( parent, state )
35{
36}
37
38VPolygon::VPolygon( VObject* parent, const TQString &points,
39 const KoPoint& topLeft, double width, double height )
40 : VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_points( points )
41{
42 init();
43}
44
45void
46VPolygon::init()
47{
48 bool bFirst = true;
49
50 TQString points = m_points.simplifyWhiteSpace();
51 points.replace( ',', ' ' );
52 points.remove( '\r' );
53 points.remove( '\n' );
54 TQStringList pointList = TQStringList::split( ' ', points );
55 TQStringList::Iterator end(pointList.end());
56 for( TQStringList::Iterator it = pointList.begin(); it != end; ++it )
57 {
58 KoPoint point;
59 point.setX( (*it).toDouble() );
60 point.setY( (*++it).toDouble() );
61 if( bFirst )
62 {
63 moveTo( point );
64 bFirst = false;
65 }
66 else
67 lineTo( point );
68 }
69 close();
70
71 TQWMatrix m;
72 m.translate( m_topLeft.x(), m_topLeft.y() );
73
74 // only tranform the path data
75 VTransformCmd cmd( 0L, m );
76 cmd.VVisitor::visitVPath( *this );
77}
78
79TQString
80VPolygon::name() const
81{
82 TQString result = VObject::name();
83 return !result.isEmpty() ? result : i18n( "Polygon" );
84}
85
86void
87VPolygon::save( TQDomElement& element ) const
88{
89 VDocument *doc = document();
90 if( doc && doc->saveAsPath() )
91 {
92 VPath::save( element );
93 return;
94 }
95
96 if( state() != deleted )
97 {
98 TQDomElement me = element.ownerDocument().createElement( "POLYGON" );
99 element.appendChild( me );
100
101 // save fill/stroke untransformed
102 VPath path( *this );
103 VTransformCmd cmd( 0L, m_matrix.invert() );
104 cmd.visit( path );
105 path.VObject::save( me );
106 //VObject::save( me );
107
108 me.setAttribute( "x", m_topLeft.x() );
109 me.setAttribute( "y", m_topLeft.y() );
110
111 me.setAttribute( "width", TQString("%1pt").arg( m_width ) );
112 me.setAttribute( "height", TQString("%1pt").arg( m_height ) );
113
114 me.setAttribute( "points", m_points );
115
116 TQString transform = buildSvgTransform();
117 if( !transform.isEmpty() )
118 me.setAttribute( "transform", transform );
119 }
120}
121
122void
123VPolygon::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
124{
125 // do not save deleted objects
126 if( state() == deleted )
127 return;
128
129 docWriter->startElement( "draw:polygon" );
130
131 docWriter->addAttribute( "draw:points", m_points );
132
133 VObject::saveOasis( store, docWriter, mainStyles, index );
134
135 docWriter->endElement();
136}
137
138void
139VPolygon::load( const TQDomElement& element )
140{
141 setState( normal );
142
143 TQDomNodeList list = element.childNodes();
144 for( uint i = 0; i < list.count(); ++i )
145 if( list.item( i ).isElement() )
146 VObject::load( list.item( i ).toElement() );
147
148 m_points = element.attribute( "points" );
149
150 m_width = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
151 m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
152
153 m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
154 m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
155
156 init();
157
158 TQString trafo = element.attribute( "transform" );
159 if( !trafo.isEmpty() )
160 transform( trafo );
161}
162
163bool
164VPolygon::loadOasis( const TQDomElement &element, KoOasisLoadingContext &context )
165{
166 setState( normal );
167
168 m_points = element.attributeNS( KoXmlNS::draw, "points", TQString() );
169
170 init();
171
172 transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", TQString() ) );
173
174 TQString trafo = element.attributeNS( KoXmlNS::draw, "transform", TQString() );
175 if( !trafo.isEmpty() )
176 transformOasis( trafo );
177
178 return VObject::loadOasis( element, context );
179}
180
181VPath*
182VPolygon::clone() const
183{
184 return new VPolygon( *this );
185}
The base class for all karbon objects.
Definition vobject.h:50
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vobject.h:163
virtual void saveOasis(KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index) const
Save this object's state to OpenDocument.
Definition vobject.cpp:116
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ normal
visible, not active
Definition vobject.h:54
virtual TQString name() const
Returns the name of the object.
Definition vobject.cpp:225
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vobject.cpp:157
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
A composite path consists of one or many subpaths.
Definition vcomposite.h:48
void close()
Closes the current subpath.
virtual void save(TQDomElement &element) const
Save this object's state to xml.
virtual VPath * clone() const
Create an exact copy of this object.