1 /* === S Y N F I G ========================================================= */
2 /*! \file layerparamtreestore.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 "layerparamtreestore.h"
33 #include "iconcontroller.h"
34 #include <gtkmm/button.h>
35 #include <synfig/paramdesc.h>
36 #include "layertree.h"
37 #include <synfigapp/action_system.h>
38 #include <synfigapp/instance.h>
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
51 using namespace studio;
53 /* === M A C R O S ========================================================= */
55 class Profiler : private etl::clock
57 const std::string name;
59 Profiler(const std::string& name):name(name) { reset(); }
60 ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
63 /* === G L O B A L S ======================================================= */
65 /* === P R O C E D U R E S ================================================= */
67 /* === M E T H O D S ======================================================= */
69 static LayerParamTreeStore::Model& ModelHack()
71 static LayerParamTreeStore::Model* model(0);
72 if(!model)model=new LayerParamTreeStore::Model;
76 LayerParamTreeStore::LayerParamTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_,LayerTree* layer_tree):
77 Gtk::TreeStore (ModelHack()),
78 CanvasTreeStore (canvas_interface_),
79 layer_tree (layer_tree)
82 // Connect all the signals
83 canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_changed));
84 canvas_interface()->signal_value_node_renamed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_renamed));
85 canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_added));
86 canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_deleted));
87 canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_replaced));
88 canvas_interface()->signal_layer_param_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_layer_param_changed));
90 canvas_interface()->signal_value_node_child_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_added));
91 canvas_interface()->signal_value_node_child_removed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_removed));
94 layer_tree->get_selection()->signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_rebuild));
96 signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_refresh));
100 LayerParamTreeStore::~LayerParamTreeStore()
102 while(!changed_connection_list.empty())
104 changed_connection_list.back().disconnect();
105 changed_connection_list.pop_back();
107 synfig::info("LayerParamTreeStore::~LayerParamTreeStore(): Deleted");
110 Glib::RefPtr<LayerParamTreeStore>
111 LayerParamTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_, LayerTree*layer_tree)
113 return Glib::RefPtr<LayerParamTreeStore>(new LayerParamTreeStore(canvas_interface_,layer_tree));
119 LayerParamTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
123 synfig::error("LayerParamTreeStore::get_value_vfunc(): Bad column!");
127 /* if(column==model.label.index())
129 synfig::Layer::Handle layer((*iter)[model.layer]);
133 Glib::Value<Glib::ustring> x;
134 g_value_init(x.gobj(),x.value_type());
136 x.set(layer->get_non_empty_description());
138 g_value_init(value.gobj(),x.value_type());
139 g_value_copy(x.gobj(),value.gobj());
143 if(column==model.label.index())
145 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
148 if(!(*iter)[model.is_toplevel])
149 return CanvasTreeStore::get_value_vfunc(iter,column,value);
150 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
151 label=param_desc.get_local_name();
153 if(!(*iter)[model.is_inconsistent])
154 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
156 label+=strprintf(" (%s)",value_desc.get_value_node()->get_id().c_str());
159 Glib::Value<Glib::ustring> x;
160 g_value_init(x.gobj(),x.value_type());
164 g_value_init(value.gobj(),x.value_type());
165 g_value_copy(x.gobj(),value.gobj());
168 if(column==model.is_toplevel.index())
171 g_value_init(x.gobj(),x.value_type());
173 TreeModel::Path path(get_path(iter));
175 x.set(path.get_depth()<=1);
177 g_value_init(value.gobj(),x.value_type());
178 g_value_copy(x.gobj(),value.gobj());
181 if(column==model.is_inconsistent.index())
183 if((*iter)[model.is_toplevel])
185 CanvasTreeStore::get_value_vfunc(iter,column,value);
190 g_value_init(x.gobj(),x.value_type());
194 g_value_init(value.gobj(),x.value_type());
195 g_value_copy(x.gobj(),value.gobj());
198 CanvasTreeStore::get_value_vfunc(iter,column,value);
204 LayerParamTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
206 //if(!iterator_sane(row))
209 if(column>=get_n_columns_vfunc())
211 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
215 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
217 g_warning("LayerTreeStore::set_value_impl: Bad value type");
223 if(column==model.value.index())
225 Glib::Value<synfig::ValueBase> x;
226 g_value_init(x.gobj(),model.value.type());
227 g_value_copy(value.gobj(),x.gobj());
229 if((bool)(*iter)[model.is_toplevel])
231 synfigapp::Action::PassiveGrouper group(canvas_interface()->get_instance().get(),_("Set Layer Params"));
233 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
235 LayerList::iterator iter2(layer_list.begin());
237 for(;iter2!=layer_list.end();++iter2)
239 if(!canvas_interface()->change_value(synfigapp::ValueDesc(*iter2,param_desc.get_name()),x.get()))
243 App::dialog_error_blocking(_("Error"),_("Unable to set all layer parameters."));
251 canvas_interface()->change_value((*iter)[model.value_desc],x.get());
257 if(column==model.active.index())
259 synfig::Layer::Handle layer((*iter)[model.layer]);
264 g_value_init(x.gobj(),model.active.type());
265 g_value_copy(value.gobj(),x.gobj());
267 synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
272 action->set_param("canvas",canvas_interface()->get_canvas());
273 action->set_param("canvas_interface",canvas_interface());
274 action->set_param("layer",layer);
275 action->set_param("new_status",bool(x.get()));
277 canvas_interface()->get_instance()->perform_action(action);
282 CanvasTreeStore::set_value_impl(iter,column, value);
284 catch(std::exception x)
300 LayerParamTreeStore::rebuild()
302 // Profiler profiler("LayerParamTreeStore::rebuild()");
303 if(queued)queued=false;
305 layer_list=layer_tree->get_selected_layers();
307 if(layer_list.size()<=0)
310 // Get rid of all the connections,
311 // and clear the connection map.
312 //while(!connection_map.empty())connection_map.begin()->second.disconnect(),connection_map.erase(connection_map.begin());
313 while(!changed_connection_list.empty())
315 changed_connection_list.back().disconnect();
316 changed_connection_list.pop_back();
319 struct REBUILD_HELPER
323 static ParamVocab::iterator find_param_desc(ParamVocab& vocab, const synfig::String& x)
325 ParamVocab::iterator iter;
327 for(iter=vocab.begin();iter!=vocab.end();++iter)
328 if(iter->get_name()==x)
333 void process_vocab(ParamVocab x)
335 ParamVocab::iterator iter;
337 for(iter=vocab.begin();iter!=vocab.end();++iter)
339 ParamVocab::iterator iter2(find_param_desc(x,iter->get_name()));
342 // remove it and start over
355 LayerList::iterator iter(layer_list.begin());
356 rebuild_helper.vocab=(*iter)->get_param_vocab();
358 for(++iter;iter!=layer_list.end();++iter)
360 rebuild_helper.process_vocab((*iter)->get_param_vocab());
361 changed_connection_list.push_back(
362 (*iter)->signal_changed().connect(
365 &LayerParamTreeStore::changed
372 ParamVocab::iterator iter;
373 for(iter=rebuild_helper.vocab.begin();iter!=rebuild_helper.vocab.end();++iter)
375 if(iter->get_hidden())
379 if(iter->get_animation_only())
381 int length(layer_list.front()->get_canvas()->rend_desc().get_frame_end()-layer_list.front()->get_canvas()->rend_desc().get_frame_start());
386 Gtk::TreeRow row(*(append()));
387 synfigapp::ValueDesc value_desc(layer_list.front(),iter->get_name());
388 CanvasTreeStore::set_row(row,value_desc);
389 if(value_desc.is_value_node())
391 changed_connection_list.push_back(
392 value_desc.get_value_node()->signal_changed().connect(
395 &LayerParamTreeStore::changed
400 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
402 Canvas::Handle canvas_handle = value_desc.get_value().get(Canvas::Handle());
403 if(canvas_handle) changed_connection_list.push_back(
404 canvas_handle->signal_changed().connect(
407 &LayerParamTreeStore::changed
412 //row[model.label] = iter->get_local_name();
413 row[model.param_desc] = *iter;
414 row[model.canvas] = layer_list.front()->get_canvas();
415 row[model.is_inconsistent] = false;
416 //row[model.is_toplevel] = true;
419 LayerList::iterator iter2(layer_list.begin());
420 ValueBase value((*iter2)->get_param(iter->get_name()));
421 for(++iter2;iter2!=layer_list.end();++iter2)
423 if(value!=((*iter2)->get_param(iter->get_name())))
425 row[model.is_inconsistent] = true;
426 while(!row.children().empty() && erase(row.children().begin()));
434 LayerParamTreeStore::queue_refresh()
439 queue_connection.disconnect();
440 queue_connection=Glib::signal_timeout().connect(
442 sigc::mem_fun(*this,&LayerParamTreeStore::refresh),
450 LayerParamTreeStore::queue_rebuild()
455 queue_connection.disconnect();
456 queue_connection=Glib::signal_timeout().connect(
458 sigc::mem_fun(*this,&LayerParamTreeStore::rebuild),
466 LayerParamTreeStore::refresh()
468 if(queued)queued=false;
470 Gtk::TreeModel::Children children_(children());
472 Gtk::TreeModel::Children::iterator iter;
474 if(!children_.empty())
475 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
477 Gtk::TreeRow row=*iter;
483 LayerParamTreeStore::refresh_row(Gtk::TreeModel::Row &row)
485 if(row[model.is_toplevel])
487 row[model.is_inconsistent] = false;
488 ParamDesc param_desc(row[model.param_desc]);
490 LayerList::iterator iter2(layer_list.begin());
491 ValueBase value((*iter2)->get_param(param_desc.get_name()));
492 for(++iter2;iter2!=layer_list.end();++iter2)
494 if(value!=((*iter2)->get_param(param_desc.get_name())))
496 row[model.is_inconsistent] = true;
497 while(!row.children().empty() && erase(row.children().begin()));
503 //handle<ValueNode> value_node=row[model.value_node];
506 CanvasTreeStore::refresh_row(row);
512 LayerParamTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc)
514 Gtk::TreeModel::Children children = row.children();
515 while(!children.empty() && erase(children.begin()));
517 CanvasTreeStore::set_row(row,value_desc);
521 LayerParamTreeStore::on_value_node_added(synfig::ValueNode::Handle /*value_node*/)
527 LayerParamTreeStore::on_value_node_deleted(synfig::ValueNode::Handle /*value_node*/)
533 LayerParamTreeStore::on_value_node_child_added(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
539 LayerParamTreeStore::on_value_node_child_removed(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
545 LayerParamTreeStore::on_value_node_changed(synfig::ValueNode::Handle /*value_node*/)
551 LayerParamTreeStore::on_value_node_renamed(synfig::ValueNode::Handle /*value_node*/)
557 LayerParamTreeStore::on_value_node_replaced(synfig::ValueNode::Handle /*replaced_value_node*/,synfig::ValueNode::Handle /*new_value_node*/)
563 LayerParamTreeStore::on_layer_param_changed(synfig::Layer::Handle /*handle*/,synfig::String /*param_name*/)