summaryrefslogtreecommitdiffstats
path: root/src/object/sp-shape-reference.cpp
blob: 1e302b4e83612760dd2ab557b9c474c56d415687 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Reference class for shapes (SVG 2 text).
 *
 * Copyright (C) 2020 Authors
 */

#include "sp-shape-reference.h"
#include "object/sp-text.h"

SPShapeReference::SPShapeReference(SPObject *obj)
    : URIReference(obj)
{
    // The text object can be detached from the document but still be
    // referenced, and its style (who's the owner of the SPShapeReference)
    // can also still be referenced even after the object got destroyed.
    _owner_release_connection = obj->connectRelease([this](SPObject *text_object) {
        assert(text_object == this->getOwner());

        // Fully detach to prevent reconnecting with a shape's modified signal
        this->detach();

        this->_owner_release_connection.disconnect();
    });

    // https://www.w3.org/TR/SVG/text.html#TextShapeInside
    // Applies to: 'text' elements
    // Inherited: no
    if (!dynamic_cast<SPText *>(obj)) {
        g_warning("shape reference on non-text object: %s", typeid(*obj).name());
        return;
    }

    // Listen to the shape's modified event to keep the text layout updated
    changedSignal().connect([this](SPObject *, SPObject *shape_object) {
        this->_shape_modified_connection.disconnect();

        if (shape_object) {
            this->_shape_modified_connection =
                shape_object->connectModified(sigc::mem_fun(*this, &SPShapeReference::on_shape_modified));
        }
    });
}

SPShapeReference::~SPShapeReference()
{ //
    _shape_modified_connection.disconnect();
    _owner_release_connection.disconnect();
}

/**
 * Slot to connect to the shape's modified signal. Requests display update of the text object.
 */
void SPShapeReference::on_shape_modified(SPObject *shape_object, unsigned flags)
{
    auto *text_object = getOwner();

    assert(text_object);
    assert(shape_object == getObject());

    if ((flags & SP_OBJECT_MODIFIED_FLAG)) {
        text_object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
    }
}

// vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :