Fix possible crash when right click on a Paste Canvas Layer that doesn't have a canva...
[synfig.git] / synfig-studio / src / synfigapp / actions / layerparamsetstatic.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerparamsetstatic.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 "layerparamsetstatic.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::LayerParamSetStatic);
49 ACTION_SET_NAME(Action::LayerParamSetStatic,"LayerParamSetStatic");
50 ACTION_SET_LOCAL_NAME(Action::LayerParamSetStatic,N_("Set Layer Parameter Static"));
51 ACTION_SET_TASK(Action::LayerParamSetStatic,"setstatic");
52 ACTION_SET_CATEGORY(Action::LayerParamSetStatic,Action::CATEGORY_VALUEDESC);
53 ACTION_SET_PRIORITY(Action::LayerParamSetStatic,0);
54 ACTION_SET_VERSION(Action::LayerParamSetStatic,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerParamSetStatic,"$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::LayerParamSetStatic::LayerParamSetStatic()
64 {
65 }
66
67 Action::ParamVocab
68 Action::LayerParamSetStatic::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::LayerParamSetStatic::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         //! Retrieves the current parameter
103         parameter = _layer->get_param(_param_name);
104         //! Check that the parameter is not a inline canvas
105         if(parameter.get_type()==ValueBase::TYPE_CANVAS && parameter.get(Canvas::Handle()))
106                 if(parameter.get(Canvas::Handle())->is_inline())
107                         return false;
108         //! Check if it is already static
109         if(parameter.get_static())
110                 return false;
111
112         return true;
113
114 }
115
116 bool
117 Action::LayerParamSetStatic::set_param(const synfig::String& name, const Action::Param &param)
118 {
119
120         if(!layer && name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
121         {
122                 ValueDesc value_desc(param.get_value_desc());
123                 if(!value_desc.parent_is_layer_param())
124                         return false;
125
126                 layer=Layer::Handle::cast_dynamic(value_desc.get_layer());
127
128                 if(!layer)
129                         return false;
130         }
131
132
133         if(param_name.empty() && name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
134         {
135                 ValueDesc value_desc(param.get_value_desc());
136                 if(!value_desc.parent_is_layer_param())
137                         return false;
138
139                 param_name=value_desc.get_param_name();
140
141                 if(param_name.empty())
142                         return false;
143         }
144
145         return Action::CanvasSpecific::set_param(name,param);
146 }
147
148 bool
149 Action::LayerParamSetStatic::is_ready()const
150 {
151         if(!layer || param_name.empty())
152                 return false;
153
154         return Action::CanvasSpecific::is_ready();
155 }
156
157 void
158 Action::LayerParamSetStatic::perform()
159 {
160         //! See if the parameter is dynamic
161         if(layer->dynamic_param_list().count(param_name))
162                 throw Error(_("This action is not for Value Nodes!"));
163
164         old_static_value=layer->get_param_static(param_name);
165
166         if(!layer->set_param_static(param_name,true))
167                 throw Error(_("Layer did not accept static value."));
168
169         //! Signal layer changed
170         layer->changed();
171         //! Signal that a layer parameter changed
172         if(get_canvas_interface())
173                 get_canvas_interface()->signal_layer_param_changed()(layer,param_name);
174 }
175
176 void
177 Action::LayerParamSetStatic::undo()
178 {
179         if(!layer->set_param_static(param_name,old_static_value))
180                 throw Error(_("Layer did not accept static value."));
181         //! Signal layer changed
182         layer->changed();
183         //! Signal that a layer parameter changed
184         if(get_canvas_interface())
185                 get_canvas_interface()->signal_layer_param_changed()(layer,param_name);
186 }