karbon

vsinus.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20
21#include <tqwmatrix.h>
22
23#include "vglobal.h"
24#include "vsinus.h"
25#include "vtransformcmd.h"
26#include <tdelocale.h>
27#include <KoUnit.h>
28#include <tqdom.h>
29#include <vdocument.h>
30
31VSinus::VSinus( VObject* parent, VState state )
32 : VPath( parent, state )
33{
34}
35
36VSinus::VSinus( VObject* parent,
37 const KoPoint& topLeft, double width, double height, uint periods )
38 : VPath( parent ), m_topLeft( topLeft ), m_width( width), m_height( height ), m_periods( periods )
39{
40 // We want at least 1 period:
41 if( m_periods < 1 )
42 m_periods = 1;
43 init();
44}
45
46void
47VSinus::init()
48{
49 KoPoint p1;
50 KoPoint p2;
51 KoPoint p3( 0.0, 0.0 );
52 moveTo( p3 );
53
54 for ( uint i = 0; i < m_periods; ++i )
55 {
56 p1.setX( i + 1.0/24.0 );
57 p1.setY( ( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
58 p2.setX( i + 1.0/12.0 );
59 p2.setY( ( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
60 p3.setX( i + 1.0/8.0 );
61 p3.setY( VGlobal::sqrt2 * 0.5 );
62 curveTo( p1, p2, p3 );
63
64 p1.setX( i + 1.0/6.0 );
65 p1.setY( ( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
66 p2.setX( i + 5.0/24.0 );
67 p2.setY( 1.0 );
68 p3.setX( i + 1.0/4.0 );
69 p3.setY( 1.0 );
70 curveTo( p1, p2, p3 );
71
72 p1.setX( i + 7.0/24.0 );
73 p1.setY( 1.0 );
74 p2.setX( i + 1.0/3.0 );
75 p2.setY( ( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
76 p3.setX( i + 3.0/8.0 );
77 p3.setY( VGlobal::sqrt2 * 0.5 );
78 curveTo( p1, p2, p3 );
79
80 p1.setX( i + 5.0/12.0 );
81 p1.setY( ( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
82 p2.setX( i + 11.0/24.0 );
83 p2.setY( ( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
84 p3.setX( i + 1.0/2.0 );
85 p3.setY( 0.0 );
86 curveTo( p1, p2, p3 );
87
88 p1.setX( i + 13.0/24.0 );
89 p1.setY( -( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
90 p2.setX( i + 7.0/12.0 );
91 p2.setY( -( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
92 p3.setX( i + 5.0/8.0 );
93 p3.setY( -VGlobal::sqrt2 * 0.5 );
94 curveTo( p1, p2, p3 );
95
96 p1.setX( i + 2.0/3.0 );
97 p1.setY( -( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
98 p2.setX( i + 17.0/24.0 );
99 p2.setY( -1.0 );
100 p3.setX( i + 3.0/4.0 );
101 p3.setY( -1.0 );
102 curveTo( p1, p2, p3 );
103
104 p1.setX( i + 19.0/24.0 );
105 p1.setY( -1.0 );
106 p2.setX( i + 5.0/6.0 );
107 p2.setY( -( 3.0 * VGlobal::sqrt2 + 2.0 ) * VGlobal::one_7 );
108 p3.setX( i + 7.0/8.0 );
109 p3.setY( -VGlobal::sqrt2 * 0.5 );
110 curveTo( p1, p2, p3 );
111
112 p1.setX( i + 11.0/12.0 );
113 p1.setY( -( 4.0 * VGlobal::sqrt2 - 2.0 ) * VGlobal::one_7 );
114 p2.setX( i + 23.0/24.0 );
115 p2.setY( -( 2.0 * VGlobal::sqrt2 - 1.0 ) * VGlobal::one_7 );
116 p3.setX( i + 1.0 );
117 p3.setY( 0.0 );
118 curveTo( p1, p2, p3 );
119 }
120
121 // Translate and scale:
122 TQWMatrix m;
123 m.translate( m_topLeft.x(), m_topLeft.y() - m_height * 0.5 );
124 m.scale( m_width / m_periods, m_height * 0.5 );
125
126 // only tranform the path data
127 VTransformCmd cmd( 0L, m );
128 cmd.VVisitor::visitVPath( *this );
129
130 m_matrix.reset();
131}
132
133TQString
134VSinus::name() const
135{
136 TQString result = VObject::name();
137 return !result.isEmpty() ? result : i18n( "Sinus" );
138}
139
140void
141VSinus::save( TQDomElement& element ) const
142{
143 VDocument *doc = document();
144 if( doc && doc->saveAsPath() )
145 {
146 VPath::save( element );
147 return;
148 }
149
150 if( state() != deleted )
151 {
152 TQDomElement me = element.ownerDocument().createElement( "SINUS" );
153 element.appendChild( me );
154
155 // save fill/stroke untransformed
156 VPath path( *this );
157 VTransformCmd cmd( 0L, m_matrix.invert() );
158 cmd.visit( path );
159 path.VObject::save( me );
160 //VObject::save( me );
161
162 me.setAttribute( "x", m_topLeft.x() );
163 me.setAttribute( "y", m_topLeft.y() );
164
165 me.setAttribute( "width", m_width );
166 me.setAttribute( "height", m_height );
167
168 me.setAttribute( "periods", m_periods );
169
170 TQString transform = buildSvgTransform();
171 if( !transform.isEmpty() )
172 me.setAttribute( "transform", transform );
173 }
174}
175
176void
177VSinus::load( const TQDomElement& element )
178{
179 setState( normal );
180
181 TQDomNodeList list = element.childNodes();
182 for( uint i = 0; i < list.count(); ++i )
183 if( list.item( i ).isElement() )
184 VObject::load( list.item( i ).toElement() );
185
186 m_width = KoUnit::parseValue( element.attribute( "width" ), 10.0 );
187 m_height = KoUnit::parseValue( element.attribute( "height" ), 10.0 );
188
189 m_topLeft.setX( KoUnit::parseValue( element.attribute( "x" ) ) );
190 m_topLeft.setY( KoUnit::parseValue( element.attribute( "y" ) ) );
191
192 m_periods = element.attribute( "periods" ).toUInt();
193
194 init();
195
196 TQString trafo = element.attribute( "transform" );
197 if( !trafo.isEmpty() )
198 transform( trafo );
199}
200
201VPath*
202VSinus::clone() const
203{
204 return new VSinus( *this );
205}
206
All non-visual, static doc info is in here.
Definition vdocument.h:53
The base class for all karbon objects.
Definition vobject.h:50
virtual TQString name() const
Returns the name of the object.
Definition vobject.cpp:225
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vobject.cpp:157
A composite path consists of one or many subpaths.
Definition vcomposite.h:48
virtual void save(TQDomElement &element) const
Save this object's state to xml.