karbon

vpolygon.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 <qdom.h>
00022 
00023 #include "vglobal.h"
00024 #include "vpolygon.h"
00025 #include "vtransformcmd.h"
00026 #include <klocale.h>
00027 #include <KoUnit.h>
00028 #include <KoStore.h>
00029 #include <KoXmlWriter.h>
00030 #include <KoXmlNS.h>
00031 #include <vdocument.h>
00032 
00033 VPolygon::VPolygon( VObject* parent, VState state ) 
00034     : VPath( parent, state )
00035 {
00036 }
00037 
00038 VPolygon::VPolygon( VObject* parent, const QString &points,
00039         const KoPoint& topLeft, double width, double height )
00040     : VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_points( points )
00041 {
00042     init();
00043 }
00044 
00045 void
00046 VPolygon::init()
00047 {
00048     bool bFirst = true;
00049 
00050     QString points = m_points.simplifyWhiteSpace();
00051     points.replace( ',', ' ' );
00052     points.remove( '\r' );
00053     points.remove( '\n' );
00054     QStringList pointList = QStringList::split( ' ', points );
00055     QStringList::Iterator end(pointList.end());
00056     for( QStringList::Iterator it = pointList.begin(); it != end; ++it )
00057     {
00058         KoPoint point;
00059         point.setX( (*it).toDouble() );
00060         point.setY( (*++it).toDouble() );
00061         if( bFirst )
00062         {
00063             moveTo( point );
00064             bFirst = false;
00065         }
00066         else
00067             lineTo( point );
00068     }
00069     close();
00070 
00071     QWMatrix m;
00072     m.translate( m_topLeft.x(), m_topLeft.y() );
00073 
00074     // only tranform the path data
00075     VTransformCmd cmd( 0L, m );
00076     cmd.VVisitor::visitVPath( *this );
00077 }
00078 
00079 QString
00080 VPolygon::name() const
00081 {
00082     QString result = VObject::name();
00083     return !result.isEmpty() ? result : i18n( "Polygon" );
00084 }
00085 
00086 void
00087 VPolygon::save( QDomElement& element ) const
00088 {
00089     VDocument *doc = document();
00090     if( doc && doc->saveAsPath() )
00091     {
00092         VPath::save( element );
00093         return;
00094     }
00095 
00096     if( state() != deleted )
00097     {
00098         QDomElement me = element.ownerDocument().createElement( "POLYGON" );
00099         element.appendChild( me );
00100 
00101         // save fill/stroke untransformed
00102         VPath path( *this );
00103         VTransformCmd cmd( 0L, m_matrix.invert() );
00104         cmd.visit( path );
00105         path.VObject::save( me );
00106         //VObject::save( me );
00107 
00108         me.setAttribute( "x", m_topLeft.x() );
00109         me.setAttribute( "y", m_topLeft.y() );
00110 
00111         me.setAttribute( "width", QString("%1pt").arg( m_width ) );
00112         me.setAttribute( "height", QString("%1pt").arg( m_height ) );
00113 
00114         me.setAttribute( "points", m_points );
00115 
00116         QString transform = buildSvgTransform();
00117         if( !transform.isEmpty() )
00118             me.setAttribute( "transform", transform );
00119     }
00120 }
00121 
00122 void
00123 VPolygon::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
00124 {
00125     // do not save deleted objects
00126     if( state() == deleted )
00127         return;
00128 
00129     docWriter->startElement( "draw:polygon" );
00130 
00131     docWriter->addAttribute( "draw:points", m_points );
00132 
00133     VObject::saveOasis( store, docWriter, mainStyles, index );
00134 
00135     docWriter->endElement();
00136 }
00137 
00138 void
00139 VPolygon::load( const QDomElement& element )
00140 {
00141     setState( normal );
00142 
00143     QDomNodeList list = element.childNodes();
00144     for( uint i = 0; i < list.count(); ++i )
00145         if( list.item( i ).isElement() )
00146             VObject::load( list.item( i ).toElement() );
00147 
00148     m_points = element.attribute( "points" );
00149 
00150     m_width  = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
00151     m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
00152 
00153     m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
00154     m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
00155 
00156     init();
00157 
00158     QString trafo = element.attribute( "transform" );
00159     if( !trafo.isEmpty() )
00160         transform( trafo );
00161 }
00162 
00163 bool
00164 VPolygon::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00165 {
00166     setState( normal );
00167 
00168     m_points = element.attributeNS( KoXmlNS::draw, "points", QString::null );
00169 
00170     init();
00171 
00172     transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", QString::null ) );
00173 
00174     QString trafo = element.attributeNS( KoXmlNS::draw, "transform", QString::null );
00175     if( !trafo.isEmpty() )
00176         transformOasis( trafo );
00177 
00178     return VObject::loadOasis( element, context );
00179 }
00180 
00181 VPath* 
00182 VPolygon::clone() const
00183 {
00184     return new VPolygon( *this );
00185 }
KDE Home | KDE Accessibility Home | Description of Access Keys