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
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "keyframetreestore.h"
34 #include <synfig/valuenode.h>
35 #include "iconcontroler.h"
36 #include <synfig/valuenode_timedswap.h>
37 #include <gtkmm/button.h>
38 #include <gtkmm/treerowreference.h>
39 #include <synfig/canvas.h>
40 #include <synfig/keyframe.h>
43 #include <ETL/smart_ptr>
44 #include <synfigapp/action.h>
45 #include <synfigapp/instance.h>
46 #include "onemoment.h"
47 #include <synfig/exception.h>
51 /* === U S I N G =========================================================== */
55 using namespace synfig;
56 using namespace studio;
58 /* === M A C R O S ========================================================= */
60 /* === G L O B A L S ======================================================= */
62 KeyframeTreeStore_Class KeyframeTreeStore::keyframe_tree_store_class_;
64 /* === C L A S S E S & S T R U C T S ======================================= */
66 struct _keyframe_iterator
68 synfig::KeyframeList::iterator iter;
74 Gtk::TreeModel::iterator keyframe_iter_2_model_iter(synfig::KeyframeList::iterator iter,int index)
76 Gtk::TreeModel::iterator ret;
78 _keyframe_iterator*& data(static_cast<_keyframe_iterator*&>(ret->gobj()->user_data));
79 data=new _keyframe_iterator();
88 synfig::KeyframeList::iterator model_iter_2_keyframe_iter(Gtk::TreeModel::iterator iter)
90 _keyframe_iterator* data(static_cast<_keyframe_iterator*>(iter->gobj()->user_data));
92 throw std::runtime_error("bad data");
96 int get_index_from_model_iter(Gtk::TreeModel::iterator iter)
98 _keyframe_iterator* data(static_cast<_keyframe_iterator*>(iter->gobj()->user_data));
100 throw std::runtime_error("bad data");
106 #ifndef TreeRowReferenceHack
107 class TreeRowReferenceHack
109 GtkTreeRowReference *gobject_;
111 TreeRowReferenceHack():
116 TreeRowReferenceHack(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::Path& path):
117 gobject_ ( gtk_tree_row_reference_new(model->gobj(), const_cast<GtkTreePath*>(path.gobj())) )
121 TreeRowReferenceHack(const TreeRowReferenceHack &x):
122 gobject_ ( x.gobject_?gtk_tree_row_reference_copy(x.gobject_):0 )
127 void swap(TreeRowReferenceHack & other)
129 GtkTreeRowReference *const temp = gobject_;
130 gobject_ = other.gobject_;
131 other.gobject_ = temp;
134 const TreeRowReferenceHack &
135 operator=(const TreeRowReferenceHack &rhs)
137 TreeRowReferenceHack temp (rhs);
142 ~TreeRowReferenceHack()
145 gtk_tree_row_reference_free(gobject_);
148 Gtk::TreeModel::Path get_path() { return Gtk::TreeModel::Path(gtk_tree_row_reference_get_path(gobject_),false); }
149 GtkTreeRowReference *gobj() { return gobject_; }
154 /* === P R O C E D U R E S ================================================= */
156 void clear_iterator(GtkTreeIter* iter)
159 iter->user_data=iter->user_data2=iter->user_data3=0;
162 /* === M E T H O D S ======================================================= */
165 KeyframeTreeStore_Class::init()
169 class_init_func_ = &KeyframeTreeStore_Class::class_init_function;
171 const GTypeInfo derived_info =
173 sizeof(GObjectClass),
185 gtype_ = g_type_register_static(G_TYPE_OBJECT, "KeyframeTreeStore", &derived_info, GTypeFlags(0));
186 Gtk::TreeModel::add_interface(get_type());
192 KeyframeTreeStore_Class::class_init_function(gpointer /*g_class*/, gpointer /*class_data*/)
197 KeyframeTreeStore::KeyframeTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
198 Glib::ObjectBase ("KeyframeTreeStore"),
199 Glib::Object (Glib::ConstructParams(keyframe_tree_store_class_.init(), (char*) 0)),
200 canvas_interface_ (canvas_interface_)
203 //reset_path_table();
205 canvas_interface()->signal_keyframe_added().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::add_keyframe));
206 canvas_interface()->signal_keyframe_removed().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::remove_keyframe));
207 canvas_interface()->signal_keyframe_changed().connect(sigc::mem_fun(*this,&studio::KeyframeTreeStore::change_keyframe));
210 KeyframeTreeStore::~KeyframeTreeStore()
212 synfig::info("KeyframeTreeStore::~KeyframeTreeStore(): Deleted");
215 Glib::RefPtr<KeyframeTreeStore>
216 KeyframeTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
218 KeyframeTreeStore *store(new KeyframeTreeStore(canvas_interface_));
219 Glib::RefPtr<KeyframeTreeStore> ret(store);
225 KeyframeTreeStore::reset_stamp()
227 stamp_=time(0)+reinterpret_cast<long>(this);
232 KeyframeTreeStore::reset_path_table()
234 Gtk::TreeModel::Children::iterator iter;
235 const Gtk::TreeModel::Children children(children());
237 for(iter = children.begin(); iter != children.end(); ++iter)
239 Gtk::TreeModel::Row row(*iter);
240 path_table_[(Keyframe)row[model.keyframe]]=TreeRowReferenceHack(Glib::RefPtr<KeyframeTreeStore>(this),Gtk::TreePath(row));
247 KeyframeTreeStore::iterator_sane(const GtkTreeIter* iter)const
249 if(iter && iter->stamp==stamp_)
251 g_warning("KeyframeTreeStore::iterator_sane(): Bad iterator stamp");
256 KeyframeTreeStore::iterator_sane(const Gtk::TreeModel::iterator& iter)const
258 return iterator_sane(iter->gobj());
262 KeyframeTreeStore::dump_iterator(const GtkTreeIter* /*gtk_iter*/, const Glib::ustring &/*name*/)const
267 g_warning("KeyframeTreeStore::dump_iterator: \"%s\" is NULL (Root?)",name.c_str());
271 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
273 if(gtk_iter->stamp!=stamp_ || !iter)
275 g_warning("KeyframeTreeStore::dump_iterator: \"%s\" is INVALID",name.c_str());
279 if((unsigned)iter->index>=canvas_interface()->get_canvas()->keyframe_list().size())
280 g_warning("KeyframeTreeStore::dump_iterator: \"%s\"(%p) has bad index(index:%d)",name.c_str(),gtk_iter,iter->index);
282 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());
287 KeyframeTreeStore::dump_iterator(const Gtk::TreeModel::iterator& iter, const Glib::ustring &name)const
289 dump_iterator(iter->gobj(),name);
293 KeyframeTreeStore::time_sorter(const Gtk::TreeModel::iterator &rhs,const Gtk::TreeModel::iterator &lhs)
297 _keyframe_iterator *rhs_iter(static_cast<_keyframe_iterator*>(rhs->gobj()->user_data));
298 _keyframe_iterator *lhs_iter(static_cast<_keyframe_iterator*>(lhs->gobj()->user_data));
300 Time diff(rhs_iter->iter->get_time()-lhs_iter->iter->get_time());
309 KeyframeTreeStore::set_value_impl(const Gtk::TreeModel::iterator& row, int column, const Glib::ValueBase& value)
311 if(!iterator_sane(row))
314 if(column>=get_n_columns_vfunc())
316 g_warning("KeyframeTreeStore::set_value_impl: Bad column (%d)",column);
320 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
322 g_warning("KeyframeTreeStore::set_value_impl: Bad value type");
326 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(row.gobj()->user_data));
330 if(column==model.time_delta.index())
332 Glib::Value<synfig::Time> x;
333 g_value_init(x.gobj(),model.time.type());
334 g_value_copy(value.gobj(),x.gobj());
336 Time new_delta(x.get());
337 if(new_delta<=Time::zero()+Time::epsilon())
343 Time old_delta((*row)[model.time_delta]);
344 if(old_delta<=Time::zero()+Time::epsilon())
349 // row(row) on the next line is bad - don't use it, because it leaves 'row' uninitialised
350 //Gtk::TreeModel::iterator row(row);
354 Time change_delta(new_delta-old_delta);
356 if(change_delta<=Time::zero()+Time::epsilon() &&change_delta>=Time::zero()-Time::epsilon())
358 // Not an error, just no change
363 Keyframe keyframe((*row)[model.keyframe]);
364 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set_delta"));
368 action->set_param("canvas",canvas_interface()->get_canvas());
369 action->set_param("canvas_interface",canvas_interface());
370 action->set_param("keyframe",keyframe);
371 action->set_param("delta",change_delta);
373 canvas_interface()->get_instance()->perform_action(action);
379 if(column==model.time.index())
381 OneMoment one_moment;
383 Glib::Value<synfig::Time> x;
384 g_value_init(x.gobj(),model.time.type());
385 g_value_copy(value.gobj(),x.gobj());
386 synfig::Keyframe keyframe(*iter->iter);
388 synfig::info("KeyframeTreeStore::set_value_impl():old_time=%s",keyframe.get_time().get_string().c_str());
389 keyframe.set_time(x.get());
390 synfig::info("KeyframeTreeStore::set_value_impl():new_time=%s",keyframe.get_time().get_string().c_str());
392 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set"));
397 action->set_param("canvas",canvas_interface()->get_canvas());
398 action->set_param("canvas_interface",canvas_interface());
399 action->set_param("keyframe",keyframe);
401 canvas_interface()->get_instance()->perform_action(action);
403 else if(column==model.description.index())
405 Glib::Value<Glib::ustring> x;
406 g_value_init(x.gobj(),model.description.type());
407 g_value_copy(value.gobj(),x.gobj());
408 synfig::Keyframe keyframe(*iter->iter);
409 keyframe.set_description(x.get());
411 synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_set"));
416 action->set_param("canvas",canvas_interface()->get_canvas());
417 action->set_param("canvas_interface",canvas_interface());
418 action->set_param("keyframe",keyframe);
420 canvas_interface()->get_instance()->perform_action(action);
422 else if(column==model.keyframe.index())
424 g_warning("KeyframeTreeStore::set_value_impl: This column is read-only");
431 catch(std::exception x)
438 KeyframeTreeStore::get_flags_vfunc ()
440 return Gtk::TREE_MODEL_LIST_ONLY;
444 KeyframeTreeStore::get_n_columns_vfunc ()
450 KeyframeTreeStore::get_column_type_vfunc (int index)
452 return model.types()[index];
456 KeyframeTreeStore::iter_next_vfunc (const iterator& xiter, iterator& iter_next) const
458 if(!iterator_sane(xiter)) return false;
460 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(xiter.gobj()->user_data));
462 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
465 _keyframe_iterator *next(new _keyframe_iterator());
466 iter_next.gobj()->user_data=static_cast<gpointer>(next);
468 next->index=iter->index+1;
469 next->iter=iter->iter;
472 if(next->iter==canvas_interface()->get_canvas()->keyframe_list().end())
475 iter_next.gobj()->stamp=stamp_;
482 KeyframeTreeStore::iter_next_vfunc (GtkTreeIter* gtk_iter)
484 if(!iterator_sane(gtk_iter)) return false;
486 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
488 // If we are already at the end, then we are very invalid
489 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
494 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
504 KeyframeTreeStore::iter_children_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* parent)
506 dump_iterator(gtk_iter,"gtk_iter");
507 dump_iterator(parent,"parent");
509 if(!parent || !iterator_sane(parent))
511 clear_iterator(gtk_iter);
515 _keyframe_iterator *iter(new _keyframe_iterator());
518 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
520 gtk_iter->user_data=static_cast<gpointer>(iter);
521 gtk_iter->stamp=stamp_;
527 KeyframeTreeStore::iter_has_child_vfunc (const GtkTreeIter*parent)
529 dump_iterator(parent,"parent");
538 KeyframeTreeStore::iter_n_children_vfunc (const GtkTreeIter* parent)
540 dump_iterator(parent,"parent");
545 return canvas_interface()->get_canvas()->keyframe_list().size();
550 KeyframeTreeStore::iter_n_root_children_vfunc () const
552 return canvas_interface()->get_canvas()->keyframe_list().size();
556 KeyframeTreeStore::iter_nth_root_child_vfunc (int n, iterator& xiter)const
558 if(canvas_interface()->get_canvas()->keyframe_list().size()==0)
565 g_warning("KeyframeTreeStore::iter_nth_root_child_vfunc: Out of range (negative index)");
568 if(n && (unsigned)n>=canvas_interface()->get_canvas()->keyframe_list().size())
570 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: Out of range (large index)");
574 _keyframe_iterator *iter(new _keyframe_iterator());
577 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
580 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
582 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: >>>BUG<<< in %s on line %d",__FILE__,__LINE__);
588 xiter.gobj()->user_data=static_cast<gpointer>(iter);
589 xiter.gobj()->stamp=stamp_;
595 KeyframeTreeStore::iter_nth_child_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* parent, int n)
597 dump_iterator(parent,"parent");
601 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: I am a list");
602 clear_iterator(gtk_iter);
608 _keyframe_iterator *iter(new _keyframe_iterator());
611 iter->iter=canvas_interface()->get_canvas()->keyframe_list().begin();
614 if(iter->iter==canvas_interface()->get_canvas()->keyframe_list().end())
616 g_warning("KeyframeTreeStore::iter_nth_child_vfunc: >>>BUG<<< in %s on line %d",__FILE__,__LINE__);
618 clear_iterator(gtk_iter);
624 gtk_iter->user_data=static_cast<gpointer>(iter);
625 gtk_iter->stamp=stamp_;
630 KeyframeTreeStore::iter_parent_vfunc (GtkTreeIter* gtk_iter, const GtkTreeIter* child)
632 dump_iterator(child,"child");
633 iterator_sane(child);
634 clear_iterator(gtk_iter);
640 KeyframeTreeStore::ref_node_vfunc (iterator& xiter)const
642 GtkTreeIter* gtk_iter(xiter.gobj());
643 if(!gtk_iter || !iterator_sane(gtk_iter)) return;
645 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
650 KeyframeTreeStore::unref_node_vfunc (iterator& xiter)const
652 GtkTreeIter* gtk_iter(xiter.gobj());
653 if(!gtk_iter || !iterator_sane(gtk_iter)) return;
655 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
661 // Make this iterator invalid
667 KeyframeTreeStore::get_path_vfunc (const TreeModel::iterator& gtk_iter)const
669 Gtk::TreeModel::Path path;
671 // If this is the root node, then return
673 if(!iterator_sane(gtk_iter))
676 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->gobj()->user_data));
678 path.append_index(iter->index);
684 KeyframeTreeStore::get_iter_vfunc (const Gtk::TreeModel::Path& path, iterator& iter)const
686 if(path.get_depth()>=1)
687 return iter_nth_root_child_vfunc(path.front(),iter);
690 g_warning("KeyframeTreeStore::get_iter_vfunc(): Bad path \"%s\"",path.to_string().c_str());
691 //clear_iterator(iter);
696 KeyframeTreeStore::iter_is_valid (const iterator& iter) const
698 return iterator_sane(iter);
702 KeyframeTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& gtk_iter, int column, Glib::ValueBase& value)const
704 dump_iterator(gtk_iter,"gtk_iter");
705 if(!iterator_sane(gtk_iter))
708 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->gobj()->user_data));
714 Glib::Value<synfig::Time> x;
715 g_value_init(x.gobj(),x.value_type());
716 x.set(iter->iter->get_time());
717 g_value_init(value.gobj(),x.value_type());
718 g_value_copy(x.gobj(),value.gobj());
721 case 3: // Time Delta
723 Glib::Value<synfig::Time> x;
724 g_value_init(x.gobj(),x.value_type());
726 synfig::Keyframe prev_keyframe(*iter->iter);
727 synfig::Keyframe keyframe;
729 KeyframeList::iterator tmp(iter->iter);
731 if(tmp==get_canvas()->keyframe_list().end())
734 g_value_init(value.gobj(),x.value_type());
735 g_value_copy(x.gobj(),value.gobj());
743 delta=keyframe.get_time()-prev_keyframe.get_time();
746 g_value_init(value.gobj(),x.value_type());
747 g_value_copy(x.gobj(),value.gobj());
750 case 1: // Description
752 g_value_init(value.gobj(),G_TYPE_STRING);
753 g_value_set_string(value.gobj(),iter->iter->get_description().c_str());
758 Glib::Value<synfig::Keyframe> x;
759 g_value_init(x.gobj(),x.value_type());
761 g_value_init(value.gobj(),x.value_type());
762 g_value_copy(x.gobj(),value.gobj());
771 KeyframeTreeStore::find_row(const synfig::Keyframe &keyframe)
773 Gtk::TreeModel::Row row(*(children().begin()));
774 dump_iterator(row,"find_row,begin");
775 const GtkTreeIter *gtk_iter(row.gobj());
776 if(!iterator_sane(gtk_iter))
777 throw std::runtime_error(_("Unable to find Keyframe in table"));
779 _keyframe_iterator *iter(static_cast<_keyframe_iterator*>(gtk_iter->user_data));
781 synfig::KeyframeList &keyframe_list(canvas_interface()->get_canvas()->keyframe_list());
782 if(keyframe_list.empty())
783 throw std::runtime_error(_("There are no keyframes n this canvas"));
787 for(iter->iter=keyframe_list.begin();iter->iter!=keyframe_list.end() && *iter->iter!=keyframe;++iter->iter)
791 if(iter->iter==keyframe_list.end())
792 throw std::runtime_error(_("Unable to find Keyframe in table"));
797 KeyframeTreeStore::add_keyframe(Keyframe keyframe)
801 Gtk::TreeRow row(find_row(keyframe));
802 dump_iterator(row.gobj(),"add_keyframe,row");
803 Gtk::TreePath path(get_path(row));
805 row_inserted(path,row);
807 old_keyframe_list=get_canvas()->keyframe_list();
808 //old_keyframe_list.add(keyframe);
809 //old_keyframe_list.sort();
811 catch(std::exception x)
818 KeyframeTreeStore::remove_keyframe(Keyframe keyframe)
824 Gtk::TreeRow row(find_row(keyframe));
825 dump_iterator(row,"remove_keyframe,row");
826 Gtk::TreePath path(get_path(row));
829 old_keyframe_list.erase(keyframe);
833 g_warning("KeyframeTreeStore::remove_keyframe: Keyframe not in table");
836 catch(std::exception x)
844 KeyframeTreeStore::change_keyframe(Keyframe keyframe)
848 Gtk::TreeRow row(find_row(keyframe));
850 unsigned int new_index(get_index_from_model_iter(row));
851 unsigned int old_index(0);
852 synfig::KeyframeList::iterator iter;
853 for(old_index=0,iter=old_keyframe_list.begin();iter!=old_keyframe_list.end() && (UniqueID)*iter!=(UniqueID)keyframe;++iter,old_index++);
855 if(iter!=old_keyframe_list.end() && new_index!=old_index)
858 std::vector<int> new_order;
859 for(unsigned int i=0;i<old_keyframe_list.size();i++)
861 new_order.push_back(i);
863 if(new_order.size()>new_index)
865 new_order.erase(new_order.begin()+new_index);
866 new_order.insert(new_order.begin()+old_index,new_index);
868 //new_order[old_index]=
870 rows_reordered (Path(), iterator(), &new_order[0]);
872 old_keyframe_list=get_canvas()->keyframe_list();
874 row=find_row(keyframe);
877 dump_iterator(row,"change_keyframe,row");
878 row_changed(get_path(row),row);
880 catch(std::exception x)