Code

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