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