summaryrefslogtreecommitdiffstats
path: root/src/object/sp-conn-end-pair.cpp
blob: 2470efff0ddb88453e00f9d63edf2975f2770ff6 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * A class for handling connector endpoint movement and libavoid interaction.
 *
 * Authors:
 *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
 *   Michael Wybrow <mjwybrow@users.sourceforge.net>
 *   Abhishek Sharma
 *
 *    * Copyright (C) 2004-2005 Monash University
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <string>
#include <glibmm/stringutils.h>

#include "attributes.h"
#include "sp-conn-end.h"
#include "uri.h"
#include "display/curve.h"
#include "xml/repr.h"
#include "sp-path.h"
#include "sp-use.h"
#include "3rdparty/adaptagrams/libavoid/router.h"
#include "document.h"
#include "sp-item-group.h"


SPConnEndPair::SPConnEndPair(SPPath *const owner)
    : _path(owner)
    , _connRef(nullptr)
    , _connType(SP_CONNECTOR_NOAVOID)
    , _connCurvature(0.0)
    , _transformed_connection()
{
    for (unsigned handle_ix = 0; handle_ix <= 1; ++handle_ix) {
        this->_connEnd[handle_ix] = new SPConnEnd(owner);
        this->_connEnd[handle_ix]->_changed_connection
            = this->_connEnd[handle_ix]->ref.changedSignal()
            .connect(sigc::bind(sigc::ptr_fun(sp_conn_end_href_changed),
                                this->_connEnd[handle_ix], owner, handle_ix));
    }
}

SPConnEndPair::~SPConnEndPair()
{
    for (auto & handle_ix : this->_connEnd) {
        delete handle_ix;
        handle_ix = nullptr;
    }
}

void SPConnEndPair::release()
{
    for (auto & handle_ix : this->_connEnd) {
        handle_ix->_changed_connection.disconnect();
        handle_ix->_delete_connection.disconnect();
        handle_ix->_transformed_connection.disconnect();
        handle_ix->_group_connection.disconnect();
        g_free(handle_ix->href);
        handle_ix->href = nullptr;
        handle_ix->ref.detach();
    }

    // If the document is being destroyed then the router instance
    // and the ConnRefs will have been destroyed with it.
    const bool routerInstanceExists = (_path->document->getRouter() != nullptr);

    if (_connRef && routerInstanceExists) {
        _connRef->router()->deleteConnector(_connRef);
    }
    _connRef = nullptr;

    _transformed_connection.disconnect();
}

void sp_conn_end_pair_build(SPObject *object)
{
    object->readAttr(SPAttr::CONNECTOR_TYPE);
    object->readAttr(SPAttr::CONNECTION_START);
    object->readAttr(SPAttr::CONNECTION_START_POINT);
    object->readAttr(SPAttr::CONNECTION_END);
    object->readAttr(SPAttr::CONNECTION_END_POINT);
    object->readAttr(SPAttr::CONNECTOR_CURVATURE);
}


static void avoid_conn_transformed(Geom::Affine const */*mp*/, SPItem *moved_item)
{
    SPPath *path = SP_PATH(moved_item);
    if (path->connEndPair.isAutoRoutingConn()) {
        path->connEndPair.tellLibavoidNewEndpoints();
    }
}


void SPConnEndPair::setAttr(const SPAttr key, gchar const *const value)
{
    switch (key) {
    case SPAttr::CONNECTOR_TYPE:
        if (value && (strcmp(value, "polyline") == 0 || strcmp(value, "orthogonal") == 0)) {
            int new_conn_type = strcmp(value, "polyline") ? SP_CONNECTOR_ORTHOGONAL : SP_CONNECTOR_POLYLINE;

            if (!_connRef) {
                _connType = new_conn_type;
                Avoid::Router *router = _path->document->getRouter();
                _connRef = new Avoid::ConnRef(router);
                _connRef->setRoutingType(new_conn_type == SP_CONNECTOR_POLYLINE ?
                    Avoid::ConnType_PolyLine : Avoid::ConnType_Orthogonal);
                _transformed_connection = _path->connectTransformed(sigc::ptr_fun(&avoid_conn_transformed));
            } else if (new_conn_type != _connType) {
                _connType = new_conn_type;
                _connRef->setRoutingType(new_conn_type == SP_CONNECTOR_POLYLINE ?
                    Avoid::ConnType_PolyLine : Avoid::ConnType_Orthogonal);
                sp_conn_reroute_path(_path);
            }
        } else {
            _connType = SP_CONNECTOR_NOAVOID;

            if (_connRef) {
                _connRef->router()->deleteConnector(_connRef);
                _connRef = nullptr;
                _transformed_connection.disconnect();
            }
        }
        break;
    case SPAttr::CONNECTOR_CURVATURE:
        if (value) {
            _connCurvature = g_strtod(value, nullptr);
            if (_connRef && _connRef->isInitialised()) {
                // Redraw the connector, but only if it has been initialised.
                sp_conn_reroute_path(_path);
            }
        }
        break;
    case SPAttr::CONNECTION_START:
        this->_connEnd[0]->setAttacherHref(value);
        break;
    case SPAttr::CONNECTION_START_POINT:
        this->_connEnd[0]->setAttacherSubHref(value);
        break;
    case SPAttr::CONNECTION_END:
        this->_connEnd[1]->setAttacherHref(value);
        break;
    case SPAttr::CONNECTION_END_POINT:
        this->_connEnd[1]->setAttacherSubHref(value);
        break;
    }
}

void SPConnEndPair::writeRepr(Inkscape::XML::Node *const repr) const
{
    char const * const attrs[] = {
        "inkscape:connection-start", "inkscape:connection-end"};
    char const * const point_attrs[] = {
        "inkscape:connection-start-point", "inkscape:connection-end-point"};
    for (unsigned handle_ix = 0; handle_ix < 2; ++handle_ix) {
        const Inkscape::URI* U = this->_connEnd[handle_ix]->ref.getURI();
        if (U) {
            auto str = U->str();
            repr->setAttribute(attrs[handle_ix], str);
        }
        const Inkscape::URI* P = this->_connEnd[handle_ix]->sub_ref.getURI();
        if (P) {
            auto str = P->str();
            repr->setAttribute(point_attrs[handle_ix], str);
        }
    }
    if (_connType == SP_CONNECTOR_POLYLINE || _connType == SP_CONNECTOR_ORTHOGONAL) {
        repr->setAttribute("inkscape:connector-curvature", Glib::Ascii::dtostr(_connCurvature));
        repr->setAttribute("inkscape:connector-type", _connType == SP_CONNECTOR_POLYLINE ? "polyline" : "orthogonal" );
    }
}

void SPConnEndPair::getAttachedItems(SPItem *h2attItem[2]) const {
    for (unsigned h = 0; h < 2; ++h) {
        auto obj = this->_connEnd[h]->ref.getObject();
        auto sub_obj = this->_connEnd[h]->sub_ref.getObject();

        if(sub_obj) {
            // For sub objects, we have to go fishing for the virtual/shadow
            // object which has the correct position for this use/symbol
            SPUse *use = dynamic_cast<SPUse *>(obj);
            if(use) {
                auto root = use->root();
                bool found = false;
                for (auto& child: root->children) {
                    if(!g_strcmp0(child.getAttribute("id"), sub_obj->getId())) {
                        h2attItem[h] = (SPItem *) &child;
                        found = true;
                    }
                }
                if(!found) {
                    g_warning("Couldn't find sub connector point!");
                }
            }
        } else {
            h2attItem[h] = obj;
        }

        // Deal with the case of the attached object being an empty group.
        // A group containing no items does not have a valid bbox, so
        // causes problems for the auto-routing code.  Also, since such a
        // group no longer has an onscreen representation and can only be
        // selected through the XML editor, it makes sense just to detach
        // connectors from them.
        if (SP_IS_GROUP(h2attItem[h])) {
            if (SP_GROUP(h2attItem[h])->getItemCount() == 0) {
                // This group is empty, so detach.
                sp_conn_end_detach(_path, h);
                h2attItem[h] = nullptr;
            }
        }
    }
}

void SPConnEndPair::getEndpoints(Geom::Point endPts[]) const
{
    SPCurve const *curve = _path->curveForEdit();
    SPItem *h2attItem[2] = {nullptr};
    getAttachedItems(h2attItem);
    Geom::Affine i2d = _path->i2doc_affine();

    for (unsigned h = 0; h < 2; ++h) {
        if (h2attItem[h]) {
            endPts[h] = h2attItem[h]->getAvoidRef().getConnectionPointPos();
        } else if (!curve->is_empty()) {
            if (h == 0) {
                endPts[h] = *(curve->first_point()) * i2d;
            } else {
                endPts[h] = *(curve->last_point()) * i2d;
            }
        }
    }
}

gdouble SPConnEndPair::getCurvature() const
{
    return _connCurvature;
}

SPConnEnd** SPConnEndPair::getConnEnds()
{
    return _connEnd;
}

bool SPConnEndPair::isOrthogonal() const
{
    return _connType == SP_CONNECTOR_ORTHOGONAL;
}


static void redrawConnectorCallback(void *ptr)
{
    auto path = static_cast<SPPath *>(ptr);
    if (path->document == nullptr) {
        // This can happen when the document is being destroyed.
        return;
    }
    sp_conn_redraw_path(path);
}

void SPConnEndPair::rerouteFromManipulation()
{
    sp_conn_reroute_path_immediate(_path);
}


// Called from SPPath::update to initialise the endpoints.
void SPConnEndPair::update()
{
    if (_connType != SP_CONNECTOR_NOAVOID) {
        g_assert(_connRef != nullptr);
        if (!_connRef->isInitialised()) {
            _updateEndPoints();
            _connRef->setCallback(&redrawConnectorCallback, _path);
        }
    }
}

void SPConnEndPair::_updateEndPoints()
{
    Geom::Point endPt[2];
    getEndpoints(endPt);

    Avoid::Point src(endPt[0][Geom::X], endPt[0][Geom::Y]);
    Avoid::Point dst(endPt[1][Geom::X], endPt[1][Geom::Y]);

    _connRef->setEndpoints(src, dst);
}


bool SPConnEndPair::isAutoRoutingConn() const
{
    return _connType != SP_CONNECTOR_NOAVOID;
}

void SPConnEndPair::makePathInvalid()
{
    g_assert(_connRef != nullptr);

    _connRef->makePathInvalid();
}


// Redraws the curve along the recalculated route
// Straight or curved
void recreateCurve(SPCurve *curve, Avoid::ConnRef *connRef, const gdouble curvature)
{
    g_assert(connRef != nullptr);

    bool straight = curvature<1e-3;

    Avoid::PolyLine route = connRef->displayRoute();
    if (!straight) route = route.curvedPolyline(curvature);
    connRef->calcRouteDist();

    curve->reset();

    curve->moveto( Geom::Point(route.ps[0].x, route.ps[0].y) );
    int pn = route.size();
    for (int i = 1; i < pn; ++i) {
        Geom::Point p(route.ps[i].x, route.ps[i].y);
        if (straight) {
            curve->lineto( p );
        } else {
            switch (route.ts[i]) {
                case 'M':
                    curve->moveto( p );
                    break;
                case 'L':
                    curve->lineto( p );
                    break;
                case 'C':
                    g_assert( i+2<pn );
                    curve->curveto( p, Geom::Point(route.ps[i+1].x, route.ps[i+1].y),
                            Geom::Point(route.ps[i+2].x, route.ps[i+2].y) );
                    i+=2;
                    break;
            }
        }
    }
}


void SPConnEndPair::tellLibavoidNewEndpoints(bool const processTransaction)
{
    if (_connRef == nullptr || !isAutoRoutingConn()) {
        // Do nothing
        return;
    }
    makePathInvalid();

    _updateEndPoints();
    if (processTransaction) {
        _connRef->router()->processTransaction();
    }
    return;
}


bool SPConnEndPair::reroutePathFromLibavoid()
{
    if (_connRef == nullptr || !isAutoRoutingConn()) {
        // Do nothing
        return false;
    }

    SPCurve *curve = _path->curve();

    recreateCurve(curve, _connRef, _connCurvature);

    Geom::Affine doc2item = _path->i2doc_affine().inverse();
    curve->transform(doc2item);

    return true;
}


/*
  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 :