kpresenter

KPrPointObject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004-2005 Thorsten Zachmann <zachmann@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KPrPointObject.h"
21#include "KPrUtils.h"
22#include "KPrDocument.h"
23#include "KPrSVGPathParser.h"
24#include <KoTextZoomHandler.h>
25#include <KoUnit.h>
26#include <tqdom.h>
27#include <tqpainter.h>
28#include <KoStyleStack.h>
29#include <KoOasisContext.h>
30#include <KoXmlNS.h>
31
32KPrPointObject::KPrPointObject()
33 : KPrShadowObject(), KPrStartEndLine( L_NORMAL, L_NORMAL )
34{
35}
36
37
38KPrPointObject::KPrPointObject( const KoPen &_pen, LineEnd _lineBegin, LineEnd _lineEnd )
39 : KPrShadowObject( _pen ), KPrStartEndLine(_lineBegin, _lineEnd)
40{
41}
42
43
44KoSize KPrPointObject::getRealSize() const
45{
46 KoSize size( ext );
47 KoPoint realOrig( orig );
48 KoPointArray p( getDrawingPoints() );
49 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
50 return size;
51}
52
53
54KoPoint KPrPointObject::getRealOrig() const
55{
56 KoSize size( ext );
57 KoPoint realOrig( orig );
58 KoPointArray p( getDrawingPoints() );
59 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
60 return realOrig;
61}
62
63
64void KPrPointObject::loadOasis( const TQDomElement &element, KoOasisContext & context, KPrLoadingInfo* info )
65{
66 kdDebug(33001) << "KPrPointObject::loadOasis" << endl;
67
68 KPrShadowObject::loadOasis( element, context, info );
69 TQString d = element.attributeNS( KoXmlNS::svg, "d", TQString());
70 kdDebug(33001) << "path d: " << d << endl;
71
72 KPrSVGPathParser parser;
73 points = parser.getPoints( d, getType() == OT_FREEHAND );
74 loadOasisApplyViewBox( element, points );
75}
76
77
78TQDomDocumentFragment KPrPointObject::save( TQDomDocument& doc, double offset )
79{
80 TQDomDocumentFragment fragment = KPrShadowObject::save( doc, offset );
81 if ( !points.isNull() ) {
82 TQDomElement elemPoints = doc.createElement( "POINTS" );
83 KoPointArray::ConstIterator it;
84 for ( it = points.begin(); it != points.end(); ++it ) {
85 TQDomElement elemPoint = doc.createElement( "Point" );
86 KoPoint point = (*it);
87 elemPoint.setAttribute( "point_x", point.x() );
88 elemPoint.setAttribute( "point_y", point.y() );
89
90 elemPoints.appendChild( elemPoint );
91 }
92 fragment.appendChild( elemPoints );
93 }
94
95 KPrStartEndLine::save( fragment,doc );
96
97 return fragment;
98}
99
100const char * KPrPointObject::getOasisElementName() const
101{
102 return "draw:custom-shape";
103}
104
105void KPrPointObject::loadOasisMarker( KoOasisContext & context )
106{
107 loadOasisMarkerElement( context, "marker-start", lineBegin );
108 loadOasisMarkerElement( context, "marker-end", lineEnd );
109}
110
111void KPrPointObject::fillStyle( KoGenStyle& styleObjectAuto, KoGenStyles& mainStyles ) const
112{
113 KPrShadowObject::fillStyle( styleObjectAuto, mainStyles );
114 saveOasisMarkerElement( mainStyles, styleObjectAuto );
115}
116
117
118double KPrPointObject::load( const TQDomElement &element )
119{
120 double offset = KPrShadowObject::load( element );
121
122 TQDomElement e = element.namedItem( "POINTS" ).toElement();
123 if ( !e.isNull() ) {
124 TQDomElement elemPoint = e.firstChild().toElement();
125 unsigned int index = 0;
126 while ( !elemPoint.isNull() ) {
127 if ( elemPoint.tagName() == "Point" ) {
128 double tmpX = 0;
129 double tmpY = 0;
130 if( elemPoint.hasAttribute( "point_x" ) )
131 tmpX = elemPoint.attribute( "point_x" ).toDouble();
132 if( elemPoint.hasAttribute( "point_y" ) )
133 tmpY = elemPoint.attribute( "point_y" ).toDouble();
134
135 points.putPoints( index, 1, tmpX,tmpY );
136 }
137 elemPoint = elemPoint.nextSibling().toElement();
138 ++index;
139 }
140 }
141 KPrStartEndLine::load( element );
142 return offset;
143}
144
145
146void KPrPointObject::setSize( double _width, double _height )
147{
148 KoSize origSize( ext );
149 KPrObject::setSize( _width, _height );
150
151 double fx = ext.width() / origSize.width();
152 double fy = ext.height() / origSize.height();
153
154 updatePoints( fx, fy );
155}
156
157
158void KPrPointObject::flip( bool horizontal )
159{
160 KPrObject::flip( horizontal );
161
162 KoPointArray tmpPoints;
163 int index = 0;
164 if ( ! horizontal )
165 {
166 KoPointArray::ConstIterator it;
167 double horiz = getSize().height()/2;
168 for ( it = points.begin(); it != points.end(); ++it ) {
169 KoPoint point = (*it);
170 if ( point.y()> horiz )
171 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
172 else
173 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
174 ++index;
175 }
176 }
177 else
178 {
179 KoPointArray::ConstIterator it;
180 double vert = getSize().width()/2;
181 for ( it = points.begin(); it != points.end(); ++it ) {
182 KoPoint point = (*it);
183 if ( point.x()> vert )
184 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
185 else
186 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
187 ++index;
188 }
189 }
190
191 points = tmpPoints;
192}
193
194
195void KPrPointObject::paint( TQPainter* _painter, KoTextZoomHandler*_zoomHandler,
196 int /* pageNum */, bool /*drawingShadow*/, bool drawContour )
197{
198 int _w = int( pen.pointWidth() );
199
200 TQPen pen2;
201 if ( drawContour ) {
202 pen2 = TQPen( TQt::black, 1, TQt::DotLine );
203 _painter->setRasterOp( TQt::NotXorROP );
204 }
205 else {
206 pen2 = pen.zoomedPen( _zoomHandler );
207 }
208 _painter->setPen( pen2 );
209
210 TQPointArray pointArray = getDrawingPoints().zoomPointArray( _zoomHandler, _w );
211 _painter->drawPolyline( pointArray );
212
213 if ( lineBegin != L_NORMAL && !drawContour ) {
214 TQPoint startPoint;
215 bool first = true;
216 TQPointArray::ConstIterator it1;
217 for ( it1 = pointArray.begin(); it1 != pointArray.end(); ++it1 ) {
218 if ( first ) {
219 startPoint = (*it1);
220 first = false;
221 }
222
223 TQPoint point = (*it1);
224 if ( startPoint != point ) {
225 float angle = KoPoint::getAngle( KoPoint( startPoint ), KoPoint( point ) );
226 drawFigureWithOffset( lineBegin, _painter, startPoint, pen2.color(), _w, angle,_zoomHandler );
227
228 break;
229 }
230 }
231 }
232
233 if ( lineEnd != L_NORMAL && !drawContour ) {
234 TQPoint endPoint;
235 bool last = true;
236 TQPointArray::ConstIterator it2 = pointArray.end();
237 for ( it2 = it2 - 1; it2 != pointArray.begin(); --it2 ) {
238 if ( last ) {
239 endPoint = (*it2);
240 last = false;
241 }
242
243 TQPoint point = (*it2);
244 if ( endPoint != point ) {
245 float angle = KoPoint::getAngle( KoPoint( endPoint ), KoPoint( point ) );
246 drawFigureWithOffset( lineEnd, _painter, endPoint, pen2.color(), _w, angle,_zoomHandler );
247
248 break;
249 }
250 }
251 }
252}
253
254
255void KPrPointObject::updatePoints( double _fx, double _fy )
256{
257 int index = 0;
258 KoPointArray tmpPoints;
259 KoPointArray::ConstIterator it;
260 for ( it = points.begin(); it != points.end(); ++it ) {
261 KoPoint point = (*it);
262 double tmpX = point.x() * _fx;
263 double tmpY = point.y() * _fy;
264
265 tmpPoints.putPoints( index, 1, tmpX,tmpY );
266 ++index;
267 }
268 points = tmpPoints;
269}
270
271
272KoPointArray KPrPointObject::getDrawingPoints() const
273{
274 return points;
275}
Temporary information used only during loading.
Base class for objects with a pen and a brush, and which takes care of painting the shadow in draw() ...
Definition KPrObject.h:406