21#include "KPrPolygonObject.h"
22#include "KPrGradient.h"
23#include <KoTextZoomHandler.h>
30#include "KPrPolygonObjectIface.h"
32#include <KoOasisContext.h>
39KPrPolygonObject::KPrPolygonObject()
44KPrPolygonObject::KPrPolygonObject(
const KoPointArray &_points,
const KoSize &_size,
45 const KoPen &_pen,
const TQBrush &_brush,
46 FillType _fillType,
const TQColor &_gColor1,
const TQColor &_gColor2, BCType _gType,
47 bool _unbalanced,
int _xfactor,
int _yfactor,
48 bool _checkConcavePolygon,
int _cornersValue,
int _sharpnessValue )
49 : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType, _unbalanced, _xfactor, _yfactor )
51 points = KoPointArray( _points );
54 checkConcavePolygon = _checkConcavePolygon;
55 cornersValue = _cornersValue;
56 sharpnessValue = _sharpnessValue;
59KPrPolygonObject &KPrPolygonObject::operator=(
const KPrPolygonObject & )
64DCOPObject* KPrPolygonObject::dcopObject()
67 dcop =
new KPrPolygonObjectIface(
this );
71bool KPrPolygonObject::saveOasisObjectAttributes( KPOasisSaveContext &sc )
const
73 sc.xmlWriter.addAttribute(
"draw:corners", cornersValue );
74 sc.xmlWriter.addAttribute(
"draw:concave", checkConcavePolygon ?
"true" :
"false" );
75 if ( checkConcavePolygon )
77 sc.xmlWriter.addAttribute(
"draw:sharpness", TQString(
"%1%").arg( sharpnessValue ) );
83const char * KPrPolygonObject::getOasisElementName()
const
85 return "draw:regular-polygon";
89TQDomDocumentFragment KPrPolygonObject::save( TQDomDocument& doc,
double offset )
91 TQDomDocumentFragment fragment = KPr2DObject::save( doc, offset );
93 TQDomElement elemSettings = doc.createElement(
"SETTINGS" );
95 elemSettings.setAttribute(
"checkConcavePolygon",
static_cast<int>( checkConcavePolygon ) );
96 elemSettings.setAttribute(
"cornersValue", cornersValue );
97 elemSettings.setAttribute(
"sharpnessValue", sharpnessValue );
99 fragment.appendChild( elemSettings );
101 if ( !points.isNull() ) {
102 TQDomElement elemPoints = doc.createElement(
"POINTS" );
103 KoPointArray::ConstIterator it;
104 for ( it = points.begin(); it != points.end(); ++it ) {
105 TQDomElement elemPoint = doc.createElement(
"Point" );
106 KoPoint point = (*it);
107 elemPoint.setAttribute(
"point_x", point.x() );
108 elemPoint.setAttribute(
"point_y", point.y() );
110 elemPoints.appendChild( elemPoint );
112 fragment.appendChild( elemPoints );
118void KPrPolygonObject::loadOasis(
const TQDomElement &element, KoOasisContext & context,
KPrLoadingInfo *info )
120 kdDebug()<<
"void KPrPolygonObject::loadOasis( const TQDomElement &element )***********\n";
121 KPr2DObject::loadOasis( element,context, info );
122 cornersValue = element.attributeNS( KoXmlNS::draw,
"corners", TQString() ).toInt();
123 checkConcavePolygon = element.attributeNS( KoXmlNS::draw,
"concave", TQString() ) ==
"true";
125 if ( checkConcavePolygon )
127 sharpnessValue = element.attributeNS( KoXmlNS::draw,
"sharpness", TQString() ).remove(
'%').toInt();
132double KPrPolygonObject::load(
const TQDomElement &element )
134 double offset=KPr2DObject::load( element );
136 TQDomElement e = element.namedItem(
"SETTINGS" ).toElement();
138 bool _checkConcavePolygon =
false;
139 int _cornersValue = 3;
140 int _sharpnessValue = 0;
142 if ( e.hasAttribute(
"checkConcavePolygon" ) )
143 _checkConcavePolygon =
static_cast<bool>( e.attribute(
"checkConcavePolygon" ).toInt() );
144 if ( e.hasAttribute(
"cornersValue" ) )
145 _cornersValue = e.attribute(
"cornersValue" ).toInt();
146 if ( e.hasAttribute(
"sharpnessValue" ) )
147 _sharpnessValue = e.attribute(
"sharpnessValue" ).toInt();
149 checkConcavePolygon = _checkConcavePolygon;
150 cornersValue = _cornersValue;
151 sharpnessValue = _sharpnessValue;
154 e = element.namedItem(
"POINTS" ).toElement();
156 TQDomElement elemPoint = e.firstChild().toElement();
157 unsigned int index = 0;
158 while ( !elemPoint.isNull() ) {
159 if ( elemPoint.tagName() ==
"Point" ) {
162 if( elemPoint.hasAttribute(
"point_x" ) )
163 tmpX = elemPoint.attribute(
"point_x" ).toDouble();
164 if( elemPoint.hasAttribute(
"point_y" ) )
165 tmpY = elemPoint.attribute(
"point_y" ).toDouble();
167 points.putPoints( index, 1, tmpX,tmpY );
169 elemPoint = elemPoint.nextSibling().toElement();
176void KPrPolygonObject::setSize(
double _width,
double _height )
178 KoSize origSize( ext );
179 KPrObject::setSize( _width, _height );
181 double fx = ext.width() / origSize.width();
182 double fy = ext.height() / origSize.height();
184 updatePoints( fx, fy );
187void KPrPolygonObject::updatePoints(
double _fx,
double _fy )
190 KoPointArray tmpPoints;
191 KoPointArray::ConstIterator it;
192 for ( it = points.begin(); it != points.end(); ++it ) {
193 KoPoint point = (*it);
194 double tmpX = point.x() * _fx;
195 double tmpY = point.y() * _fy;
197 tmpPoints.putPoints( index, 1, tmpX,tmpY );
203void KPrPolygonObject::paint( TQPainter* _painter,KoTextZoomHandler*_zoomHandler,
204 int ,
bool drawingShadow,
bool drawContour )
206 int _w = ( pen.style() == TQt::NoPen ) ? 1 : pen.width();
209 TQPointArray pointArray2 = points.zoomPointArray( _zoomHandler );
210 TQPen pen3( TQt::black, 1, TQt::DotLine );
211 _painter->setPen( pen3 );
212 _painter->setRasterOp( TQt::NotXorROP );
213 _painter->drawPolygon( pointArray2 );
217 TQPointArray pointArray = points.zoomPointArray( _zoomHandler, _w );
218 TQPen pen2 = pen.zoomedPen( _zoomHandler );
220 if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) {
221 _painter->setPen( pen2 );
222 _painter->setBrush( getBrush() );
223 _painter->drawPolygon( pointArray );
226 TQSize size( _zoomHandler->zoomSize( ext ) );
227 if ( m_redrawGradientPix || gradient->size() != size )
229 m_redrawGradientPix =
false;
230 gradient->setSize( size );
231 TQRegion clipregion( pointArray );
232 m_gradientPix.resize( size );
233 m_gradientPix.fill( TQt::white );
236 p.begin( &m_gradientPix );
237 p.setClipRegion( clipregion );
238 p.drawPixmap( 0, 0, gradient->pixmap() );
241 m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
244 TQRect _rect = pointArray.boundingRect();
245 _painter->drawPixmap( 0, 0, m_gradientPix, 0, 0, _rect.width(), _rect.height() );
247 _painter->setPen( pen2 );
248 _painter->setBrush( TQt::NoBrush );
249 _painter->drawPolygon( pointArray );
254void KPrPolygonObject::drawPolygon()
256 kdDebug()<<
"void KPrPolygonObject::drawPolygon()***********\n";
257 KoRect _rect( 0, 0, ext.width(), ext.height() );
258 double angle = 2 * M_PI / cornersValue;
259 double diameter =
static_cast<double>( TQMAX( _rect.width(), _rect.height() ) );
260 double radius = diameter * 0.5;
262 KoPointArray _points( checkConcavePolygon ? cornersValue * 2 : cornersValue );
263 _points.setPoint( 0, 0, tqRound( -radius ) );
266 double ymin = tqRound( -radius );
268 if ( checkConcavePolygon ) {
271 double r = radius - ( sharpnessValue / 100.0 * radius );
272 for (
int i = 1; i < cornersValue * 2; ++i ) {
279 xp = radius * sin( a );
280 yp = -radius * cos( a );
283 _points.setPoint( i, xp, yp );
292 for (
int i = 1; i < cornersValue; ++i ) {
293 double xp = radius * sin( a );
294 double yp = -radius * cos( a );
296 _points.setPoint( i, xp, yp );
305 KoRect _changRect = _points.boundingRect();
306 double fx = _rect.width() / _changRect.width();
307 double fy = _rect.height() / _changRect.height();
310 KoPointArray tmpPoints;
311 KoPointArray::ConstIterator it;
312 for ( it = _points.begin(); it != _points.end(); ++it ) {
313 KoPoint point = (*it);
314 double tmpX = ( point.x() - xmin) * fx;
315 double tmpY = ( point.y() - ymin) * fy;
317 tmpPoints.putPoints( _index, 1, tmpX,tmpY );
323 if ( getFillType() == FT_GRADIENT && gradient )
324 m_redrawGradientPix =
true;
327void KPrPolygonObject::flip(
bool horizontal )
329 KPr2DObject::flip( horizontal );
331 KoPointArray tmpPoints;
335 KoPointArray::ConstIterator it;
336 double horiz = getSize().height()/2;
337 for ( it = points.begin(); it != points.end(); ++it ) {
338 KoPoint point = (*it);
339 if ( point.y()> horiz )
340 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
342 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
348 KoPointArray::ConstIterator it;
349 double vert = getSize().width()/2;
350 for ( it = points.begin(); it != points.end(); ++it ) {
351 KoPoint point = (*it);
352 if ( point.x()> vert )
353 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
355 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
362KoSize KPrPolygonObject::getRealSize()
const {
364 KoPoint realOrig( orig );
365 KoPointArray p( points );
366 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
370KoPoint KPrPolygonObject::getRealOrig()
const {
372 KoPoint realOrig( orig );
373 KoPointArray p( points );
374 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
Temporary information used only during loading.