kpresenter

KPrBezierCurveObject.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
00004    Copyright (C) 2005-2006 Thorsten Zachmann <zachmann@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "KPrBezierCurveObject.h"
00023 #include "KPrCubicBezierCurveObjectIface.h"
00024 #include "KPrQuadricBezierCurveObjectIface.h"
00025 #include "KPrUtils.h"
00026 #include <KoTextZoomHandler.h>
00027 #include <qpainter.h>
00028 #include <qwmatrix.h>
00029 #include <qdom.h>
00030 #include "KoPointArray.h"
00031 #include <kdebug.h>
00032 
00033 #include <math.h>
00034 using namespace std;
00035 
00036 KPrBezierCurveObject::KPrBezierCurveObject()
00037 : KPrPointObject()
00038 {
00039 }
00040 
00041 KPrBezierCurveObject::KPrBezierCurveObject( const KoPointArray &_controlPoints,
00042                                             const KoPointArray &_allPoints,
00043                                             const KoSize &_size, const KoPen &_pen,
00044                                             LineEnd _lineBegin, LineEnd _lineEnd )
00045 : KPrPointObject( _pen, _lineBegin, _lineEnd )
00046 {
00047     points = KoPointArray( _controlPoints );
00048     allPoints = KoPointArray( _allPoints );
00049 
00050     ext = _size;
00051 }
00052 
00053 
00054 KPrBezierCurveObject &KPrBezierCurveObject::operator=( const KPrBezierCurveObject & )
00055 {
00056     return *this;
00057 }
00058 
00059 
00060 bool KPrBezierCurveObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
00061 {
00062     KoRect rect( getRect() );
00063     sc.xmlWriter.addAttribute("svg:viewBox", QString( "0 0 %1 %2" ).arg( int( rect.width() * 100 ) )
00064                                                                    .arg( int( rect.height() * 100 ) ) );
00065 
00066     unsigned int pointCount = points.count();
00067     unsigned int pos = 0;
00068 
00069     QString d;
00070     d += QString( "M%1 %2" ).arg( int( points.at(pos).x() * 100 ) )
00071                             .arg( int( points.at(pos).y() * 100 ) );
00072 
00073     while ( pos + 4 <= pointCount )
00074     {
00075         d += QString( "C%1 %2 %3 %4 %5 %6" ).arg( int( points.at( pos + 2 ).x() * 100 ) )
00076                                             .arg( int( points.at( pos + 2 ).y() * 100 ) )
00077                                             .arg( int( points.at( pos + 3 ).x() * 100 ) )
00078                                             .arg( int( points.at( pos + 3 ).y() * 100 ) )
00079                                             .arg( int( points.at( pos + 1 ).x() * 100 ) )
00080                                             .arg( int( points.at( pos + 1 ).y() * 100 ) );
00081         pos += 4;
00082     }
00083 
00084     if ( pos < pointCount )
00085     {
00086         d += QString( "L%1 %2" ).arg( int( points.at( pos + 1 ).x() * 100 ) )
00087                                 .arg( int( points.at( pos + 1 ).y() * 100 ) );
00088     }
00089 
00090     sc.xmlWriter.addAttribute( "svg:d", d );
00091     
00092     return true;
00093 }
00094 
00095 
00096 const char * KPrBezierCurveObject::getOasisElementName() const
00097 {
00098     return "draw:path";
00099 }
00100 
00101 
00102 void KPrBezierCurveObject::loadOasis( const QDomElement &element, KoOasisContext & context,  KPrLoadingInfo* info )
00103 {
00104     kdDebug(33001) << "KPrBezierCurveObject::loadOasis" << endl;
00105     KPrPointObject::loadOasis( element, context, info );
00106 
00107     allPoints = bezier2polyline( points );
00108 
00109     //load marker
00110     loadOasisMarker( context );
00111 }
00112 
00113 QDomDocumentFragment KPrBezierCurveObject::save( QDomDocument& doc, double offset )
00114 {
00115     return KPrPointObject::save( doc,offset );
00116 }
00117 
00118 double KPrBezierCurveObject::load(const QDomElement &element)
00119 {
00120     double offset = KPrPointObject::load( element );
00121 
00122     allPoints = bezier2polyline( points );
00123 
00124     return offset;
00125 }
00126 
00127 void KPrBezierCurveObject::updatePoints( double _fx, double _fy )
00128 {
00129     KPrPointObject::updatePoints( _fx, _fy );
00130 
00131     int index = 0;
00132     KoPointArray tmpPoints;
00133     KoPointArray::ConstIterator it;
00134     for ( it = allPoints.begin(); it != allPoints.end(); ++it ) {
00135         KoPoint point = (*it);
00136         double tmpX = point.x() * _fx;
00137         double tmpY = point.y() * _fy;
00138 
00139         tmpPoints.putPoints( index, 1, tmpX,tmpY );
00140         ++index;
00141     }
00142     allPoints = tmpPoints;
00143 }
00144 
00145 KoPointArray KPrBezierCurveObject::bezier2polyline( const KoPointArray &bezierPoints )
00146 {
00147     if ( bezierPoints.isNull() )
00148         return bezierPoints;
00149 
00150     KoPointArray _points( bezierPoints );
00151     KoPointArray _allPoints;
00152     unsigned int pointCount = _points.count();
00153 
00154     if ( pointCount == 2 ) // line
00155     {
00156         _allPoints = _points;
00157     }
00158     else // cubic bezier curve
00159     { 
00160         KoPointArray tmpPointArray;
00161         unsigned int _tmpIndex = 0;
00162         unsigned int count = 0;
00163         while ( count < pointCount ) 
00164         {
00165             if ( pointCount >= ( count + 4 ) ) // for cubic bezier curve
00166             {
00167                 double _firstX = _points.at( count ).x();
00168                 double _firstY = _points.at( count ).y();
00169 
00170                 double _fourthX = _points.at( count + 1 ).x();
00171                 double _fourthY = _points.at( count + 1 ).y();
00172 
00173                 double _secondX = _points.at( count + 2 ).x();
00174                 double _secondY = _points.at( count + 2 ).y();
00175 
00176                 double _thirdX = _points.at( count + 3 ).x();
00177                 double _thirdY = _points.at( count + 3 ).y();
00178 
00179                 KoPointArray bezierPoint;
00180                 bezierPoint.putPoints( 0, 4, _firstX,_firstY, _secondX,_secondY,
00181                                              _thirdX,_thirdY, _fourthX,_fourthY );
00182                 bezierPoint = bezierPoint.cubicBezier();
00183 
00184                 KoPointArray::ConstIterator it;
00185                 for ( it = bezierPoint.begin(); it != bezierPoint.end(); ++it ) 
00186                 {
00187                     KoPoint _point = (*it);
00188                     tmpPointArray.putPoints( _tmpIndex, 1, _point.x(), _point.y() );
00189                     ++_tmpIndex;
00190                 }
00191 
00192                 count += 4;
00193             }
00194             else // for line
00195             {
00196                 double _x1 = _points.at( count ).x();
00197                 double _y1 = _points.at( count ).y();
00198 
00199                 double _x2 = _points.at( count + 1 ).x();
00200                 double _y2 = _points.at( count + 1 ).y();
00201 
00202                 tmpPointArray.putPoints( _tmpIndex, 2, _x1,_y1, _x2,_y2 );
00203                 _tmpIndex += 2;
00204                 count += 2;
00205             }
00206         }
00207 
00208         _allPoints = tmpPointArray;
00209     }
00210 
00211     return _allPoints;
00212 }
00213 
00214 void KPrBezierCurveObject::flip(bool horizontal )
00215 {
00216     KPrPointObject::flip( horizontal );
00217 
00218     KoPointArray tmpPoints;
00219     int index = 0;
00220     if ( ! horizontal )
00221     {
00222         KoPointArray::ConstIterator it;
00223         double horiz = getSize().height()/2;
00224         for ( it = allPoints.begin(); it != allPoints.end(); ++it )
00225         {
00226             KoPoint point = (*it);
00227             if ( point.y()> horiz )
00228                 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
00229             else
00230                 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
00231             ++index;
00232         }
00233     }
00234     else
00235     {
00236         KoPointArray::ConstIterator it;
00237         double vert = getSize().width()/2;
00238         for ( it = allPoints.begin(); it != allPoints.end(); ++it )
00239         {
00240             KoPoint point = (*it);
00241             if ( point.y()> vert )
00242                 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
00243             else
00244                 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
00245             ++index;
00246         }
00247     }
00248 
00249     allPoints = tmpPoints;
00250 }
00251 
00252 
00253 KoPointArray KPrBezierCurveObject::getDrawingPoints() const
00254 {
00255   return allPoints;
00256 }
00257 
00258 
00259 KPrCubicBezierCurveObject::KPrCubicBezierCurveObject()
00260 : KPrBezierCurveObject()
00261 {
00262 }
00263 
00264 
00265 KPrCubicBezierCurveObject::KPrCubicBezierCurveObject( const KoPointArray &_controlPoints, 
00266                                                       const KoPointArray &_allPoints, 
00267                                                       const KoSize & _size, const KoPen &_pen, 
00268                                                       LineEnd _lineBegin, LineEnd _lineEnd )
00269 : KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
00270 {
00271 }
00272 
00273 
00274 DCOPObject* KPrCubicBezierCurveObject::dcopObject()
00275 {
00276     if ( !dcop )
00277         dcop = new KPrCubicBezierCurveObjectIface( this );
00278     return dcop;
00279 }
00280 
00281 
00282 KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject()
00283 : KPrBezierCurveObject()
00284 {
00285 }
00286 
00287 
00288 KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject( const KoPointArray &_controlPoints, 
00289                                                           const KoPointArray &_allPoints, 
00290                                                           const KoSize & _size, const KoPen &_pen, 
00291                                                           LineEnd _lineBegin, LineEnd _lineEnd )
00292 : KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
00293 {
00294 }
00295 
00296 
00297 DCOPObject* KPrQuadricBezierCurveObject::dcopObject()
00298 {
00299     if ( !dcop )
00300         dcop = new KPrQuadricBezierCurveObjectIface( this );
00301     return dcop;
00302 }
KDE Home | KDE Accessibility Home | Description of Access Keys