Fix crash when right click on a empty canvas parameter of a Paste Canvas layer.
[synfig.git] / synfig-studio / src / synfigapp / actions / layerparamunsetstatic.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerparamunsetstatic.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2010 Carlos López
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layerparamunsetstatic.h"
34 #include <synfigapp/canvasinterface.h>
35
36 #include <synfigapp/general.h>
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT(Action::LayerParamUnSetStatic);
49 ACTION_SET_NAME(Action::LayerParamUnSetStatic,"LayerParamUnSetStatic");
50 ACTION_SET_LOCAL_NAME(Action::LayerParamUnSetStatic,N_("Allow Animation"));
51 ACTION_SET_TASK(Action::LayerParamUnSetStatic,"unsetstatic");
52 ACTION_SET_CATEGORY(Action::LayerParamUnSetStatic,Action::CATEGORY_VALUEDESC);
53 ACTION_SET_PRIORITY(Action::LayerParamUnSetStatic,0);
54 ACTION_SET_VERSION(Action::LayerParamUnSetStatic,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerParamUnSetStatic,"$Id$");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::LayerParamUnSetStatic::LayerParamUnSetStatic()
64 {
65 }
66
67 Action::ParamVocab
68 Action::LayerParamUnSetStatic::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71
72         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
73                 .set_local_name(_("Value Desc"))
74         );
75
76         return ret;
77 }
78
79 bool
80 Action::LayerParamUnSetStatic::is_candidate(const ParamList &x)
81 {
82         if(!candidate_check(get_param_vocab(),x))
83                 return false;
84
85         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
86
87         if(!value_desc.parent_is_layer_param())
88                 return false;
89
90         synfig::ValueBase parameter;
91         synfig::Layer::Handle _layer;
92         synfig::String _param_name;
93         _layer = value_desc.get_layer();
94         _param_name = value_desc.get_param_name();
95
96         if(!_layer || _param_name.empty())
97                 return false;
98
99         //!Check that the parameter is not Value Node (Const, Animated or Linkable)
100         if(_layer->dynamic_param_list().count(_param_name))
101                 return false;
102
103         //! Retrieves the current parameter
104         parameter = _layer->get_param(_param_name);
105         //! Check that the parameter is not a inline canvas
106         if(parameter.get_type()==ValueBase::TYPE_CANVAS && parameter.get(Canvas::Handle()))
107                 if(parameter.get(Canvas::Handle())->is_inline())
108                         return false;
109         //! Check if it is not static
110         if(!parameter.get_static())
111                 return false;
112
113         return true;
114
115 }
116
117 bool
118 Action::LayerParamUnSetStatic::set_param(const synfig::String& name, const Action::Param &param)
119 {
120
121         if(!layer && name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
122         {
123                 ValueDesc value_desc(param.get_value_desc());
124                 if(!value_desc.parent_is_layer_param())
125                         return false;
126
127                 layer=Layer::Handle::cast_dynamic(value_desc.get_layer());
128
129                 if(!layer)
130                         return false;
131         }
132
133
134         if(param_name.empty() && name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
135         {
136                 ValueDesc value_desc(param.get_value_desc());
137
138                 if(!value_desc.parent_is_layer_param())
139                         return false;
140
141                 param_name=value_desc.get_param_name();
142
143                 if(param_name.empty())
144                         return false;
145
146         }
147
148         return Action::CanvasSpecific::set_param(name,param);
149 }
150
151 bool
152 Action::LayerParamUnSetStatic::is_ready()const
153 {
154         if(!layer || param_name.empty())
155                 return false;
156
157         return Action::CanvasSpecific::is_ready();
158 }
159
160 void
161 Action::LayerParamUnSetStatic::perform()
162 {
163         //! See if the parameter is dynamic
164         if(layer->dynamic_param_list().count(param_name))
165                 throw Error(_("This action is not for Value Nodes!"));
166
167         old_static_value=layer->get_param_static(param_name);
168
169         if(!layer->set_param_static(param_name,false))
170                 throw Error(_("Layer did not accept static value."));
171
172         //! Signal layer changed
173         layer->changed();
174         //! Signal that a layer parameter changed
175         if(get_canvas_interface())
176                 get_canvas_interface()->signal_layer_param_changed()(layer,param_name);
177 }
178
179 void
180 Action::LayerParamUnSetStatic::undo()
181 {
182         if(!layer->set_param(param_name,old_static_value))
183                 throw Error(_("Layer did not accept parameter."));
184         //! Signal layer changed
185         layer->changed();
186         //! Signal that a layer parameter changed
187         if(get_canvas_interface())
188                 get_canvas_interface()->signal_layer_param_changed()(layer,param_name);
189 }