kpresenter

KPrClosedLineObject.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) 2002 Toshitaka Fujioka <fujioka@kde.org>
00004    Copyright (C) 2005 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 "KPrClosedLineObject.h"
00023 #include "KPrPointObject.h"
00024 #include "KPrGradient.h"
00025 #include "KPrSVGPathParser.h"
00026 #include <KoTextZoomHandler.h>
00027 #include <kdebug.h>
00028 #include <qbitmap.h>
00029 #include <qregion.h>
00030 #include <qregexp.h>
00031 #include <qdom.h>
00032 #include <qpicture.h>
00033 #include <qpainter.h>
00034 #include <KoOasisContext.h>
00035 #include <math.h>
00036 #include <KoUnit.h>
00037 #include <KoXmlNS.h>
00038 #include <KoDom.h>
00039 
00040 using namespace std;
00041 
00042 KPrClosedLineObject::KPrClosedLineObject()
00043     : KPr2DObject()
00044 {
00045 }
00046 
00047 KPrClosedLineObject::KPrClosedLineObject( const KoPointArray &_points, const KoSize &_size, const KoPen &_pen, const QBrush &_brush,
00048                                         FillType _fillType, const QColor &_gColor1, const QColor &_gColor2, BCType _gType,
00049                                         bool _unbalanced, int _xfactor, int _yfactor, const QString _typeString )
00050     : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType, _unbalanced, _xfactor, _yfactor )
00051 {
00052     points = KoPointArray( _points );
00053     ext = _size;
00054     typeString = _typeString;
00055 }
00056 
00057 KPrClosedLineObject::KPrClosedLineObject( const KPrPointObject &object )
00058 : KPr2DObject( object.getPen(), QBrush::NoBrush, FT_BRUSH, QColor(), QColor(), BCT_PLAIN, false, 0, 0 )
00059 {
00060     ext = object.getSize();
00061     orig = object.getOrig();
00062     objectName = object.getObjectName();
00063     points = object.getPoints().copy();
00064     points.putPoints( points.count(), 1, points.at( 0 ).x(), points.at( 0 ).y() );
00065     switch ( object.getType() )
00066     {
00067         case OT_FREEHAND:
00068             typeString = i18n( "Closed Freehand" );
00069             break;
00070         case OT_POLYLINE:
00071             typeString = i18n( "Closed Polyline" );
00072             break;
00073         case OT_CUBICBEZIERCURVE:
00074             typeString = i18n( "Closed Cubic Bezier Curve" );
00075             break;
00076         case OT_QUADRICBEZIERCURVE:
00077             typeString = i18n( "Closed Quadric Bezier Curve" );
00078             break;
00079         default:    
00080             break;
00081     }
00082             
00083 }
00084 
00085 KPrClosedLineObject &KPrClosedLineObject::operator=( const KPrClosedLineObject & )
00086 {
00087     return *this;
00088 }
00089 
00090 #if 0
00091 DCOPObject* KPrClosedLineObject::dcopObject()
00092 {
00093     if ( !dcop )
00094         dcop = new KPClosedLineObjectIface( this );
00095     return dcop;
00096 }
00097 #endif
00098 
00099 QDomDocumentFragment KPrClosedLineObject::save( QDomDocument& doc, double offset )
00100 {
00101     QDomDocumentFragment fragment = KPr2DObject::save( doc, offset );
00102 
00103     QDomElement elemObjectsName = doc.createElement( "OBJECTSNAME" );
00104 
00105     elemObjectsName.setAttribute( "NAME", typeString );
00106 
00107     fragment.appendChild( elemObjectsName );
00108 
00109     if ( !points.isNull() ) {
00110         QDomElement elemPoints = doc.createElement( "POINTS" );
00111         KoPointArray::ConstIterator it;
00112         for ( it = points.begin(); it != points.end(); ++it ) {
00113             QDomElement elemPoint = doc.createElement( "Point" );
00114             KoPoint point = (*it);
00115             elemPoint.setAttribute( "point_x", point.x() );
00116             elemPoint.setAttribute( "point_y", point.y() );
00117 
00118             elemPoints.appendChild( elemPoint );
00119         }
00120         fragment.appendChild( elemPoints );
00121     }
00122 
00123     return fragment;
00124 }
00125 
00126 bool KPrClosedLineObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
00127 {
00128     KPrShadowObject::saveOasisDrawPoints( points, sc );
00129     return true;
00130 }
00131 
00132 const char * KPrClosedLineObject::getOasisElementName() const
00133 {
00134     return "draw:polygon";
00135 }
00136 
00137 
00138 double KPrClosedLineObject::load( const QDomElement &element )
00139 {
00140     double offset = KPr2DObject::load( element );
00141 
00142     QDomElement e = element.namedItem( "OBJECTSNAME" ).toElement();
00143     if ( !e.isNull() ) {
00144         if ( e.hasAttribute( "NAME" ) )
00145             typeString = e.attribute( "NAME" );
00146     }
00147 
00148     e = element.namedItem( "POINTS" ).toElement();
00149     if ( !e.isNull() ) {
00150         QDomElement elemPoint = e.firstChild().toElement();
00151         unsigned int index = 0;
00152         while ( !elemPoint.isNull() ) {
00153             if ( elemPoint.tagName() == "Point" ) {
00154                 double tmpX = 0;
00155                 double tmpY = 0;
00156                 if( elemPoint.hasAttribute( "point_x" ) )
00157                     tmpX = elemPoint.attribute( "point_x" ).toDouble();
00158                 if( elemPoint.hasAttribute( "point_y" ) )
00159                     tmpY = elemPoint.attribute( "point_y" ).toDouble();
00160 
00161                 points.putPoints( index, 1, tmpX,tmpY );
00162             }
00163             elemPoint = elemPoint.nextSibling().toElement();
00164             ++index;
00165         }
00166     }
00167     return offset;
00168 }
00169 
00170 void KPrClosedLineObject::setSize( double _width, double _height )
00171 {
00172     KoSize origSize( ext );
00173     KPrObject::setSize( _width, _height );
00174 
00175     double fx = ext.width() / origSize.width();
00176     double fy = ext.height() / origSize.height();
00177 
00178     updatePoints( fx, fy );
00179 }
00180 
00181 void KPrClosedLineObject::updatePoints( double _fx, double _fy )
00182 {
00183     int index = 0;
00184     KoPointArray tmpPoints;
00185     KoPointArray::ConstIterator it;
00186     for ( it = points.begin(); it != points.end(); ++it ) {
00187         KoPoint point = (*it);
00188         double tmpX = point.x() * _fx;
00189         double tmpY = point.y() * _fy;
00190 
00191         tmpPoints.putPoints( index, 1, tmpX,tmpY );
00192         ++index;
00193     }
00194     points = tmpPoints;
00195 }
00196 
00197 void KPrClosedLineObject::paint( QPainter* _painter,KoTextZoomHandler*_zoomHandler,
00198                                 int /* pageNum */, bool drawingShadow, bool drawContour )
00199 {
00200     int _w = ( pen.style() == Qt::NoPen ) ? 1 : int( pen.pointWidth() );
00201 
00202     if ( drawContour ) {
00203         QPointArray pointArray2 = points.zoomPointArray( _zoomHandler );
00204         QPen pen3( Qt::black, 1, Qt::DotLine );
00205         _painter->setPen( pen3 );
00206         _painter->setRasterOp( Qt::NotXorROP );
00207         _painter->drawPolygon( pointArray2 );
00208         return;
00209     }
00210 
00211     QPointArray pointArray = points.zoomPointArray( _zoomHandler, _w );
00212     QPen pen2 = pen.zoomedPen( _zoomHandler );
00213 
00214     if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) {
00215         _painter->setPen( pen2 );
00216         _painter->setBrush( getBrush() );
00217         _painter->drawPolygon( pointArray );
00218     }
00219     else {
00220         QSize size( _zoomHandler->zoomSize( ext ) );
00221         if ( m_redrawGradientPix || gradient->size() != size )
00222         {
00223             m_redrawGradientPix = false;
00224             gradient->setSize( size );
00225             QRegion clipregion( pointArray );
00226             m_gradientPix.resize( size );
00227             m_gradientPix.fill( Qt::white );
00228 
00229             QPainter p;
00230             p.begin( &m_gradientPix );
00231             p.setClipRegion( clipregion );
00232             p.drawPixmap( 0, 0, gradient->pixmap() );
00233             p.end();
00234 
00235             m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
00236         }
00237 
00238         QRect _rect = pointArray.boundingRect();
00239         _painter->drawPixmap( 0, 0, m_gradientPix, 0, 0, _rect.width(), _rect.height() );
00240 
00241         _painter->setPen( pen2 );
00242         _painter->setBrush( Qt::NoBrush );
00243         _painter->drawPolygon( pointArray );
00244     }
00245 }
00246 
00247 void KPrClosedLineObject::flip( bool horizontal )
00248 {
00249     KPr2DObject::flip( horizontal );
00250     // flip the points
00251     KoPointArray tmpPoints;
00252     int index = 0;
00253     if ( ! horizontal )
00254     {
00255         KoPointArray::ConstIterator it;
00256         double horiz = getSize().height()/2;
00257         for ( it = points.begin(); it != points.end(); ++it ) {
00258             KoPoint point = (*it);
00259             if ( point.y()> horiz )
00260                 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
00261             else
00262                 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
00263             ++index;
00264         }
00265     }
00266     else
00267     {
00268         KoPointArray::ConstIterator it;
00269         double vert = getSize().width()/2;
00270         for ( it = points.begin(); it != points.end(); ++it ) {
00271             KoPoint point = (*it);
00272             if ( point.x()> vert )
00273                 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
00274             else
00275                 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
00276             ++index;
00277         }
00278     }
00279     points = tmpPoints;
00280 }
00281 
00282 void KPrClosedLineObject::loadOasis( const QDomElement &element, KoOasisContext & context, KPrLoadingInfo *info )
00283 {
00284     kdDebug()<<"void KPrClosedLineObject::loadOasis( const QDomElement &element )***********\n";
00285     KPr2DObject::loadOasis( element,context, info );
00286     QString tag( element.tagName() );
00287     if ( tag == "polygon" )
00288     {
00289         KPrShadowObject::loadOasisDrawPoints( points, element, context, info );
00290     }
00291     else if ( tag == "path" ) // this is used to load closed draw:path objects
00292     {
00293         QString d = element.attributeNS( KoXmlNS::svg, "d", QString::null);
00294         kdDebug(33001) << "path d: " << d << endl;
00295 
00296         KPrSVGPathParser parser;
00297         points = parser.getPoints( d, true );
00298         loadOasisApplyViewBox( element, points );
00299     }
00300     else if ( tag == "custom-shape" )
00301     {
00302         QDomElement enhancedGeometry = KoDom::namedItemNS( element, KoXmlNS::draw, "enhanced-geometry" );
00303 
00304         if ( !enhancedGeometry.isNull() )
00305         {
00306             QString d = enhancedGeometry.attributeNS( KoXmlNS::draw, "enhanced-path", QString::null );
00307             QRegExp rx( "^([0-9 MLZ]+)N$" );
00308             if ( rx.search( d ) != -1 )
00309             {
00310                 d = rx.cap( 1 );
00311                 kdDebug(33001) << "enhanced-path d: " << d << endl;
00312 
00313                 KPrSVGPathParser parser;
00314                 points = parser.getPoints( d, true );
00315                 loadOasisApplyViewBox( enhancedGeometry, points );
00316             }
00317         }
00318     }
00319     else
00320     {
00321         kdDebug(33001) << "KPrClosedLineObject::loadOasis unsupported tag" << endl;
00322     }
00323 }
00324 
00325 KoSize KPrClosedLineObject::getRealSize() const {
00326     KoSize size( ext );
00327     KoPoint realOrig( orig );
00328     KoPointArray p( points );
00329     getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00330     return size;
00331 }
00332 
00333 KoPoint KPrClosedLineObject::getRealOrig() const {
00334     KoSize size( ext );
00335     KoPoint realOrig( orig );
00336     KoPointArray p( points );
00337     getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00338     return realOrig;
00339 }
KDE Home | KDE Accessibility Home | Description of Access Keys