00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00102 VPath path( *this );
00103 VTransformCmd cmd( 0L, m_matrix.invert() );
00104 cmd.visit( path );
00105 path.VObject::save( me );
00106
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 docWriter->startElement( "draw:polygon" );
00126
00127 docWriter->addAttribute( "draw:points", m_points );
00128
00129 VObject::saveOasis( store, docWriter, mainStyles, index );
00130
00131 docWriter->endElement();
00132 }
00133
00134 void
00135 VPolygon::load( const QDomElement& element )
00136 {
00137 setState( normal );
00138
00139 QDomNodeList list = element.childNodes();
00140 for( uint i = 0; i < list.count(); ++i )
00141 if( list.item( i ).isElement() )
00142 VObject::load( list.item( i ).toElement() );
00143
00144 m_points = element.attribute( "points" );
00145
00146 m_width = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
00147 m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
00148
00149 m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
00150 m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
00151
00152 init();
00153
00154 QString trafo = element.attribute( "transform" );
00155 if( !trafo.isEmpty() )
00156 transform( trafo );
00157 }
00158
00159 bool
00160 VPolygon::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00161 {
00162 setState( normal );
00163
00164 m_points = element.attributeNS( KoXmlNS::draw, "points", QString::null );
00165
00166 init();
00167
00168 transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", QString::null ) );
00169
00170 QString trafo = element.attributeNS( KoXmlNS::draw, "transform", QString::null );
00171 if( !trafo.isEmpty() )
00172 transformOasis( trafo );
00173
00174 return VObject::loadOasis( element, context );
00175 }
00176
00177 VPath*
00178 VPolygon::clone() const
00179 {
00180 return new VPolygon( *this );
00181 }