Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-switch.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SVG <switch> implementation
4 *
5 * Authors:
6 * Andrius R. <knutux@gmail.com>
7 * MenTaLguY <mental@rydia.net>
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2006 authors
12 *
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15
16#include <glibmm/i18n.h>
17
18#include "sp-switch.h"
20#include "conditions.h"
21
22#include <sigc++/adaptors/bind.h>
23
25 this->_cached_item = nullptr;
26}
27
31
33 SPObject *first = nullptr;
34
35 for (auto& child: children) {
36 if (is<SPItem>(&child) && sp_item_evaluate(cast<SPItem>(&child))) {
37 first = &child;
38 break;
39 }
40 }
41
42 return first;
43}
44
45std::vector<SPObject*> SPSwitch::_childList(bool add_ref, SPObject::Action action) {
46 if ( action != SPObject::ActionGeneral ) {
47 return this->childList(add_ref, action);
48 }
49
51 std::vector<SPObject*> x;
52 if (nullptr == child)
53 return x;
54
55 if (add_ref) {
56 //g_object_ref (G_OBJECT (child));
58 }
59 x.push_back(child);
60 return x;
61}
62
63const char *SPSwitch::typeName() const {
64 return "switch";
65}
66
67const char *SPSwitch::displayName() const {
68 return _("Conditional Group");
69}
70
71gchar *SPSwitch::description() const {
72 gint len = this->getItemCount();
73 return g_strdup_printf(
74 ngettext("of <b>%d</b> object", "of <b>%d</b> objects", len), len);
75}
76
82
88
95
96void SPSwitch::_reevaluate(bool /*add_to_drawing*/) {
97 SPObject *evaluated_child = _evaluateFirst();
98 if (!evaluated_child || _cached_item == evaluated_child) {
99 return;
100 }
101
103
104 std::vector<SPObject*> item_list = _childList(false, SPObject::ActionShow);
105 for ( std::vector<SPObject*>::const_reverse_iterator iter=item_list.rbegin();iter!=item_list.rend();++iter) {
106 SPObject *o = *iter;
107 if ( !is<SPItem>(o) ) {
108 continue;
109 }
110
111 auto child = cast<SPItem>(o);
112 child->setEvaluated(o == evaluated_child);
113 }
114
115 _cached_item = evaluated_child;
116 _release_connection = evaluated_child->connectRelease(sigc::bind(sigc::ptr_fun(&SPSwitch::_releaseItem), this));
117
118 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
119}
120
122{
123 selection->_releaseLastItem(obj);
124}
125
127{
128 if (nullptr == this->_cached_item || this->_cached_item != obj)
129 return;
130
131 this->_release_connection.disconnect();
132 this->_cached_item = nullptr;
133}
134
135void SPSwitch::_showChildren (Inkscape::Drawing &drawing, Inkscape::DrawingItem *ai, unsigned int key, unsigned int flags) {
136 SPObject *evaluated_child = this->_evaluateFirst();
137
138 std::vector<SPObject*> l = this->_childList(false, SPObject::ActionShow);
139
140 for ( std::vector<SPObject*>::const_reverse_iterator iter=l.rbegin();iter!=l.rend();++iter) {
141 SPObject *o = *iter;
142
143 if (is<SPItem>(o)) {
144 auto child = cast<SPItem>(o);
145 child->setEvaluated(o == evaluated_child);
146 Inkscape::DrawingItem *ac = child->invoke_show (drawing, key, flags);
147
148 if (ac) {
149 ai->appendChild(ac);
150 }
151 }
152 }
153}
154
155/*
156 Local Variables:
157 mode:c++
158 c-file-style:"stroustrup"
159 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
160 indent-tabs-mode:nil
161 fill-column:99
162 End:
163*/
164// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
SVG drawing item for display.
void appendChild(DrawingItem *item)
Interface for refcounted XML nodes.
Definition node.h:80
void order_changed(Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref) override
int getItemCount() const
void remove_child(Inkscape::XML::Node *child) override
std::vector< SPItem * > item_list()
void child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) override
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
std::vector< SPObject * > childList(bool add_ref, Action action=ActionGeneral)
Retrieves the children as a std vector object, optionally ref'ing the children in the process,...
sigc::connection connectRelease(sigc::slot< void(SPObject *)> slot)
Connects to the release request signal.
Definition sp-object.h:237
@ ActionGeneral
Definition sp-object.h:324
ChildrenList children
Definition sp-object.h:907
void requestDisplayUpdate(unsigned int flags)
Queues an deferred update of this object's display.
SPObject * _cached_item
Definition sp-switch.h:37
gchar * description() const override
Definition sp-switch.cpp:71
~SPSwitch() override
Definition sp-switch.cpp:28
void _showChildren(Inkscape::Drawing &drawing, Inkscape::DrawingItem *ai, unsigned int key, unsigned int flags) override
std::vector< SPObject * > _childList(bool add_ref, SPObject::Action action)
Definition sp-switch.cpp:45
void _releaseLastItem(SPObject *obj)
static void _releaseItem(SPObject *obj, SPSwitch *selection)
sigc::connection _release_connection
Definition sp-switch.h:38
const char * typeName() const override
The item's type name, not node tag name.
Definition sp-switch.cpp:63
void _reevaluate(bool add_to_arena=false)
Definition sp-switch.cpp:96
void order_changed(Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref) override
Definition sp-switch.cpp:89
void remove_child(Inkscape::XML::Node *child) override
Definition sp-switch.cpp:83
void child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) override
Definition sp-switch.cpp:77
const char * displayName() const override
The item's type name as a translated human string.
Definition sp-switch.cpp:67
SPObject * _evaluateFirst()
Definition sp-switch.cpp:32
bool sp_item_evaluate(SPItem const *item)
TODO: insert short description here.
Group belonging to an SVG drawing element.
static cairo_user_data_key_t key
Ocnode * child[8]
Definition quantize.cpp:33
Ocnode ** ref
Definition quantize.cpp:32
auto len
Definition safe-printf.h:21
SPObject * sp_object_ref(SPObject *object, SPObject *owner)
Increase reference count of object, with possible debugging.