summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrLineObject.cpp
blob: 1cd9881942310766ccc630191a90605617d4f0ef (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 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 "KPrLineObject.h"
#include "KPrUtils.h"
#include "KPrDocument.h"
#include "KPrLineObjectIface.h"
#include "KoPointArray.h"
#include <KoStyleStack.h>
#include <KoOasisContext.h>

#include <tqpainter.h>
#include <tqwmatrix.h>
#include <tqdom.h>
#include <kdebug.h>
#include <KoUnit.h>
#include <KoTextZoomHandler.h>
#include <math.h>
#include <KoDom.h>
#include <KoXmlNS.h>

using namespace std;

KPrLineObject::KPrLineObject()
    : KPrShadowObject(), KPrStartEndLine( L_NORMAL, L_NORMAL )
{
    lineType = LT_HORZ;
}

KPrLineObject::KPrLineObject( const KoPen &_pen, LineEnd _lineBegin,
                            LineEnd _lineEnd, LineType _lineType )
    : KPrShadowObject( _pen ), KPrStartEndLine( _lineBegin, _lineEnd )
{
    lineType = _lineType;
}

KPrLineObject &KPrLineObject::operator=( const KPrLineObject & )
{
    return *this;
}

DCOPObject* KPrLineObject::dcopObject()
{
    if ( !dcop )
        dcop = new KPrLineObjectIface( this );
    return dcop;
}


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


bool KPrLineObject::saveOasisObjectAttributes( KPOasisSaveContext &/*sc*/ ) const
{
    // nothing to do
    return true;
}

void KPrLineObject::saveOasisPosObject( KoXmlWriter &xmlWriter, int indexObj ) const
{
    xmlWriter.addAttribute( "draw:id", "object" + TQString::number( indexObj ) );

    double x1 = 0.0;
    double y1 = 0.0;
    double x2 = 0.0;
    double y2 = 0.0;
    KoPoint center( ext.width() / 2, ext.height() / 2 );

    switch ( lineType )
    {
        case LT_LD_RU:
            x1 = -center.x();
            y1 = center.y();
            x2 = -x1;
            y2 = -y1;
            break;
        case LT_HORZ:
            x1 = -center.x();
            x2 = -x1;
            break;
        case LT_VERT:
            y1 = -center.y();
            y2 = -y1;
            break;
        case LT_LU_RD:
            x1 = -center.x();
            y1 = -center.y();
            x2 = -x1;
            y2 = -y1;
            break;
    }
    if ( kAbs( angle ) > 1E-6 )
    {
        double angInRad = -angle * M_PI / 180.0;
        TQWMatrix m( cos( angInRad ), -sin( angInRad ), sin( angInRad ), cos( angInRad ), 0, 0 );
        double transX1 = 0.0;
        double transY1 = 0.0;
        double transX2 = 0.0;
        double transY2 = 0.0;
        m.map( x1, y1, &transX1, &transY1 );
        m.map( x2, y2, &transX2, &transY2 );
        x1 = transX1;
        y1 = transY1;
        x2 = transX2;
        y2 = transY2;
    }
    
    x1 += orig.x() + center.x();
    y1 += orig.y() + center.y();
    x2 += orig.x() + center.x();
    y2 += orig.y() + center.y();

    //save all into pt
    xmlWriter.addAttributePt( "svg:x1", x1 );
    xmlWriter.addAttributePt( "svg:y1", y1 );
    xmlWriter.addAttributePt( "svg:x2", x2 );
    xmlWriter.addAttributePt( "svg:y2", y2 );
}

const char * KPrLineObject::getOasisElementName() const
{
    return "draw:line";
}

TQDomDocumentFragment KPrLineObject::save( TQDomDocument& doc, double offset )
{
    TQDomDocumentFragment fragment=KPrShadowObject::save(doc, offset);
    if (lineType!=LT_HORZ)
        fragment.appendChild(KPrObject::createValueElement("LINETYPE", static_cast<int>(lineType), doc));
    KPrStartEndLine::save( fragment, doc );
    return fragment;
}

void KPrLineObject::loadOasis(const TQDomElement &element, KoOasisContext & context, KPrLoadingInfo *info)
{
    KPrShadowObject::loadOasis(element, context, info);

    double x1 = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x1", TQString() ) );
    double y1 = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y1", TQString() ) );
    double x2 = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x2", TQString() ) );
    double y2 = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y2", TQString() ) );

    kdDebug()<<" KPrLineObject::loadOasis(const TQDomElement &element) : x1 "<< x1 <<" y1 : "<<y1<<" x2 :"<<x2 <<" y2 "<<y2<<endl;
    double x = TQMIN( x1, x2 );
    double y = TQMIN( y1, y2 );

    orig.setX( x );
    orig.setY( y );

    ext.setWidth( fabs( x1 - x2 ) );
    ext.setHeight( fabs( y1 - y2 ) );

    if ( y1 == y2 )
    {
        lineType=LT_HORZ;
        //define default height
        ext.setHeight( 10 );
        orig.setY( y - 5.0 );
    }
    else if ( x1 == x2 )
    {
        lineType=LT_VERT;
        //define default width
        ext.setWidth( 10 );
        orig.setX( x - 5.0 );
    }
    else if ( ( x1 < x2 && y1 < y2 ) || ( x1 > x2 && y1 > y2 ) )
         lineType=LT_LU_RD;
    else
        lineType=LT_LD_RU;

    kdDebug()<<"KPrLineObject::loadOasis(const TQDomElement &element) : real position x :"<<orig.x()<<" y "<<orig.y()<< " width :"<<ext.width()<<" height :"<<ext.height()<<endl;

    TQString attr = (x1 <= x2) ?  "marker-start" : "marker-end";
    loadOasisMarkerElement( context, attr, lineBegin );

    attr = (x1 <= x2) ?  "marker-end" : "marker-start";
    loadOasisMarkerElement( context, attr, lineEnd );
}

double KPrLineObject::load(const TQDomElement &element)
{
    double offset=KPrShadowObject::load(element);
    TQDomElement e=element.namedItem("LINETYPE").toElement();
    if(!e.isNull()) {
        int tmp=0;
        if(e.hasAttribute("value"))
            tmp=e.attribute("value").toInt();
        lineType=static_cast<LineType>(tmp);
    }
    KPrStartEndLine::load( element );
    return offset;
}

void KPrLineObject::paint( TQPainter* _painter, KoTextZoomHandler*_zoomHandler,
                          int /* pageNum */, bool /*drawingShadow*/, bool drawContour )
{
    double ow = ext.width();
    double oh = ext.height();
    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 );


    KoSize diff1( 0, 0 ), diff2( 0, 0 );

    float _angle = 0;
    switch ( lineType )
    {
    case LT_HORZ: {
        _angle = 0;
    } break;
    case LT_VERT: {
        _angle = 90 ;
    } break;
    case LT_LU_RD: {
        KoRect _rect = KoRect( orig, ext );
        KoPoint pnt1 = _rect.topLeft();
        KoPoint pnt2 = _rect.bottomRight();

        _angle = KoPoint::getAngle( pnt1, pnt2 ) - 180.0;
    } break;
    case LT_LD_RU: {
        KoRect _rect = KoRect( orig, ext );
        KoPoint pnt1 = _rect.bottomLeft();
        KoPoint pnt2 = _rect.topRight();

        _angle = KoPoint::getAngle( pnt1, pnt2 ) - 180.0;
    } break;
    }

    if ( lineBegin != L_NORMAL )
        diff1 = getOffset( lineBegin, _w, _angle + 180.0);

    if ( lineEnd != L_NORMAL )
        diff2 = getOffset( lineEnd, _w, _angle );

    switch ( lineType )
    {
    case LT_HORZ: {
        if ( lineBegin != L_NORMAL && !drawContour )
            drawFigure( lineBegin, _painter,
                        KoPoint( 0, oh / 2.0 ),
                        pen2.color(), _w, 180.0, _zoomHandler );

        if ( lineEnd != L_NORMAL && !drawContour )
            drawFigure( lineEnd, _painter,
                        KoPoint( ow , oh / 2.0),
                        pen2.color(), _w, 0.0, _zoomHandler );

        _painter->drawLine( _zoomHandler->zoomItX( - diff1.width() ),
                            _zoomHandler->zoomItY( oh / 2 ),
                            _zoomHandler->zoomItX( ow - diff2.width() ),
                            _zoomHandler->zoomItY( oh / 2 ) );
    } break;
    case LT_VERT: {
        if ( lineBegin != L_NORMAL && !drawContour )
            drawFigure( lineBegin, _painter,
                        KoPoint( ow / 2.0, 0 ),
                        pen2.color(), _w, 270.0, _zoomHandler );

        if ( lineEnd != L_NORMAL && !drawContour )
            drawFigure( lineEnd, _painter,
                        KoPoint( ow / 2.0, oh ),
                        pen2.color(), _w, 90.0, _zoomHandler );

        _painter->drawLine( _zoomHandler->zoomItX( ow / 2 ),
                            _zoomHandler->zoomItX( - diff1.height() ),
                            _zoomHandler->zoomItX( ow / 2 ),
                            _zoomHandler->zoomItY( oh - diff2.height() ) );
    } break;
    case LT_LU_RD: {
        if ( lineBegin != L_NORMAL && !drawContour ) {
            _painter->save();
            drawFigure( lineBegin, _painter,
                        KoPoint( 0, 0 ), pen2.color(),
                        _w, _angle + 180, _zoomHandler );
            _painter->restore();
        }
        if ( lineEnd != L_NORMAL && !drawContour ) {
            _painter->save();
            _painter->translate( _zoomHandler->zoomItX( ow ),
                                 _zoomHandler->zoomItY( oh ) );
            drawFigure( lineEnd, _painter,
                        KoPoint( 0, 0 ), pen2.color(),
                        _w, _angle, _zoomHandler );
            _painter->restore();

        }
        _painter->drawLine( _zoomHandler->zoomItX( - diff1.width() ),
                            _zoomHandler->zoomItY( - diff1.height() ),
                            _zoomHandler->zoomItX( ow - diff2.width() ),
                            _zoomHandler->zoomItY( oh - diff2.height() ) );
    } break;
    case LT_LD_RU: {
        if ( lineBegin != L_NORMAL && !drawContour ) {
            _painter->save();
            _painter->translate( _zoomHandler->zoomItX( 0 ),
                                 _zoomHandler->zoomItY( oh ) );
            drawFigure( lineBegin, _painter,
                        KoPoint( 0, 0 ), pen2.color(),
                        _w, _angle + 180,_zoomHandler );
            _painter->restore();
        }
        if ( lineEnd != L_NORMAL && !drawContour ) {
            _painter->save();
            _painter->translate( _zoomHandler->zoomItX( ow ),
                                 _zoomHandler->zoomItY( 0 ) );
            drawFigure( lineEnd, _painter,
                        KoPoint( 0, 0 ), pen2.color(),
                        _w, _angle,_zoomHandler );
            _painter->restore();
        }
        _painter->drawLine( _zoomHandler->zoomItX( - diff1.width() ),
                            _zoomHandler->zoomItY( oh - diff1.height() ),
                            _zoomHandler->zoomItX( ow - diff2.width() ),
                            _zoomHandler->zoomItY( - diff2.height() ));
    } break;
    }
}

void KPrLineObject::flip( bool horizontal )
{
    KPrObject::flip( horizontal );
    if ( ! horizontal )
    {
        if ( lineType == LT_LU_RD )
            lineType = LT_LD_RU;
        else if ( lineType == LT_LD_RU )
            lineType = LT_LU_RD;
    }
    else
    {
        if ( lineType == LT_LU_RD )
            lineType = LT_LD_RU;
        else if ( lineType == LT_LD_RU )
            lineType = LT_LU_RD;

        LineEnd tmp = lineBegin;
        lineBegin = lineEnd;
        lineEnd = tmp;
    }
}

KoSize KPrLineObject::getRealSize() const {
    KoPoint realOrig( orig );
    KoSize size( ext );

    KoPointArray points(4);

    if ( lineType == LT_LU_RD || lineType == LT_LD_RU )
    {
        double objAngle = atan( ext.width() / ext.height() );
        double x = cos( objAngle ) * pen.pointWidth();
        double y = sin( objAngle ) * pen.pointWidth();

        if ( lineType == LT_LU_RD )
        {
            points.setPoint( 0, x, 0 );
            points.setPoint( 1, 0, y );
            points.setPoint( 2, ext.width() + x, ext.height() );
            points.setPoint( 3, ext.width(), ext.height() + y );
        }
        else
        {
            points.setPoint( 0, 0, ext.height() );
            points.setPoint( 1, x, ext.height() + y );
            points.setPoint( 2, ext.width(), 0 );
            points.setPoint( 3, ext.width() + x, y );
        }
        realOrig.setX( realOrig.x() - x / 2.0 );
        realOrig.setY( realOrig.y() - y / 2.0 );
        size.setWidth( size.width() + x );
        size.setHeight( size.height() + y );
    }

    if ( angle == 0.0 && lineType == LT_HORZ )
    {
        size.setHeight( pen.pointWidth() );
    }
    else if ( angle == 0.0 && lineType == LT_VERT )
    {
        size.setWidth( pen.pointWidth() );
    }
    else
    {
        if ( lineType == LT_HORZ )
        {
            points.setPoint( 0, 0, ( ext.height() - pen.pointWidth() ) / 2.0 );
            points.setPoint( 1, 0, ( ext.height() + pen.pointWidth() ) / 2.0 );
            points.setPoint( 2, ext.width(), ( ext.height() - pen.pointWidth() ) / 2.0 );
            points.setPoint( 3, ext.width(), ( ext.height() + pen.pointWidth() ) / 2.0 );
        }
        else if ( lineType == LT_VERT )
        {
            points.setPoint( 0, ( ext.width() - pen.pointWidth() ) / 2.0, 0 );
            points.setPoint( 1, ( ext.width() + pen.pointWidth() ) / 2.0, 0 );
            points.setPoint( 2, ( ext.width() - pen.pointWidth() ) / 2.0, ext.height() );
            points.setPoint( 3, ( ext.width() + pen.pointWidth() ) / 2.0, ext.height() );
        }

        getRealSizeAndOrigFromPoints( points, angle, size, realOrig );
    }

    return size;
}

KoPoint KPrLineObject::getRealOrig() const {
    KoPoint realOrig( orig );
    KoSize size( ext );

    KoPointArray points(4);

    if ( lineType == LT_LU_RD || lineType == LT_LD_RU )
    {
        double objAngle = atan( ext.width() / ext.height() );
        double x = cos( objAngle ) * pen.pointWidth();
        double y = sin( objAngle ) * pen.pointWidth();

        if ( lineType == LT_LU_RD )
        {
            points.setPoint( 0, x, 0 );
            points.setPoint( 1, 0, y );
            points.setPoint( 2, ext.width() + x, ext.height() );
            points.setPoint( 3, ext.width(), ext.height() + y );
        }
        else
        {
            points.setPoint( 0, 0, ext.height() );
            points.setPoint( 1, x, ext.height() + y );
            points.setPoint( 2, ext.width(), 0 );
            points.setPoint( 3, ext.width() + x, y );
        }
        realOrig.setX( realOrig.x() - x / 2.0 );
        realOrig.setY( realOrig.y() - y / 2.0 );
        size.setWidth( size.width() + x );
        size.setHeight( size.height() + y );
    }

    if ( angle == 0.0 && lineType == LT_HORZ )
    {
        realOrig.setY( realOrig.y() + ( ext.height() - pen.pointWidth() ) / 2.0 );
    }
    else if ( angle == 0.0 && lineType == LT_VERT )
    {
        realOrig.setX( realOrig.x() + ( ext.width() - pen.pointWidth() ) / 2.0 );
    }
    else
    {
        if ( lineType == LT_HORZ )
        {
            points.setPoint( 0, 0, ( ext.height() - pen.pointWidth() ) / 2.0 );
            points.setPoint( 1, 0, ( ext.height() + pen.pointWidth() ) / 2.0 );
            points.setPoint( 2, ext.width(), ( ext.height() - pen.pointWidth() ) / 2.0 );
            points.setPoint( 3, ext.width(), ( ext.height() + pen.pointWidth() ) / 2.0 );
        }
        else if ( lineType == LT_VERT )
        {
            points.setPoint( 0, ( ext.width() - pen.pointWidth() ) / 2.0, 0 );
            points.setPoint( 1, ( ext.width() + pen.pointWidth() ) / 2.0, 0 );
            points.setPoint( 2, ( ext.width() - pen.pointWidth() ) / 2.0, ext.height() );
            points.setPoint( 3, ( ext.width() + pen.pointWidth() ) / 2.0, ext.height() );
        }

        getRealSizeAndOrigFromPoints( points, angle, size, realOrig );
    }

    return realOrig;
}