summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/kiviosdk/kivio_base_connector_stencil.cpp
blob: a9e7892827346b3e7c2a1df8f6c9a7830f81ea94 (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
/*
 * 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.
 */

/*
 * Implementing a connector involves deriving from this class.
 *
 * You need to implement the following routines:
 *   setStartPoint
 *   setEndPoint
 *   checkForCollision
 *   duplicate
 *   paint
 *   paintOutline
 *   saveXML
 *   loadXML
 *
 * Save and Load should call saveConnectors, loadConnectors, saveProperties, and
 * loadProperties.  These are helper functions which take care of the common
 * acts of saving/loading connectors and colors/line-styles, etc...
 *
 */

#include "kivio_arrowhead.h"
#include "kivio_base_connector_stencil.h"
#include "kivio_common.h"
#include "kivio_connector_point.h"
#include "kivio_custom_drag_data.h"
#include "kivio_fill_style.h"
#include "kivio_intra_stencil_data.h"
#include "kivio_layer.h"
#include "kivio_line_style.h"
#include "kivio_page.h"
#include "kivio_painter.h"
#include "kivio_point.h"
#include "kivio_stencil.h"

#include <kdebug.h>
#include <KoZoomHandler.h>

/**
 * Default constructor.
 *
 * This will allocate a new fill style object, a list for
 * connection points, and set some default values.
 */
KivioBaseConnectorStencil::KivioBaseConnectorStencil()
    : KivioStencil()
{
    m_pFillStyle = new KivioFillStyle();
    m_pLineStyle = new KivioLineStyle();

    m_pConnectorPoints = new TQPtrList<KivioConnectorPoint>;
    m_pConnectorPoints->setAutoDelete(true);

}


/**
 * Destructor
 */
KivioBaseConnectorStencil::~KivioBaseConnectorStencil()
{
    delete m_pFillStyle;
    delete m_pLineStyle;
    delete m_pConnectorPoints;
}

void KivioBaseConnectorStencil::setFGColor( TQColor c )
{
    m_pLineStyle->setColor(c);
}

TQColor KivioBaseConnectorStencil::fgColor()
{
    return m_pLineStyle->color();
}

void KivioBaseConnectorStencil::setLineWidth( double f )
{
    m_pLineStyle->setWidth(f);
}

double KivioBaseConnectorStencil::lineWidth()
{
    return m_pLineStyle->width();
}

void KivioBaseConnectorStencil::setBGColor( TQColor c )
{
    m_pFillStyle->setColor(c);
}

TQColor KivioBaseConnectorStencil::bgColor()
{
    return m_pFillStyle->color();
}


/////////////////////////////////
// Position functions
/////////////////////////////////
void KivioBaseConnectorStencil::setX( double x )
{
    double dx = x - m_x;

    m_x += dx;

    KivioConnectorPoint *p = m_pConnectorPoints->first();
    while( p )
    {
        p->setX( p->x() + dx, false );
        p->disconnect();

        p = m_pConnectorPoints->next();
    }


    m_x = x;
}

void KivioBaseConnectorStencil::setY( double y )
{
    double dy = y - m_y;

    m_y += dy;

    KivioConnectorPoint *p = m_pConnectorPoints->first();
    while( p )
    {
        p->setY( p->y() + dy, false );
        p->disconnect();

        p = m_pConnectorPoints->next();
    }

    m_y = y;
}

void KivioBaseConnectorStencil::setPosition( double x, double y )
{
    double dx = x - m_x;
    double dy = y - m_y;

    m_x += dx;
    m_y += dy;

    KivioConnectorPoint *p = m_pConnectorPoints->first();
    while( p )
    {
        p->setPosition( p->x()+dx, p->y()+dy, false );
        p->disconnect();

        p = m_pConnectorPoints->next();
    }


    m_x = x;
    m_y = y;
}



/////////////////////////////
// Connection tool functions
/////////////////////////////
void KivioBaseConnectorStencil::setStartPoint( double /*x*/, double /*y*/ )
{
    /* Derived class must implement this function */
    // m_start.setPosition( x, y, false );
    // m_start.disconnect();
}


void KivioBaseConnectorStencil::setEndPoint( double /*x*/, double /*y*/ )
{
    /* Derived class must implement this function */
    // m_end.setPosition( x, y, false );
    // m_end.disconnect();
}


void KivioBaseConnectorStencil::paint( KivioIntraStencilData * )
{
    /* Derived class must implement this */
}

void KivioBaseConnectorStencil::paintOutline( KivioIntraStencilData *pData )
{
    /* Derived class should implement this */
    paint( pData );
}

void KivioBaseConnectorStencil::paintConnectorTargets( KivioIntraStencilData * )
{
}

void KivioBaseConnectorStencil::paintSelectionHandles( KivioIntraStencilData *pData )
{
  // Handle Width
  const double HW = 6.0f;
  const double HWP1 = HW+1.0f;

  // Handle Width Over 2
  const double HWo2 = HW/2.0f;

  // Stencil data
  KoZoomHandler* zoomHandler = pData->zoomHandler;
  KivioPainter *painter = pData->painter;
  double x1, y1;

  painter->setLineWidth(1.0f);
  painter->setFGColor(TQColor(0,0,0));

  KivioConnectorPoint *p = m_pConnectorPoints->first();
  while( p )
  {
    x1 = zoomHandler->zoomItX(p->x()) - HWo2;
    y1 = zoomHandler->zoomItY(p->y()) - HWo2;

    if( p->target() )
        painter->setBGColor(TQColor(200,0,0));
    else
        painter->setBGColor(TQColor(0,200,0));

    painter->fillRect( x1, y1, HWP1, HWP1 );

    p = m_pConnectorPoints->next();
  }
}



///////////////////////////////
// Collision detection
///////////////////////////////
KivioCollisionType KivioBaseConnectorStencil::checkForCollision( KoPoint *, double )
{
    /* Derived class must implement this */
    return kctNone;
}




/////////////////////////////////
// Custom dragging
/////////////////////////////////
/**
 * Custom drag the connector points.
 *
 * The default action of this function is to locate the point
 * in the connector list by the id and then drag it around.
 * Then attempt to snap it to another stencil.  Otherwise
 * disconnect it.
 */
void KivioBaseConnectorStencil::customDrag( KivioCustomDragData *pData )
{
    double _x = pData->x;
    double _y = pData->y;
    int id = pData->id;

    KivioConnectorPoint *p;

    // Locate the point specified by id
    p = m_pConnectorPoints->at( id - (kctCustom+1));

    if( !p )
    {
       kdDebug(43000) << "KivioBaseConnectorStencil::customDrag() - KivioConnectorPoint id: " << (id - (kctCustom+1)) << "  not found\n" << endl;
       return;
    }

    p->setPosition( _x, _y, true );


    KivioLayer *pCurLayer = pData->page->curLayer();
    KivioLayer *pLayer = pData->page->firstLayer(); //pData->page->curLayer();

    while( pLayer )
    {
        // To be connected to, a layer must be visible and connectable
        if( pLayer!=pCurLayer )
        {
            if( pLayer->connectable()==false || pLayer->visible()==false )
            {
                pLayer = pData->page->nextLayer();
                continue;
            }
        }

        // Tell the layer to search for a target
        if( pLayer->connectPointToTarget( p, 8.0f ) )
        {
            return;
        }

        pLayer = pData->page->nextLayer();
    }


    // Nope, disconnect
    p->disconnect();
}


/**
 * Sets the position and dimensions of this stencil based on its connection points.
 */
void KivioBaseConnectorStencil::updateGeometry()
{
    double minX, minY, maxX, maxY;

    minX = 1000000000000.0f;
    minY = minX;
    maxX = -100000000000.0f;
    maxY = maxX;

    KivioConnectorPoint *p;
    p = m_pConnectorPoints->first();
    while( p )
    {
        if( p->x() < minX )
            minX = p->x();
        if( p->x() > maxX )
            maxX = p->x();
        if( p->y() < minY )
            minY = p->y();
        if( p->y() > maxY )
            maxY = p->y();

        p = m_pConnectorPoints->next();
    }

    m_x = minX;
    m_y = minY;
    m_w = maxX - minX + 1.0f;
    m_h = maxY - minY + 1.0f;
}



// file i/o routines
bool KivioBaseConnectorStencil::loadXML( const TQDomElement & )
{
    return false;
}

TQDomElement KivioBaseConnectorStencil::saveXML( TQDomDocument & )
{
    return TQDomElement();
}



KivioStencil *KivioBaseConnectorStencil::duplicate()
{
    /* Derived class must implement this function */
    return NULL;
}


bool KivioBaseConnectorStencil::boolAllTrue( bool *boolArray, int count )
{
    int i;

    for( i=0; i<count; i++ )
    {
        if( boolArray[i]==false )
            return false;
    }

    return true;
}

bool KivioBaseConnectorStencil::boolContainsFalse( bool *boolArray, int count )
{
    int i;

    for( i=0; i<count; i++ )
    {
        if( boolArray[i]==false )
            return true;
    }

    return false;
}


void KivioBaseConnectorStencil::searchForConnections( KivioPage *pPage )
{
    bool *done = new bool[ m_pConnectorPoints->count() ];

    int i;

    for( i=0; i<(int)m_pConnectorPoints->count(); i++ )
        done[i] = false;

    KivioConnectorPoint *p;

    i=0;
    p = m_pConnectorPoints->first();
    while( p )
    {
        if( p->targetId() == -1 )
            done[i] = true;

        i++;
        p = m_pConnectorPoints->next();
    }


    // No connections? BaiL!
    if( boolAllTrue( done, m_pConnectorPoints->count() ) )
    {
       delete [] done;
       return;
    }



    KivioLayer *pLayer = pPage->firstLayer();
    while( pLayer && ( boolContainsFalse(done, m_pConnectorPoints->count()) ) )
    {
        KivioStencil *pStencil = pLayer->firstStencil();

        while( pStencil && ( boolContainsFalse(done, m_pConnectorPoints->count()) ) )
        {
            // No connecting to ourself!
            if( pStencil != this )
            {

                // Iterate through all connectors attempting to connect it to the stencil.
                // If it connects, mark it as done
                i=0;
                p = m_pConnectorPoints->first();
                while( p )
                {
                    if( !done[i] &&
                         p->targetId() != -1 )
                    {
                        if( pStencil->connectToTarget( p, p->targetId() ) )
                        {
                            done[i] = true;
                        }
                    }

                    i++;
                    p = m_pConnectorPoints->next();
                }

            }

            pStencil = pLayer->nextStencil();
        }

        pLayer = pPage->nextLayer();
    }

    delete [] done;
}


//////////////////////
// resize handles
//////////////////////
int KivioBaseConnectorStencil::resizeHandlePositions()
{
    return (int)krhpNone;
}

TQDomElement KivioBaseConnectorStencil::saveConnectors( TQDomDocument &doc )
{
    TQDomElement eConns = doc.createElement("KivioConnectors");

    KivioConnectorPoint *p;
    p = m_pConnectorPoints->first();
    while( p )
    {
        eConns.appendChild( p->saveXML(doc) );

        p = m_pConnectorPoints->next();
    }

    return eConns;
}

bool KivioBaseConnectorStencil::loadConnectors( const TQDomElement &e )
{
    m_pConnectorPoints->clear();

    KivioConnectorPoint *p;

    TQDomNode node = e.firstChild();
    TQDomElement e2;
    TQString name;

    while( !node.isNull() )
    {
        e2 = node.toElement();
        name = e2.nodeName();

        if( name == "KivioConnectorPoint" )
        {
            p = new KivioConnectorPoint();
            p->setStencil(this);
            p->loadXML( e2 );

            m_pConnectorPoints->append( p );
            p = NULL;
        }

        node = node.nextSibling();
    }

    return true;
}

TQDomElement KivioBaseConnectorStencil::saveProperties( TQDomDocument &doc )
{
    TQDomElement propE = doc.createElement("KivioConnectorProperties");

    propE.appendChild( m_pLineStyle->saveXML( doc ) );

    propE.appendChild( m_pFillStyle->saveXML( doc ) );

    return propE;
}

bool KivioBaseConnectorStencil::loadProperties( const TQDomElement &e )
{
    TQDomNode node;
    TQString nodeName;

    node = e.firstChild();
    while( !node.isNull() )
    {
        nodeName = node.nodeName();
        if( nodeName == "KivioFillStyle" )
        {
            m_pFillStyle->loadXML( node.toElement() );
        }
        else if( nodeName == "KivioLineStyle" )
        {
            m_pLineStyle->loadXML( node.toElement() );
        }

        node = node.nextSibling();
    }

    return true;
}