karbon

vsinus.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 <qwmatrix.h>
00022 
00023 #include "vglobal.h"
00024 #include "vsinus.h"
00025 #include "vtransformcmd.h"
00026 #include <klocale.h>
00027 #include <KoUnit.h>
00028 #include <qdom.h>
00029 #include <vdocument.h>
00030 
00031 VSinus::VSinus( VObject* parent, VState state )
00032     : VPath( parent, state )
00033 {
00034 }
00035 
00036 VSinus::VSinus( VObject* parent,
00037         const KoPoint& topLeft, double width, double height, uint periods )
00038     : VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_periods( periods )
00039 {
00040     // We want at least 1 period:
00041     if( m_periods < 1 )
00042         m_periods = 1;
00043     init();
00044 }
00045 
00046 void
00047 VSinus::init()
00048 {
00049     KoPoint p1;
00050     KoPoint p2;
00051     KoPoint p3( 0.0, 0.0 );
00052     moveTo( p3 );
00053 
00054     for ( uint i = 0; i < m_periods; ++i )
00055     {
00056         p1.setX( i + 1.0/24.0 );
00057         p1.setY( ( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
00058         p2.setX( i + 1.0/12.0 );
00059         p2.setY( ( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
00060         p3.setX( i + 1.0/8.0 );
00061         p3.setY( VGlobal::sqrt2 * 0.5 );
00062         curveTo( p1, p2, p3 );
00063 
00064         p1.setX( i + 1.0/6.0 );
00065         p1.setY( ( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
00066         p2.setX( i + 5.0/24.0 );
00067         p2.setY( 1.0 );
00068         p3.setX( i + 1.0/4.0 );
00069         p3.setY( 1.0 );
00070         curveTo( p1, p2, p3 );
00071 
00072         p1.setX( i + 7.0/24.0 );
00073         p1.setY( 1.0 );
00074         p2.setX( i + 1.0/3.0 );
00075         p2.setY( ( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
00076         p3.setX( i + 3.0/8.0 );
00077         p3.setY( VGlobal::sqrt2 * 0.5 );
00078         curveTo( p1, p2, p3 );
00079 
00080         p1.setX( i + 5.0/12.0 );
00081         p1.setY( ( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
00082         p2.setX( i + 11.0/24.0 );
00083         p2.setY( ( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
00084         p3.setX( i + 1.0/2.0 );
00085         p3.setY( 0.0 );
00086         curveTo( p1, p2, p3 );
00087 
00088         p1.setX( i + 13.0/24.0 );
00089         p1.setY( -( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
00090         p2.setX( i + 7.0/12.0 );
00091         p2.setY( -( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
00092         p3.setX( i + 5.0/8.0 );
00093         p3.setY( -VGlobal::sqrt2 * 0.5 );
00094         curveTo( p1, p2, p3 );
00095 
00096         p1.setX( i + 2.0/3.0 );
00097         p1.setY( -( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
00098         p2.setX( i + 17.0/24.0 );
00099         p2.setY( -1.0 );
00100         p3.setX( i + 3.0/4.0 );
00101         p3.setY( -1.0 );
00102         curveTo( p1, p2, p3 );
00103 
00104         p1.setX( i + 19.0/24.0 );
00105         p1.setY( -1.0 );
00106         p2.setX( i + 5.0/6.0 );
00107         p2.setY( -( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
00108         p3.setX( i + 7.0/8.0 );
00109         p3.setY( -VGlobal::sqrt2 * 0.5 );
00110         curveTo( p1, p2, p3 );
00111 
00112         p1.setX( i + 11.0/12.0 );
00113         p1.setY( -( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
00114         p2.setX( i + 23.0/24.0 );
00115         p2.setY( -( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
00116         p3.setX( i + 1.0 );
00117         p3.setY( 0.0 );
00118         curveTo( p1, p2, p3 );
00119     }
00120 
00121     // Translate and scale:
00122     QWMatrix m;
00123     m.translate( m_topLeft.x(), m_topLeft.y() - m_height * 0.5 );
00124     m.scale( m_width / m_periods, m_height * 0.5 );
00125 
00126     // only tranform the path data
00127     VTransformCmd cmd( 0L, m );
00128     cmd.VVisitor::visitVPath( *this );
00129 
00130     m_matrix.reset();
00131 }
00132 
00133 QString
00134 VSinus::name() const
00135 {
00136     QString result = VObject::name();
00137     return !result.isEmpty() ? result : i18n( "Sinus" );
00138 }
00139 
00140 void
00141 VSinus::save( QDomElement& element ) const
00142 {
00143     VDocument *doc = document();
00144     if( doc && doc->saveAsPath() )
00145     {
00146         VPath::save( element );
00147         return;
00148     }
00149 
00150     if( state() != deleted )
00151     {
00152         QDomElement me = element.ownerDocument().createElement( "SINUS" );
00153         element.appendChild( me );
00154 
00155         // save fill/stroke untransformed
00156         VPath path( *this );
00157         VTransformCmd cmd( 0L, m_matrix.invert() );
00158         cmd.visit( path );
00159         path.VObject::save( me );
00160         //VObject::save( me );
00161 
00162         me.setAttribute( "x", m_topLeft.x() );
00163         me.setAttribute( "y", m_topLeft.y() );
00164 
00165         me.setAttribute( "width", m_width );
00166         me.setAttribute( "height", m_height );
00167 
00168         me.setAttribute( "periods", m_periods );
00169 
00170         QString transform = buildSvgTransform();
00171         if( !transform.isEmpty() )
00172             me.setAttribute( "transform", transform );
00173     }
00174 }
00175 
00176 void
00177 VSinus::load( const QDomElement& element )
00178 {
00179     setState( normal );
00180 
00181     QDomNodeList list = element.childNodes();
00182     for( uint i = 0; i < list.count(); ++i )
00183         if( list.item( i ).isElement() )
00184             VObject::load( list.item( i ).toElement() );
00185 
00186     m_width  = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
00187     m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
00188 
00189     m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
00190     m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
00191 
00192     m_periods  = element.attribute( "periods" ).toUInt();
00193 
00194     init();
00195 
00196     QString trafo = element.attribute( "transform" );
00197     if( !trafo.isEmpty() )
00198         transform( trafo );
00199 }
00200 
00201 VPath* 
00202 VSinus::clone() const
00203 {
00204     return new VSinus( *this );
00205 }
00206 
KDE Home | KDE Accessibility Home | Description of Access Keys