kpresenter

KPrClosedLineObject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Toshitaka Fujioka <fujioka@kde.org>
3 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KPrClosedLineObject.h"
22#include "KPrPointObject.h"
23#include "KPrGradient.h"
24#include "KPrSVGPathParser.h"
25#include <KoTextZoomHandler.h>
26#include <kdebug.h>
27#include <tqbitmap.h>
28#include <tqregion.h>
29#include <tqregexp.h>
30#include <tqdom.h>
31#include <tqpicture.h>
32#include <tqpainter.h>
33#include <KoOasisContext.h>
34#include <math.h>
35#include <KoUnit.h>
36#include <KoXmlNS.h>
37#include <KoDom.h>
38
39using namespace std;
40
41KPrClosedLineObject::KPrClosedLineObject()
42 : KPr2DObject()
43{
44}
45
46KPrClosedLineObject::KPrClosedLineObject( const KoPointArray &_points, const KoSize &_size, const KoPen &_pen, const TQBrush &_brush,
47 FillType _fillType, const TQColor &_gColor1, const TQColor &_gColor2, BCType _gType,
48 bool _unbalanced, int _xfactor, int _yfactor, const TQString _typeString )
49 : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType, _unbalanced, _xfactor, _yfactor )
50{
51 points = KoPointArray( _points );
52 ext = _size;
53 typeString = _typeString;
54}
55
56KPrClosedLineObject::KPrClosedLineObject( const KPrPointObject &object )
57: KPr2DObject( object.getPen(), TQt::NoBrush, FT_BRUSH, TQColor(), TQColor(), BCT_PLAIN, false, 0, 0 )
58{
59 ext = object.getSize();
60 orig = object.getOrig();
61 objectName = object.getObjectName();
62 points = object.getPoints().copy();
63 points.putPoints( points.count(), 1, points.at( 0 ).x(), points.at( 0 ).y() );
64 switch ( object.getType() )
65 {
66 case OT_FREEHAND:
67 typeString = i18n( "Closed Freehand" );
68 break;
69 case OT_POLYLINE:
70 typeString = i18n( "Closed Polyline" );
71 break;
72 case OT_CUBICBEZIERCURVE:
73 typeString = i18n( "Closed Cubic Bezier Curve" );
74 break;
75 case OT_QUADRICBEZIERCURVE:
76 typeString = i18n( "Closed Quadric Bezier Curve" );
77 break;
78 default:
79 break;
80 }
81
82}
83
84KPrClosedLineObject &KPrClosedLineObject::operator=( const KPrClosedLineObject & )
85{
86 return *this;
87}
88
89#if 0
90DCOPObject* KPrClosedLineObject::dcopObject()
91{
92 if ( !dcop )
93 dcop = new KPClosedLineObjectIface( this );
94 return dcop;
95}
96#endif
97
98TQDomDocumentFragment KPrClosedLineObject::save( TQDomDocument& doc, double offset )
99{
100 TQDomDocumentFragment fragment = KPr2DObject::save( doc, offset );
101
102 TQDomElement elemObjectsName = doc.createElement( "OBJECTSNAME" );
103
104 elemObjectsName.setAttribute( "NAME", typeString );
105
106 fragment.appendChild( elemObjectsName );
107
108 if ( !points.isNull() ) {
109 TQDomElement elemPoints = doc.createElement( "POINTS" );
110 KoPointArray::ConstIterator it;
111 for ( it = points.begin(); it != points.end(); ++it ) {
112 TQDomElement elemPoint = doc.createElement( "Point" );
113 KoPoint point = (*it);
114 elemPoint.setAttribute( "point_x", point.x() );
115 elemPoint.setAttribute( "point_y", point.y() );
116
117 elemPoints.appendChild( elemPoint );
118 }
119 fragment.appendChild( elemPoints );
120 }
121
122 return fragment;
123}
124
125bool KPrClosedLineObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
126{
128 return true;
129}
130
131const char * KPrClosedLineObject::getOasisElementName() const
132{
133 return "draw:polygon";
134}
135
136
137double KPrClosedLineObject::load( const TQDomElement &element )
138{
139 double offset = KPr2DObject::load( element );
140
141 TQDomElement e = element.namedItem( "OBJECTSNAME" ).toElement();
142 if ( !e.isNull() ) {
143 if ( e.hasAttribute( "NAME" ) )
144 typeString = e.attribute( "NAME" );
145 }
146
147 e = element.namedItem( "POINTS" ).toElement();
148 if ( !e.isNull() ) {
149 TQDomElement elemPoint = e.firstChild().toElement();
150 unsigned int index = 0;
151 while ( !elemPoint.isNull() ) {
152 if ( elemPoint.tagName() == "Point" ) {
153 double tmpX = 0;
154 double tmpY = 0;
155 if( elemPoint.hasAttribute( "point_x" ) )
156 tmpX = elemPoint.attribute( "point_x" ).toDouble();
157 if( elemPoint.hasAttribute( "point_y" ) )
158 tmpY = elemPoint.attribute( "point_y" ).toDouble();
159
160 points.putPoints( index, 1, tmpX,tmpY );
161 }
162 elemPoint = elemPoint.nextSibling().toElement();
163 ++index;
164 }
165 }
166 return offset;
167}
168
169void KPrClosedLineObject::setSize( double _width, double _height )
170{
171 KoSize origSize( ext );
172 KPrObject::setSize( _width, _height );
173
174 double fx = ext.width() / origSize.width();
175 double fy = ext.height() / origSize.height();
176
177 updatePoints( fx, fy );
178}
179
180void KPrClosedLineObject::updatePoints( double _fx, double _fy )
181{
182 int index = 0;
183 KoPointArray tmpPoints;
184 KoPointArray::ConstIterator it;
185 for ( it = points.begin(); it != points.end(); ++it ) {
186 KoPoint point = (*it);
187 double tmpX = point.x() * _fx;
188 double tmpY = point.y() * _fy;
189
190 tmpPoints.putPoints( index, 1, tmpX,tmpY );
191 ++index;
192 }
193 points = tmpPoints;
194}
195
196void KPrClosedLineObject::paint( TQPainter* _painter,KoTextZoomHandler*_zoomHandler,
197 int /* pageNum */, bool drawingShadow, bool drawContour )
198{
199 int _w = ( pen.style() == TQt::NoPen ) ? 1 : int( pen.pointWidth() );
200
201 if ( drawContour ) {
202 TQPointArray pointArray2 = points.zoomPointArray( _zoomHandler );
203 TQPen pen3( TQt::black, 1, TQt::DotLine );
204 _painter->setPen( pen3 );
205 _painter->setRasterOp( TQt::NotXorROP );
206 _painter->drawPolygon( pointArray2 );
207 return;
208 }
209
210 TQPointArray pointArray = points.zoomPointArray( _zoomHandler, _w );
211 TQPen pen2 = pen.zoomedPen( _zoomHandler );
212
213 if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) {
214 _painter->setPen( pen2 );
215 _painter->setBrush( getBrush() );
216 _painter->drawPolygon( pointArray );
217 }
218 else {
219 TQSize size( _zoomHandler->zoomSize( ext ) );
220 if ( m_redrawGradientPix || gradient->size() != size )
221 {
222 m_redrawGradientPix = false;
223 gradient->setSize( size );
224 TQRegion clipregion( pointArray );
225 m_gradientPix.resize( size );
226 m_gradientPix.fill( TQt::white );
227
228 TQPainter p;
229 p.begin( &m_gradientPix );
230 p.setClipRegion( clipregion );
231 p.drawPixmap( 0, 0, gradient->pixmap() );
232 p.end();
233
234 m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
235 }
236
237 TQRect _rect = pointArray.boundingRect();
238 _painter->drawPixmap( 0, 0, m_gradientPix, 0, 0, _rect.width(), _rect.height() );
239
240 _painter->setPen( pen2 );
241 _painter->setBrush( TQt::NoBrush );
242 _painter->drawPolygon( pointArray );
243 }
244}
245
246void KPrClosedLineObject::flip( bool horizontal )
247{
248 KPr2DObject::flip( horizontal );
249 // flip the points
250 KoPointArray tmpPoints;
251 int index = 0;
252 if ( ! horizontal )
253 {
254 KoPointArray::ConstIterator it;
255 double horiz = getSize().height()/2;
256 for ( it = points.begin(); it != points.end(); ++it ) {
257 KoPoint point = (*it);
258 if ( point.y()> horiz )
259 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
260 else
261 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
262 ++index;
263 }
264 }
265 else
266 {
267 KoPointArray::ConstIterator it;
268 double vert = getSize().width()/2;
269 for ( it = points.begin(); it != points.end(); ++it ) {
270 KoPoint point = (*it);
271 if ( point.x()> vert )
272 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
273 else
274 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
275 ++index;
276 }
277 }
278 points = tmpPoints;
279}
280
281void KPrClosedLineObject::loadOasis( const TQDomElement &element, KoOasisContext & context, KPrLoadingInfo *info )
282{
283 kdDebug()<<"void KPrClosedLineObject::loadOasis( const TQDomElement &element )***********\n";
284 KPr2DObject::loadOasis( element,context, info );
285 TQString tag( element.tagName() );
286 if ( tag == "polygon" )
287 {
288 KPrShadowObject::loadOasisDrawPoints( points, element, context, info );
289 }
290 else if ( tag == "path" ) // this is used to load closed draw:path objects
291 {
292 TQString d = element.attributeNS( KoXmlNS::svg, "d", TQString());
293 kdDebug(33001) << "path d: " << d << endl;
294
295 KPrSVGPathParser parser;
296 points = parser.getPoints( d, true );
297 loadOasisApplyViewBox( element, points );
298 }
299 else if ( tag == "custom-shape" )
300 {
301 TQDomElement enhancedGeometry = KoDom::namedItemNS( element, KoXmlNS::draw, "enhanced-geometry" );
302
303 if ( !enhancedGeometry.isNull() )
304 {
305 TQString d = enhancedGeometry.attributeNS( KoXmlNS::draw, "enhanced-path", TQString() );
306 TQRegExp rx( "^([0-9 MLZ]+)N$" );
307 if ( rx.search( d ) != -1 )
308 {
309 d = rx.cap( 1 );
310 kdDebug(33001) << "enhanced-path d: " << d << endl;
311
312 KPrSVGPathParser parser;
313 points = parser.getPoints( d, true );
314 loadOasisApplyViewBox( enhancedGeometry, points );
315 }
316 }
317 }
318 else
319 {
320 kdDebug(33001) << "KPrClosedLineObject::loadOasis unsupported tag" << endl;
321 }
322}
323
324KoSize KPrClosedLineObject::getRealSize() const {
325 KoSize size( ext );
326 KoPoint realOrig( orig );
327 KoPointArray p( points );
328 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
329 return size;
330}
331
332KoPoint KPrClosedLineObject::getRealOrig() const {
333 KoSize size( ext );
334 KoPoint realOrig( orig );
335 KoPointArray p( points );
336 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
337 return realOrig;
338}
Temporary information used only during loading.
bool loadOasisDrawPoints(KoPointArray &points, const TQDomElement &element, KoOasisContext &context, KPrLoadingInfo *info)
Helper method for loading draw:points.
static bool saveOasisDrawPoints(const KoPointArray &points, KPOasisSaveContext &sc)
Helper method for saving draw:points.