Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc2 / src / synfigapp / actions / valuenodelinkconnect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenodelinkconnect.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenodelinkconnect.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
45 ACTION_INIT(Action::ValueNodeLinkConnect);
46 ACTION_SET_NAME(Action::ValueNodeLinkConnect,"value_node_link_connect");
47 ACTION_SET_LOCAL_NAME(Action::ValueNodeLinkConnect,_("Connect ValueNode Link"));
48 ACTION_SET_TASK(Action::ValueNodeLinkConnect,"connect");
49 ACTION_SET_CATEGORY(Action::ValueNodeLinkConnect,Action::CATEGORY_LAYER|Action::CATEGORY_VALUENODE);
50 ACTION_SET_PRIORITY(Action::ValueNodeLinkConnect,0);
51 ACTION_SET_VERSION(Action::ValueNodeLinkConnect,"0.0");
52 ACTION_SET_CVS_ID(Action::ValueNodeLinkConnect,"$Id$");
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 Action::ValueNodeLinkConnect::ValueNodeLinkConnect():
61         index(-1)       // Initially set it to negative one so that we know when it has changed
62 {
63 }
64
65 Action::ParamVocab
66 Action::ValueNodeLinkConnect::get_param_vocab()
67 {
68         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
69
70         ret.push_back(ParamDesc("parent_value_node",Param::TYPE_VALUENODE)
71                 .set_local_name(_("Parent ValueNode"))
72         );
73
74         ret.push_back(ParamDesc("index",Param::TYPE_INTEGER)
75                 .set_local_name(_("Index"))
76         );
77
78         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
79                 .set_local_name(_("ValueNode to be connected"))
80         );
81
82         return ret;
83 }
84
85 bool
86 Action::ValueNodeLinkConnect::is_candidate(const ParamList &x)
87 {
88         return candidate_check(get_param_vocab(),x);
89 }
90
91 bool
92 Action::ValueNodeLinkConnect::set_param(const synfig::String& name, const Action::Param &param)
93 {
94         if(name=="parent_value_node" && param.get_type()==Param::TYPE_VALUENODE)
95         {
96                 parent_value_node=LinkableValueNode::Handle::cast_dynamic(param.get_value_node());
97
98                 return static_cast<bool>(parent_value_node);
99         }
100
101         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
102         {
103                 new_value_node=param.get_value_node();
104
105                 return true;
106         }
107
108         if(name=="index" && param.get_type()==Param::TYPE_INTEGER)
109         {
110                 index=param.get_integer();
111
112                 return true;
113         }
114
115         return Action::CanvasSpecific::set_param(name,param);
116 }
117
118 bool
119 Action::ValueNodeLinkConnect::is_ready()const
120 {
121         if(!new_value_node || !parent_value_node || index==-1)
122                 return false;
123         return Action::CanvasSpecific::is_ready();
124 }
125
126 void
127 Action::ValueNodeLinkConnect::perform()
128 {
129         if(parent_value_node->link_count()<=index)
130                 throw Error(_("Bad index, too big. LinkCount=%d, Index=%d"),parent_value_node->link_count(),index);
131
132         old_value_node=parent_value_node->get_link(index);
133
134         if(!parent_value_node->set_link(index,new_value_node))
135                 throw Error(_("Parent would not accept link"));
136
137         /*set_dirty(true);
138
139         if(get_canvas_interface())
140         {
141                 get_canvas_interface()->signal_value_node_changed()(parent_value_node);
142         }*/
143 }
144
145 void
146 Action::ValueNodeLinkConnect::undo()
147 {
148         if(parent_value_node->link_count()<=index)
149                 throw Error(_("Bad index, too big. LinkCount=%d, Index=%d"),parent_value_node->link_count(),index);
150
151         if(!parent_value_node->set_link(index,old_value_node))
152                 throw Error(_("Parent would not accept old link"));
153
154         /*set_dirty(true);
155
156         if(get_canvas_interface())
157         {
158                 get_canvas_interface()->signal_value_node_changed()(parent_value_node);
159         }*/
160 }