1 /* === S Y N F I G ========================================================= */
2 /*! \file keyframetreestore.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "keyframetreestore.h"
33 #include <synfig/valuenode.h>
34 #include "iconcontroler.h"
35 #include <synfig/valuenode_timedswap.h>
36 #include <gtkmm/button.h>
37 #include <gtkmm/treerowreference.h>
38 #include <synfig/canvas.h>
39 #include <synfig/keyframe.h>
42 #include <ETL/smart_ptr>
43 #include <synfigapp/action.h>
44 #include <synfigapp/instance.h>
45 #include "onemoment.h"
46 #include <synfig/exception.h>
50 /* === U S I N G =========================================================== */
54 using namespace synfig;
55 using namespace studio;
57 /* === M A C R O S ========================================================= */
59 /* === G L O B A L S ======================================================= */
61 KeyframeTreeStore_Class KeyframeTreeStore::keyframe_tree_store_class_;
63 /* === C L A S S E S & S T R U C T S ======================================= */
65 struct _keyframe_iterator
67 synfig::KeyframeList::iterator iter;
73 Gtk::TreeModel::iterator keyframe_iter_2_model_iter(synfig::KeyframeList::iterator iter,int index)
75 Gtk::TreeModel::iterator ret;
77 _keyframe_iterator*& data(static_cast<_keyframe_iterator*&>(ret->gobj()->user_data));
78 data=new _keyframe_iterator();
87 synfig::KeyframeList::iterator model_iter_2_keyframe_iter(Gtk::TreeModel::iterator iter)
89 _keyframe_iterator* data(static_cast<_keyframe_iterator*>(iter->gobj()->user_data));
91 throw std::runtime_error("bad data");
95 int get_index_from_model_iter(Gtk::TreeModel::iterator iter)
97 _keyframe_iterator* data(static_cast<_keyframe_iterator*>(iter->gobj()->user_data));
99 throw std::runtime_error("bad data");
105 #ifndef TreeRowReferenceHack
106 class TreeRowReferenceHack
108 GtkTreeRowReference *gobject_;
110 TreeRowReferenceHack():
115 TreeRowReferenceHack(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::Path& path):
116 gobject_ ( gtk_tree_row_reference_new(model->gobj(), const_cast<GtkTreePath*>(path.gobj())) )
120 TreeRowReferenceHack(const TreeRowReferenceHack &x):
121 gobject_ ( x.gobject_?gtk_tree_row_reference_copy(x.gobject_):0 )
126 void swap(TreeRowReferenceHack & other)
128 GtkTreeRowReference *const temp = gobject_;
129 gobject_ = other.gobject_;
130 other.gobject_ = temp;
133 const TreeRowReferenceHack &
134 operator=(const TreeRowReferenceHack &rhs)
136 TreeRowReferenceHack temp (rhs);
141 ~TreeRowReferenceHack()
144 gtk_tree_row_reference_free(gobject_);
147 Gtk::TreeModel::Path get_path() { return Gtk::TreeModel::Path(gtk_tree_row_reference_get_path(gobject_),false); }
148 GtkTreeRowReference *gobj() { return gobject_; }
153 /* === P R O C E D U R E S ================================================= */
155 void clear_iterator(GtkTreeIter* iter)
158 iter->user_data=iter->user_data2=iter->user_data3=0;
161 /* === M E T H O D S ======================================================= */
164 KeyframeTreeStore_Class::init()
168 class_init_func_ = &KeyframeTreeStore_Class::class_init_function;
170 const GTypeInfo derived_info =
172 sizeof(GObjectClass),
184 gtype_ = g_type_register_static(G_TYPE_OBJECT, "KeyframeTreeStore", &derived_info, GTypeFlags(0));
185 Gtk::TreeModel::add_interface(get_type());
191 KeyframeTreeStore_Class::class_init_function(gpointer g_class, gpointer class_data)
196 KeyframeTreeStore::KeyframeTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
197 Glib::ObjectBase ("KeyframeTreeStore"),
198 Glib::Object (Glib::ConstructParams(keyframe_tree_store_class_.init(), (char*) 0)),
199 canvas_interface_ (canvas_interface_)
202 //reset_path_table();
204 canvas_interface()->signal_keyframe_added().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::add_keyframe));
205 canvas_interface()->signal_keyframe_removed().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::remove_keyframe));
206 canvas_interface()->signal_keyframe_changed().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::change_keyframe));
209 KeyframeTreeStore::~KeyframeTreeStore()
211 synfig::info("KeyframeTreeStore::~KeyframeTreeStore(): Deleted");
214 Glib::RefPtr<KeyframeTreeStore>
215 KeyframeTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
217 KeyframeTreeStore *store(new KeyframeTreeStore(canvas_interface_));
218 Glib::RefPtr<KeyframeTreeStore> ret(store);
224 KeyframeTreeStore::reset_stamp()
226 stamp_=time(0)+reinterpret_cast<long>(this);
231 KeyframeTreeStore::reset_path_table()
233 Gtk::TreeModel::Children::iterator iter;
234 const Gtk::TreeModel::Children children(children());
236 for(iter = children.begin(); iter != children.end(); ++iter)
238 Gtk::TreeModel::Row row(*iter);
239 path_table_[(Keyframe)row[model.keyframe]]=TreeRowReferenceHack(Glib::RefPtr<KeyframeTreeStore>(this),Gtk::TreePath(row));
246 KeyframeTreeStore::iterator_sane(const GtkTreeIter* iter)const
248 if(iter && iter->stamp==stamp_)
250 g_warning("KeyframeTreeStore::iterator_sane(): Bad iterator stamp");
255 KeyframeTreeStore::iterator_sane(const Gtk::TreeModel::iterator& iter)const
257 return iterator_sane(iter->gobj());
261 KeyframeTreeStore::dump_iterator(const GtkTreeIter* gtk_iter, const Glib::ustring &name)const
266 g_warning("KeyframeTreeStore::dump_iterator: \"%s\" is NULL (Root?)",name.c_str());
270 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
272 if(gtk_iter->stamp!=stamp_ || !iter)
274 g_warning("KeyframeTreeStore::dump_iterator: \"%s\" is INVALID",name.c_str());
278 if((unsigned)iter->index>=canvas_interface()->get_canvas()->keyframe_list().size())
279 g_warning("KeyframeTreeStore::dump_iterator: \"%s\"(%p) has bad index(index:%d)",name.c_str(),gtk_iter,iter->index);
281 g_warning("KeyframeTreeStore::dump_iterator: \"%s\"(%p) ref:%d, index:%d, time:%s",name.c_str(),gtk_iter,iter->ref_count,iter->index,iter->iter->get_time().get_string().c_str());
286 KeyframeTreeStore::dump_iterator(const Gtk::TreeModel::iterator& iter, const Glib::ustring &name)const
288 dump_iterator(iter->gobj(),name);
292 KeyframeTreeStore::time_sorter(const Gtk::TreeModel::iterator &rhs,const Gtk::TreeModel::iterator &lhs)
296 _keyframe_iterator *rhs_iter(static_cast<_keyframe_iterator*>(rhs->gobj()->user_data));
297 _keyframe_iterator *lhs_iter(static_cast<_keyframe_iterator*>(lhs->gobj()->user_data));
299 Time diff(rhs_iter->iter->get_time()-lhs_iter->iter->get_time());
308 KeyframeTreeStore::set_value_impl(const Gtk::TreeModel::iterator& row, int column, const Glib::ValueBase& value)
310 if(!iterator_sane(row))
313 if(column>=get_n_columns_vfunc())
315 g_warning("KeyframeTreeStore::set_value_impl: Bad column (%d)",column);
319 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
321 g_warning("KeyframeTreeStore::set_value_impl: Bad value type");
325 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(row.gobj()->user_data));
329 if(column==model.time_delta.index())
331 Glib::Value<synfig::Time> x;
332 g_value_init(x.gobj(),model.time.type());
333 g_value_copy(value.gobj(),x.gobj());
335 Time new_delta(x.get());
336 if(new_delta<=Time::zero()+Time::epsilon())
342 Time old_delta((*row)[model.time_delta]);
343 if(old_delta<=Time::zero()+Time::epsilon())
348 //Gtk::TreeModel::iterator row(row);
352 Time change_delta(new_delta-old_delta);
354 if(change_delta<=Time::zero()+Time::epsilon() &&change_delta>=Time::zero()-Time::epsilon())
356 // Not an error, just no change
361 Keyframe keyframe((*row)[model.keyframe]);
362 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set_delta"));
366 action->set_param("canvas",canvas_interface()->get_canvas());
367 action->set_param("canvas_interface",canvas_interface());
368 action->set_param("keyframe",keyframe);
369 action->set_param("delta",change_delta);
371 canvas_interface()->get_instance()->perform_action(action);
377 if(column==model.time.index())
379 OneMoment one_moment;
381 Glib::Value<synfig::Time> x;
382 g_value_init(x.gobj(),model.time.type());
383 g_value_copy(value.gobj(),x.gobj());
384 synfig::Keyframe keyframe(*iter->iter);
386 synfig::info("KeyframeTreeStore::set_value_impl():old_time=%s",keyframe.get_time().get_string().c_str());
387 keyframe.set_time(x.get());
388 synfig::info("KeyframeTreeStore::set_value_impl():new_time=%s",keyframe.get_time().get_string().c_str());
390 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set"));
395 action->set_param("canvas",canvas_interface()->get_canvas());
396 action->set_param("canvas_interface",canvas_interface());
397 action->set_param("keyframe",keyframe);
399 canvas_interface()->get_instance()->perform_action(action);
401 else if(column==model.description.index())
403 Glib::Value<Glib::ustring> x;
404 g_value_init(x.gobj(),model.description.type());
405 g_value_copy(value.gobj(),x.gobj());
406 synfig::Keyframe keyframe(*iter->iter);
407 keyframe.set_description(x.get());
409 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set"));
414 action->set_param("canvas",canvas_interface()->get_canvas());
415 action->set_param("canvas_interface",canvas_interface());
416 action->set_param("keyframe",keyframe);
418 canvas_interface()->get_instance()->perform_action(action);
420 else if(column==model.keyframe.index())
422 g_warning("KeyframeTreeStore::set_value_impl: This column is read-only");
429 catch(std::exception x)
436 KeyframeTreeStore::get_flags_vfunc ()
438 return Gtk::TREE_MODEL_LIST_ONLY;
442 KeyframeTreeStore::get_n_columns_vfunc ()
448 KeyframeTreeStore::get_column_type_vfunc (int index)
450 return model.types()[index];
454 KeyframeTreeStore::iter_next_vfunc (const iterator& xiter, iterator& iter_next) const
456 if(!iterator_sane(xiter)) return false;
458 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(xiter.gobj()->user_data));
460 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
463 _keyframe_iterator *next(new _keyframe_iterator());
464 iter_next.gobj()->user_data=static_cast<gpointer>(next);
466 next->index=iter->index+1;
467 next->iter=iter->iter;
470 if(next->iter==canvas_interface()->get_canvas()->keyframe_list().end())
473 iter_next.gobj()->stamp=stamp_;
480 KeyframeTreeStore::iter_next_vfunc (GtkTreeIter* gtk_iter)
482 if(!iterator_sane(gtk_iter)) return false;
484 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
486 // If we are already at the end, then we are very invalid
487 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
492 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
502 KeyframeTreeStore::iter_children_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* parent)
504 dump_iterator(gtk_iter,"gtk_iter");
505 dump_iterator(parent,"parent");
507 if(!parent || !iterator_sane(parent))
509 clear_iterator(gtk_iter);
513 _keyframe_iterator *iter(new _keyframe_iterator());
516 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
518 gtk_iter->user_data=static_cast<gpointer>(iter);
519 gtk_iter->stamp=stamp_;
525 KeyframeTreeStore::iter_has_child_vfunc (const GtkTreeIter*parent)
527 dump_iterator(parent,"parent");
536 KeyframeTreeStore::iter_n_children_vfunc (const GtkTreeIter* parent)
538 dump_iterator(parent,"parent");
543 return canvas_interface()->get_canvas()->keyframe_list().size();
548 KeyframeTreeStore::iter_n_root_children_vfunc () const
550 return canvas_interface()->get_canvas()->keyframe_list().size();
554 KeyframeTreeStore::iter_nth_root_child_vfunc (int n, iterator& xiter)const
556 if(canvas_interface()->get_canvas()->keyframe_list().size()==0)
563 g_warning("KeyframeTreeStore::iter_nth_root_child_vfunc: Out of range (negative index)");
566 if(n && (unsigned)n>=canvas_interface()->get_canvas()->keyframe_list().size())
568 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: Out of range (large index)");
572 _keyframe_iterator *iter(new _keyframe_iterator());
575 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
578 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
580 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: >>>BUG<<< in %s on line %d",__FILE__,__LINE__);
586 xiter.gobj()->user_data=static_cast<gpointer>(iter);
587 xiter.gobj()->stamp=stamp_;
593 KeyframeTreeStore::iter_nth_child_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* parent, int n)
595 dump_iterator(parent,"parent");
599 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: I am a list");
600 clear_iterator(gtk_iter);
606 _keyframe_iterator *iter(new _keyframe_iterator());
609 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
612 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
614 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: >>>BUG<<< in %s on line %d",__FILE__,__LINE__);
616 clear_iterator(gtk_iter);
622 gtk_iter->user_data=static_cast<gpointer>(iter);
623 gtk_iter->stamp=stamp_;
628 KeyframeTreeStore::iter_parent_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* child)
630 dump_iterator(child,"child");
631 iterator_sane(child);
632 clear_iterator(gtk_iter);
638 KeyframeTreeStore::ref_node_vfunc (iterator& xiter)const
640 GtkTreeIter* gtk_iter(xiter.gobj());
641 if(!gtk_iter || !iterator_sane(gtk_iter)) return;
643 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
648 KeyframeTreeStore::unref_node_vfunc (iterator& xiter)const
650 GtkTreeIter* gtk_iter(xiter.gobj());
651 if(!gtk_iter || !iterator_sane(gtk_iter)) return;
653 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
659 // Make this iterator invalid
665 KeyframeTreeStore::get_path_vfunc (const TreeModel::iterator& gtk_iter)const
667 Gtk::TreeModel::Path path;
669 // If this is the root node, then return
671 if(!iterator_sane(gtk_iter))
674 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->gobj()->user_data));
676 path.append_index(iter->index);
682 KeyframeTreeStore::get_iter_vfunc (const Gtk::TreeModel::Path& path, iterator& iter)const
684 if(path.get_depth()>=1)
685 return iter_nth_root_child_vfunc(path.front(),iter);
688 g_warning("KeyframeTreeStore::get_iter_vfunc(): Bad path \"%s\"",path.to_string().c_str());
689 //clear_iterator(iter);
694 KeyframeTreeStore::iter_is_valid (const iterator& iter) const
696 return iterator_sane(iter);
700 KeyframeTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& gtk_iter, int column, Glib::ValueBase& value)const
702 dump_iterator(gtk_iter,"gtk_iter");
703 if(!iterator_sane(gtk_iter))
706 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->gobj()->user_data));
712 Glib::Value<synfig::Time> x;
713 g_value_init(x.gobj(),x.value_type());
714 x.set(iter->iter->get_time());
715 g_value_init(value.gobj(),x.value_type());
716 g_value_copy(x.gobj(),value.gobj());
719 case 3: // Time Delta
721 Glib::Value<synfig::Time> x;
722 g_value_init(x.gobj(),x.value_type());
724 synfig::Keyframe prev_keyframe(*iter->iter);
725 synfig::Keyframe keyframe;
727 KeyframeList::iterator tmp(iter->iter);
729 if(tmp==get_canvas()->keyframe_list().end())
732 g_value_init(value.gobj(),x.value_type());
733 g_value_copy(x.gobj(),value.gobj());
741 delta=keyframe.get_time()-prev_keyframe.get_time();
744 g_value_init(value.gobj(),x.value_type());
745 g_value_copy(x.gobj(),value.gobj());
748 case 1: // Description
750 g_value_init(value.gobj(),G_TYPE_STRING);
751 g_value_set_string(value.gobj(),iter->iter->get_description().c_str());
756 Glib::Value<synfig::Keyframe> x;
757 g_value_init(x.gobj(),x.value_type());
759 g_value_init(value.gobj(),x.value_type());
760 g_value_copy(x.gobj(),value.gobj());
769 KeyframeTreeStore::find_row(const synfig::Keyframe &keyframe)
771 Gtk::TreeModel::Row row(*(children().begin()));
772 dump_iterator(row,"find_row,begin");
773 const GtkTreeIter *gtk_iter(row.gobj());
774 if(!iterator_sane(gtk_iter))
775 throw std::runtime_error(_("Unable to find Keyframe in table"));
777 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
779 synfig::KeyframeList &keyframe_list(canvas_interface()->get_canvas()->keyframe_list());
780 if(keyframe_list.empty())
781 throw std::runtime_error(_("There are no keyframes n this canvas"));
785 for(iter->iter=keyframe_list.begin();iter->iter!=keyframe_list.end() && *iter->iter!=keyframe;++iter->iter)
789 if(iter->iter==keyframe_list.end())
790 throw std::runtime_error(_("Unable to find Keyframe in table"));
795 KeyframeTreeStore::add_keyframe(Keyframe keyframe)
799 Gtk::TreeRow row(find_row(keyframe));
800 dump_iterator(row.gobj(),"add_keyframe,row");
801 Gtk::TreePath path(get_path(row));
803 row_inserted(path,row);
805 old_keyframe_list=get_canvas()->keyframe_list();
806 //old_keyframe_list.add(keyframe);
807 //old_keyframe_list.sort();
809 catch(std::exception x)
816 KeyframeTreeStore::remove_keyframe(Keyframe keyframe)
822 Gtk::TreeRow row(find_row(keyframe));
823 dump_iterator(row,"remove_keyframe,row");
824 Gtk::TreePath path(get_path(row));
827 old_keyframe_list.erase(keyframe);
831 g_warning("KeyframeTreeStore::remove_keyframe: Keyframe not in table");
834 catch(std::exception x)
842 KeyframeTreeStore::change_keyframe(Keyframe keyframe)
846 Gtk::TreeRow row(find_row(keyframe));
848 unsigned int new_index(get_index_from_model_iter(row));
849 unsigned int old_index(0);
850 synfig::KeyframeList::iterator iter;
851 for(old_index=0,iter=old_keyframe_list.begin();iter!=old_keyframe_list.end() && (UniqueID)*iter!=(UniqueID)keyframe;++iter,old_index++);
853 if(iter!=old_keyframe_list.end() && new_index!=old_index)
856 std::vector<int> new_order;
857 for(unsigned int i=0;i<old_keyframe_list.size();i++)
859 new_order.push_back(i);
861 if(new_order.size()>new_index)
863 new_order.erase(new_order.begin()+new_index);
864 new_order.insert(new_order.begin()+old_index,new_index);
866 //new_order[old_index]=
868 rows_reordered (Path(), iterator(), &new_order[0]);
870 old_keyframe_list=get_canvas()->keyframe_list();
872 row=find_row(keyframe);
875 dump_iterator(row,"change_keyframe,row");
876 row_changed(get_path(row),row);
878 catch(std::exception x)