Get the 'Raise Layer' action to list the layer(s) that were raised in the History...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layerraise.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerraise.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 "layerraise.h"
33 #include "layermove.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_NO_GET_LOCAL_NAME(Action::LayerRaise);
49 ACTION_SET_NAME(Action::LayerRaise,"layer_raise");
50 ACTION_SET_LOCAL_NAME(Action::LayerRaise,N_("Raise Layer"));
51 ACTION_SET_TASK(Action::LayerRaise,"raise");
52 ACTION_SET_CATEGORY(Action::LayerRaise,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerRaise,9);
54 ACTION_SET_VERSION(Action::LayerRaise,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerRaise,"$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::LayerRaise::LayerRaise()
64 {
65 }
66
67 synfig::String
68 Action::LayerRaise::get_local_name()const
69 {
70         String ret;
71
72         if (layers.empty())
73                 return _("Raise Layer");
74
75         ret = strprintf("%s '%s'",
76                                         (layers.size() == 1
77                                          ? _("Raise Layer")
78                                          : _("Raise Layers")),
79                                         (*layers.begin())->get_non_empty_description().c_str());
80
81         for(std::list<synfig::Layer::Handle>::const_iterator iter=++layers.begin(); iter!=layers.end(); ++iter)
82                 ret += strprintf(", '%s'", ((*iter)->get_non_empty_description().c_str()));
83         return ret;
84 }
85
86 Action::ParamVocab
87 Action::LayerRaise::get_param_vocab()
88 {
89         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
90
91         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
92                 .set_local_name(_("Layer"))
93                 .set_desc(_("Layer to be raised"))
94                 .set_supports_multiple()
95         );
96
97         return ret;
98 }
99
100 bool
101 Action::LayerRaise::is_candidate(const ParamList &x)
102 {
103         if(!candidate_check(get_param_vocab(),x))
104                 return false;
105         if(x.find("layer")->second.get_layer()->get_depth()==0)
106                 return false;
107         return true;
108 }
109
110 bool
111 Action::LayerRaise::set_param(const synfig::String& name, const Action::Param &param)
112 {
113         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
114         {
115                 layers.push_back(param.get_layer());
116
117                 return true;
118         }
119
120         return Action::CanvasSpecific::set_param(name,param);
121 }
122
123 bool
124 Action::LayerRaise::is_ready()const
125 {
126         if(layers.empty())
127                 return false;
128         return Action::CanvasSpecific::is_ready();
129 }
130
131 void
132 Action::LayerRaise::prepare()
133 {
134         std::list<synfig::Layer::Handle>::reverse_iterator iter;
135
136         clear();
137
138         for(iter=layers.rbegin();!(iter==layers.rend());++iter)
139         {
140                 Layer::Handle layer(*iter);
141
142                 Canvas::Handle subcanvas(layer->get_canvas());
143
144                 // Find the iterator for the layer
145                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
146
147                 // If we couldn't find the layer in the canvas, then bail
148                 if(*iter!=layer)
149                         throw Error(_("This layer doesn't exist anymore."));
150
151                 // If the subcanvas isn't the same as the canvas,
152                 // then it had better be an inline canvas. If not,
153                 // bail
154                 //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
155                 //      throw Error(_("This layer doesn't belong to this canvas anymore"));
156
157                 int new_index=iter-subcanvas->begin();
158
159                 if(new_index==0)
160                         continue;
161
162                 new_index--;
163
164                 Action::Handle layer_move(LayerMove::create());
165
166                 layer_move->set_param("canvas",get_canvas());
167                 layer_move->set_param("canvas_interface",get_canvas_interface());
168                 layer_move->set_param("layer",layer);
169                 layer_move->set_param("new_index",new_index);
170
171                 add_action_front(layer_move);
172         }
173 }