00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KPrPointObject.h"
00022 #include "KPrUtils.h"
00023 #include "KPrDocument.h"
00024 #include "KPrSVGPathParser.h"
00025 #include <KoTextZoomHandler.h>
00026 #include <KoUnit.h>
00027 #include <qdom.h>
00028 #include <qpainter.h>
00029 #include <KoStyleStack.h>
00030 #include <KoOasisContext.h>
00031 #include <KoXmlNS.h>
00032
00033 KPrPointObject::KPrPointObject()
00034 : KPrShadowObject(), KPrStartEndLine( L_NORMAL, L_NORMAL )
00035 {
00036 }
00037
00038
00039 KPrPointObject::KPrPointObject( const KoPen &_pen, LineEnd _lineBegin, LineEnd _lineEnd )
00040 : KPrShadowObject( _pen ), KPrStartEndLine(_lineBegin, _lineEnd)
00041 {
00042 }
00043
00044
00045 KoSize KPrPointObject::getRealSize() const
00046 {
00047 KoSize size( ext );
00048 KoPoint realOrig( orig );
00049 KoPointArray p( getDrawingPoints() );
00050 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00051 return size;
00052 }
00053
00054
00055 KoPoint KPrPointObject::getRealOrig() const
00056 {
00057 KoSize size( ext );
00058 KoPoint realOrig( orig );
00059 KoPointArray p( getDrawingPoints() );
00060 getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
00061 return realOrig;
00062 }
00063
00064
00065 void KPrPointObject::loadOasis( const QDomElement &element, KoOasisContext & context, KPrLoadingInfo* info )
00066 {
00067 kdDebug(33001) << "KPrPointObject::loadOasis" << endl;
00068
00069 KPrShadowObject::loadOasis( element, context, info );
00070 QString d = element.attributeNS( KoXmlNS::svg, "d", QString::null);
00071 kdDebug(33001) << "path d: " << d << endl;
00072
00073 KPrSVGPathParser parser;
00074 points = parser.getPoints( d, getType() == OT_FREEHAND );
00075 loadOasisApplyViewBox( element, points );
00076 }
00077
00078
00079 QDomDocumentFragment KPrPointObject::save( QDomDocument& doc, double offset )
00080 {
00081 QDomDocumentFragment fragment = KPrShadowObject::save( doc, offset );
00082 if ( !points.isNull() ) {
00083 QDomElement elemPoints = doc.createElement( "POINTS" );
00084 KoPointArray::ConstIterator it;
00085 for ( it = points.begin(); it != points.end(); ++it ) {
00086 QDomElement elemPoint = doc.createElement( "Point" );
00087 KoPoint point = (*it);
00088 elemPoint.setAttribute( "point_x", point.x() );
00089 elemPoint.setAttribute( "point_y", point.y() );
00090
00091 elemPoints.appendChild( elemPoint );
00092 }
00093 fragment.appendChild( elemPoints );
00094 }
00095
00096 KPrStartEndLine::save( fragment,doc );
00097
00098 return fragment;
00099 }
00100
00101 const char * KPrPointObject::getOasisElementName() const
00102 {
00103 return "draw:custom-shape";
00104 }
00105
00106 void KPrPointObject::loadOasisMarker( KoOasisContext & context )
00107 {
00108 loadOasisMarkerElement( context, "marker-start", lineBegin );
00109 loadOasisMarkerElement( context, "marker-end", lineEnd );
00110 }
00111
00112 void KPrPointObject::fillStyle( KoGenStyle& styleObjectAuto, KoGenStyles& mainStyles ) const
00113 {
00114 KPrShadowObject::fillStyle( styleObjectAuto, mainStyles );
00115 saveOasisMarkerElement( mainStyles, styleObjectAuto );
00116 }
00117
00118
00119 double KPrPointObject::load( const QDomElement &element )
00120 {
00121 double offset = KPrShadowObject::load( element );
00122
00123 QDomElement e = element.namedItem( "POINTS" ).toElement();
00124 if ( !e.isNull() ) {
00125 QDomElement elemPoint = e.firstChild().toElement();
00126 unsigned int index = 0;
00127 while ( !elemPoint.isNull() ) {
00128 if ( elemPoint.tagName() == "Point" ) {
00129 double tmpX = 0;
00130 double tmpY = 0;
00131 if( elemPoint.hasAttribute( "point_x" ) )
00132 tmpX = elemPoint.attribute( "point_x" ).toDouble();
00133 if( elemPoint.hasAttribute( "point_y" ) )
00134 tmpY = elemPoint.attribute( "point_y" ).toDouble();
00135
00136 points.putPoints( index, 1, tmpX,tmpY );
00137 }
00138 elemPoint = elemPoint.nextSibling().toElement();
00139 ++index;
00140 }
00141 }
00142 KPrStartEndLine::load( element );
00143 return offset;
00144 }
00145
00146
00147 void KPrPointObject::setSize( double _width, double _height )
00148 {
00149 KoSize origSize( ext );
00150 KPrObject::setSize( _width, _height );
00151
00152 double fx = ext.width() / origSize.width();
00153 double fy = ext.height() / origSize.height();
00154
00155 updatePoints( fx, fy );
00156 }
00157
00158
00159 void KPrPointObject::flip( bool horizontal )
00160 {
00161 KPrObject::flip( horizontal );
00162
00163 KoPointArray tmpPoints;
00164 int index = 0;
00165 if ( ! horizontal )
00166 {
00167 KoPointArray::ConstIterator it;
00168 double horiz = getSize().height()/2;
00169 for ( it = points.begin(); it != points.end(); ++it ) {
00170 KoPoint point = (*it);
00171 if ( point.y()> horiz )
00172 tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
00173 else
00174 tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
00175 ++index;
00176 }
00177 }
00178 else
00179 {
00180 KoPointArray::ConstIterator it;
00181 double vert = getSize().width()/2;
00182 for ( it = points.begin(); it != points.end(); ++it ) {
00183 KoPoint point = (*it);
00184 if ( point.x()> vert )
00185 tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
00186 else
00187 tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
00188 ++index;
00189 }
00190 }
00191
00192 points = tmpPoints;
00193 }
00194
00195
00196 void KPrPointObject::paint( QPainter* _painter, KoTextZoomHandler*_zoomHandler,
00197 int , bool , bool drawContour )
00198 {
00199 int _w = int( pen.pointWidth() );
00200
00201 QPen pen2;
00202 if ( drawContour ) {
00203 pen2 = QPen( Qt::black, 1, Qt::DotLine );
00204 _painter->setRasterOp( Qt::NotXorROP );
00205 }
00206 else {
00207 pen2 = pen.zoomedPen( _zoomHandler );
00208 }
00209 _painter->setPen( pen2 );
00210
00211 QPointArray pointArray = getDrawingPoints().zoomPointArray( _zoomHandler, _w );
00212 _painter->drawPolyline( pointArray );
00213
00214 if ( lineBegin != L_NORMAL && !drawContour ) {
00215 QPoint startPoint;
00216 bool first = true;
00217 QPointArray::ConstIterator it1;
00218 for ( it1 = pointArray.begin(); it1 != pointArray.end(); ++it1 ) {
00219 if ( first ) {
00220 startPoint = (*it1);
00221 first = false;
00222 }
00223
00224 QPoint point = (*it1);
00225 if ( startPoint != point ) {
00226 float angle = KoPoint::getAngle( KoPoint( startPoint ), KoPoint( point ) );
00227 drawFigureWithOffset( lineBegin, _painter, startPoint, pen2.color(), _w, angle,_zoomHandler );
00228
00229 break;
00230 }
00231 }
00232 }
00233
00234 if ( lineEnd != L_NORMAL && !drawContour ) {
00235 QPoint endPoint;
00236 bool last = true;
00237 QPointArray::ConstIterator it2 = pointArray.end();
00238 for ( it2 = it2 - 1; it2 != pointArray.begin(); --it2 ) {
00239 if ( last ) {
00240 endPoint = (*it2);
00241 last = false;
00242 }
00243
00244 QPoint point = (*it2);
00245 if ( endPoint != point ) {
00246 float angle = KoPoint::getAngle( KoPoint( endPoint ), KoPoint( point ) );
00247 drawFigureWithOffset( lineEnd, _painter, endPoint, pen2.color(), _w, angle,_zoomHandler );
00248
00249 break;
00250 }
00251 }
00252 }
00253 }
00254
00255
00256 void KPrPointObject::updatePoints( double _fx, double _fy )
00257 {
00258 int index = 0;
00259 KoPointArray tmpPoints;
00260 KoPointArray::ConstIterator it;
00261 for ( it = points.begin(); it != points.end(); ++it ) {
00262 KoPoint point = (*it);
00263 double tmpX = point.x() * _fx;
00264 double tmpY = point.y() * _fy;
00265
00266 tmpPoints.putPoints( index, 1, tmpX,tmpY );
00267 ++index;
00268 }
00269 points = tmpPoints;
00270 }
00271
00272
00273 KoPointArray KPrPointObject::getDrawingPoints() const
00274 {
00275 return points;
00276 }