kpresenter

KPrBezierCurveObject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
3 Copyright (C) 2005-2006 Thorsten Zachmann <zachmann@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KPrBezierCurveObject.h"
22#include "KPrCubicBezierCurveObjectIface.h"
23#include "KPrQuadricBezierCurveObjectIface.h"
24#include "KPrUtils.h"
25#include <KoTextZoomHandler.h>
26#include <tqpainter.h>
27#include <tqwmatrix.h>
28#include <tqdom.h>
29#include "KoPointArray.h"
30#include <kdebug.h>
31
32#include <math.h>
33using namespace std;
34
35KPrBezierCurveObject::KPrBezierCurveObject()
36: KPrPointObject()
37{
38}
39
40KPrBezierCurveObject::KPrBezierCurveObject( const KoPointArray &_controlPoints,
41 const KoPointArray &_allPoints,
42 const KoSize &_size, const KoPen &_pen,
43 LineEnd _lineBegin, LineEnd _lineEnd )
44: KPrPointObject( _pen, _lineBegin, _lineEnd )
45{
46 points = KoPointArray( _controlPoints );
47 allPoints = KoPointArray( _allPoints );
48
49 ext = _size;
50}
51
52
53KPrBezierCurveObject &KPrBezierCurveObject::operator=( const KPrBezierCurveObject & )
54{
55 return *this;
56}
57
58
59bool KPrBezierCurveObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
60{
61 KoRect rect( getRect() );
62 sc.xmlWriter.addAttribute("svg:viewBox", TQString( "0 0 %1 %2" ).arg( int( rect.width() * 100 ) )
63 .arg( int( rect.height() * 100 ) ) );
64
65 unsigned int pointCount = points.count();
66 unsigned int pos = 0;
67
68 TQString d;
69 d += TQString( "M%1 %2" ).arg( int( points.at(pos).x() * 100 ) )
70 .arg( int( points.at(pos).y() * 100 ) );
71
72 while ( pos + 4 <= pointCount )
73 {
74 d += TQString( "C%1 %2 %3 %4 %5 %6" ).arg( int( points.at( pos + 2 ).x() * 100 ) )
75 .arg( int( points.at( pos + 2 ).y() * 100 ) )
76 .arg( int( points.at( pos + 3 ).x() * 100 ) )
77 .arg( int( points.at( pos + 3 ).y() * 100 ) )
78 .arg( int( points.at( pos + 1 ).x() * 100 ) )
79 .arg( int( points.at( pos + 1 ).y() * 100 ) );
80 pos += 4;
81 }
82
83 if ( pos < pointCount )
84 {
85 d += TQString( "L%1 %2" ).arg( int( points.at( pos + 1 ).x() * 100 ) )
86 .arg( int( points.at( pos + 1 ).y() * 100 ) );
87 }
88
89 sc.xmlWriter.addAttribute( "svg:d", d );
90
91 return true;
92}
93
94
95const char * KPrBezierCurveObject::getOasisElementName() const
96{
97 return "draw:path";
98}
99
100
101void KPrBezierCurveObject::loadOasis( const TQDomElement &element, KoOasisContext & context, KPrLoadingInfo* info )
102{
103 kdDebug(33001) << "KPrBezierCurveObject::loadOasis" << endl;
104 KPrPointObject::loadOasis( element, context, info );
105
106 allPoints = bezier2polyline( points );
107
108 //load marker
109 loadOasisMarker( context );
110}
111
112TQDomDocumentFragment KPrBezierCurveObject::save( TQDomDocument& doc, double offset )
113{
114 return KPrPointObject::save( doc,offset );
115}
116
117double KPrBezierCurveObject::load(const TQDomElement &element)
118{
119 double offset = KPrPointObject::load( element );
120
121 allPoints = bezier2polyline( points );
122
123 return offset;
124}
125
126void KPrBezierCurveObject::updatePoints( double _fx, double _fy )
127{
128 KPrPointObject::updatePoints( _fx, _fy );
129
130 int index = 0;
131 KoPointArray tmpPoints;
132 KoPointArray::ConstIterator it;
133 for ( it = allPoints.begin(); it != allPoints.end(); ++it ) {
134 KoPoint point = (*it);
135 double tmpX = point.x() * _fx;
136 double tmpY = point.y() * _fy;
137
138 tmpPoints.putPoints( index, 1, tmpX,tmpY );
139 ++index;
140 }
141 allPoints = tmpPoints;
142}
143
144KoPointArray KPrBezierCurveObject::bezier2polyline( const KoPointArray &bezierPoints )
145{
146 if ( bezierPoints.isNull() )
147 return bezierPoints;
148
149 KoPointArray _points( bezierPoints );
150 KoPointArray _allPoints;
151 unsigned int pointCount = _points.count();
152
153 if ( pointCount == 2 ) // line
154 {
155 _allPoints = _points;
156 }
157 else // cubic bezier curve
158 {
159 KoPointArray tmpPointArray;
160 unsigned int _tmpIndex = 0;
161 unsigned int count = 0;
162 while ( count < pointCount )
163 {
164 if ( pointCount >= ( count + 4 ) ) // for cubic bezier curve
165 {
166 double _firstX = _points.at( count ).x();
167 double _firstY = _points.at( count ).y();
168
169 double _fourthX = _points.at( count + 1 ).x();
170 double _fourthY = _points.at( count + 1 ).y();
171
172 double _secondX = _points.at( count + 2 ).x();
173 double _secondY = _points.at( count + 2 ).y();
174
175 double _thirdX = _points.at( count + 3 ).x();
176 double _thirdY = _points.at( count + 3 ).y();
177
178 KoPointArray bezierPoint;
179 bezierPoint.putPoints( 0, 4, _firstX,_firstY, _secondX,_secondY,
180 _thirdX,_thirdY, _fourthX,_fourthY );
181 bezierPoint = bezierPoint.cubicBezier();
182
183 KoPointArray::ConstIterator it;
184 for ( it = bezierPoint.begin(); it != bezierPoint.end(); ++it )
185 {
186 KoPoint _point = (*it);
187 tmpPointArray.putPoints( _tmpIndex, 1, _point.x(), _point.y() );
188 ++_tmpIndex;
189 }
190
191 count += 4;
192 }
193 else // for line
194 {
195 double _x1 = _points.at( count ).x();
196 double _y1 = _points.at( count ).y();
197
198 double _x2 = _points.at( count + 1 ).x();
199 double _y2 = _points.at( count + 1 ).y();
200
201 tmpPointArray.putPoints( _tmpIndex, 2, _x1,_y1, _x2,_y2 );
202 _tmpIndex += 2;
203 count += 2;
204 }
205 }
206
207 _allPoints = tmpPointArray;
208 }
209
210 return _allPoints;
211}
212
213void KPrBezierCurveObject::flip(bool horizontal )
214{
215 KPrPointObject::flip( horizontal );
216
217 KoPointArray tmpPoints;
218 int index = 0;
219 if ( ! horizontal )
220 {
221 KoPointArray::ConstIterator it;
222 double horiz = getSize().height()/2;
223 for ( it = allPoints.begin(); it != allPoints.end(); ++it )
224 {
225 KoPoint point = (*it);
226 if ( point.y()> horiz )
227 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
228 else
229 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
230 ++index;
231 }
232 }
233 else
234 {
235 KoPointArray::ConstIterator it;
236 double vert = getSize().width()/2;
237 for ( it = allPoints.begin(); it != allPoints.end(); ++it )
238 {
239 KoPoint point = (*it);
240 if ( point.y()> vert )
241 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
242 else
243 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
244 ++index;
245 }
246 }
247
248 allPoints = tmpPoints;
249}
250
251
252KoPointArray KPrBezierCurveObject::getDrawingPoints() const
253{
254 return allPoints;
255}
256
257
258KPrCubicBezierCurveObject::KPrCubicBezierCurveObject()
259: KPrBezierCurveObject()
260{
261}
262
263
264KPrCubicBezierCurveObject::KPrCubicBezierCurveObject( const KoPointArray &_controlPoints,
265 const KoPointArray &_allPoints,
266 const KoSize & _size, const KoPen &_pen,
267 LineEnd _lineBegin, LineEnd _lineEnd )
268: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
269{
270}
271
272
273DCOPObject* KPrCubicBezierCurveObject::dcopObject()
274{
275 if ( !dcop )
276 dcop = new KPrCubicBezierCurveObjectIface( this );
277 return dcop;
278}
279
280
281KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject()
282: KPrBezierCurveObject()
283{
284}
285
286
287KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject( const KoPointArray &_controlPoints,
288 const KoPointArray &_allPoints,
289 const KoSize & _size, const KoPen &_pen,
290 LineEnd _lineBegin, LineEnd _lineEnd )
291: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
292{
293}
294
295
296DCOPObject* KPrQuadricBezierCurveObject::dcopObject()
297{
298 if ( !dcop )
299 dcop = new KPrQuadricBezierCurveObjectIface( this );
300 return dcop;
301}
Temporary information used only during loading.