a8de89e746de82be7ee68965a1a5638981686a13
[synfig.git] / synfig-studio / trunk / src / synfigapp / action.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file action.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 "action.h"
33 #include "instance.h"
34
35
36 #include "actions/layerremove.h"
37 #include "actions/layermove.h"
38 #include "actions/layerraise.h"
39 #include "actions/layerlower.h"
40 #include "actions/layeradd.h"
41 #include "actions/layeractivate.h"
42 #include "actions/layerparamset.h"
43 #include "actions/layerparamconnect.h"
44 #include "actions/layerparamdisconnect.h"
45 #include "actions/layerencapsulate.h"
46 #include "actions/layerduplicate.h"
47 #include "actions/layersetdesc.h"
48
49 #include "actions/valuenodeconstset.h"
50 #include "actions/valuenodeadd.h"
51 #include "actions/valuenodereplace.h"
52 #include "actions/valuenodelinkconnect.h"
53 #include "actions/valuenodelinkdisconnect.h"
54 #include "actions/valuenodedynamiclistinsert.h"
55 #include "actions/valuenodedynamiclistremove.h"
56 #include "actions/valuenodedynamiclistinsertsmart.h"
57 #include "actions/valuenodedynamiclistremovesmart.h"
58 #include "actions/valuenodedynamiclistloop.h"
59 #include "actions/valuenodedynamiclistunloop.h"
60 #include "actions/valuenodedynamiclistrotateorder.h"
61 #include "actions/valuenoderename.h"
62 #include "actions/valuenoderemove.h"
63
64 #include "actions/valuedescset.h"
65 #include "actions/valuedescexport.h"
66 #include "actions/valuedescconvert.h"
67 #include "actions/valuedescconnect.h"
68 #include "actions/valuedescdisconnect.h"
69 #include "actions/valuedesclink.h"
70
71 #include "actions/waypointadd.h"
72 #include "actions/waypointset.h"
73 #include "actions/waypointsetsmart.h"
74 #include "actions/waypointremove.h"
75
76 #include "actions/activepointadd.h"
77 #include "actions/activepointset.h"
78 #include "actions/activepointsetsmart.h"
79 #include "actions/activepointsetoff.h"
80 #include "actions/activepointseton.h"
81 #include "actions/activepointremove.h"
82
83 #include "actions/keyframeadd.h"
84 #include "actions/keyframeset.h"
85 #include "actions/keyframeremove.h"
86 #include "actions/keyframeduplicate.h"
87 #include "actions/keyframewaypointset.h"
88 #include "actions/keyframesetdelta.h"
89
90 #include "actions/timepointsmove.h"
91 #include "actions/timepointscopy.h"
92 #include "actions/timepointsdelete.h"
93
94 #include "actions/canvasrenddescset.h"
95 #include "actions/canvasadd.h"
96 #include "actions/canvasremove.h"
97
98 #include "actions/editmodeset.h"
99
100 #include "actions/blinepointtangentmerge.h"
101 #include "actions/blinepointtangentsplit.h"
102
103 #include "actions/gradientset.h"
104 #include "actions/colorset.h"
105
106 #include "actions/groupaddlayers.h"
107 #include "actions/groupremovelayers.h"
108 #include "actions/groupremove.h"
109 #include "actions/grouprename.h"
110
111 #include "canvasinterface.h"
112
113 #include "general.h"
114
115 #endif
116
117 using namespace std;
118 using namespace etl;
119 using namespace synfig;
120 using namespace synfigapp;
121 using namespace Action;
122
123 /* === P R O C E D U R E S ================================================= */
124
125 /* === S T A T I C S ======================================================= */
126
127 synfigapp::Action::Book *book_;
128
129 /* === M E T H O D S ======================================================= */
130
131 #define ADD_ACTION(x) { BookEntry &be(book()[x::name__]); \
132         be.name=x::name__; \
133         be.local_name=dgettext("synfigstudio",x::local_name__); \
134         be.version=x::version__; \
135         be.task=x::task__; \
136         be.priority=x::priority__; \
137         be.category=x::category__; \
138         be.factory=x::create; \
139         be.get_param_vocab=x::get_param_vocab; \
140         be.is_candidate=x::is_candidate; \
141         }
142
143
144 Action::Main::Main()
145 {
146         book_=new synfigapp::Action::Book();
147
148         ADD_ACTION(Action::LayerRemove);
149         ADD_ACTION(Action::LayerMove);
150         ADD_ACTION(Action::LayerRaise);
151         ADD_ACTION(Action::LayerLower);
152         ADD_ACTION(Action::LayerAdd);
153         ADD_ACTION(Action::LayerActivate);
154         ADD_ACTION(Action::LayerParamSet);
155         ADD_ACTION(Action::LayerParamConnect);
156         ADD_ACTION(Action::LayerParamDisconnect);
157         ADD_ACTION(Action::LayerEncapsulate);
158         ADD_ACTION(Action::LayerDuplicate);
159         ADD_ACTION(Action::LayerSetDesc);
160
161         ADD_ACTION(Action::ValueNodeConstSet);
162         ADD_ACTION(Action::ValueNodeAdd);
163         ADD_ACTION(Action::ValueNodeReplace);
164         ADD_ACTION(Action::ValueNodeLinkConnect);
165         ADD_ACTION(Action::ValueNodeLinkDisconnect);
166         ADD_ACTION(Action::ValueNodeDynamicListInsert);
167         ADD_ACTION(Action::ValueNodeDynamicListRemove);
168         ADD_ACTION(Action::ValueNodeDynamicListInsertSmart);
169         ADD_ACTION(Action::ValueNodeDynamicListRemoveSmart);
170         ADD_ACTION(Action::ValueNodeDynamicListLoop);
171         ADD_ACTION(Action::ValueNodeDynamicListUnLoop);
172         ADD_ACTION(Action::ValueNodeDynamicListRotateOrder);
173         ADD_ACTION(Action::ValueNodeRename);
174         ADD_ACTION(Action::ValueNodeRemove);
175
176         ADD_ACTION(Action::ValueDescSet);
177         ADD_ACTION(Action::ValueDescExport);
178         ADD_ACTION(Action::ValueDescConvert);
179         ADD_ACTION(Action::ValueDescConnect);
180         ADD_ACTION(Action::ValueDescDisconnect);
181         ADD_ACTION(Action::ValueDescLink);
182
183         ADD_ACTION(Action::WaypointAdd);
184         ADD_ACTION(Action::WaypointSet);
185         ADD_ACTION(Action::WaypointSetSmart);
186         ADD_ACTION(Action::WaypointRemove);
187
188         ADD_ACTION(Action::ActivepointAdd);
189         ADD_ACTION(Action::ActivepointSet);
190         ADD_ACTION(Action::ActivepointSetSmart);
191         ADD_ACTION(Action::ActivepointSetOn);
192         ADD_ACTION(Action::ActivepointSetOff);
193         ADD_ACTION(Action::ActivepointRemove);
194
195         ADD_ACTION(Action::KeyframeAdd);
196         ADD_ACTION(Action::KeyframeSet);
197         ADD_ACTION(Action::KeyframeRemove);
198         ADD_ACTION(Action::KeyframeDuplicate);
199         ADD_ACTION(Action::KeyframeWaypointSet);
200         ADD_ACTION(Action::KeyframeSetDelta);
201
202         ADD_ACTION(Action::CanvasRendDescSet);
203         ADD_ACTION(Action::CanvasAdd);
204         ADD_ACTION(Action::CanvasRemove);
205
206         ADD_ACTION(Action::EditModeSet);
207
208         ADD_ACTION(Action::BLinePointTangentMerge);
209         ADD_ACTION(Action::BLinePointTangentSplit);
210
211         ADD_ACTION(Action::GradientSet);
212         ADD_ACTION(Action::ColorSet);
213
214         ADD_ACTION(Action::TimepointsMove);
215         ADD_ACTION(Action::TimepointsCopy);
216         ADD_ACTION(Action::TimepointsDelete);
217
218         ADD_ACTION(Action::GroupAddLayers);
219         ADD_ACTION(Action::GroupRemoveLayers);
220         ADD_ACTION(Action::GroupRemove);
221         ADD_ACTION(Action::GroupRename);
222 }
223
224 Action::Main::~Main()
225 {
226         delete book_;
227
228 }
229
230
231 Action::Book& Action::book() { return *book_; }
232
233
234 Action::Handle
235 Action::create(const synfig::String &name)
236 {
237         if(!book().count(name))
238                 return 0; //! \todo perhaps we should throw something instead?
239         return book()[name].factory();
240 }
241
242
243 Action::CandidateList
244 Action::compile_candidate_list(const ParamList& param_list, Category category)
245 {
246         Action::CandidateList ret;
247
248         Book::const_iterator iter;
249
250         //synfig::info("param_list.size()=%d",param_list.size());
251
252         for(iter=book().begin();iter!=book().end();++iter)
253         {
254                 if((iter->second.category&category))
255                 {
256                         if(iter->second.is_candidate(param_list))
257                                 ret.push_back(iter->second);
258                         else
259                         {
260                                 //synfig::info("Action \"%s\" is not a candidate",iter->second.name.c_str());
261                         }
262                 }
263         }
264
265         return ret;
266 }
267
268 Action::CandidateList::iterator
269 Action::CandidateList::find(const synfig::String& x)
270 {
271         iterator iter;
272         for(iter=begin();iter!=end();++iter)
273                 if(iter->name==x)
274                         break;
275         return iter;
276 }
277
278 void
279 Action::Base::set_param_list(const ParamList &param_list)
280 {
281         ParamList::const_iterator iter;
282
283         for(iter=param_list.begin();iter!=param_list.end();++iter)
284                 set_param(iter->first,iter->second);
285 }
286
287 synfig::String
288 Action::Base::get_layer_descriptions(const std::list<synfig::Layer::Handle> layers, synfig::String singular_prefix, synfig::String plural_prefix)
289 {
290         String ret;
291         bool first = true;
292
293         if (plural_prefix.empty())
294                 plural_prefix = singular_prefix;
295
296         if (layers.empty())
297                 return plural_prefix;
298
299         if (layers.size() == 1)
300                 ret = singular_prefix;
301         else
302                 ret = plural_prefix;
303
304         if (!ret.empty())
305                 ret.push_back(' ');
306
307         for(std::list<synfig::Layer::Handle>::const_iterator iter=layers.begin(); iter!=layers.end(); ++iter)
308         {
309                 if (first)
310                         first = false;
311                 else
312                         ret += ", ";
313
314                 ret += strprintf("'%s'", (*iter)->get_non_empty_description().c_str());
315         }
316
317         return ret;
318 }
319
320 synfig::String
321 Action::Base::get_layer_descriptions(const std::list<std::pair<synfig::Layer::Handle,int> > layers, synfig::String singular_prefix, synfig::String plural_prefix)
322 {
323         String ret;
324         bool first = true;
325
326         if (plural_prefix.empty())
327                 plural_prefix = singular_prefix;
328
329         if (layers.empty())
330                 return plural_prefix;
331
332         if (layers.size() == 1)
333                 ret = singular_prefix;
334         else
335                 ret = plural_prefix;
336
337         if (!ret.empty())
338                 ret.push_back(' ');
339
340         for(std::list<std::pair<synfig::Layer::Handle,int> >::const_iterator iter=layers.begin(); iter!=layers.end(); ++iter)
341         {
342                 if (first)
343                         first = false;
344                 else
345                         ret += ", ";
346
347                 ret += strprintf("'%s'", iter->first->get_non_empty_description().c_str());
348         }
349
350         return ret;
351 }
352
353 void
354 Super::perform()
355 {
356         set_dirty(false);
357
358         prepare();
359
360         ActionList::const_iterator iter;
361         for(iter=action_list_.begin();iter!=action_list_.end();++iter)
362         {
363                 try
364                 {
365                         try
366                         {
367                                 (*iter)->perform();
368                                 CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
369                                 if(canvas_specific && canvas_specific->is_dirty())
370                                         set_dirty(true);
371                         }
372                         catch(...)
373                         {
374                                 if(iter!=action_list_.begin())
375                                 {
376                                         for(--iter;iter!=action_list_.begin();--iter)
377                                                 (*iter)->undo();
378                                         (*iter)->undo();
379                                 }
380                                 throw;
381                         }
382                 }
383                 catch(Error x)
384                 {
385                         throw Error(x.get_type(),((*iter)->get_name()+": "+x.get_desc()).c_str());
386                 }
387         }
388 }
389
390 void
391 Super::undo()
392 {
393         set_dirty(false);
394
395         ActionList::const_reverse_iterator iter;
396         for(iter=const_cast<const ActionList &>(action_list_).rbegin();iter!=const_cast<const ActionList &>(action_list_).rend();++iter)
397         {
398                 try {
399                         (*iter)->undo();
400                         CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
401                         if(canvas_specific && canvas_specific->is_dirty())
402                                 set_dirty(true);
403                 }
404                 catch(...)
405                 {
406                         if(iter!=const_cast<const ActionList &>(action_list_).rbegin())
407                         {
408                                 for(--iter;iter!=const_cast<const ActionList &>(action_list_).rbegin();--iter)
409                                         (*iter)->perform();
410                                 (*iter)->perform();
411                         }
412                         throw;
413                 }
414         }
415 }
416
417 void
418 Super::add_action(etl::handle<Undoable> action)
419 {
420         action_list_.push_back(action);
421         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
422         if(specific_action && !get_canvas())
423                 set_canvas(specific_action->get_canvas());
424 }
425
426 void
427 Super::add_action_front(etl::handle<Undoable> action)
428 {
429         action_list_.push_front(action);
430         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
431         if(specific_action && !get_canvas())
432                 set_canvas(specific_action->get_canvas());
433 }
434
435
436 Group::Group(const synfig::String &str):
437         name_(str),
438         ready_(true)
439 {
440 }
441
442 Group::~Group()
443 {
444 }
445
446
447
448
449 Action::ParamVocab
450 Action::CanvasSpecific::get_param_vocab()
451 {
452         ParamVocab ret;
453
454         ret.push_back(ParamDesc("canvas",Param::TYPE_CANVAS)
455                 .set_local_name(_("Canvas"))
456                 .set_desc(_("Selected Canvas"))
457         );
458
459         ret.push_back(ParamDesc("canvas_interface",Param::TYPE_CANVASINTERFACE)
460                 .set_local_name(_("Canvas Interface"))
461                 .set_desc(_("Canvas Interface"))
462                 .set_optional(true)
463         );
464
465
466         return ret;
467 }
468
469 bool
470 CanvasSpecific::set_param(const synfig::String& name, const Param &param)
471 {
472         if(name=="canvas" && param.get_type()==Param::TYPE_CANVAS)
473         {
474                 if(!param.get_canvas())
475                         return false;
476                 set_canvas(param.get_canvas());
477
478                 return true;
479         }
480         if(name=="canvas_interface" && param.get_type()==Param::TYPE_CANVASINTERFACE)
481         {
482                 if(!param.get_canvas_interface())
483                         return false;
484                 set_canvas_interface(param.get_canvas_interface());
485                 if(!get_canvas())
486                         set_canvas(get_canvas_interface()->get_canvas());
487
488                 return true;
489         }
490         if(name=="edit_mode" && param.get_type()==Param::TYPE_EDITMODE)
491         {
492                 set_edit_mode(param.get_edit_mode());
493
494                 return true;
495         }
496
497         return false;
498 }
499
500 bool
501 CanvasSpecific::is_ready()const
502 {
503         if(!get_canvas())
504                 return false;
505         return true;
506 }
507
508 EditMode
509 CanvasSpecific::get_edit_mode()const
510 {
511         if(mode_!=MODE_UNDEFINED)
512                 return mode_;
513
514         if(get_canvas_interface())
515                 return get_canvas_interface()->get_mode();
516
517         return MODE_NORMAL;
518 }