summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/kiviosdk/kivio_shape.cpp
blob: 302c6547928a33c7aa025f8b74f327f0d7dd870f (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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
 * Kivio - Visual Modelling and Flowcharting
 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <kdebug.h>
#include "kivio_common.h"
#include "kivio_fill_style.h"
#include "kivio_line_style.h"
#include "kivio_point.h"
#include "kivio_shape.h"
#include "kivio_text_style.h"

/**
 * Default constructor
 *
 * Does nothing
 */
KivioShape::KivioShape()
{
}


/**
 * Copy constructor
 *
 * @param source The object to copy data from
 *
 * Copies all data from source
 */
KivioShape::KivioShape( const KivioShape &source )
{
    source.m_shapeData.copyInto( &m_shapeData );
}


/**
 * Destructor
 */
KivioShape::~KivioShape()
{
}


/**
 * Copy all data into pTarget
 *
 * @param pTarget The target &ref KivioShape to copy into
 *
 * This will copy all data contained in this, into pTarget.
 */
void KivioShape::copyInto( KivioShape *pTarget ) const
{
    if( !pTarget )
        return;

    m_shapeData.copyInto( &(pTarget->m_shapeData) );
}


/**
 * Load this object from an XML element
 *
 * @param e The element to load from
 * @returns true on success, false on failure.
 */
bool KivioShape::loadXML( const TQDomElement &e )
{
    TQDomElement ele;
    TQDomNode node = e.firstChild();

    TQString name = XmlReadString( e, "name", "" );
    m_shapeData.setName( name );
    m_shapeData.setShapeType( (KivioShapeData::KivioShapeType)XmlReadInt( e, "shapeType", -1 ));

    if( m_shapeData.name().isEmpty() ||
        m_shapeData.shapeType() == -1 )
    {
       kdWarning(43000) << "-LOAD KivioShape::loadXML() - Unknown shape or bad name read. Shape load aborted." << endl;
       return false;
    }

    while( !node.isNull() )
    {
        TQString nodeName = node.nodeName();
        ele = node.toElement();

        if( nodeName == "KivioShapeData" )
        {
            m_shapeData.loadXML( ele );
        }

        node = node.nextSibling();
    }
    return true;
}


/**
 * Save this object to an XML element
 *
 * @param doc The document to save to
 * @returns TQDomElement representing this object.
 */
TQDomElement KivioShape::saveXML( TQDomDocument &doc )
{
    TQDomElement e = doc.createElement("KivioShape");

    XmlWriteString( e, "name", m_shapeData.m_name );
    XmlWriteInt( e, "shapeType", m_shapeData.m_shapeType );

    e.appendChild( m_shapeData.saveXML( doc ) );

    return e;
}


/**
 * Loads a shape of type arc
 *
 * @param e The element to load from
 * @returns A newly allocated KivioShape, or NULL on error.
 *
 * FIXME: implement this
 */
KivioShape *KivioShape::loadShapeArc( const TQDomElement & )
{
    return NULL;
}


/**
 * Loads a shape of type closed-path.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeClosedPath( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstClosedPath;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The line array  is made up of pairs of points
        if( nodeName == "KivioPoint" )
        {
            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
            pPoint->loadXML( node.toElement() );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioFillStyle" )
        {
            pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }
    
        node = node.nextSibling();
    }
        
    return pShape;
}


/**
 * Loads a shape of type bezier
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeBezier( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstBezier;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The line array  is made up of pairs of points
        if( nodeName == "KivioPoint" )
        {
            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
            pPoint->loadXML( node.toElement() );
            if( pPoint->pointType() != KivioPoint::kptBezier )
            {
	       kdDebug(43000) << "KivioShape::loadShapeBezier() - Non-bezier point found.  Aborting shape." << endl;
                delete pPoint;
                delete pShape;
                return NULL;
            }
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    if( pShape->m_shapeData.m_pOriginalPointList->count() != 4 )
    {
       kdDebug(43000) << "KivioShape::loadShapeBezier() - Wrong number of points loaded, should be 4, shape aborted" << endl;
        delete pShape;
        return NULL;
    }
    
    return pShape;
}


/**
 * Loads a shape of type ellipse.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeEllipse( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstEllipse;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
    pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 0.0f ), XmlReadFloat( e, "h", 0.0f ) );
        
    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        if( nodeName == "KivioFillStyle" )
        {
            pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    return pShape;
}


/**
 * Loads a shape of type line-array.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeLineArray( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    TQDomElement lineElement;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstLineArray;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The line array  is made up of pairs of points
        if( nodeName == "Line" )
        {
            lineElement = node.toElement();

            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint( XmlReadFloat( lineElement, "x1", 0.0f ), XmlReadFloat( lineElement, "y1", 0.0f ) );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = new KivioPoint( XmlReadFloat( lineElement, "x2", 0.0f ), XmlReadFloat( lineElement, "y2", 0.0f ) );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }

    return pShape;
}


/**
 * Loads a shape of type open-path.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeOpenPath( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstOpenPath;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The line array  is made up of pairs of points
        if( nodeName == "KivioPoint" )
        {
            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
            pPoint->loadXML( node.toElement() );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
        
    return pShape;
}


/**
 * Loads a shape of type pie.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 *
 * FIXME: Implement this
 */
KivioShape *KivioShape::loadShapePie( const TQDomElement & )
{
    return NULL;
}


/**
 * Loads a shape of type polygon.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapePolygon( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstPolygon;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The polygon is made up of a series of points
        if( nodeName == "KivioPoint" )
        {
            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint();
            pPoint->loadXML( node.toElement() );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioFillStyle" )
        {
            pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    return pShape;
}


/**
 * Loads a shape of type polyline.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapePolyline( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstPolyline;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // The polygon is made up of a series of points
        if( nodeName == "KivioPoint" )
        {
            // Allocate a new point, load it, and store it in the list
            pPoint = new KivioPoint();
            pPoint->loadXML( node.toElement() );
            pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
            pPoint = NULL;
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    return pShape;
}


/**
 * Loads a shape of type rectangle.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeRectangle( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstRectangle;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
    pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );
    
    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // Read the fill style
        if( nodeName == "KivioFillStyle" )
        {
            pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    return pShape;
}



/**
 * Loads a shape of type round rectangle.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeRoundRectangle( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    KivioPoint *pPoint = NULL;
    TQDomNode node;
    TQString nodeName;
    

    // Create the new shape to load into
    pShape = new KivioShape();
    
    // Load the type, name, and lineWidth properties
    pShape->m_shapeData.m_shapeType = KivioShapeData::kstRoundRectangle;
    pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );

    pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
    pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );
    
    // Read and store the radii of the curves
    pPoint = new KivioPoint(0.0f, 0.0f);
    pPoint->set( XmlReadFloat( e, "r1", 1.0f ), XmlReadFloat( e, "r2", 1.0f ) );
    pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
    
    // Iterate through the nodes loading the various items
    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        
        // Read the fill style
        if( nodeName == "KivioFillStyle" )
        {
            pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }
    
    return pShape;
}


/**
 * Loads a shape of type textbox.
 *
 * @param e The element to load from.
 * @returns A newly allocated KivioShape, or NULL on error.
 */
KivioShape *KivioShape::loadShapeTextBox( const TQDomElement &e )
{
    KivioShape *pShape = NULL;
    TQDomNode node;
    TQString nodeName;
    KivioTextStyle ts;
    TQString name;


    // Create the new shape to load into
    pShape = new KivioShape();

    // Load the type and name
    pShape->m_shapeData.setShapeType(KivioShapeData::kstTextBox);
    pShape->m_shapeData.setName( XmlReadString( e, "name", "" ) );
    pShape->m_shapeData.setTextColor(XmlReadColor(e,"color",TQColor(0,0,0)));

    pShape->m_shapeData.m_position.set( 
       XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
    pShape->m_shapeData.m_dimensions.set( 
       XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );

    // Now look for the KivioTextStyle
    node = e.firstChild();
    while( !node.isNull() )
    {
       nodeName = node.nodeName();
       if( nodeName == "KivioTextStyle" )
       {
	  ts.loadXML( node.toElement() );
	  pShape->m_shapeData.setTextStyle( &ts );
       }

       node = node.nextSibling();
    }
//    TQString text = XmlReadString( e, "text", "" );
//    pShape->m_shapeData.setText( text );


    return pShape;
}