Code

adapt code to new Maybe/bbox regime
[inkscape.git] / src / sp-conn-end.cpp
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                 h2bbox_icoordsys[h] = *bbox;
76             } else {
77                 // FIXME
78                 h2bbox_icoordsys[h] = NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
79             }
80             h2i2anc[h] = i2anc_affine(h2attItem[h], ancestor);
81             h2endPt_icoordsys[h] = h2bbox_icoordsys[h].midpoint();
82         }
84         // For each attached object, change the corresponding point to be
85         // on the edge of the bbox.
86         NR::Point h2endPt_pcoordsys[2];
87         for (unsigned h = 0; h < 2; ++h) {
88             h2endPt_icoordsys[h] = calc_bbox_conn_pt(h2bbox_icoordsys[h],
89                                          ( last_seg_endPt[h] / h2i2anc[h] ));
90             h2endPt_pcoordsys[h] = h2endPt_icoordsys[h] * h2i2anc[h] / path2anc;
91         }
92         change_endpts(path->curve, h2endPt_pcoordsys);
93     } else {
94         // We leave the unattached endpoint where it is, and adjust the
95         // position of the attached endpoint to be on the edge of the bbox.
96         unsigned ind;
97         NR::Point other_endpt;
98         NR::Point last_seg_pt;
99         if (h2attItem[0] != NULL) {
100             other_endpt = sp_curve_last_point(path->curve);
101             last_seg_pt = sp_curve_second_point(path->curve);
102             ind = 0;
103         }
104         else {
105             other_endpt = sp_curve_first_point(path->curve);
106             last_seg_pt = sp_curve_penultimate_point(path->curve);
107             ind = 1;
108         }
109         NR::Point h2endPt_icoordsys[2];
110         NR::Matrix h2i2anc;
112         NR::Rect otherpt_rect = NR::Rect(other_endpt, other_endpt);
113         NR::Rect h2bbox_icoordsys[2] = { otherpt_rect, otherpt_rect };
114         NR::Maybe<NR::Rect> bbox = h2attItem[ind]->getBounds(NR::identity());
115         if (bbox) {
116             h2bbox_icoordsys[ind] = *bbox;
117         } else {
118             // FIXME
119             h2bbox_icoordsys[ind] = NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
120         }
122         h2i2anc = i2anc_affine(h2attItem[ind], ancestor);
123         h2endPt_icoordsys[ind] = h2bbox_icoordsys[ind].midpoint();
125         h2endPt_icoordsys[!ind] = other_endpt;
127         // For the attached object, change the corresponding point to be
128         // on the edge of the bbox.
129         NR::Point h2endPt_pcoordsys[2];
130         h2endPt_icoordsys[ind] = calc_bbox_conn_pt(h2bbox_icoordsys[ind],
131                                                  ( last_seg_pt / h2i2anc ));
132         h2endPt_pcoordsys[ind] = h2endPt_icoordsys[ind] * h2i2anc / path2anc;
134         // Leave the other where it is.
135         h2endPt_pcoordsys[!ind] = other_endpt;
137         change_endpts(path->curve, h2endPt_pcoordsys);
138     }
139     if (updatePathRepr) {
140         path->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
141         path->updateRepr();
142     }
145 // TODO: This triggering of makeInvalidPath could be cleaned up to be
146 //       another option passed to move_compensate.
147 static void
148 sp_conn_end_shape_move_compensate(NR::Matrix const *mp, SPItem *moved_item,
149                             SPPath *const path)
151     if (path->connEndPair.isAutoRoutingConn()) {
152         path->connEndPair.makePathInvalid();
153     }
154     sp_conn_end_move_compensate(mp, moved_item, path);
158 void
159 sp_conn_adjust_invalid_path(SPPath *const path)
161     sp_conn_end_move_compensate(NULL, NULL, path);
164 void
165 sp_conn_adjust_path(SPPath *const path)
167     if (path->connEndPair.isAutoRoutingConn()) {
168         path->connEndPair.makePathInvalid();
169     }
170     // Don't update the path repr or else connector dragging is slowed by
171     // constant update of values to the xml editor, and each step is also
172     // needlessly remembered by undo/redo.
173     bool const updatePathRepr = false;
174     sp_conn_end_move_compensate(NULL, NULL, path, updatePathRepr);
177 static NR::Point
178 calc_bbox_conn_pt(NR::Rect const &bbox, NR::Point const &p)
180     using NR::X;
181     using NR::Y;
182     NR::Point const ctr(bbox.midpoint());
183     NR::Point const lengths(bbox.dimensions());
184     if ( ctr == p ) {
185         /* Arbitrarily choose centre of right edge. */
186         return NR::Point(ctr[X] + .5 * lengths[X],
187                          ctr[Y]);
188     }
189     NR::Point const cp( p - ctr );
190     NR::Dim2 const edgeDim = ( ( fabs(lengths[Y] * cp[X]) <
191                                  fabs(lengths[X] * cp[Y])  )
192                                ? Y
193                                : X );
194     NR::Dim2 const otherDim = (NR::Dim2) !edgeDim;
195     NR::Point offset;
196     offset[edgeDim] = (signed_one(cp[edgeDim])
197                        * lengths[edgeDim]);
198     offset[otherDim] = (lengths[edgeDim]
199                         * cp[otherDim]
200                         / fabs(cp[edgeDim]));
201     g_assert((offset[otherDim] >= 0) == (cp[otherDim] >= 0));
202 #ifndef NDEBUG
203     for (unsigned d = 0; d < 2; ++d) {
204         g_assert(fabs(offset[d]) <= lengths[d] + .125);
205     }
206 #endif
207     return ctr + .5 * offset;
210 static double signed_one(double const x)
212     return (x < 0
213             ? -1.
214             : 1.);
217 static void
218 change_endpts(SPCurve *const curve, NR::Point const h2endPt[2])
220 #if 0
221     sp_curve_reset(curve);
222     sp_curve_moveto(curve, h2endPt[0]);
223     sp_curve_lineto(curve, h2endPt[1]);
224 #else
225     sp_curve_move_endpoints(curve, h2endPt[0], h2endPt[1]);
226 #endif
229 static void
230 sp_conn_end_deleted(SPObject *, SPObject *const owner, unsigned const handle_ix)
232     // todo: The first argument is the deleted object, or just NULL if
233     //       called by sp_conn_end_detach.
234     g_return_if_fail(handle_ix < 2);
235     char const *const attr_str[] = {"inkscape:connection-start",
236                                     "inkscape:connection-end"};
237     SP_OBJECT_REPR(owner)->setAttribute(attr_str[handle_ix], NULL);
238     /* I believe this will trigger sp_conn_end_href_changed. */
241 void
242 sp_conn_end_detach(SPObject *const owner, unsigned const handle_ix)
244     sp_conn_end_deleted(NULL, owner, handle_ix);
247 void
248 SPConnEnd::setAttacherHref(gchar const *value)
250     if ( value && href && ( strcmp(value, href) == 0 ) ) {
251         /* No change, do nothing. */
252     } else {
253         g_free(href);
254         href = NULL;
255         if (value) {
256             // First, set the href field, because sp_conn_end_href_changed will need it.
257             href = g_strdup(value);
259             // Now do the attaching, which emits the changed signal.
260             try {
261                 ref.attach(Inkscape::URI(value));
262             } catch (Inkscape::BadURIException &e) {
263                 /* TODO: Proper error handling as per
264                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.  (Also needed for
265                  * sp-use.) */
266                 g_warning("%s", e.what());
267                 ref.detach();
268             }
269         } else {
270             ref.detach();
271         }
272     }
275 void
276 sp_conn_end_href_changed(SPObject *old_ref, SPObject *ref,
277                          SPConnEnd *connEndPtr, SPPath *const path, unsigned const handle_ix)
279     g_return_if_fail(connEndPtr != NULL);
280     SPConnEnd &connEnd = *connEndPtr;
281     connEnd._delete_connection.disconnect();
282     connEnd._transformed_connection.disconnect();
284     if (connEnd.href) {
285         SPObject *refobj = connEnd.ref.getObject();
286         if (refobj) {
287             connEnd._delete_connection
288                 = SP_OBJECT(refobj)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_conn_end_deleted),
289                                                               SP_OBJECT(path), handle_ix));
290             connEnd._transformed_connection
291                 = SP_ITEM(refobj)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_conn_end_shape_move_compensate),
292                                                                  path));
293         }
294     }
299 /*
300   Local Variables:
301   mode:c++
302   c-file-style:"stroustrup"
303   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
304   indent-tabs-mode:nil
305   fill-column:99
306   End:
307 */
308 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :