00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KPrPolygonObject.h"
00023 #include "KPrGradient.h"
00024 #include <KoTextZoomHandler.h>
00025 #include <kdebug.h>
00026 #include <qbitmap.h>
00027 #include <qregion.h>
00028 #include <qdom.h>
00029 #include <qpicture.h>
00030 #include <qpainter.h>
00031 #include "KPrPolygonObjectIface.h"
00032 #include <KoUnit.h>
00033 #include <KoOasisContext.h>
00034 #include <math.h>
00035 #include <KoXmlNS.h>
00036 #include "KoPen.h"
00037
00038 using namespace std;
00039
00040 KPrPolygonObject::KPrPolygonObject()
00041 : KPr2DObject()
00042 {
00043 }
00044
00045 KPrPolygonObject::KPrPolygonObject( const KoPointArray &_points, const KoSize &_size,
00046 const KoPen &_pen, const QBrush &_brush,
00047 FillType _fillType, const QColor &_gColor1, const QColor &_gColor2, BCType _gType,
00048 bool _unbalanced, int _xfactor, int _yfactor,
00049 bool _checkConcavePolygon, int _cornersValue, int _sharpnessValue )
00050 : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType, _unbalanced, _xfactor, _yfactor )
00051 {
00052 points = KoPointArray( _points );
00053 ext = _size;
00054
00055 checkConcavePolygon = _checkConcavePolygon;
00056 cornersValue = _cornersValue;
00057 sharpnessValue = _sharpnessValue;
00058 }
00059
00060 KPrPolygonObject &KPrPolygonObject::operator=( const KPrPolygonObject & )
00061 {
00062 return *this;
00063 }
00064
00065 DCOPObject* KPrPolygonObject::dcopObject()
00066 {
00067 if ( !dcop )
00068 dcop = new KPrPolygonObjectIface( this );
00069 return dcop;
00070 }
00071
00072 bool KPrPolygonObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
00073 {
00074 sc.xmlWriter.addAttribute( "draw:corners", cornersValue );
00075 sc.xmlWriter.addAttribute( "draw:concave", checkConcavePolygon ? "true" : "false" );
00076 if ( checkConcavePolygon )
00077 {
00078 sc.xmlWriter.addAttribute( "draw:sharpness", QString( "%1%").arg( sharpnessValue ) );
00079 }
00080
00081 return true;
00082 }
00083
00084 const char * KPrPolygonObject::getOasisElementName() const
00085 {
00086 return "draw:regular-polygon";
00087 }
00088
00089
00090 QDomDocumentFragment KPrPolygonObject::save( QDomDocument& doc, double offset )
00091 {
00092 QDomDocumentFragment fragment = KPr2DObject::save( doc, offset );
00093
00094 QDomElement elemSettings = doc.createElement( "SETTINGS" );
00095
00096 elemSettings.setAttribute( "checkConcavePolygon", static_cast<int>( checkConcavePolygon ) );
00097 elemSettings.setAttribute( "cornersValue", cornersValue );
00098 elemSettings.setAttribute( "sharpnessValue", sharpnessValue );
00099
00100 fragment.appendChild( elemSettings );
00101
00102 if ( !points.isNull() ) {
00103 QDomElement elemPoints = doc.createElement( "POINTS" );
00104 KoPointArray::ConstIterator it;
00105 for ( it = points.begin(); it != points.end(); ++it ) {
00106 QDomElement elemPoint = doc.createElement( "Point" );
00107 KoPoint point = (*it);
00108 elemPoint.setAttribute( "point_x", point.x() );
00109 elemPoint.setAttribute( "point_y", point.y() );
00110
00111 elemPoints.appendChild( elemPoint );
00112 }
00113 fragment.appendChild( elemPoints );
00114 }
00115
00116 return fragment;
00117 }
00118
00119 void KPrPolygonObject::loadOasis( const QDomElement &element, KoOasisContext & context, KPrLoadingInfo *info )
00120 {
00121 kdDebug()<<"void KPrPolygonObject::loadOasis( const QDomElement &element )***********\n";
00122 KPr2DObject::loadOasis( element,context, info );
00123 cornersValue = element.attributeNS( KoXmlNS::draw, "corners", QString::null ).toInt();
00124 checkConcavePolygon = element.attributeNS( KoXmlNS::draw, "concave", QString::null ) == "true";
00125 sharpnessValue = 0;
00126 if ( checkConcavePolygon )
00127 {
00128 sharpnessValue = element.attributeNS( KoXmlNS::draw, "sharpness", QString::null ).remove( '%').toInt();
00129 }
00130 drawPolygon();
00131 }
00132
00133 double KPrPolygonObject::load( const QDomElement &element )
00134 {
00135 double offset=KPr2DObject::load( element );
00136
00137 QDomElement e = element.namedItem( "SETTINGS" ).toElement();
00138 if ( !e.isNull() ) {
00139 bool _checkConcavePolygon = false;
00140 int _cornersValue = 3;
00141 int _sharpnessValue = 0;
00142
00143 if ( e.hasAttribute( "checkConcavePolygon" ) )
00144 _checkConcavePolygon = static_cast<bool>( e.attribute( "checkConcavePolygon" ).toInt() );
00145 if ( e.hasAttribute( "cornersValue" ) )
00146 _cornersValue = e.attribute( "cornersValue" ).toInt();
00147 if ( e.hasAttribute( "sharpnessValue" ) )
00148 _sharpnessValue = e.attribute( "sharpnessValue" ).toInt();
00149
00150 checkConcavePolygon = _checkConcavePolygon;
00151 cornersValue = _cornersValue;
00152 sharpnessValue = _sharpnessValue;
00153 }
00154
00155 e = element.namedItem( "POINTS" ).toElement();
00156 if ( !e.isNull() ) {
00157 QDomElement elemPoint = e.firstChild().toElement();
00158 unsigned int index = 0;
00159 while ( !elemPoint.isNull() ) {
00160 if ( elemPoint.tagName() == "Point" ) {
00161 double tmpX = 0;
00162 double tmpY = 0;
00163 if( elemPoint.hasAttribute( "point_x" ) )
00164 tmpX = elemPoint.attribute( "point_x" ).toDouble();
00165 if( elemPoint.hasAttribute( "point_y" ) )
00166 tmpY = elemPoint.attribute( "point_y" ).toDouble();
00167
00168 points.putPoints( index, 1, tmpX,tmpY );
00169 }
00170 elemPoint = elemPoint.nextSibling().toElement();
00171 ++index;
00172 }
00173 }
00174 return offset;
00175 }
00176
00177 void KPrPolygonObject::setSize( double _width, double _height )
00178 {
00179 KoSize origSize( ext );
00180 KPrObject::setSize( _width, _height );
00181
00182 double fx = ext.width() / origSize.width();
00183 double fy = ext.height() / origSize.height();
00184
00185 updatePoints( fx, fy );
00186 }
00187
00188 void KPrPolygonObject::updatePoints( double _fx, double _fy )
00189 {
00190 int index = 0;
00191 KoPointArray tmpPoints;
00192 KoPointArray::ConstIterator it;
00193 for ( it = points.begin(); it != points.end(); ++it ) {
00194 KoPoint point = (*it);
00195 double tmpX = point.x() * _fx;
00196 double tmpY = point.y() * _fy;
00197
00198 tmpPoints.putPoints( index, 1, tmpX,tmpY );
00199 ++index;
00200 }
00201 points = tmpPoints;
00202 }
00203
00204 void KPrPolygonObject::paint( QPainter* _painter,KoTextZoomHandler*_zoomHandler,
00205 int , bool drawingShadow, bool drawContour )
00206 {
00207 int _w = ( pen.style() == Qt::NoPen ) ? 1 : pen.width();
00208
00209 if ( drawContour ) {
00210 QPointArray pointArray2 = points.zoomPointArray( _zoomHandler );
00211 QPen pen3( Qt::black, 1, Qt::DotLine );
00212 _painter->setPen( pen3 );
00213 _painter->setRasterOp( Qt::NotXorROP );
00214 _painter->drawPolygon( pointArray2 );
00215 return;
00216 }
00217
00218 QPointArray pointArray = points.zoomPointArray( _zoomHandler, _w );
00219 QPen pen2 = pen.zoomedPen( _zoomHandler );
00220
00221 if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) {
00222 _painter->setPen( pen2 );
00223 _painter->setBrush( getBrush() );
00224 _painter->drawPolygon( pointArray );
00225 }
00226 else {
00227 QSize size( _zoomHandler->zoomSize( ext ) );
00228 if ( m_redrawGradientPix || gradient->size() != size )
00229 {
00230 m_redrawGradientPix = false;
00231 gradient->setSize( size );
00232 QRegion clipregion( pointArray );
00233 m_gradientPix.resize( size );
00234 m_gradientPix.fill( Qt::white );
00235
00236 QPainter p;
00237 p.begin( &m_gradientPix );
00238 p.setClipRegion( clipregion );
00239 p.drawPixmap( 0, 0, gradient->pixmap() );
00240 p.end();
00241
00242 m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
00243 }
00244
00245 QRect _rect = pointArray.boundingRect();
00246 _painter->drawPixmap( 0, 0, m_gradientPix, 0, 0, _rect.width(), _rect.height() );
00247
00248 _painter->setPen( pen2 );
00249 _painter->setBrush( Qt::NoBrush );
00250 _painter->drawPolygon( pointArray );
00251
00252 }
00253 }
00254
00255 void KPrPolygonObject::drawPolygon()
00256 {
00257 kdDebug()<<"void KPrPolygonObject::drawPolygon()***********\n";
00258 KoRect _rect( 0, 0, ext.width(), ext.height() );
00259 double angle = 2 * M_PI / cornersValue;
00260 double diameter = static_cast<double>( QMAX( _rect.width(), _rect.height() ) );
00261 double radius = diameter * 0.5;
00262
00263 KoPointArray _points( checkConcavePolygon ? cornersValue * 2 : cornersValue );
00264 _points.setPoint( 0, 0, qRound( -radius ) );
00265
00266 double xmin = 0;
00267 double ymin = qRound( -radius );
00268
00269 if ( checkConcavePolygon ) {
00270 angle = angle / 2.0;
00271 double a = angle;
00272 double r = radius - ( sharpnessValue / 100.0 * radius );
00273 for ( int i = 1; i < cornersValue * 2; ++i ) {
00274 double xp, yp;
00275 if ( i % 2 ) {
00276 xp = r * sin( a );
00277 yp = -r * cos( a );
00278 }
00279 else {
00280 xp = radius * sin( a );
00281 yp = -radius * cos( a );
00282 }
00283 a += angle;
00284 _points.setPoint( i, xp, yp );
00285 if (xp < xmin)
00286 xmin = xp;
00287 if (yp < ymin)
00288 ymin = yp;
00289 }
00290 }
00291 else {
00292 double a = angle;
00293 for ( int i = 1; i < cornersValue; ++i ) {
00294 double xp = radius * sin( a );
00295 double yp = -radius * cos( a );
00296 a += angle;
00297 _points.setPoint( i, xp, yp );
00298 if (xp < xmin)
00299 xmin = xp;
00300 if (yp < ymin)
00301 ymin = yp;
00302 }
00303 }
00304
00305
00306 KoRect _changRect = _points.boundingRect();
00307 double fx = _rect.width() / _changRect.width();
00308 double fy = _rect.height() / _changRect.height();
00309
00310 int _index = 0;
00311 KoPointArray tmpPoints;
00312 KoPointArray::ConstIterator it;
00313 for ( it = _points.begin(); it != _points.end(); ++it ) {
00314 KoPoint point = (*it);
00315 double tmpX = ( point.x() - xmin) * fx;
00316 double tmpY = ( point.y() - ymin) * fy;
00317
00318 tmpPoints.putPoints( _index, 1, tmpX,tmpY );
00319 ++_index;
00320 }
00321
00322 points = tmpPoints;
00323
00324 if ( getFillType() == FT_GRADIENT && gradient )
00325 m_redrawGradientPix = true;
00326 }
00327
00328 void KPrPolygonObject::flip( bool horizontal )
00329 {
00330 KPr2DObject::flip( horizontal );
00331
00332 KoPointArray tmpPoints;
00333 int index = 0;
00334 if ( ! horizontal )
00335 {
00336 KoPointArray::ConstIterator it;
00337 double horiz = getSize().height()/2;
00338 for ( it = points.begin(); it != points.end(); ++it ) {
00339 KoPoint point = (*it);
00340 if ( point.y()> horiz )
00341 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
00342 else
00343 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
00344 ++index;
00345 }
00346 }
00347 else
00348 {
00349 KoPointArray::ConstIterator it;
00350 double vert = getSize().width()/2;
00351 for ( it = points.begin(); it != points.end(); ++it ) {
00352 KoPoint point = (*it);
00353 if ( point.x()> vert )
00354 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
00355 else
00356 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
00357 ++index;
00358 }
00359 }
00360 points = tmpPoints;
00361 }
00362
00363 KoSize KPrPolygonObject::getRealSize() const {
00364 KoSize size( ext );
00365 KoPoint realOrig( orig );
00366 KoPointArray p( points );
00367 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00368 return size;
00369 }
00370
00371 KoPoint KPrPolygonObject::getRealOrig() const {
00372 KoSize size( ext );
00373 KoPoint realOrig( orig );
00374 KoPointArray p( points );
00375 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00376 return realOrig;
00377 }