~ubuntu-branches/ubuntu/edgy/koffice/edgy-updates

« back to all changes in this revision

Viewing changes to kpresenter/kppolylineobject.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040509113300-xi5t1z4yxe7n03x7
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
 
2
/* This file is part of the KDE project
 
3
   Copyright (C) 2001 Toshitaka Fujioka <fujioka@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., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
#include "kppolylineobject.h"
 
22
#include "kpresenter_utils.h"
 
23
#include "KPPolyLineObjectIface.h"
 
24
 
 
25
#include <qpainter.h>
 
26
#include <qwmatrix.h>
 
27
#include <qdom.h>
 
28
 
 
29
#include <kdebug.h>
 
30
#include <kozoomhandler.h>
 
31
#include <math.h>
 
32
using namespace std;
 
33
 
 
34
KPPolylineObject::KPPolylineObject()
 
35
    : KPPointObject()
 
36
{
 
37
    lineBegin = L_NORMAL;
 
38
    lineEnd = L_NORMAL;
 
39
}
 
40
 
 
41
DCOPObject* KPPolylineObject::dcopObject()
 
42
{
 
43
    if ( !dcop )
 
44
        dcop = new KPPolyLineObjectIface( this );
 
45
    return dcop;
 
46
}
 
47
 
 
48
KPPolylineObject::KPPolylineObject(  const KoPointArray &_points, const KoSize &_size,
 
49
                                     const QPen &_pen, LineEnd _lineBegin, LineEnd _lineEnd )
 
50
    : KPPointObject( _pen )
 
51
{
 
52
    points = KoPointArray( _points );
 
53
    ext = _size;
 
54
    lineBegin = _lineBegin;
 
55
    lineEnd = _lineEnd;
 
56
}
 
57
 
 
58
KPPolylineObject &KPPolylineObject::operator=( const KPPolylineObject & )
 
59
{
 
60
    return *this;
 
61
}
 
62
 
 
63
QDomDocumentFragment KPPolylineObject::save( QDomDocument& doc, double offset )
 
64
{
 
65
    QDomDocumentFragment fragment = KPShadowObject::save( doc, offset );
 
66
    if ( !points.isNull() ) {
 
67
        QDomElement elemPoints = doc.createElement( "POINTS" );
 
68
        KoPointArray::ConstIterator it;
 
69
        for ( it = points.begin(); it != points.end(); ++it ) {
 
70
            QDomElement elemPoint = doc.createElement( "Point" );
 
71
            KoPoint point = (*it);
 
72
            elemPoint.setAttribute( "point_x", point.x() );
 
73
            elemPoint.setAttribute( "point_y", point.y() );
 
74
 
 
75
            elemPoints.appendChild( elemPoint );
 
76
        }
 
77
        fragment.appendChild( elemPoints );
 
78
    }
 
79
 
 
80
    if ( lineBegin != L_NORMAL )
 
81
        fragment.appendChild( KPObject::createValueElement( "LINEBEGIN",
 
82
                                                            static_cast<int>( lineBegin ), doc ) );
 
83
 
 
84
    if ( lineEnd != L_NORMAL )
 
85
        fragment.appendChild( KPObject::createValueElement( "LINEEND",
 
86
                                                            static_cast<int>( lineEnd ), doc ) );
 
87
 
 
88
    return fragment;
 
89
}
 
90
 
 
91
double KPPolylineObject::load(const QDomElement &element)
 
92
{
 
93
    double offset=KPShadowObject::load( element );
 
94
 
 
95
    QDomElement e = element.namedItem( "POINTS" ).toElement();
 
96
    if ( !e.isNull() ) {
 
97
        QDomElement elemPoint = e.firstChild().toElement();
 
98
        unsigned int index = 0;
 
99
        while ( !elemPoint.isNull() ) {
 
100
            if ( elemPoint.tagName() == "Point" ) {
 
101
                double tmpX = 0;
 
102
                double tmpY = 0;
 
103
                if( elemPoint.hasAttribute( "point_x" ) )
 
104
                    tmpX = elemPoint.attribute( "point_x" ).toDouble();
 
105
                if( elemPoint.hasAttribute( "point_y" ) )
 
106
                    tmpY = elemPoint.attribute( "point_y" ).toDouble();
 
107
 
 
108
                points.putPoints( index, 1, tmpX,tmpY );
 
109
            }
 
110
            elemPoint = elemPoint.nextSibling().toElement();
 
111
            ++index;
 
112
        }
 
113
    }
 
114
 
 
115
    e = element.namedItem( "LINEBEGIN" ).toElement();
 
116
    if( !e.isNull() ) {
 
117
        int tmp = 0;
 
118
        if( e.hasAttribute( "value" ) )
 
119
            tmp = e.attribute( "value" ).toInt();
 
120
        lineBegin = static_cast<LineEnd>( tmp );
 
121
    }
 
122
 
 
123
    e = element.namedItem( "LINEEND" ).toElement();
 
124
    if( !e.isNull() ) {
 
125
        int tmp = 0;
 
126
        if( e.hasAttribute( "value" ) )
 
127
            tmp = e.attribute( "value" ).toInt();
 
128
        lineEnd = static_cast<LineEnd>( tmp );
 
129
    }
 
130
    return offset;
 
131
}
 
132
 
 
133
void KPPolylineObject::paint( QPainter* _painter,KoZoomHandler*_zoomHandler,
 
134
                              bool /*drawingShadow*/, bool drawContour )
 
135
{
 
136
    int _w = pen.width();
 
137
 
 
138
    QPen pen2;
 
139
    if ( drawContour ) {
 
140
        pen2 = QPen( Qt::black, 1, Qt::DotLine );
 
141
        _painter->setRasterOp( Qt::NotXorROP );
 
142
    }
 
143
    else {
 
144
        pen2 = pen;
 
145
        pen2.setWidth( _zoomHandler->zoomItX( pen.width() ) );
 
146
    }
 
147
    _painter->setPen( pen2 );
 
148
 
 
149
    QPointArray pointArray = points.zoomPointArray( _zoomHandler, _w );
 
150
 
 
151
    _painter->drawPolyline( pointArray );
 
152
 
 
153
    if ( lineBegin != L_NORMAL && !drawContour &&!isClosed()) {
 
154
        QPoint startPoint;
 
155
        bool first = true;
 
156
        QPointArray::ConstIterator it1;
 
157
        for ( it1 = pointArray.begin(); it1 != pointArray.end(); ++it1 ) {
 
158
            if ( first ) {
 
159
                startPoint = (*it1);
 
160
                first = false;
 
161
            }
 
162
 
 
163
            QPoint point = (*it1);
 
164
            if ( startPoint != point ) {
 
165
                float angle = KoPoint::getAngle( startPoint, point );
 
166
                drawFigureWithOffset( lineBegin, _painter, startPoint, pen2.color(), _w, angle,_zoomHandler );
 
167
 
 
168
                break;
 
169
            }
 
170
        }
 
171
    }
 
172
 
 
173
    if ( lineEnd != L_NORMAL && !drawContour &&!isClosed()) {
 
174
        QPoint endPoint;
 
175
        bool last = true;
 
176
        QPointArray::ConstIterator it2 = pointArray.end();
 
177
        for ( it2 = it2 - 1; it2 != pointArray.begin(); --it2 ) {
 
178
            if ( last ) {
 
179
                endPoint = (*it2);
 
180
                last = false;
 
181
            }
 
182
 
 
183
            QPoint point = (*it2);
 
184
            if ( endPoint != point ) {
 
185
                float angle = KoPoint::getAngle( endPoint, point );
 
186
                drawFigureWithOffset( lineEnd, _painter, endPoint, pen2.color(), _w, angle,_zoomHandler );
 
187
 
 
188
                break;
 
189
            }
 
190
        }
 
191
    }
 
192
}
 
193
 
 
194
void KPPolylineObject::setSize( double _width, double _height )
 
195
{
 
196
    KoSize origSize( ext );
 
197
    KPObject::setSize( _width, _height );
 
198
 
 
199
    double fx = (double)( (double)ext.width() / (double)origSize.width() );
 
200
    double fy = (double)( (double)ext.height() / (double)origSize.height() );
 
201
 
 
202
    updatePoints( fx, fy );
 
203
}
 
204
 
 
205
void KPPolylineObject::updatePoints( double _fx, double _fy )
 
206
{
 
207
    int index = 0;
 
208
    KoPointArray tmpPoints;
 
209
    KoPointArray::ConstIterator it;
 
210
    for ( it = points.begin(); it != points.end(); ++it ) {
 
211
        KoPoint point = (*it);
 
212
        double tmpX = point.x() * _fx;
 
213
        double tmpY = point.y() * _fy;
 
214
 
 
215
        tmpPoints.putPoints( index, 1, tmpX,tmpY );
 
216
        ++index;
 
217
    }
 
218
    points = tmpPoints;
 
219
}
 
220
 
 
221
 
 
222
void KPPolylineObject::flip( bool horizontal )
 
223
{
 
224
    KPObject::flip( horizontal );
 
225
 
 
226
    KoPointArray tmpPoints;
 
227
    int index = 0;
 
228
    if ( horizontal )
 
229
    {
 
230
        KoPointArray::ConstIterator it;
 
231
        double horiz = getSize().height()/2;
 
232
        for ( it = points.begin(); it != points.end(); ++it ) {
 
233
            KoPoint point = (*it);
 
234
            if ( point.y()> horiz )
 
235
                tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
 
236
            else
 
237
                tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
 
238
            ++index;
 
239
        }
 
240
 
 
241
    }
 
242
    else
 
243
    {
 
244
        KoPointArray::ConstIterator it;
 
245
        double vert = getSize().width()/2;
 
246
        for ( it = points.begin(); it != points.end(); ++it ) {
 
247
            KoPoint point = (*it);
 
248
            if ( point.x()> vert )
 
249
                tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
 
250
            else
 
251
                tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
 
252
            ++index;
 
253
        }
 
254
 
 
255
    }
 
256
    points = tmpPoints;
 
257
}
 
258
 
 
259
void KPPolylineObject::closeObject(bool _close)
 
260
{
 
261
    points = getCloseObject( points, _close, isClosed() );
 
262
}
 
263
 
 
264
bool KPPolylineObject::isClosed()const
 
265
{
 
266
    return ( points.at(0) == points.at(points.count()-1) );
 
267
}