summaryrefslogtreecommitdiffstats
path: root/src/object/sp-switch.cpp
blob: 6d7c812b23039915bab4336437bc6ea5ef3adde3 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * SVG <switch> implementation
 *
 * Authors:
 *   Andrius R. <knutux@gmail.com>
 *   MenTaLguY  <mental@rydia.net>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Abhishek Sharma
 *
 * Copyright (C) 2006 authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glibmm/i18n.h>

#include "sp-switch.h"
#include "display/drawing-group.h"
#include "conditions.h"

#include <sigc++/adaptors/bind.h>

SPSwitch::SPSwitch() : SPGroup() {
    this->_cached_item = nullptr;
}

SPSwitch::~SPSwitch() {
    _releaseLastItem(_cached_item);
}

SPObject *SPSwitch::_evaluateFirst() {
    SPObject *first = nullptr;

    for (auto& child: children) {
        if (SP_IS_ITEM(&child) && sp_item_evaluate(SP_ITEM(&child))) {
        	first = &child;
            break;
        }
    }

    return first;
}

std::vector<SPObject*> SPSwitch::_childList(bool add_ref, SPObject::Action action) {
    if ( action != SPObject::ActionGeneral ) {
        return this->childList(add_ref, action);
    }

    SPObject *child = _evaluateFirst();
    std::vector<SPObject*> x;
    if (nullptr == child)
        return x;

    if (add_ref) {
        //g_object_ref (G_OBJECT (child));
    	sp_object_ref(child);
    }
    x.push_back(child);
    return x;
}

const char *SPSwitch::typeName() const {
    return "switch";
}

const char *SPSwitch::displayName() const {
    return _("Conditional Group");
}

gchar *SPSwitch::description() const {
    gint len = this->getItemCount();
    return g_strdup_printf(
        ngettext(_("of <b>%d</b> object"), _("of <b>%d</b> objects"), len), len);
}

void SPSwitch::child_added(Inkscape::XML::Node* child, Inkscape::XML::Node* ref) {
    SPGroup::child_added(child, ref);

    this->_reevaluate(true);
}

void SPSwitch::remove_child(Inkscape::XML::Node *child) {
    SPGroup::remove_child(child);

    this->_reevaluate();
}

void SPSwitch::order_changed (Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
{
    SPGroup::order_changed(child, old_ref, new_ref);

	this->_reevaluate();
}

void SPSwitch::_reevaluate(bool /*add_to_drawing*/) {
    SPObject *evaluated_child = _evaluateFirst();
    if (!evaluated_child || _cached_item == evaluated_child) {
        return;
    }

    _releaseLastItem(_cached_item);

    std::vector<SPObject*> item_list = _childList(false, SPObject::ActionShow);
    for ( std::vector<SPObject*>::const_reverse_iterator iter=item_list.rbegin();iter!=item_list.rend();++iter) {
        SPObject *o = *iter;
        if ( !SP_IS_ITEM (o) ) {
            continue;
        }

        SPItem * child = SP_ITEM(o);
        child->setEvaluated(o == evaluated_child);
    }

    _cached_item = evaluated_child;
    _release_connection = evaluated_child->connectRelease(sigc::bind(sigc::ptr_fun(&SPSwitch::_releaseItem), this));

    this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
}

void SPSwitch::_releaseItem(SPObject *obj, SPSwitch *selection)
{
    selection->_releaseLastItem(obj);
}

void SPSwitch::_releaseLastItem(SPObject *obj)
{
    if (nullptr == this->_cached_item || this->_cached_item != obj)
        return;

    this->_release_connection.disconnect();
    this->_cached_item = nullptr;
}

void SPSwitch::_showChildren (Inkscape::Drawing &drawing, Inkscape::DrawingItem *ai, unsigned int key, unsigned int flags) {
    SPObject *evaluated_child = this->_evaluateFirst();

    std::vector<SPObject*> l = this->_childList(false, SPObject::ActionShow);

    for ( std::vector<SPObject*>::const_reverse_iterator iter=l.rbegin();iter!=l.rend();++iter) {
        SPObject *o = *iter;

        if (SP_IS_ITEM (o)) {
            SPItem * child = SP_ITEM(o);
            child->setEvaluated(o == evaluated_child);
            Inkscape::DrawingItem *ac = child->invoke_show (drawing, key, flags);

            if (ac) {
                ai->appendChild(ac);
            }
        }
    }
}

/*
  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:fileencoding=utf-8:textwidth=99 :