Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / stable / 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 #include <synfigapp/general.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
47 ACTION_INIT(Action::ValueNodeLinkConnect);
48 ACTION_SET_NAME(Action::ValueNodeLinkConnect,"value_node_link_connect");
49 ACTION_SET_LOCAL_NAME(Action::ValueNodeLinkConnect,N_("Connect ValueNode Link"));
50 ACTION_SET_TASK(Action::ValueNodeLinkConnect,"connect");
51 ACTION_SET_CATEGORY(Action::ValueNodeLinkConnect,Action::CATEGORY_LAYER|Action::CATEGORY_VALUENODE);
52 ACTION_SET_PRIORITY(Action::ValueNodeLinkConnect,0);
53 ACTION_SET_VERSION(Action::ValueNodeLinkConnect,"0.0");
54 ACTION_SET_CVS_ID(Action::ValueNodeLinkConnect,"$Id$");
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::ValueNodeLinkConnect::ValueNodeLinkConnect():
63         index(-1)       // Initially set it to negative one so that we know when it has changed
64 {
65 }
66
67 Action::ParamVocab
68 Action::ValueNodeLinkConnect::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71
72         ret.push_back(ParamDesc("parent_value_node",Param::TYPE_VALUENODE)
73                 .set_local_name(_("Parent ValueNode"))
74         );
75
76         ret.push_back(ParamDesc("index",Param::TYPE_INTEGER)
77                 .set_local_name(_("Index"))
78         );
79
80         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
81                 .set_local_name(_("ValueNode to be connected"))
82         );
83
84         return ret;
85 }
86
87 bool
88 Action::ValueNodeLinkConnect::is_candidate(const ParamList &x)
89 {
90         return candidate_check(get_param_vocab(),x);
91 }
92
93 bool
94 Action::ValueNodeLinkConnect::set_param(const synfig::String& name, const Action::Param &param)
95 {
96         if(name=="parent_value_node" && param.get_type()==Param::TYPE_VALUENODE)
97         {
98                 parent_value_node=LinkableValueNode::Handle::cast_dynamic(param.get_value_node());
99
100                 return static_cast<bool>(parent_value_node);
101         }
102
103         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
104         {
105                 new_value_node=param.get_value_node();
106
107                 return true;
108         }
109
110         if(name=="index" && param.get_type()==Param::TYPE_INTEGER)
111         {
112                 index=param.get_integer();
113
114                 return true;
115         }
116
117         return Action::CanvasSpecific::set_param(name,param);
118 }
119
120 bool
121 Action::ValueNodeLinkConnect::is_ready()const
122 {
123         if(!new_value_node || !parent_value_node || index==-1)
124                 return false;
125         return Action::CanvasSpecific::is_ready();
126 }
127
128 void
129 Action::ValueNodeLinkConnect::perform()
130 {
131         if(parent_value_node->link_count()<=index)
132                 throw Error(_("Bad index, too big. LinkCount=%d, Index=%d"),parent_value_node->link_count(),index);
133
134         old_value_node=parent_value_node->get_link(index);
135
136         if(!parent_value_node->set_link(index,new_value_node))
137                 throw Error(_("Parent would not accept link"));
138
139         /*set_dirty(true);
140
141         if(get_canvas_interface())
142         {
143                 get_canvas_interface()->signal_value_node_changed()(parent_value_node);
144         }*/
145 }
146
147 void
148 Action::ValueNodeLinkConnect::undo()
149 {
150         if(parent_value_node->link_count()<=index)
151                 throw Error(_("Bad index, too big. LinkCount=%d, Index=%d"),parent_value_node->link_count(),index);
152
153         if(!parent_value_node->set_link(index,old_value_node))
154                 throw Error(_("Parent would not accept old link"));
155
156         /*set_dirty(true);
157
158         if(get_canvas_interface())
159         {
160                 get_canvas_interface()->signal_value_node_changed()(parent_value_node);
161         }*/
162 }