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     // do not save deleted objects
00116     if( state() == deleted )
00117         return;
00118 
00119     docWriter->startElement( "draw:polyline" );
00120 
00121     docWriter->addAttribute( "svg:points", m_points );
00122 
00123     VObject::saveOasis( store, docWriter, mainStyles, index );
00124 
00125     docWriter->endElement();
00126 }
00127 
00128 void
00129 VPolyline::load( const QDomElement& element )
00130 {
00131     setState( normal );
00132 
00133     QDomNodeList list = element.childNodes();
00134     for( uint i = 0; i < list.count(); ++i )
00135         if( list.item( i ).isElement() )
00136             VObject::load( list.item( i ).toElement() );
00137 
00138     m_points = element.attribute( "points" );
00139 
00140     init();
00141 
00142     QString trafo = element.attribute( "transform" );
00143     if( !trafo.isEmpty() )
00144         transform( trafo );
00145 }
00146 
00147 bool
00148 VPolyline::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00149 {
00150     setState( normal );
00151 
00152     if( element.localName() == "line" )
00153     {
00154         KoPoint p1, p2;
00155         p1.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x1", QString::null ) ) );
00156         p1.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y1", QString::null ) ) );
00157         p2.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x2", QString::null ) ) );
00158         p2.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y2", QString::null ) ) );
00159         
00160         m_points = QString( "%1,%2 %3,%4" ).arg( p1.x() ).arg( p1.y() ).arg( p2.x() ).arg( p2.y() );
00161 
00162         moveTo( p1 );
00163         lineTo( p2 );
00164     }
00165     else if( element.localName() == "polyline" )
00166     {
00167         m_points = element.attributeNS( KoXmlNS::draw, "points", QString::null );
00168         init();
00169     }
00170 
00171     transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", QString::null ) );
00172 
00173     QString trafo = element.attributeNS( KoXmlNS::draw, "transform", QString::null );
00174     if( !trafo.isEmpty() )
00175         transformOasis( trafo );
00176 
00177     return VObject::loadOasis( element, context );
00178 }
00179 
00180 VPath* 
00181 VPolyline::clone() const
00182 {
00183     return new VPolyline( *this );
00184 }
KDE Home | KDE Accessibility Home | Description of Access Keys