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