karbon

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