karbon

vrectangle.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 "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     // Catch case, when radius is larger than width or height:
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         // save fill/stroke untransformed
00119         VPath path( *this );
00120         VTransformCmd cmd( 0L, m_matrix.invert() );
00121         cmd.visit( path );
00122         path.VObject::save( me );
00123         //VObject::save( me );
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     // do not save deleted objects
00144     if( state() == deleted )
00145         return;
00146 
00147     // different rx/ry is not supported by oasis, so act like it is a normal path
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     //save all into pt
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys