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
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 }