2 #include "display/curve.h"
3 #include "libnr/nr-matrix-div.h"
4 #include "libnr/nr-matrix-fns.h"
5 #include "xml/repr.h"
6 #include "sp-conn-end.h"
7 #include "sp-path.h"
8 #include "uri.h"
9 #include "document.h"
12 static void change_endpts(SPCurve *const curve, NR::Point const h2endPt[2]);
13 static NR::Point calc_bbox_conn_pt(NR::Rect const &bbox, NR::Point const &p);
14 static double signed_one(double const x);
16 SPConnEnd::SPConnEnd(SPObject *const owner) :
17 ref(owner),
18 href(NULL),
19 _changed_connection(),
20 _delete_connection(),
21 _transformed_connection()
22 {
23 }
25 static SPObject const *
26 get_nearest_common_ancestor(SPObject const *const obj, SPItem const *const objs[2]) {
27 SPObject const *anc_sofar = obj;
28 for (unsigned i = 0; i < 2; ++i) {
29 if ( objs[i] != NULL ) {
30 anc_sofar = anc_sofar->nearestCommonAncestor(objs[i]);
31 }
32 }
33 return anc_sofar;
34 }
36 static void
37 sp_conn_end_move_compensate(NR::Matrix const *mp, SPItem *moved_item,
38 SPPath *const path,
39 bool const updatePathRepr = true)
40 {
41 // TODO: SPItem::getBounds gives the wrong result for some objects
42 // that have internal representations that are updated later
43 // by the sp_*_update functions, e.g., text.
44 sp_document_ensure_up_to_date(path->document);
46 // Get the new route around obstacles.
47 path->connEndPair.reroutePath();
49 SPItem *h2attItem[2];
50 path->connEndPair.getAttachedItems(h2attItem);
51 if ( !h2attItem[0] && !h2attItem[1] ) {
52 if (updatePathRepr) {
53 path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
54 path->updateRepr();
55 }
56 return;
57 }
59 SPItem const *const path_item = SP_ITEM(path);
60 SPObject const *const ancestor = get_nearest_common_ancestor(path_item, h2attItem);
61 NR::Matrix const path2anc(i2anc_affine(path_item, ancestor));
63 if (h2attItem[0] != NULL && h2attItem[1] != NULL) {
64 /* Initial end-points: centre of attached object. */
65 NR::Point h2endPt_icoordsys[2];
66 NR::Matrix h2i2anc[2];
67 NR::Rect h2bbox_icoordsys[2];
68 NR::Point last_seg_endPt[2] = {
69 sp_curve_second_point(path->curve),
70 sp_curve_penultimate_point(path->curve)
71 };
72 for (unsigned h = 0; h < 2; ++h) {
73 NR::Maybe<NR::Rect> bbox = h2attItem[h]->getBounds(NR::identity());
74 if (!bbox) {
75 if (updatePathRepr) {
76 path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
77 path->updateRepr();
78 }
79 return;
80 }
81 h2bbox_icoordsys[h] = *bbox;
82 h2i2anc[h] = i2anc_affine(h2attItem[h], ancestor);
83 h2endPt_icoordsys[h] = h2bbox_icoordsys[h].midpoint();
84 }
86 // For each attached object, change the corresponding point to be
87 // on the edge of the bbox.
88 NR::Point h2endPt_pcoordsys[2];
89 for (unsigned h = 0; h < 2; ++h) {
90 h2endPt_icoordsys[h] = calc_bbox_conn_pt(h2bbox_icoordsys[h],
91 ( last_seg_endPt[h] / h2i2anc[h] ));
92 h2endPt_pcoordsys[h] = h2endPt_icoordsys[h] * h2i2anc[h] / path2anc;
93 }
94 change_endpts(path->curve, h2endPt_pcoordsys);
95 } else {
96 // We leave the unattached endpoint where it is, and adjust the
97 // position of the attached endpoint to be on the edge of the bbox.
98 unsigned ind;
99 NR::Point other_endpt;
100 NR::Point last_seg_pt;
101 if (h2attItem[0] != NULL) {
102 other_endpt = sp_curve_last_point(path->curve);
103 last_seg_pt = sp_curve_second_point(path->curve);
104 ind = 0;
105 }
106 else {
107 other_endpt = sp_curve_first_point(path->curve);
108 last_seg_pt = sp_curve_penultimate_point(path->curve);
109 ind = 1;
110 }
111 NR::Point h2endPt_icoordsys[2];
112 NR::Matrix h2i2anc;
114 NR::Rect otherpt_rect = NR::Rect(other_endpt, other_endpt);
115 NR::Rect h2bbox_icoordsys[2] = { otherpt_rect, otherpt_rect };
116 NR::Maybe<NR::Rect> bbox = h2attItem[ind]->getBounds(NR::identity());
117 if (!bbox) {
118 if (updatePathRepr) {
119 path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
120 path->updateRepr();
121 }
122 return;
123 }
125 h2bbox_icoordsys[ind] = *bbox;
126 h2i2anc = i2anc_affine(h2attItem[ind], ancestor);
127 h2endPt_icoordsys[ind] = h2bbox_icoordsys[ind].midpoint();
129 h2endPt_icoordsys[!ind] = other_endpt;
131 // For the attached object, change the corresponding point to be
132 // on the edge of the bbox.
133 NR::Point h2endPt_pcoordsys[2];
134 h2endPt_icoordsys[ind] = calc_bbox_conn_pt(h2bbox_icoordsys[ind],
135 ( last_seg_pt / h2i2anc ));
136 h2endPt_pcoordsys[ind] = h2endPt_icoordsys[ind] * h2i2anc / path2anc;
138 // Leave the other where it is.
139 h2endPt_pcoordsys[!ind] = other_endpt;
141 change_endpts(path->curve, h2endPt_pcoordsys);
142 }
143 if (updatePathRepr) {
144 path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
145 path->updateRepr();
146 }
147 }
149 // TODO: This triggering of makeInvalidPath could be cleaned up to be
150 // another option passed to move_compensate.
151 static void
152 sp_conn_end_shape_move_compensate(NR::Matrix const *mp, SPItem *moved_item,
153 SPPath *const path)
154 {
155 if (path->connEndPair.isAutoRoutingConn()) {
156 path->connEndPair.makePathInvalid();
157 }
158 sp_conn_end_move_compensate(mp, moved_item, path);
159 }
162 void
163 sp_conn_adjust_invalid_path(SPPath *const path)
164 {
165 sp_conn_end_move_compensate(NULL, NULL, path);
166 }
168 void
169 sp_conn_adjust_path(SPPath *const path)
170 {
171 if (path->connEndPair.isAutoRoutingConn()) {
172 path->connEndPair.makePathInvalid();
173 }
174 // Don't update the path repr or else connector dragging is slowed by
175 // constant update of values to the xml editor, and each step is also
176 // needlessly remembered by undo/redo.
177 bool const updatePathRepr = false;
178 sp_conn_end_move_compensate(NULL, NULL, path, updatePathRepr);
179 }
181 static NR::Point
182 calc_bbox_conn_pt(NR::Rect const &bbox, NR::Point const &p)
183 {
184 using NR::X;
185 using NR::Y;
186 NR::Point const ctr(bbox.midpoint());
187 NR::Point const lengths(bbox.dimensions());
188 if ( ctr == p ) {
189 /* Arbitrarily choose centre of right edge. */
190 return NR::Point(ctr[X] + .5 * lengths[X],
191 ctr[Y]);
192 }
193 NR::Point const cp( p - ctr );
194 NR::Dim2 const edgeDim = ( ( fabs(lengths[Y] * cp[X]) <
195 fabs(lengths[X] * cp[Y]) )
196 ? Y
197 : X );
198 NR::Dim2 const otherDim = (NR::Dim2) !edgeDim;
199 NR::Point offset;
200 offset[edgeDim] = (signed_one(cp[edgeDim])
201 * lengths[edgeDim]);
202 offset[otherDim] = (lengths[edgeDim]
203 * cp[otherDim]
204 / fabs(cp[edgeDim]));
205 g_assert((offset[otherDim] >= 0) == (cp[otherDim] >= 0));
206 #ifndef NDEBUG
207 for (unsigned d = 0; d < 2; ++d) {
208 g_assert(fabs(offset[d]) <= lengths[d] + .125);
209 }
210 #endif
211 return ctr + .5 * offset;
212 }
214 static double signed_one(double const x)
215 {
216 return (x < 0
217 ? -1.
218 : 1.);
219 }
221 static void
222 change_endpts(SPCurve *const curve, NR::Point const h2endPt[2])
223 {
224 #if 0
225 sp_curve_reset(curve);
226 sp_curve_moveto(curve, h2endPt[0]);
227 sp_curve_lineto(curve, h2endPt[1]);
228 #else
229 sp_curve_move_endpoints(curve, h2endPt[0], h2endPt[1]);
230 #endif
231 }
233 static void
234 sp_conn_end_deleted(SPObject *, SPObject *const owner, unsigned const handle_ix)
235 {
236 // todo: The first argument is the deleted object, or just NULL if
237 // called by sp_conn_end_detach.
238 g_return_if_fail(handle_ix < 2);
239 char const *const attr_str[] = {"inkscape:connection-start",
240 "inkscape:connection-end"};
241 SP_OBJECT_REPR(owner)->setAttribute(attr_str[handle_ix], NULL);
242 /* I believe this will trigger sp_conn_end_href_changed. */
243 }
245 void
246 sp_conn_end_detach(SPObject *const owner, unsigned const handle_ix)
247 {
248 sp_conn_end_deleted(NULL, owner, handle_ix);
249 }
251 void
252 SPConnEnd::setAttacherHref(gchar const *value)
253 {
254 if ( value && href && ( strcmp(value, href) == 0 ) ) {
255 /* No change, do nothing. */
256 } else {
257 g_free(href);
258 href = NULL;
259 if (value) {
260 // First, set the href field, because sp_conn_end_href_changed will need it.
261 href = g_strdup(value);
263 // Now do the attaching, which emits the changed signal.
264 try {
265 ref.attach(Inkscape::URI(value));
266 } catch (Inkscape::BadURIException &e) {
267 /* TODO: Proper error handling as per
268 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. (Also needed for
269 * sp-use.) */
270 g_warning("%s", e.what());
271 ref.detach();
272 }
273 } else {
274 ref.detach();
275 }
276 }
277 }
279 void
280 sp_conn_end_href_changed(SPObject *old_ref, SPObject *ref,
281 SPConnEnd *connEndPtr, SPPath *const path, unsigned const handle_ix)
282 {
283 g_return_if_fail(connEndPtr != NULL);
284 SPConnEnd &connEnd = *connEndPtr;
285 connEnd._delete_connection.disconnect();
286 connEnd._transformed_connection.disconnect();
288 if (connEnd.href) {
289 SPObject *refobj = connEnd.ref.getObject();
290 if (refobj) {
291 connEnd._delete_connection
292 = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_conn_end_deleted),
293 SP_OBJECT(path), handle_ix));
294 connEnd._transformed_connection
295 = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_conn_end_shape_move_compensate),
296 path));
297 }
298 }
299 }
303 /*
304 Local Variables:
305 mode:c++
306 c-file-style:"stroustrup"
307 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
308 indent-tabs-mode:nil
309 fill-column:99
310 End:
311 */
312 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :