~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kpresenter/KPrBezierCurveObject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

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