karbon

vpolyline.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 
00021 #include "vpolyline.h"
00022 #include <qdom.h>
00023 
00024 #include "vglobal.h"
00025 #include <klocale.h>
00026 #include <vdocument.h>
00027 #include "vtransformcmd.h"
00028 
00029 #include <KoStore.h>
00030 #include <KoXmlWriter.h>
00031 #include <KoXmlNS.h>
00032 
00033 VPolyline::VPolyline( VObject* parent, VState state )
00034     : VPath( parent, state )
00035 {
00036 }
00037 
00038 /*VPolyline::VPolyline( VObject* parent, VState state ) 
00039     : VPath( parent, state )
00040 {
00041 }*/
00042 
00043 /*VPolyline::VPolyline( VObject* parent, const QString &points ) 
00044     : VPath( parent ), m_points( points )
00045 {
00046     init();
00047 }*/
00048 
00049 void
00050 VPolyline::init()
00051 {
00052     bool bFirst = true;
00053 
00054     QString points = m_points.simplifyWhiteSpace();
00055     points.replace( ',', ' ' );
00056     points.remove( '\r' );
00057     points.remove( '\n' );
00058     QStringList pointList = QStringList::split( ' ', points );
00059     QStringList::Iterator end(pointList.end());
00060     for( QStringList::Iterator it = pointList.begin(); it != end; ++it )
00061     {
00062         KoPoint point;
00063         point.setX( (*it).toDouble() );
00064         point.setY( (*++it).toDouble() );
00065         if( bFirst )
00066         {
00067             moveTo( point );
00068             bFirst = false;
00069         }
00070         else
00071             lineTo( point );
00072     }
00073 }
00074 
00075 QString
00076 VPolyline::name() const
00077 {
00078     QString result = VObject::name();
00079     return !result.isEmpty() ? result : i18n( "Polyline" );
00080 }
00081 
00082 void
00083 VPolyline::save( QDomElement& element ) const
00084 {
00085     VDocument *doc = document();
00086     if( doc && doc->saveAsPath() )
00087     {
00088         VPath::save( element );
00089         return;
00090     }
00091 
00092     if( state() != deleted )
00093     {
00094         QDomElement me = element.ownerDocument().createElement( "POLYLINE" );
00095         element.appendChild( me );
00096 
00097         // save fill/stroke untransformed
00098         VPath path( *this );
00099         VTransformCmd cmd( 0L, m_matrix.invert() );
00100         cmd.visit( path );
00101         path.VObject::save( me );
00102         //VObject::save( me );
00103 
00104         me.setAttribute( "points", m_points );
00105 
00106         QString transform = buildSvgTransform();
00107         if( !transform.isEmpty() )
00108             me.setAttribute( "transform", transform );
00109     }
00110 }
00111 
00112 void
00113 VPolyline::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
00114 {
00115     docWriter->startElement( "draw:polyline" );
00116 
00117     docWriter->addAttribute( "svg:points", m_points );
00118 
00119     VObject::saveOasis( store, docWriter, mainStyles, index );
00120 
00121     docWriter->endElement();
00122 }
00123 
00124 void
00125 VPolyline::load( const QDomElement& element )
00126 {
00127     setState( normal );
00128 
00129     QDomNodeList list = element.childNodes();
00130     for( uint i = 0; i < list.count(); ++i )
00131         if( list.item( i ).isElement() )
00132             VObject::load( list.item( i ).toElement() );
00133 
00134     m_points = element.attribute( "points" );
00135 
00136     init();
00137 
00138     QString trafo = element.attribute( "transform" );
00139     if( !trafo.isEmpty() )
00140         transform( trafo );
00141 }
00142 
00143 bool
00144 VPolyline::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00145 {
00146     setState( normal );
00147 
00148     if( element.localName() == "line" )
00149     {
00150         KoPoint p1, p2;
00151         p1.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x1", QString::null ) ) );
00152         p1.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y1", QString::null ) ) );
00153         p2.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x2", QString::null ) ) );
00154         p2.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y2", QString::null ) ) );
00155         
00156         m_points = QString( "%1,%2 %3,%4" ).arg( p1.x() ).arg( p1.y() ).arg( p2.x() ).arg( p2.y() );
00157 
00158         moveTo( p1 );
00159         lineTo( p2 );
00160     }
00161     else if( element.localName() == "polyline" )
00162     {
00163         m_points = element.attributeNS( KoXmlNS::draw, "points", QString::null );
00164         init();
00165     }
00166 
00167     transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", QString::null ) );
00168 
00169     QString trafo = element.attributeNS( KoXmlNS::draw, "transform", QString::null );
00170     if( !trafo.isEmpty() )
00171         transformOasis( trafo );
00172 
00173     return VObject::loadOasis( element, context );
00174 }
00175 
00176 VPath* 
00177 VPolyline::clone() const
00178 {
00179     return new VPolyline( *this );
00180 }
KDE Home | KDE Accessibility Home | Description of Access Keys