summaryrefslogtreecommitdiffstats
path: root/kivio/kiviopart/kiviosdk/kivio_connector_point.cpp
blob: d408bee645fd0d38a1239df541815c418fd0c861 (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
/*
 * 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_stencil.h"

KivioConnectorPoint::KivioConnectorPoint()
{
    m_pTarget = 0L;
    m_pStencil = 0L;
    m_targetId = -1;
    m_connectable = true;
}

KivioConnectorPoint::KivioConnectorPoint( KivioStencil *pParent, bool conn )
{
    m_pTarget = 0L;
    m_pStencil = pParent;
    m_targetId = -1;
    m_connectable = conn;
}

KivioConnectorPoint::~KivioConnectorPoint()
{
    if( m_pTarget )
    {
        m_pTarget->removeConnectorPointFromList( this );
        m_pTarget = 0L;
    }

    m_pStencil = 0L;
}

/**
 * Sets the target to a value.
 *
 * @param pTarget The target to connect to
 *
 * This sets the KivioConnectorTarget we are attached to.  In addition to
 * setting the pointer, it also tells the target to add this as a connected
 * point.
 */
void KivioConnectorPoint::setTarget( KivioConnectorTarget *pTarget  )
{
   if( m_connectable == false )
   {
      return;
   }

    if( m_pTarget )
    {
         m_pTarget->removeConnectorPointFromList( this );
    }


    m_pTarget = pTarget;

    m_pos.setX(pTarget->x());
    m_pos.setY(pTarget->y());

    m_pTarget->addConnectorPointToList( this );
}


/**
 * Sets the x coordinate.
 *
 * @param newX The new value
 * @param updateStencil Whether or not the stencil's geometry should be updated (def=true)
 *
 * Sets the X coordinate to a new value, and optionally
 * updates the stencil's geometry if updateStencil is true.
 */
void KivioConnectorPoint::setX( double newX, bool updateStencil )
{
  double oldX = m_pos.x();
  m_pos.setX(newX);

  if( updateStencil && m_pStencil )
    m_pStencil->updateConnectorPoints(this, oldX, m_pos.y());
}


/**
 * Sets the y coordinate.
 *
 * @param newY The new value
 * @param updateStencil Whether or not the stencil's geometry should be updated (def=true)
 *
 * Sets the Y coordinate to a new value, and optionally
 * updates the stencil's geometry if updateStencil is true.
 */
void KivioConnectorPoint::setY( double newY, bool updateStencil )
{
   double oldY = m_pos.y();
    m_pos.setY(newY);

    if( updateStencil && m_pStencil )
        m_pStencil->updateConnectorPoints(this, m_pos.x(), oldY);
}


/**
 * Sets the coordinates.
 *
 * @param newX The new x value
 * @param newY The new y value
 * @param updateStencil Whether or not the stencil's geometry should be updated (def=true)
 *
 * Sets the X & Y coordinates to new values, and optionally
 * updates the stencil's geometry if updateStencil is true.
 */
 void KivioConnectorPoint::setPosition( double newX, double newY, bool updateStencil )
{
  double oldX = m_pos.x();
  double oldY = m_pos.y();

  m_pos.setCoords(newX, newY);

  if( updateStencil && m_pStencil )
    m_pStencil->updateConnectorPoints(this, oldX, oldY);
}


/**
 * Disconnects from the target, with optional behavior.
 *
 * @param removeFromTargetList Whether we should tell the target to remove us from it's list or not. (Default=true).
 *
 * Call this function if you want this connector to disconnect itself from it's target.  If removeFromTargetList is
 * true, then we call KivioConnectorTarget::removeConnectorPointFromList() which causes the target to look for
 * us in his list of connections and remove us.
 */
void KivioConnectorPoint::disconnect( bool removeFromTargetList )
{
    if( m_pTarget )
    {
        if( removeFromTargetList == true )
            m_pTarget->removeConnectorPointFromList(this);

        m_pTarget = 0L;
        m_targetId = -1;
    }

}


/**
 * Loads this object from an XML file.
 *
 * @param e The TQDomElement to load from.
 *
 * This will load the necessary values from an XML file.
 */
bool KivioConnectorPoint::loadXML( const TQDomElement &e )
{
    m_pos.setX(XmlReadFloat( e, "x", 1.0f ));
    m_pos.setY(XmlReadFloat( e, "y", 1.0f ));
    m_targetId = XmlReadInt( e, "targetId", -1 );
    m_connectable = (bool)XmlReadInt( e, "connectable", (int)true );

    return true;
}


/**
 * Saves this object to an XML file.
 *
 * @param doc The TQDomDocument to save to.
 *
 * This creates a new TQDomElement and saves its data to it.
 *
 * @returns The new element created and saved to.
 */
TQDomElement KivioConnectorPoint::saveXML( TQDomDocument &doc )
{
    TQDomElement e = doc.createElement("KivioConnectorPoint");

    XmlWriteFloat( e, "x", m_pos.x() );
    XmlWriteFloat( e, "y", m_pos.y() );
    XmlWriteInt( e, "connectable", (int)m_connectable );

    if( m_targetId != -1 )
        XmlWriteInt( e, "targetId", m_targetId );

    return e;
}

void KivioConnectorPoint::moveBy( double _x, double _y, bool updateStencil )
{
   m_pos += KoPoint(_x, _y);

  if( updateStencil && m_pStencil )
    m_pStencil->updateConnectorPoints(this, _x, _y);
}

bool KivioConnectorPoint::isConnected()
{
  return (m_pTarget != 0L);
}