summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrPointObject.cpp
blob: faad4820299958487a5c89ed01db903c787fc757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This file is part of the KDE project
   Copyright (C) 2004-2005 Thorsten Zachmann  <zachmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KPrPointObject.h"
#include "KPrUtils.h"
#include "KPrDocument.h"
#include "KPrSVGPathParser.h"
#include <KoTextZoomHandler.h>
#include <KoUnit.h>
#include <tqdom.h>
#include <tqpainter.h>
#include <KoStyleStack.h>
#include <KoOasisContext.h>
#include <KoXmlNS.h>

KPrPointObject::KPrPointObject()
    : KPrShadowObject(), KPrStartEndLine( L_NORMAL, L_NORMAL )
{
}


KPrPointObject::KPrPointObject( const KoPen &_pen, LineEnd _lineBegin, LineEnd _lineEnd )
    : KPrShadowObject( _pen ), KPrStartEndLine(_lineBegin, _lineEnd)
{
}


KoSize KPrPointObject::getRealSize() const
{
    KoSize size( ext );
    KoPoint realOrig( orig );
    KoPointArray p( getDrawingPoints() );
    getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
    return size;
}


KoPoint KPrPointObject::getRealOrig() const
{
    KoSize size( ext );
    KoPoint realOrig( orig );
    KoPointArray p( getDrawingPoints() );
    getRealSizeAndOrigFromPoints( p, angle, size, realOrig );
    return realOrig;
}


void KPrPointObject::loadOasis( const TQDomElement &element, KoOasisContext & context,  KPrLoadingInfo* info )
{
    kdDebug(33001) << "KPrPointObject::loadOasis" << endl;

    KPrShadowObject::loadOasis( element, context, info );
    TQString d = element.attributeNS( KoXmlNS::svg, "d", TQString());
    kdDebug(33001) << "path d: " << d << endl;

    KPrSVGPathParser parser;
    points = parser.getPoints( d, getType() == OT_FREEHAND );
    loadOasisApplyViewBox( element, points );
}


TQDomDocumentFragment KPrPointObject::save( TQDomDocument& doc, double offset )
{
    TQDomDocumentFragment fragment = KPrShadowObject::save( doc, offset );
    if ( !points.isNull() ) {
        TQDomElement elemPoints = doc.createElement( "POINTS" );
        KoPointArray::ConstIterator it;
        for ( it = points.begin(); it != points.end(); ++it ) {
            TQDomElement elemPoint = doc.createElement( "Point" );
            KoPoint point = (*it);
            elemPoint.setAttribute( "point_x", point.x() );
            elemPoint.setAttribute( "point_y", point.y() );

            elemPoints.appendChild( elemPoint );
        }
        fragment.appendChild( elemPoints );
    }

    KPrStartEndLine::save( fragment,doc );

    return fragment;
}

const char * KPrPointObject::getOasisElementName() const
{
    return "draw:custom-shape";
}

void KPrPointObject::loadOasisMarker( KoOasisContext & context )
{
    loadOasisMarkerElement( context, "marker-start", lineBegin );
    loadOasisMarkerElement( context, "marker-end", lineEnd );
}

void KPrPointObject::fillStyle( KoGenStyle& styleObjectAuto, KoGenStyles& mainStyles ) const
{
    KPrShadowObject::fillStyle( styleObjectAuto, mainStyles );
    saveOasisMarkerElement( mainStyles, styleObjectAuto );
}


double KPrPointObject::load( const TQDomElement &element )
{
    double offset = KPrShadowObject::load( element );

    TQDomElement e = element.namedItem( "POINTS" ).toElement();
    if ( !e.isNull() ) {
        TQDomElement elemPoint = e.firstChild().toElement();
        unsigned int index = 0;
        while ( !elemPoint.isNull() ) {
            if ( elemPoint.tagName() == "Point" ) {
                double tmpX = 0;
                double tmpY = 0;
                if( elemPoint.hasAttribute( "point_x" ) )
                    tmpX = elemPoint.attribute( "point_x" ).toDouble();
                if( elemPoint.hasAttribute( "point_y" ) )
                    tmpY = elemPoint.attribute( "point_y" ).toDouble();

                points.putPoints( index, 1, tmpX,tmpY );
            }
            elemPoint = elemPoint.nextSibling().toElement();
            ++index;
        }
    }
    KPrStartEndLine::load( element );
    return offset;
}


void KPrPointObject::setSize( double _width, double _height )
{
    KoSize origSize( ext );
    KPrObject::setSize( _width, _height );

    double fx = ext.width() / origSize.width();
    double fy = ext.height() / origSize.height();

    updatePoints( fx, fy );
}


void KPrPointObject::flip( bool horizontal )
{
    KPrObject::flip( horizontal );

    KoPointArray tmpPoints;
    int index = 0;
    if ( ! horizontal )
    {
        KoPointArray::ConstIterator it;
        double horiz = getSize().height()/2;
        for ( it = points.begin(); it != points.end(); ++it ) {
            KoPoint point = (*it);
            if ( point.y()> horiz )
                tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
            else
                tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
            ++index;
        }
    }
    else
    {
        KoPointArray::ConstIterator it;
        double vert = getSize().width()/2;
        for ( it = points.begin(); it != points.end(); ++it ) {
            KoPoint point = (*it);
            if ( point.x()> vert )
                tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
            else
                tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
            ++index;
        }
    }

    points = tmpPoints;
}


void KPrPointObject::paint( TQPainter* _painter, KoTextZoomHandler*_zoomHandler,
                           int /* pageNum */, bool /*drawingShadow*/, bool drawContour )
{
    int _w = int( pen.pointWidth() );

    TQPen pen2;
    if ( drawContour ) {
        pen2 = TQPen( TQt::black, 1, TQt::DotLine );
        _painter->setRasterOp( TQt::NotXorROP );
    }
    else {
        pen2 = pen.zoomedPen( _zoomHandler );
    }
    _painter->setPen( pen2 );

    TQPointArray pointArray = getDrawingPoints().zoomPointArray( _zoomHandler, _w );
    _painter->drawPolyline( pointArray );

    if ( lineBegin != L_NORMAL && !drawContour ) {
        TQPoint startPoint;
        bool first = true;
        TQPointArray::ConstIterator it1;
        for ( it1 = pointArray.begin(); it1 != pointArray.end(); ++it1 ) {
            if ( first ) {
                startPoint = (*it1);
                first = false;
            }

            TQPoint point = (*it1);
            if ( startPoint != point ) {
                float angle = KoPoint::getAngle( KoPoint( startPoint ), KoPoint( point ) );
                drawFigureWithOffset( lineBegin, _painter, startPoint, pen2.color(), _w, angle,_zoomHandler );

                break;
            }
        }
    }

    if ( lineEnd != L_NORMAL && !drawContour ) {
        TQPoint endPoint;
        bool last = true;
        TQPointArray::ConstIterator it2 = pointArray.end();
        for ( it2 = it2 - 1; it2 != pointArray.begin(); --it2 ) {
            if ( last ) {
                endPoint = (*it2);
                last = false;
            }

            TQPoint point = (*it2);
            if ( endPoint != point ) {
                float angle = KoPoint::getAngle( KoPoint( endPoint ), KoPoint( point ) );
                drawFigureWithOffset( lineEnd, _painter, endPoint, pen2.color(), _w, angle,_zoomHandler );

                break;
            }
        }
    }
}


void KPrPointObject::updatePoints( double _fx, double _fy )
{
    int index = 0;
    KoPointArray tmpPoints;
    KoPointArray::ConstIterator it;
    for ( it = points.begin(); it != points.end(); ++it ) {
        KoPoint point = (*it);
        double tmpX = point.x() * _fx;
        double tmpY = point.y() * _fy;

        tmpPoints.putPoints( index, 1, tmpX,tmpY );
        ++index;
    }
    points = tmpPoints;
}


KoPointArray KPrPointObject::getDrawingPoints() const
{
  return points;
}