00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "vrectangle.h"
00022 #include <klocale.h>
00023 #include <KoUnit.h>
00024 #include <KoStore.h>
00025 #include <KoXmlWriter.h>
00026 #include <KoXmlNS.h>
00027 #include <qdom.h>
00028 #include <kdebug.h>
00029 #include "vglobal.h"
00030 #include <vdocument.h>
00031 #include "vtransformcmd.h"
00032
00033 VRectangle::VRectangle( VObject* parent, VState state )
00034 : VPath( parent, state )
00035 {
00036 }
00037
00038 VRectangle::VRectangle( VObject* parent,
00039 const KoPoint& topLeft, double width, double height, double rx, double ry )
00040 : VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_rx( rx ), m_ry( ry )
00041 {
00042 setDrawCenterNode();
00043
00044 if( m_rx < 0.0 ) m_rx = 0.0;
00045 if( m_ry < 0.0 ) m_ry = 0.0;
00046
00047 if( m_rx > m_width * 0.5 )
00048 m_rx = m_width * 0.5;
00049 if( m_ry > m_height * 0.5 )
00050 m_ry = m_height * 0.5;
00051
00052 init();
00053 }
00054
00055 void
00056 VRectangle::init()
00057 {
00058 if( m_rx == 0 && m_ry == 0 )
00059 {
00060 moveTo( m_topLeft );
00061 lineTo( KoPoint( m_topLeft.x(), m_topLeft.y() - m_height ) );
00062 lineTo( KoPoint( m_topLeft.x() + m_width, m_topLeft.y() - m_height ) );
00063 lineTo( KoPoint( m_topLeft.x() + m_width, m_topLeft.y() ) );
00064 }
00065 else
00066 {
00067 double rx = m_rx;
00068 double ry = m_ry;
00069 double x = m_topLeft.x();
00070 double y = m_topLeft.y();
00071 moveTo( KoPoint( x + rx, y ) );
00072 curveTo( KoPoint( x + rx * ( 1 - 0.552 ), y ),
00073 KoPoint( x, y - ry * ( 1 - 0.552 ) ),
00074 KoPoint( x, y - ry ) );
00075 if( ry < m_height / 2 )
00076 lineTo( KoPoint( x, y - m_height + ry ) );
00077 curveTo( KoPoint( x, y - m_height + ry * ( 1 - 0.552 ) ),
00078 KoPoint( x + rx * ( 1 - 0.552 ), y - m_height ),
00079 KoPoint( x + rx, y - m_height ) );
00080 if( rx < m_width / 2 )
00081 lineTo( KoPoint( x + m_width - rx, y - m_height ) );
00082 curveTo( KoPoint( x + m_width - rx * ( 1 - 0.552 ), y - m_height ),
00083 KoPoint( x + m_width, y - m_height + ry * ( 1 - 0.552 ) ),
00084 KoPoint( x + m_width, y - m_height + ry ) );
00085 if( ry < m_height / 2 )
00086 lineTo( KoPoint( x + m_width, y - ry ) );
00087 curveTo( KoPoint( x + m_width, y - ry * ( 1 - 0.552 ) ),
00088 KoPoint( x + m_width - rx * ( 1 - 0.552 ), y ),
00089 KoPoint( x + m_width - rx, y ) );
00090 if( rx < m_width / 2 )
00091 lineTo( KoPoint( x + rx, y ) );
00092 }
00093 close();
00094 }
00095
00096 QString
00097 VRectangle::name() const
00098 {
00099 QString result = VObject::name();
00100 return !result.isEmpty() ? result : i18n( "Rectangle" );
00101 }
00102
00103 void
00104 VRectangle::save( QDomElement& element ) const
00105 {
00106 VDocument *doc = document();
00107 if( doc && doc->saveAsPath() )
00108 {
00109 VPath::save( element );
00110 return;
00111 }
00112
00113 if( state() != deleted )
00114 {
00115 QDomElement me = element.ownerDocument().createElement( "RECT" );
00116 element.appendChild( me );
00117
00118
00119 VPath path( *this );
00120 VTransformCmd cmd( 0L, m_matrix.invert() );
00121 cmd.visit( path );
00122 path.VObject::save( me );
00123
00124
00125 me.setAttribute( "x", m_topLeft.x() );
00126 me.setAttribute( "y", m_topLeft.y() );
00127
00128 me.setAttribute( "width", QString("%1pt").arg( m_width ) );
00129 me.setAttribute( "height", QString("%1pt").arg( m_height ) );
00130
00131 me.setAttribute( "rx", m_rx );
00132 me.setAttribute( "ry", m_ry );
00133
00134 QString transform = buildSvgTransform();
00135 if( !transform.isEmpty() )
00136 me.setAttribute( "transform", transform );
00137 }
00138 }
00139
00140 void
00141 VRectangle::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
00142 {
00143
00144 if( state() == deleted )
00145 return;
00146
00147
00148 if( m_rx != 0. && m_ry != 0. && m_rx != m_ry )
00149 return VPath::saveOasis( store, docWriter, mainStyles, index );
00150
00151 docWriter->startElement( "draw:rect" );
00152
00153
00154 docWriter->addAttributePt( "svg:x", m_topLeft.x() );
00155 docWriter->addAttributePt( "svg:y", m_topLeft.y()-m_height );
00156 docWriter->addAttributePt( "svg:width", m_width );
00157 docWriter->addAttributePt( "svg:height", m_height );
00158
00159 if( m_rx != 0. && m_ry != 0. && m_rx == m_ry )
00160 docWriter->addAttributePt( "draw:corner-radius", m_rx );
00161
00162 VObject::saveOasis( store, docWriter, mainStyles, index );
00163
00164 QWMatrix tmpMat;
00165 tmpMat.scale( 1, -1 );
00166 tmpMat.translate( 0, -document()->height() );
00167
00168 QString transform = buildOasisTransform( m_matrix*tmpMat );
00169 if( !transform.isEmpty() )
00170 docWriter->addAttribute( "draw:transform", transform );
00171
00172 docWriter->endElement();
00173 }
00174
00175 bool
00176 VRectangle::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00177 {
00178 setState( normal );
00179
00180 m_width = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", QString::null ), 10.0 );
00181 m_height = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", QString::null ), 10.0 );
00182
00183 m_topLeft.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", QString::null ) ) );
00184 m_topLeft.setY( m_height + KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", QString::null ) ) );
00185
00186 m_rx = m_ry = KoUnit::parseValue( element.attributeNS( KoXmlNS::draw, "corner-radius", QString::null ) );
00187
00188 init();
00189
00190 transformByViewbox( element, element.attributeNS( KoXmlNS::svg, "viewBox", QString::null ) );
00191
00192 QString trafo = element.attributeNS( KoXmlNS::draw, "transform", QString::null );
00193 if( !trafo.isEmpty() )
00194 transformOasis( trafo );
00195
00196 return VObject::loadOasis( element, context );
00197 }
00198
00199 void
00200 VRectangle::load( const QDomElement& element )
00201 {
00202 setState( normal );
00203
00204 QDomNodeList list = element.childNodes();
00205 for( uint i = 0; i < list.count(); ++i )
00206 if( list.item( i ).isElement() )
00207 VObject::load( list.item( i ).toElement() );
00208
00209 m_width = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
00210 m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
00211
00212 m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
00213 m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
00214
00215 m_rx = KoUnit::parseValue( element.attribute( "rx" ) );
00216 m_ry = KoUnit::parseValue( element.attribute( "ry" ) );
00217
00218 init();
00219
00220 QString trafo = element.attribute( "transform" );
00221 if( !trafo.isEmpty() )
00222 transform( trafo );
00223 }
00224
00225 VPath*
00226 VRectangle::clone() const
00227 {
00228 return new VRectangle( *this );
00229 }