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 <cstring>
14 #include <string>
15 #include <iostream>
16 #include <glibmm/stringutils.h>
18 #include "attributes.h"
19 #include "sp-conn-end.h"
20 #include "uri.h"
21 #include "display/curve.h"
22 #include "xml/repr.h"
23 #include "sp-path.h"
24 #include "libavoid/vertices.h"
25 #include "libavoid/router.h"
26 #include "document.h"
27 #include "sp-item-group.h"
30 SPConnEndPair::SPConnEndPair(SPPath *const owner)
31 : _path(owner)
32 , _connRef(NULL)
33 , _connType(SP_CONNECTOR_NOAVOID)
34 , _connCurvature(0.0)
35 , _transformed_connection()
36 {
37 for (unsigned handle_ix = 0; handle_ix <= 1; ++handle_ix) {
38 this->_connEnd[handle_ix] = new SPConnEnd(SP_OBJECT(owner));
39 this->_connEnd[handle_ix]->_changed_connection
40 = this->_connEnd[handle_ix]->ref.changedSignal()
41 .connect(sigc::bind(sigc::ptr_fun(sp_conn_end_href_changed),
42 this->_connEnd[handle_ix], owner, handle_ix));
43 }
44 }
46 SPConnEndPair::~SPConnEndPair()
47 {
48 for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
49 delete this->_connEnd[handle_ix];
50 this->_connEnd[handle_ix] = NULL;
51 }
52 }
54 void
55 SPConnEndPair::release()
56 {
57 for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
58 this->_connEnd[handle_ix]->_changed_connection.disconnect();
59 this->_connEnd[handle_ix]->_delete_connection.disconnect();
60 this->_connEnd[handle_ix]->_transformed_connection.disconnect();
61 g_free(this->_connEnd[handle_ix]->href);
62 this->_connEnd[handle_ix]->href = NULL;
63 this->_connEnd[handle_ix]->ref.detach();
64 }
66 // If the document is being destroyed then the router instance
67 // and the ConnRefs will have been destroyed with it.
68 const bool routerInstanceExists = (_path->document->router != NULL);
70 if (_connRef && routerInstanceExists) {
71 _connRef->removeFromGraph();
72 delete _connRef;
73 }
74 _connRef = NULL;
76 _transformed_connection.disconnect();
77 }
79 void
80 sp_conn_end_pair_build(SPObject *object)
81 {
82 sp_object_read_attr(object, "inkscape:connector-type");
83 sp_object_read_attr(object, "inkscape:connection-start");
84 sp_object_read_attr(object, "inkscape:connection-end");
85 sp_object_read_attr(object, "inkscape:connector-curvature");
86 }
89 static void
90 avoid_conn_transformed(Geom::Matrix const */*mp*/, SPItem *moved_item)
91 {
92 SPPath *path = SP_PATH(moved_item);
93 if (path->connEndPair.isAutoRoutingConn()) {
94 path->connEndPair.tellLibavoidNewEndpoints();
95 }
96 }
99 void
100 SPConnEndPair::setAttr(unsigned const key, gchar const *const value)
101 {
102 if (key == SP_ATTR_CONNECTOR_TYPE) {
103 if (value && (strcmp(value, "polyline") == 0 || strcmp(value, "orthogonal") == 0)) {
104 int newconnType = strcmp(value, "polyline") ? SP_CONNECTOR_ORTHOGONAL : SP_CONNECTOR_POLYLINE;
106 if (!_connRef)
107 {
108 _connType = newconnType;
109 Avoid::Router *router = _path->document->router;
110 GQuark itemID = g_quark_from_string(SP_OBJECT(_path)->id);
111 _connRef = new Avoid::ConnRef(router, itemID);
112 switch (newconnType)
113 {
114 case SP_CONNECTOR_POLYLINE:
115 _connRef->setRoutingType(Avoid::ConnType_PolyLine);
116 break;
117 case SP_CONNECTOR_ORTHOGONAL:
118 _connRef->setRoutingType(Avoid::ConnType_Orthogonal);
119 }
120 _transformed_connection = _path->connectTransformed(
121 sigc::ptr_fun(&avoid_conn_transformed));
122 }
123 else
124 if (newconnType != _connType)
125 {
126 _connType = newconnType;
127 switch (newconnType)
128 {
129 case SP_CONNECTOR_POLYLINE:
130 _connRef->setRoutingType(Avoid::ConnType_PolyLine);
131 break;
132 case SP_CONNECTOR_ORTHOGONAL:
133 _connRef->setRoutingType(Avoid::ConnType_Orthogonal);
134 }
135 sp_conn_reroute_path(_path);
136 }
137 }
138 else {
139 _connType = SP_CONNECTOR_NOAVOID;
141 if (_connRef) {
142 _connRef->removeFromGraph();
143 delete _connRef;
144 _connRef = NULL;
145 _transformed_connection.disconnect();
146 }
147 }
148 return;
149 }
150 else if (key == SP_ATTR_CONNECTOR_CURVATURE) {
151 if (value) {
152 _connCurvature = g_strtod(value, NULL);
153 if (_connRef && _connRef->isInitialised()) {
154 // Redraw the connector, but only if it has been initialised.
155 sp_conn_reroute_path(_path);
156 }
157 }
158 return;
159 }
161 unsigned const handle_ix = key - SP_ATTR_CONNECTION_START;
162 g_assert( handle_ix <= 1 );
163 this->_connEnd[handle_ix]->setAttacherHref(value, _path);
164 }
166 void
167 SPConnEndPair::writeRepr(Inkscape::XML::Node *const repr) const
168 {
169 for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
170 if (this->_connEnd[handle_ix]->ref.getURI()) {
171 char const * const attr_strs[] = {"inkscape:connection-start",
172 "inkscape:connection-end"};
173 std::ostringstream ostr;
174 ostr<<this->_connEnd[handle_ix]->ref.getURI()->toString()<<"_"<<
175 (this->_connEnd[handle_ix]->type == ConnPointDefault ? "d":"u") <<
176 "_" << this->_connEnd[handle_ix]->id;
179 repr->setAttribute(attr_strs[handle_ix], ostr.str().c_str());
180 }
181 }
182 repr->setAttribute("inkscape:connector-curvature", Glib::Ascii::dtostr(_connCurvature).c_str());
183 if (_connType == SP_CONNECTOR_POLYLINE || _connType == SP_CONNECTOR_ORTHOGONAL)
184 repr->setAttribute("inkscape:connector-type", _connType == SP_CONNECTOR_POLYLINE ? "polyline" : "orthogonal" );
185 }
187 void
188 SPConnEndPair::getAttachedItems(SPItem *h2attItem[2]) const {
189 for (unsigned h = 0; h < 2; ++h) {
190 h2attItem[h] = this->_connEnd[h]->ref.getObject();
192 // Deal with the case of the attached object being an empty group.
193 // A group containing no items does not have a valid bbox, so
194 // causes problems for the auto-routing code. Also, since such a
195 // group no longer has an onscreen representation and can only be
196 // selected through the XML editor, it makes sense just to detach
197 // connectors from them.
198 if (SP_IS_GROUP(h2attItem[h])) {
199 if (SP_GROUP(h2attItem[h])->group->getItemCount() == 0) {
200 // This group is empty, so detach.
201 sp_conn_end_detach(_path, h);
202 h2attItem[h] = NULL;
203 }
204 }
205 }
206 }
208 void
209 SPConnEndPair::getEndpoints(Geom::Point endPts[]) const {
210 SPCurve *curve = _path->original_curve ? _path->original_curve : _path->curve;
211 SPItem *h2attItem[2];
212 getAttachedItems(h2attItem);
214 for (unsigned h = 0; h < 2; ++h) {
215 if ( h2attItem[h] ) {
216 g_assert(h2attItem[h]->avoidRef);
217 endPts[h] = h2attItem[h]->avoidRef->getConnectionPointPos(_connEnd[h]->type, _connEnd[h]->id);
218 }
219 else
220 {
221 if (h == 0) {
222 endPts[h] = *(curve->first_point());
223 }
224 else {
225 endPts[h] = *(curve->last_point());
226 }
227 }
228 }
229 }
231 gdouble
232 SPConnEndPair::getCurvature(void) const {
233 return _connCurvature;
234 }
236 SPConnEnd**
237 SPConnEndPair::getConnEnds(void)
238 {
239 return _connEnd;
240 }
242 bool
243 SPConnEndPair::isOrthogonal(void) const {
244 return _connType == SP_CONNECTOR_ORTHOGONAL;
245 }
248 static void redrawConnectorCallback(void *ptr)
249 {
250 SPPath *path = SP_PATH(ptr);
251 if (path->document == NULL) {
252 // This can happen when the document is being destroyed.
253 return;
254 }
255 sp_conn_redraw_path(path);
256 }
258 void
259 SPConnEndPair::rerouteFromManipulation(void)
260 {
261 sp_conn_reroute_path_immediate(_path);
262 }
265 // Called from sp_path_update to initialise the endpoints.
266 void
267 SPConnEndPair::update(void)
268 {
269 if (_connType != SP_CONNECTOR_NOAVOID) {
270 g_assert(_connRef != NULL);
271 if (!(_connRef->isInitialised())) {
272 Geom::Point endPt[2];
273 getEndpoints(endPt);
275 Avoid::Point src(endPt[0][Geom::X], endPt[0][Geom::Y]);
276 Avoid::Point dst(endPt[1][Geom::X], endPt[1][Geom::Y]);
278 _connRef->setEndpoints(src, dst);
279 _connRef->setCallback(&redrawConnectorCallback, _path);
280 }
281 // Store the ID of the objects attached to the connector.
282 storeIds();
283 }
284 }
287 void SPConnEndPair::storeIds(void)
288 {
289 if (_connEnd[0]->href) {
290 gchar ** href_strarray = NULL;
291 href_strarray = g_strsplit(_connEnd[0]->href, "_", 0);
292 // href begins with a '#' which we don't want.
293 const char *startId = href_strarray[0] + 1;
294 GQuark itemId = g_quark_from_string(startId);
295 _connRef->setEndPointId(Avoid::VertID::src, itemId);
296 g_strfreev(href_strarray);
297 }
298 else {
299 _connRef->setEndPointId(Avoid::VertID::src, 0);
300 }
301 if (_connEnd[1]->href) {
302 gchar ** href_strarray = NULL;
303 href_strarray = g_strsplit(_connEnd[1]->href, "_", 0);
304 // href begins with a '#' which we don't want.
305 const char *endId = href_strarray[0] + 1;
306 GQuark itemId = g_quark_from_string(endId);
307 _connRef->setEndPointId(Avoid::VertID::tar, itemId);
308 g_strfreev(href_strarray);
309 }
310 else {
311 _connRef->setEndPointId(Avoid::VertID::tar, 0);
312 }
313 }
316 bool
317 SPConnEndPair::isAutoRoutingConn(void)
318 {
319 if (_connType != SP_CONNECTOR_NOAVOID) {
320 return true;
321 }
322 return false;
323 }
325 void
326 SPConnEndPair::makePathInvalid(void)
327 {
328 _connRef->makePathInvalid();
329 }
332 // Redraws the curve along the recalculated route
333 // Straight or curved
334 void recreateCurve(SPCurve *curve, Avoid::ConnRef *connRef, const gdouble curvature)
335 {
336 bool straight = curvature<1e-3;
338 Avoid::PolyLine route = connRef->displayRoute();
339 if (!straight)
340 route = route.curvedPolyline(curvature);
341 connRef->calcRouteDist();
343 curve->reset();
345 curve->moveto( Geom::Point(route.ps[0].x, route.ps[0].y) );
346 int pn = route.size();
347 for (int i = 1; i < pn; ++i) {
348 Geom::Point p(route.ps[i].x, route.ps[i].y);
349 if (straight) {
350 curve->lineto( p );
351 }
352 else {
353 switch (route.ts[i]) {
354 case 'M':
355 curve->moveto( p );
356 break;
357 case 'L':
358 curve->lineto( p );
359 break;
360 case 'C':
361 g_assert( i+2<pn );
362 curve->curveto( p, Geom::Point(route.ps[i+1].x, route.ps[i+1].y),
363 Geom::Point(route.ps[i+2].x, route.ps[i+2].y) );
364 i+=2;
365 break;
366 }
367 }
368 }
369 }
372 void
373 SPConnEndPair::tellLibavoidNewEndpoints(const bool processTransaction)
374 {
375 if (!isAutoRoutingConn()) {
376 // Do nothing
377 return;
378 }
379 makePathInvalid();
381 Geom::Point endPt[2];
382 getEndpoints(endPt);
384 Avoid::Point src(endPt[0][Geom::X], endPt[0][Geom::Y]);
385 Avoid::Point dst(endPt[1][Geom::X], endPt[1][Geom::Y]);
387 _connRef->setEndpoints(src, dst);
388 if (processTransaction)
389 {
390 _connRef->router()->processTransaction();
391 }
392 return;
393 }
396 bool
397 SPConnEndPair::reroutePathFromLibavoid(void)
398 {
399 if (!isAutoRoutingConn()) {
400 // Do nothing
401 return false;
402 }
404 SPCurve *curve = _path->original_curve ?_path->original_curve : _path->curve;
406 recreateCurve( curve, _connRef, _connCurvature );
408 Geom::Matrix doc2item = sp_item_i2doc_affine(SP_ITEM(_path)).inverse();
409 curve->transform(doc2item);
411 return true;
412 }
415 /*
416 Local Variables:
417 mode:c++
418 c-file-style:"stroustrup"
419 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
420 indent-tabs-mode:nil
421 fill-column:99
422 End:
423 */
424 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :