summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/kiviosdk/kivio_connector_target.cpp
blob: 60fe031616c5883803d524c79d608815b45ca365 (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
/*
 * 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 "kivio_common.h"
#include "kivio_connector_point.h"
#include "kivio_connector_target.h"
#include "kivio_point.h"
#include "kivio_stencil.h"
#include "kivio_intra_stencil_data.h"

#include <kdebug.h>

KivioConnectorTarget::KivioConnectorTarget()
    : m_pConnectors(NULL)
{
    m_position.setX( 0.0f );
    m_position.setY( 0.0f );
    m_pConnectors = new QPtrList<KivioConnectorPoint>;
    m_pConnectors->setAutoDelete(false);
    m_id = -1;
    m_xOffset = m_yOffset = 0;
}

KivioConnectorTarget::KivioConnectorTarget(double x, double y)
{
    m_position.setX( x );
    m_position.setY( y );
    m_pConnectors = new QPtrList<KivioConnectorPoint>;
    m_pConnectors->setAutoDelete(false);

    m_id = -1;
    m_xOffset = m_yOffset = 0;
}

KivioConnectorTarget::KivioConnectorTarget(double x, double y, double xOffset, double yOffset)
{
    m_position.setX( x );
    m_position.setY( y );
    m_pConnectors = new QPtrList<KivioConnectorPoint>;
    m_pConnectors->setAutoDelete(false);

    m_id = -1;
    setOffsets(xOffset, yOffset);
}

/**
 * Duplicates this target.
 *
 * Duplicates this target, only the position.  The connections
 * are not preserved.
 */
KivioConnectorTarget *KivioConnectorTarget::duplicate()
{
    KivioConnectorTarget *pTarget = new KivioConnectorTarget( m_position.x(), m_position.y(), m_xOffset, m_yOffset );

    return pTarget;
}


/**
 * Kill this object and disconnects all KivioConnectorPoints from this.
 *
 * This is a slightly odd function in that it does not call the disconnect() method
 * of KivioConnectorPoint.  The reason for this is that FIX ME DAVE!
 */
KivioConnectorTarget::~KivioConnectorTarget()
{
    // Iterate through all connectors diconnecting them from this
    KivioConnectorPoint *point;

    if( m_pConnectors )
    {
        point = m_pConnectors->first();
        point = m_pConnectors->take();

        while( point )
        {
	   kdDebug(43000) << "KivioConnectorTarget:: -> diconnecting" << endl;

            // Disconnect the point.  But tell the point to not call
            // KivioConnectorTarget::removeConnectorFromList() because it will cause our
            // position in the list to be screwed up.
            point->disconnect( false );

            point = m_pConnectors->take();
        }

        delete m_pConnectors;
        m_pConnectors = NULL;
    }
}


/**
 * Loads this object from an XML file.
 */
bool KivioConnectorTarget::loadXML( const QDomElement &e )
{
    if( e.tagName().compare( "KivioConnectorTarget" ) != 0 )
    {
       kdDebug(43000) << "Attempted to load KivioConnectorTarget from non-KivioConnectorTarget element" << endl;
        return false;
    }

    m_position.setX(XmlReadFloat( e, "x", 1.0f ));
    m_position.setY(XmlReadFloat( e, "y", 1.0f ));

    m_id = XmlReadInt( e, "id", -1 );

    return true;
}


/**
 * Saves this object to an XML file.
 */
QDomElement KivioConnectorTarget::saveXML( QDomDocument &doc )
{
    QDomElement e;

    e = doc.createElement("KivioConnectorTarget");

    XmlWriteFloat( e, "x", m_position.x() );
    XmlWriteFloat( e, "y", m_position.y() );

    if( m_id != -1 )
        XmlWriteInt( e, "id", m_id );

    return e;
}


/**
 * Adds a KivioConnectorPoint to the list of connections.
 */
void KivioConnectorTarget::addConnectorPointToList( KivioConnectorPoint *p )
{
    if( p )
        m_pConnectors->append(p);
}


/**
 * Removes a KivioConnectorPoint from the list of connections.
 *
 * Removes a KivioConnectorPoint from the list of connections. Note, it does
 * not disconnect the point on the KivioConnectorPoint side.  This function
 * is mainly used by KivioConnectorPoint, and probably shouldn't be used by
 * anything/one else unless you really know what you are doing.
 */
bool KivioConnectorTarget::removeConnectorPointFromList( KivioConnectorPoint *p )
{
    if( !p )
        return false;


    return m_pConnectors->remove(p);
}


/**
 * Sets a new position and updates all connected points.
 */
void KivioConnectorTarget::setX( float _x )
{
    m_position.setX(_x);

    KivioConnectorPoint *pPoint = m_pConnectors->first();
    while( pPoint )
    {
        pPoint->setX( _x, true );

        pPoint = m_pConnectors->next();
    }
}


/**
 * Sets a new position and updates all connected points.
 */

void KivioConnectorTarget::setY( float _y )
{
    m_position.setY(_y);

    KivioConnectorPoint *pPoint = m_pConnectors->first();
    while( pPoint )
    {
        pPoint->setY( _y, true );

        pPoint = m_pConnectors->next();
    }
}


/**
 * Sets a new position and updates all connected points.
 */

void KivioConnectorTarget::setPosition( float _x, float _y )
{
    m_position.setX( _x );
    m_position.setY( _y );

    KivioConnectorPoint *pPoint = m_pConnectors->first();
    while( pPoint )
    {
        pPoint->setPosition( _x, _y, true );

        pPoint = m_pConnectors->next();
    }
}


/**
 * Issues paintOutline requests to all connected stencils.
 */
void KivioConnectorTarget::paintOutline( KivioIntraStencilData *pData )
{
    KivioConnectorPoint *pPoint;
    KivioStencil *pStencil;

    pPoint = m_pConnectors->first();
    while( pPoint )
    {
        pStencil = pPoint->stencil();

        if( pStencil )
            pStencil->paintOutline( pData );

        pPoint = m_pConnectors->next();
    }
}


/**
 * Tests is this target has any connections.
 */
bool KivioConnectorTarget::hasConnections()
{
    KivioConnectorPoint *pPoint = m_pConnectors->first();

    if( pPoint )
        return true;

    return false;
}


/**
 * Sets this target's unique ID, for generateIds() during a saveXML().
 */
void KivioConnectorTarget::setId( int i )
{
    KivioConnectorPoint *pPoint = m_pConnectors->first();
    m_id= i;

    while( pPoint )
    {
        pPoint->setTargetId( i );

        pPoint = m_pConnectors->next();
    }
}

void KivioConnectorTarget::setOffsets(double x, double y)
{
  m_xOffset = x;
  m_yOffset = y;
}