21#include "vrectangle.h"
25#include <KoXmlWriter.h>
31#include "vtransformcmd.h"
33VRectangle::VRectangle(
VObject* parent, VState state )
34 :
VPath( parent, state )
38VRectangle::VRectangle(
VObject* parent,
39 const KoPoint& topLeft,
double width,
double height,
double rx,
double ry )
40 :
VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_rx( rx ), m_ry( ry )
44 if( m_rx < 0.0 ) m_rx = 0.0;
45 if( m_ry < 0.0 ) m_ry = 0.0;
47 if( m_rx > m_width * 0.5 )
49 if( m_ry > m_height * 0.5 )
50 m_ry = m_height * 0.5;
58 if( m_rx == 0 && m_ry == 0 )
61 lineTo( KoPoint( m_topLeft.x(), m_topLeft.y() - m_height ) );
62 lineTo( KoPoint( m_topLeft.x() + m_width, m_topLeft.y() - m_height ) );
63 lineTo( KoPoint( m_topLeft.x() + m_width, m_topLeft.y() ) );
69 double x = m_topLeft.x();
70 double y = m_topLeft.y();
71 moveTo( KoPoint( x + rx, y ) );
72 curveTo( KoPoint( x + rx * ( 1 - 0.552 ), y ),
73 KoPoint( x, y - ry * ( 1 - 0.552 ) ),
74 KoPoint( x, y - ry ) );
75 if( ry < m_height / 2 )
76 lineTo( KoPoint( x, y - m_height + ry ) );
77 curveTo( KoPoint( x, y - m_height + ry * ( 1 - 0.552 ) ),
78 KoPoint( x + rx * ( 1 - 0.552 ), y - m_height ),
79 KoPoint( x + rx, y - m_height ) );
80 if( rx < m_width / 2 )
81 lineTo( KoPoint( x + m_width - rx, y - m_height ) );
82 curveTo( KoPoint( x + m_width - rx * ( 1 - 0.552 ), y - m_height ),
83 KoPoint( x + m_width, y - m_height + ry * ( 1 - 0.552 ) ),
84 KoPoint( x + m_width, y - m_height + ry ) );
85 if( ry < m_height / 2 )
86 lineTo( KoPoint( x + m_width, y - ry ) );
87 curveTo( KoPoint( x + m_width, y - ry * ( 1 - 0.552 ) ),
88 KoPoint( x + m_width - rx * ( 1 - 0.552 ), y ),
89 KoPoint( x + m_width - rx, y ) );
90 if( rx < m_width / 2 )
91 lineTo( KoPoint( x + rx, y ) );
100 return !result.isEmpty() ? result : i18n(
"Rectangle" );
104VRectangle::save( TQDomElement& element )
const
107 if( doc && doc->saveAsPath() )
115 TQDomElement me = element.ownerDocument().createElement(
"RECT" );
116 element.appendChild( me );
120 VTransformCmd cmd( 0L, m_matrix.invert() );
122 path.VObject::save( me );
125 me.setAttribute(
"x", m_topLeft.x() );
126 me.setAttribute(
"y", m_topLeft.y() );
128 me.setAttribute(
"width", TQString(
"%1pt").arg( m_width ) );
129 me.setAttribute(
"height", TQString(
"%1pt").arg( m_height ) );
131 me.setAttribute(
"rx", m_rx );
132 me.setAttribute(
"ry", m_ry );
134 TQString transform = buildSvgTransform();
135 if( !transform.isEmpty() )
136 me.setAttribute(
"transform", transform );
141VRectangle::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles,
int &index )
const
148 if( m_rx != 0. && m_ry != 0. && m_rx != m_ry )
151 docWriter->startElement(
"draw:rect" );
154 docWriter->addAttributePt(
"svg:x", m_topLeft.x() );
155 docWriter->addAttributePt(
"svg:y", m_topLeft.y()-m_height );
156 docWriter->addAttributePt(
"svg:width", m_width );
157 docWriter->addAttributePt(
"svg:height", m_height );
159 if( m_rx != 0. && m_ry != 0. && m_rx == m_ry )
160 docWriter->addAttributePt(
"draw:corner-radius", m_rx );
165 tmpMat.scale( 1, -1 );
166 tmpMat.translate( 0, -
document()->height() );
168 TQString transform = buildOasisTransform( m_matrix*tmpMat );
169 if( !transform.isEmpty() )
170 docWriter->addAttribute(
"draw:transform", transform );
172 docWriter->endElement();
176VRectangle::loadOasis(
const TQDomElement &element, KoOasisLoadingContext &context )
180 m_width = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg,
"width", TQString() ), 10.0 );
181 m_height = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg,
"height", TQString() ), 10.0 );
183 m_topLeft.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg,
"x", TQString() ) ) );
184 m_topLeft.setY( m_height + KoUnit::parseValue( element.attributeNS( KoXmlNS::svg,
"y", TQString() ) ) );
186 m_rx = m_ry = KoUnit::parseValue( element.attributeNS( KoXmlNS::draw,
"corner-radius", TQString() ) );
190 transformByViewbox( element, element.attributeNS( KoXmlNS::svg,
"viewBox", TQString() ) );
192 TQString trafo = element.attributeNS( KoXmlNS::draw,
"transform", TQString() );
193 if( !trafo.isEmpty() )
194 transformOasis( trafo );
200VRectangle::load(
const TQDomElement& element )
204 TQDomNodeList list = element.childNodes();
205 for( uint i = 0; i < list.count(); ++i )
206 if( list.item( i ).isElement() )
209 m_width = KoUnit::parseValue( element.attribute(
"width" ), 10.0 );
210 m_height = KoUnit::parseValue( element.attribute(
"height" ), 10.0 );
212 m_topLeft.setX( KoUnit::parseValue( element.attribute(
"x" ) ) );
213 m_topLeft.setY( KoUnit::parseValue( element.attribute(
"y" ) ) );
215 m_rx = KoUnit::parseValue( element.attribute(
"rx" ) );
216 m_ry = KoUnit::parseValue( element.attribute(
"ry" ) );
220 TQString trafo = element.attribute(
"transform" );
221 if( !trafo.isEmpty() )
228 return new VRectangle( *
this );
The base class for all karbon objects.
virtual void setState(const VState state)
Sets the state to a specified new state.
virtual void saveOasis(KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index) const
Save this object's state to OpenDocument.
@ deleted
deleted, nearly dead
@ normal
visible, not active
virtual TQString name() const
Returns the name of the object.
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
VDocument * document() const
Return document the object belongs to.
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
VState state() const
Get the state the object is in.
A composite path consists of one or many subpaths.
virtual void saveOasis(KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index) const
Save this object's state to OpenDocument.
void close()
Closes the current subpath.
virtual void save(TQDomElement &element) const
Save this object's state to xml.
virtual VPath * clone() const
Create an exact copy of this object.