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();
108 if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
109 synfig::info("LayerParamTreeStore::~LayerParamTreeStore(): Deleted");
112 Glib::RefPtr<LayerParamTreeStore>
113 LayerParamTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_, LayerTree*layer_tree)
115 return Glib::RefPtr<LayerParamTreeStore>(new LayerParamTreeStore(canvas_interface_,layer_tree));
121 LayerParamTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
125 synfig::error("LayerParamTreeStore::get_value_vfunc(): Bad column!");
129 /* if(column==model.label.index())
131 synfig::Layer::Handle layer((*iter)[model.layer]);
135 Glib::Value<Glib::ustring> x;
136 g_value_init(x.gobj(),x.value_type());
138 x.set(layer->get_non_empty_description());
140 g_value_init(value.gobj(),x.value_type());
141 g_value_copy(x.gobj(),value.gobj());
145 if(column==model.label.index())
147 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
150 if(!(*iter)[model.is_toplevel])
151 return CanvasTreeStore::get_value_vfunc(iter,column,value);
152 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
153 label=param_desc.get_local_name();
155 if(!(*iter)[model.is_inconsistent])
156 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
158 label+=strprintf(" (%s)",value_desc.get_value_node()->get_id().c_str());
161 Glib::Value<Glib::ustring> x;
162 g_value_init(x.gobj(),x.value_type());
166 g_value_init(value.gobj(),x.value_type());
167 g_value_copy(x.gobj(),value.gobj());
170 if(column==model.is_toplevel.index())
173 g_value_init(x.gobj(),x.value_type());
175 TreeModel::Path path(get_path(iter));
177 x.set(path.get_depth()<=1);
179 g_value_init(value.gobj(),x.value_type());
180 g_value_copy(x.gobj(),value.gobj());
183 if(column==model.is_inconsistent.index())
185 if((*iter)[model.is_toplevel])
187 CanvasTreeStore::get_value_vfunc(iter,column,value);
192 g_value_init(x.gobj(),x.value_type());
196 g_value_init(value.gobj(),x.value_type());
197 g_value_copy(x.gobj(),value.gobj());
200 CanvasTreeStore::get_value_vfunc(iter,column,value);
206 LayerParamTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
208 //if(!iterator_sane(row))
211 if(column>=get_n_columns_vfunc())
213 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
217 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
219 g_warning("LayerTreeStore::set_value_impl: Bad value type");
225 if(column==model.value.index())
227 Glib::Value<synfig::ValueBase> x;
228 g_value_init(x.gobj(),model.value.type());
229 g_value_copy(value.gobj(),x.gobj());
231 if((bool)(*iter)[model.is_toplevel])
233 synfigapp::Action::PassiveGrouper group(canvas_interface()->get_instance().get(),_("Set Layer Params"));
235 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
237 LayerList::iterator iter2(layer_list.begin());
239 for(;iter2!=layer_list.end();++iter2)
241 if(!canvas_interface()->change_value(synfigapp::ValueDesc(*iter2,param_desc.get_name()),x.get()))
245 App::dialog_error_blocking(_("Error"),_("Unable to set all layer parameters."));
253 canvas_interface()->change_value((*iter)[model.value_desc],x.get());
259 if(column==model.active.index())
261 synfig::Layer::Handle layer((*iter)[model.layer]);
266 g_value_init(x.gobj(),model.active.type());
267 g_value_copy(value.gobj(),x.gobj());
269 synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
274 action->set_param("canvas",canvas_interface()->get_canvas());
275 action->set_param("canvas_interface",canvas_interface());
276 action->set_param("layer",layer);
277 action->set_param("new_status",bool(x.get()));
279 canvas_interface()->get_instance()->perform_action(action);
284 CanvasTreeStore::set_value_impl(iter,column, value);
286 catch(std::exception x)
302 LayerParamTreeStore::rebuild()
304 // Profiler profiler("LayerParamTreeStore::rebuild()");
305 if(queued)queued=false;
307 layer_list=layer_tree->get_selected_layers();
309 if(layer_list.size()<=0)
312 // Get rid of all the connections,
313 // and clear the connection map.
314 //while(!connection_map.empty())connection_map.begin()->second.disconnect(),connection_map.erase(connection_map.begin());
315 while(!changed_connection_list.empty())
317 changed_connection_list.back().disconnect();
318 changed_connection_list.pop_back();
321 struct REBUILD_HELPER
325 static ParamVocab::iterator find_param_desc(ParamVocab& vocab, const synfig::String& x)
327 ParamVocab::iterator iter;
329 for(iter=vocab.begin();iter!=vocab.end();++iter)
330 if(iter->get_name()==x)
335 void process_vocab(ParamVocab x)
337 ParamVocab::iterator iter;
339 for(iter=vocab.begin();iter!=vocab.end();++iter)
341 ParamVocab::iterator iter2(find_param_desc(x,iter->get_name()));
344 // remove it and start over
357 LayerList::iterator iter(layer_list.begin());
358 rebuild_helper.vocab=(*iter)->get_param_vocab();
360 for(++iter;iter!=layer_list.end();++iter)
362 rebuild_helper.process_vocab((*iter)->get_param_vocab());
363 changed_connection_list.push_back(
364 (*iter)->signal_changed().connect(
367 &LayerParamTreeStore::changed
374 ParamVocab::iterator iter;
375 for(iter=rebuild_helper.vocab.begin();iter!=rebuild_helper.vocab.end();++iter)
377 if(iter->get_hidden())
381 if(iter->get_animation_only())
383 int length(layer_list.front()->get_canvas()->rend_desc().get_frame_end()-layer_list.front()->get_canvas()->rend_desc().get_frame_start());
388 Gtk::TreeRow row(*(append()));
389 synfigapp::ValueDesc value_desc(layer_list.front(),iter->get_name());
390 CanvasTreeStore::set_row(row,value_desc);
391 if(value_desc.is_value_node())
393 changed_connection_list.push_back(
394 value_desc.get_value_node()->signal_changed().connect(
397 &LayerParamTreeStore::changed
402 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
404 Canvas::Handle canvas_handle = value_desc.get_value().get(Canvas::Handle());
405 if(canvas_handle) changed_connection_list.push_back(
406 canvas_handle->signal_changed().connect(
409 &LayerParamTreeStore::changed
414 //row[model.label] = iter->get_local_name();
415 row[model.param_desc] = *iter;
416 row[model.canvas] = layer_list.front()->get_canvas();
417 row[model.is_inconsistent] = false;
418 //row[model.is_toplevel] = true;
421 LayerList::iterator iter2(layer_list.begin());
422 ValueBase value((*iter2)->get_param(iter->get_name()));
423 for(++iter2;iter2!=layer_list.end();++iter2)
425 if(value!=((*iter2)->get_param(iter->get_name())))
427 row[model.is_inconsistent] = true;
428 while(!row.children().empty() && erase(row.children().begin()))
437 LayerParamTreeStore::queue_refresh()
442 queue_connection.disconnect();
443 queue_connection=Glib::signal_timeout().connect(
445 sigc::mem_fun(*this,&LayerParamTreeStore::refresh),
453 LayerParamTreeStore::queue_rebuild()
458 queue_connection.disconnect();
459 queue_connection=Glib::signal_timeout().connect(
461 sigc::mem_fun(*this,&LayerParamTreeStore::rebuild),
469 LayerParamTreeStore::refresh()
471 if(queued)queued=false;
473 Gtk::TreeModel::Children children_(children());
475 Gtk::TreeModel::Children::iterator iter;
477 if(!children_.empty())
478 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
480 Gtk::TreeRow row=*iter;
486 LayerParamTreeStore::refresh_row(Gtk::TreeModel::Row &row)
488 if(row[model.is_toplevel])
490 row[model.is_inconsistent] = false;
491 ParamDesc param_desc(row[model.param_desc]);
493 LayerList::iterator iter2(layer_list.begin());
494 ValueBase value((*iter2)->get_param(param_desc.get_name()));
495 for(++iter2;iter2!=layer_list.end();++iter2)
497 if(value!=((*iter2)->get_param(param_desc.get_name())))
499 row[model.is_inconsistent] = true;
500 while(!row.children().empty() && erase(row.children().begin()))
507 //handle<ValueNode> value_node=row[model.value_node];
510 CanvasTreeStore::refresh_row(row);
516 LayerParamTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc)
518 Gtk::TreeModel::Children children = row.children();
519 while(!children.empty() && erase(children.begin()))
522 CanvasTreeStore::set_row(row,value_desc);
526 LayerParamTreeStore::on_value_node_added(synfig::ValueNode::Handle /*value_node*/)
532 LayerParamTreeStore::on_value_node_deleted(synfig::ValueNode::Handle /*value_node*/)
538 LayerParamTreeStore::on_value_node_child_added(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
544 LayerParamTreeStore::on_value_node_child_removed(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
550 LayerParamTreeStore::on_value_node_changed(synfig::ValueNode::Handle /*value_node*/)
556 LayerParamTreeStore::on_value_node_renamed(synfig::ValueNode::Handle /*value_node*/)
562 LayerParamTreeStore::on_value_node_replaced(synfig::ValueNode::Handle /*replaced_value_node*/,synfig::ValueNode::Handle /*new_value_node*/)
568 LayerParamTreeStore::on_layer_param_changed(synfig::Layer::Handle /*handle*/,synfig::String /*param_name*/)