Code

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