Code

a1c4697e55007eecbd85d90faa8ee94354303e5a
[inkscape.git] / src / sp-conn-end-pair.cpp
1 /*
2  * A class for handling connector endpoint movement and libavoid interaction.
3  *
4  * Authors:
5  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
6  *   Michael Wybrow <mjwybrow@users.sourceforge.net>
7  *
8  *    * Copyright (C) 2004-2005 Monash University
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include "attributes.h"
14 #include "sp-conn-end.h"
15 #include "uri.h"
16 #include "display/curve.h"
17 #include "xml/repr.h"
18 #include "sp-path.h"
19 #include "libavoid/vertices.h"
20 #include "libavoid/router.h"
21 #include "document.h"
24 SPConnEndPair::SPConnEndPair(SPPath *const owner)
25     : _invalid_path_connection()
26     , _path(owner)
27     , _connRef(NULL)
28     , _connType(SP_CONNECTOR_NOAVOID)
29     , _transformed_connection()
30 {
31     for (unsigned handle_ix = 0; handle_ix <= 1; ++handle_ix) {
32         this->_connEnd[handle_ix] = new SPConnEnd(SP_OBJECT(owner));
33         this->_connEnd[handle_ix]->_changed_connection
34             = this->_connEnd[handle_ix]->ref.changedSignal()
35             .connect(sigc::bind(sigc::ptr_fun(sp_conn_end_href_changed),
36                                 this->_connEnd[handle_ix], owner, handle_ix));
37     }
38 }
40 SPConnEndPair::~SPConnEndPair()
41 {
42     for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
43         delete this->_connEnd[handle_ix];
44         this->_connEnd[handle_ix] = NULL;
45     }
46     if (_connRef) {
47         _connRef->removeFromGraph();
48         delete _connRef;
49         _connRef = NULL;
50     }
52     _invalid_path_connection.disconnect();
53     _transformed_connection.disconnect();
54 }
56 void
57 SPConnEndPair::release()
58 {
59     for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
60         this->_connEnd[handle_ix]->_changed_connection.disconnect();
61         this->_connEnd[handle_ix]->_delete_connection.disconnect();
62         this->_connEnd[handle_ix]->_transformed_connection.disconnect();
63         g_free(this->_connEnd[handle_ix]->href);
64         this->_connEnd[handle_ix]->href = NULL;
65         this->_connEnd[handle_ix]->ref.detach();
66     }
67 }
69 void
70 sp_conn_end_pair_build(SPObject *object)
71 {
72     sp_object_read_attr(object, "inkscape:connector-type");
73     sp_object_read_attr(object, "inkscape:connection-start");
74     sp_object_read_attr(object, "inkscape:connection-end");
75 }
78 static void
79 avoid_conn_move(NR::Matrix const *mp, SPItem *moved_item)
80 {
81     // Reroute connector
82     SPPath *path = SP_PATH(moved_item);
83     path->connEndPair.makePathInvalid();
84     sp_conn_adjust_invalid_path(path);
85 }
88 void
89 SPConnEndPair::setAttr(unsigned const key, gchar const *const value)
90 {
91     if (key == SP_ATTR_CONNECTOR_TYPE) {
92         if (value && (strcmp(value, "polyline") == 0)) {
93             _connType = SP_CONNECTOR_POLYLINE;
95             Avoid::Router *router = _path->document->router;
96             GQuark itemID = g_quark_from_string(SP_OBJECT(_path)->id);
97             _connRef = new Avoid::ConnRef(router, itemID);
98             _invalid_path_connection = connectInvalidPath(
99                     sigc::ptr_fun(&sp_conn_adjust_invalid_path));
100             _transformed_connection = _path->connectTransformed(
101                     sigc::ptr_fun(&avoid_conn_move));
102         }
103         else {
104             _connType = SP_CONNECTOR_NOAVOID;
106             if (_connRef) {
107                 _connRef->removeFromGraph();
108                 delete _connRef;
109                 _connRef = NULL;
110                 _invalid_path_connection.disconnect();
111                 _transformed_connection.disconnect();
112             }
113         }
114         return;
116     }
118     unsigned const handle_ix = key - SP_ATTR_CONNECTION_START;
119     g_assert( handle_ix <= 1 );
120     this->_connEnd[handle_ix]->setAttacherHref(value);
123 void
124 SPConnEndPair::writeRepr(Inkscape::XML::Node *const repr) const
126     for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
127         if (this->_connEnd[handle_ix]->ref.getURI()) {
128             char const * const attr_strs[] = {"inkscape:connection-start",
129                                               "inkscape:connection-end"};
130             gchar *uri_string = this->_connEnd[handle_ix]->ref.getURI()->toString();
131             repr->setAttribute(attr_strs[handle_ix], uri_string);
132             g_free(uri_string);
133         }
134     }
137 void
138 SPConnEndPair::getAttachedItems(SPItem *h2attItem[2]) const {
139     for (unsigned h = 0; h < 2; ++h) {
140         h2attItem[h] = this->_connEnd[h]->ref.getObject();
141     }
144 void
145 SPConnEndPair::getEndpoints(NR::Point endPts[]) const {
146     SPCurve *curve = _path->curve;
147     SPItem *h2attItem[2];
148     getAttachedItems(h2attItem);
150     for (unsigned h = 0; h < 2; ++h) {
151         if ( h2attItem[h] ) {
152             NR::Rect const bbox = h2attItem[h]->invokeBbox(sp_item_i2doc_affine(h2attItem[h]));
153             endPts[h] = bbox.midpoint();
154         }
155         else
156         {
157             if (h == 0) {
158                 endPts[h] = sp_curve_first_point(curve);
159             }
160             else {
161                 endPts[h] = sp_curve_last_point(curve);
162             }
163         }
164     }
167 sigc::connection
168 SPConnEndPair::connectInvalidPath(sigc::slot<void, SPPath *> slot)
170     return _invalid_path_signal.connect(slot);
173 static void emitPathInvalidationNotification(void *ptr)
175     // We emit a signal here rather than just calling the reroute function
176     // since this allows all the movement action computation to happen,
177     // then all connectors (that require it) will be rerouted.  Otherwise,
178     // one connector could get rerouted several times as a result of
179     // dragging a couple of shapes.
181     SPPath *path = SP_PATH(ptr);
182     path->connEndPair._invalid_path_signal.emit(path);
185 void
186 SPConnEndPair::rerouteFromManipulation(void)
188     _connRef->makePathInvalid();
189     sp_conn_adjust_path(_path);
192 void
193 SPConnEndPair::reroute(void)
195     sp_conn_adjust_path(_path);
198 // Called from sp_path_update to initialise the endpoints.
199 void
200 SPConnEndPair::update(void)
202     if (_connType != SP_CONNECTOR_NOAVOID) {
203         g_assert(_connRef != NULL);
204         if (!(_connRef->isInitialised())) {
205             NR::Point endPt[2];
206             getEndpoints(endPt);
208             Avoid::Point src = { endPt[0][NR::X], endPt[0][NR::Y] };
209             Avoid::Point dst = { endPt[1][NR::X], endPt[1][NR::Y] };
211             _connRef->lateSetup(src, dst);
212             _connRef->setCallback(&emitPathInvalidationNotification, _path);
213         }
214         // Store the ID of the objects attached to the connector.
215         storeIds();
216     }
220 void SPConnEndPair::storeIds(void)
222     if (_connEnd[0]->href) {
223         // href begins with a '#' which we don't want.
224         const char *startId = _connEnd[0]->href + 1;
225         GQuark itemId = g_quark_from_string(startId);
226         _connRef->setEndPointId(Avoid::VertID::src, itemId);
227     }
228     else {
229         _connRef->setEndPointId(Avoid::VertID::src, 0);
230     }
231     if (_connEnd[1]->href) {
232         // href begins with a '#' which we don't want.
233         const char *endId = _connEnd[1]->href + 1;
234         GQuark itemId = g_quark_from_string(endId);
235         _connRef->setEndPointId(Avoid::VertID::tar, itemId);
236     }
237     else {
238         _connRef->setEndPointId(Avoid::VertID::tar, 0);
239     }
243 bool
244 SPConnEndPair::isAutoRoutingConn(void)
246     if (_connType != SP_CONNECTOR_NOAVOID) {
247         return true;
248     }
249     return false;
252 void
253 SPConnEndPair::makePathInvalid(void)
255     _connRef->makePathInvalid();
258 void
259 SPConnEndPair::reroutePath(void)
261     if (!isAutoRoutingConn()) {
262         // Do nothing
263         return;
264     }
266     SPCurve *curve = _path->curve;
268     NR::Point endPt[2];
269     getEndpoints(endPt);
271     Avoid::Point src = { endPt[0][NR::X], endPt[0][NR::Y] };
272     Avoid::Point dst = { endPt[1][NR::X], endPt[1][NR::Y] };
274     _connRef->updateEndPoint(Avoid::VertID::src, src);
275     _connRef->updateEndPoint(Avoid::VertID::tar, dst);
277     _connRef->generatePath(src, dst);
279     Avoid::PolyLine route = _connRef->route();
280     _connRef->calcRouteDist();
282     sp_curve_reset(curve);
283     sp_curve_moveto(curve, endPt[0]);
285     for (int i = 1; i < route.pn; ++i) {
286         NR::Point p(route.ps[i].x, route.ps[i].y);
287         sp_curve_lineto(curve, p);
288     }
291 /*
292   Local Variables:
293   mode:c++
294   c-file-style:"stroustrup"
295   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
296   indent-tabs-mode:nil
297   fill-column:99
298   End:
299 */
300 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :