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