Fix a crash when running single-threaded and dragging the time slider.
[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 #endif
114
115 using namespace std;
116 using namespace etl;
117 using namespace synfig;
118 using namespace synfigapp;
119 using namespace Action;
120
121 /* === P R O C E D U R E S ================================================= */
122
123 /* === S T A T I C S ======================================================= */
124
125 synfigapp::Action::Book *book_;
126
127 /* === M E T H O D S ======================================================= */
128
129 #define ADD_ACTION(x) { BookEntry &be(book()[x::name__]); \
130         be.name=x::name__; \
131         be.local_name=x::local_name__; \
132         be.version=x::version__; \
133         be.task=x::task__; \
134         be.priority=x::priority__; \
135         be.category=x::category__; \
136         be.factory=x::create; \
137         be.get_param_vocab=x::get_param_vocab; \
138         be.is_candidate=x::is_candidate; \
139         }
140
141
142 Action::Main::Main()
143 {
144         book_=new synfigapp::Action::Book();
145
146         ADD_ACTION(Action::LayerRemove);
147         ADD_ACTION(Action::LayerMove);
148         ADD_ACTION(Action::LayerRaise);
149         ADD_ACTION(Action::LayerLower);
150         ADD_ACTION(Action::LayerAdd);
151         ADD_ACTION(Action::LayerActivate);
152         ADD_ACTION(Action::LayerParamSet);
153         ADD_ACTION(Action::LayerParamConnect);
154         ADD_ACTION(Action::LayerParamDisconnect);
155         ADD_ACTION(Action::LayerEncapsulate);
156         ADD_ACTION(Action::LayerDuplicate);
157         ADD_ACTION(Action::LayerSetDesc);
158
159         ADD_ACTION(Action::ValueNodeConstSet);
160         ADD_ACTION(Action::ValueNodeAdd);
161         ADD_ACTION(Action::ValueNodeReplace);
162         ADD_ACTION(Action::ValueNodeLinkConnect);
163         ADD_ACTION(Action::ValueNodeLinkDisconnect);
164         ADD_ACTION(Action::ValueNodeDynamicListInsert);
165         ADD_ACTION(Action::ValueNodeDynamicListRemove);
166         ADD_ACTION(Action::ValueNodeDynamicListInsertSmart);
167         ADD_ACTION(Action::ValueNodeDynamicListRemoveSmart);
168         ADD_ACTION(Action::ValueNodeDynamicListLoop);
169         ADD_ACTION(Action::ValueNodeDynamicListUnLoop);
170         ADD_ACTION(Action::ValueNodeDynamicListRotateOrder);
171         ADD_ACTION(Action::ValueNodeRename);
172         ADD_ACTION(Action::ValueNodeRemove);
173
174         ADD_ACTION(Action::ValueDescSet);
175         ADD_ACTION(Action::ValueDescExport);
176         ADD_ACTION(Action::ValueDescConvert);
177         ADD_ACTION(Action::ValueDescConnect);
178         ADD_ACTION(Action::ValueDescDisconnect);
179         ADD_ACTION(Action::ValueDescLink);
180
181         ADD_ACTION(Action::WaypointAdd);
182         ADD_ACTION(Action::WaypointSet);
183         ADD_ACTION(Action::WaypointSetSmart);
184         ADD_ACTION(Action::WaypointRemove);
185
186         ADD_ACTION(Action::ActivepointAdd);
187         ADD_ACTION(Action::ActivepointSet);
188         ADD_ACTION(Action::ActivepointSetSmart);
189         ADD_ACTION(Action::ActivepointSetOn);
190         ADD_ACTION(Action::ActivepointSetOff);
191         ADD_ACTION(Action::ActivepointRemove);
192
193         ADD_ACTION(Action::KeyframeAdd);
194         ADD_ACTION(Action::KeyframeSet);
195         ADD_ACTION(Action::KeyframeRemove);
196         ADD_ACTION(Action::KeyframeDuplicate);
197         ADD_ACTION(Action::KeyframeWaypointSet);
198         ADD_ACTION(Action::KeyframeSetDelta);
199
200         ADD_ACTION(Action::CanvasRendDescSet);
201         ADD_ACTION(Action::CanvasAdd);
202         ADD_ACTION(Action::CanvasRemove);
203
204         ADD_ACTION(Action::EditModeSet);
205
206         ADD_ACTION(Action::BLinePointTangentMerge);
207         ADD_ACTION(Action::BLinePointTangentSplit);
208
209         ADD_ACTION(Action::GradientSet);
210         ADD_ACTION(Action::ColorSet);
211
212         ADD_ACTION(Action::TimepointsMove);
213         ADD_ACTION(Action::TimepointsCopy);
214         ADD_ACTION(Action::TimepointsDelete);
215
216         ADD_ACTION(Action::GroupAddLayers);
217         ADD_ACTION(Action::GroupRemoveLayers);
218         ADD_ACTION(Action::GroupRemove);
219         ADD_ACTION(Action::GroupRename);
220 }
221
222 Action::Main::~Main()
223 {
224         delete book_;
225
226 }
227
228
229 Action::Book& Action::book() { return *book_; }
230
231
232 Action::Handle
233 Action::create(const String &name)
234 {
235         if(!book().count(name))
236                 return 0; //! \todo perhaps we should throw something instead?
237         return book()[name].factory();
238 }
239
240
241 Action::CandidateList
242 Action::compile_candidate_list(const ParamList& param_list, Category category)
243 {
244         Action::CandidateList ret;
245
246         Book::const_iterator iter;
247
248         //synfig::info("param_list.size()=%d",param_list.size());
249
250         for(iter=book().begin();iter!=book().end();++iter)
251         {
252                 if((iter->second.category&category))
253                 {
254                         if(iter->second.is_candidate(param_list))
255                                 ret.push_back(iter->second);
256                         else
257                         {
258                                 //synfig::info("Action \"%s\" is not a candidate",iter->second.name.c_str());
259                         }
260                 }
261         }
262
263         return ret;
264 }
265
266 Action::CandidateList::iterator
267 Action::CandidateList::find(const String& x)
268 {
269         iterator iter;
270         for(iter=begin();iter!=end();++iter)
271                 if(iter->name==x)
272                         break;
273         return iter;
274 }
275
276 void
277 Action::Base::set_param_list(const ParamList &param_list)
278 {
279         ParamList::const_iterator iter;
280
281         for(iter=param_list.begin();iter!=param_list.end();++iter)
282                 set_param(iter->first,iter->second);
283 }
284
285 void
286 Super::perform()
287 {
288         set_dirty(false);
289
290         prepare();
291
292         ActionList::const_iterator iter;
293         for(iter=action_list_.begin();iter!=action_list_.end();++iter)
294         {
295                 try
296                 {
297                         try
298                         {
299                                 (*iter)->perform();
300                                 CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
301                                 if(canvas_specific && canvas_specific->is_dirty())
302                                         set_dirty(true);
303                         }
304                         catch(...)
305                         {
306                                 if(iter!=action_list_.begin())
307                                 {
308                                         for(--iter;iter!=action_list_.begin();--iter)
309                                                 (*iter)->undo();
310                                         (*iter)->undo();
311                                 }
312                                 throw;
313                         }
314                 }
315                 catch(Error x)
316                 {
317                         throw Error(x.get_type(),((*iter)->get_name()+": "+x.get_desc()).c_str());
318                 }
319         }
320 }
321
322 void
323 Super::undo()
324 {
325         set_dirty(false);
326
327         ActionList::const_reverse_iterator iter;
328         for(iter=const_cast<const ActionList &>(action_list_).rbegin();iter!=const_cast<const ActionList &>(action_list_).rend();++iter)
329         {
330                 try {
331                         (*iter)->undo();
332                         CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
333                         if(canvas_specific && canvas_specific->is_dirty())
334                                 set_dirty(true);
335                 }
336                 catch(...)
337                 {
338                         if(iter!=const_cast<const ActionList &>(action_list_).rbegin())
339                         {
340                                 for(--iter;iter!=const_cast<const ActionList &>(action_list_).rbegin();--iter)
341                                         (*iter)->perform();
342                                 (*iter)->perform();
343                         }
344                         throw;
345                 }
346         }
347 }
348
349 void
350 Super::add_action(etl::handle<Undoable> action)
351 {
352         action_list_.push_back(action);
353         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
354         if(specific_action && !get_canvas())
355                 set_canvas(specific_action->get_canvas());
356 }
357
358 void
359 Super::add_action_front(etl::handle<Undoable> action)
360 {
361         action_list_.push_front(action);
362         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
363         if(specific_action && !get_canvas())
364                 set_canvas(specific_action->get_canvas());
365 }
366
367
368 Group::Group(const std::string &str):
369         name_(str),
370         ready_(true)
371 {
372 }
373
374 Group::~Group()
375 {
376 }
377
378
379
380
381 Action::ParamVocab
382 Action::CanvasSpecific::get_param_vocab()
383 {
384         ParamVocab ret;
385
386         ret.push_back(ParamDesc("canvas",Param::TYPE_CANVAS)
387                 .set_local_name(_("Canvas"))
388                 .set_desc(_("Selected Canvas"))
389         );
390
391         ret.push_back(ParamDesc("canvas_interface",Param::TYPE_CANVASINTERFACE)
392                 .set_local_name(_("Canvas Interface"))
393                 .set_desc(_("Canvas Interface"))
394                 .set_optional(true)
395         );
396
397
398         return ret;
399 }
400
401 bool
402 CanvasSpecific::set_param(const synfig::String& name, const Param &param)
403 {
404         if(name=="canvas" && param.get_type()==Param::TYPE_CANVAS)
405         {
406                 if(!param.get_canvas())
407                         return false;
408                 set_canvas(param.get_canvas());
409
410                 return true;
411         }
412         if(name=="canvas_interface" && param.get_type()==Param::TYPE_CANVASINTERFACE)
413         {
414                 if(!param.get_canvas_interface())
415                         return false;
416                 set_canvas_interface(param.get_canvas_interface());
417                 if(!get_canvas())
418                         set_canvas(get_canvas_interface()->get_canvas());
419
420                 return true;
421         }
422         if(name=="edit_mode" && param.get_type()==Param::TYPE_EDITMODE)
423         {
424                 set_edit_mode(param.get_edit_mode());
425
426                 return true;
427         }
428
429         return false;
430 }
431
432 bool
433 CanvasSpecific::is_ready()const
434 {
435         if(!get_canvas())
436                 return false;
437         return true;
438 }
439
440 EditMode
441 CanvasSpecific::get_edit_mode()const
442 {
443         if(mode_!=MODE_UNDEFINED)
444                 return mode_;
445
446         if(get_canvas_interface())
447                 return get_canvas_interface()->get_mode();
448
449         return MODE_NORMAL;
450 }