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 "iconcontroler.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>
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
49 using namespace studio;
51 /* === M A C R O S ========================================================= */
53 class Profiler : private etl::clock
55 const std::string name;
57 Profiler(const std::string& name):name(name) { reset(); }
58 ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
61 /* === G L O B A L S ======================================================= */
63 /* === P R O C E D U R E S ================================================= */
65 /* === M E T H O D S ======================================================= */
67 static LayerParamTreeStore::Model& ModelHack()
69 static LayerParamTreeStore::Model* model(0);
70 if(!model)model=new LayerParamTreeStore::Model;
74 LayerParamTreeStore::LayerParamTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_,LayerTree* layer_tree):
75 Gtk::TreeStore (ModelHack()),
76 CanvasTreeStore (canvas_interface_),
77 layer_tree (layer_tree)
80 // Connect all the signals
81 canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_changed));
82 canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_added));
83 canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_deleted));
84 canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_replaced));
85 canvas_interface()->signal_layer_param_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_layer_param_changed));
87 canvas_interface()->signal_value_node_child_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_added));
88 canvas_interface()->signal_value_node_child_removed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_removed));
91 layer_tree->get_selection()->signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_rebuild));
93 signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_refresh));
97 LayerParamTreeStore::~LayerParamTreeStore()
99 while(!changed_connection_list.empty())
101 changed_connection_list.back().disconnect();
102 changed_connection_list.pop_back();
104 synfig::info("LayerParamTreeStore::~LayerParamTreeStore(): Deleted");
107 Glib::RefPtr<LayerParamTreeStore>
108 LayerParamTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_, LayerTree*layer_tree)
110 return Glib::RefPtr<LayerParamTreeStore>(new LayerParamTreeStore(canvas_interface_,layer_tree));
116 LayerParamTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
120 synfig::error("LayerParamTreeStore::get_value_vfunc(): Bad column!");
124 /* if(column==model.label.index())
126 synfig::Layer::Handle layer((*iter)[model.layer]);
130 Glib::Value<Glib::ustring> x;
131 g_value_init(x.gobj(),x.value_type());
134 if(!layer->get_description().empty())
135 x.set(layer->get_description());
137 x.set(layer->get_local_name());
139 g_value_init(value.gobj(),x.value_type());
140 g_value_copy(x.gobj(),value.gobj());
144 if(column==model.label.index())
146 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
149 if(!(*iter)[model.is_toplevel])
150 return CanvasTreeStore::get_value_vfunc(iter,column,value);
151 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
152 label=param_desc.get_local_name();
154 if(!(*iter)[model.is_inconsistent])
155 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
157 label+=strprintf(" (%s)",value_desc.get_value_node()->get_id().c_str());
160 Glib::Value<Glib::ustring> x;
161 g_value_init(x.gobj(),x.value_type());
165 g_value_init(value.gobj(),x.value_type());
166 g_value_copy(x.gobj(),value.gobj());
169 if(column==model.is_toplevel.index())
172 g_value_init(x.gobj(),x.value_type());
174 TreeModel::Path path(get_path(iter));
176 x.set(path.get_depth()<=1);
178 g_value_init(value.gobj(),x.value_type());
179 g_value_copy(x.gobj(),value.gobj());
182 if(column==model.is_inconsistent.index())
184 if((*iter)[model.is_toplevel])
186 CanvasTreeStore::get_value_vfunc(iter,column,value);
191 g_value_init(x.gobj(),x.value_type());
195 g_value_init(value.gobj(),x.value_type());
196 g_value_copy(x.gobj(),value.gobj());
199 CanvasTreeStore::get_value_vfunc(iter,column,value);
205 LayerParamTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
207 //if(!iterator_sane(row))
210 if(column>=get_n_columns_vfunc())
212 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
216 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
218 g_warning("LayerTreeStore::set_value_impl: Bad value type");
224 if(column==model.value.index())
226 Glib::Value<synfig::ValueBase> x;
227 g_value_init(x.gobj(),model.value.type());
228 g_value_copy(value.gobj(),x.gobj());
230 if((bool)(*iter)[model.is_toplevel])
232 synfigapp::Action::PassiveGrouper group(canvas_interface()->get_instance().get(),_("Set Layer Params"));
234 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
236 LayerList::iterator iter2(layer_list.begin());
238 for(;iter2!=layer_list.end();++iter2)
240 if(!canvas_interface()->change_value(synfigapp::ValueDesc(*iter2,param_desc.get_name()),x.get()))
244 App::dialog_error_blocking(_("Error"),_("Unable to set all layer parameters."));
252 canvas_interface()->change_value((*iter)[model.value_desc],x.get());
258 if(column==model.active.index())
260 synfig::Layer::Handle layer((*iter)[model.layer]);
265 g_value_init(x.gobj(),model.active.type());
266 g_value_copy(value.gobj(),x.gobj());
268 synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
273 action->set_param("canvas",canvas_interface()->get_canvas());
274 action->set_param("canvas_interface",canvas_interface());
275 action->set_param("layer",layer);
276 action->set_param("new_status",bool(x.get()));
278 canvas_interface()->get_instance()->perform_action(action);
283 CanvasTreeStore::set_value_impl(iter,column, value);
285 catch(std::exception x)
301 LayerParamTreeStore::rebuild()
303 Profiler profiler("LayerParamTreeStore::rebuild()");
304 if(queued)queued=false;
306 layer_list=layer_tree->get_selected_layers();
308 if(layer_list.size()<=0)
311 // Get rid of all the connections,
312 // and clear the connection map.
313 //while(!connection_map.empty())connection_map.begin()->second.disconnect(),connection_map.erase(connection_map.begin());
314 while(!changed_connection_list.empty())
316 changed_connection_list.back().disconnect();
317 changed_connection_list.pop_back();
320 struct REBUILD_HELPER
324 static ParamVocab::iterator find_param_desc(ParamVocab& vocab, const synfig::String& x)
326 ParamVocab::iterator iter;
328 for(iter=vocab.begin();iter!=vocab.end();++iter)
329 if(iter->get_name()==x)
334 void process_vocab(ParamVocab x)
336 ParamVocab::iterator iter;
338 for(iter=vocab.begin();iter!=vocab.end();++iter)
340 ParamVocab::iterator iter2(find_param_desc(x,iter->get_name()));
343 // remove it and start over
356 LayerList::iterator iter(layer_list.begin());
357 rebuild_helper.vocab=(*iter)->get_param_vocab();
359 for(++iter;iter!=layer_list.end();++iter)
361 rebuild_helper.process_vocab((*iter)->get_param_vocab());
362 changed_connection_list.push_back(
363 (*iter)->signal_changed().connect(
366 &LayerParamTreeStore::changed
373 ParamVocab::iterator iter;
374 for(iter=rebuild_helper.vocab.begin();iter!=rebuild_helper.vocab.end();++iter)
376 if(iter->get_hidden())
380 if(iter->get_animation_only())
382 int length(layer_list.front()->get_canvas()->rend_desc().get_frame_end()-layer_list.front()->get_canvas()->rend_desc().get_frame_start());
387 Gtk::TreeRow row(*(append()));
388 synfigapp::ValueDesc value_desc(layer_list.front(),iter->get_name());
389 CanvasTreeStore::set_row(row,value_desc);
390 if(value_desc.is_value_node())
392 changed_connection_list.push_back(
393 value_desc.get_value_node()->signal_changed().connect(
396 &LayerParamTreeStore::changed
401 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
403 Canvas::Handle canvas_handle = value_desc.get_value().get(Canvas::Handle());
404 if(canvas_handle) changed_connection_list.push_back(
405 canvas_handle->signal_changed().connect(
408 &LayerParamTreeStore::changed
413 //row[model.label] = iter->get_local_name();
414 row[model.param_desc] = *iter;
415 row[model.canvas] = layer_list.front()->get_canvas();
416 row[model.is_inconsistent] = false;
417 //row[model.is_toplevel] = true;
420 LayerList::iterator iter2(layer_list.begin());
421 ValueBase value((*iter2)->get_param(iter->get_name()));
422 for(++iter2;iter2!=layer_list.end();++iter2)
424 if(value!=((*iter2)->get_param(iter->get_name())))
426 row[model.is_inconsistent] = true;
427 while(!row.children().empty() && erase(row.children().begin()));
435 LayerParamTreeStore::queue_refresh()
440 queue_connection.disconnect();
441 queue_connection=Glib::signal_timeout().connect(
443 sigc::mem_fun(*this,&LayerParamTreeStore::refresh),
451 LayerParamTreeStore::queue_rebuild()
456 queue_connection.disconnect();
457 queue_connection=Glib::signal_timeout().connect(
459 sigc::mem_fun(*this,&LayerParamTreeStore::rebuild),
467 LayerParamTreeStore::refresh()
469 if(queued)queued=false;
471 Gtk::TreeModel::Children children_(children());
473 Gtk::TreeModel::Children::iterator iter;
475 if(!children_.empty())
476 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
478 Gtk::TreeRow row=*iter;
484 LayerParamTreeStore::refresh_row(Gtk::TreeModel::Row &row)
486 if(row[model.is_toplevel])
488 row[model.is_inconsistent] = false;
489 ParamDesc param_desc(row[model.param_desc]);
491 LayerList::iterator iter2(layer_list.begin());
492 ValueBase value((*iter2)->get_param(param_desc.get_name()));
493 for(++iter2;iter2!=layer_list.end();++iter2)
495 if(value!=((*iter2)->get_param(param_desc.get_name())))
497 row[model.is_inconsistent] = true;
498 while(!row.children().empty() && erase(row.children().begin()));
504 //handle<ValueNode> value_node=row[model.value_node];
507 CanvasTreeStore::refresh_row(row);
513 LayerParamTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc)
515 Gtk::TreeModel::Children children = row.children();
516 while(!children.empty() && erase(children.begin()));
518 CanvasTreeStore::set_row(row,value_desc);
522 LayerParamTreeStore::on_value_node_added(ValueNode::Handle /*value_node*/)
528 LayerParamTreeStore::on_value_node_deleted(etl::handle<ValueNode> /*value_node*/)
534 LayerParamTreeStore::on_value_node_child_added(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
540 LayerParamTreeStore::on_value_node_child_removed(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
546 LayerParamTreeStore::on_value_node_changed(etl::handle<ValueNode> /*value_node*/)
552 LayerParamTreeStore::on_value_node_replaced(synfig::ValueNode::Handle /*replaced_value_node*/,synfig::ValueNode::Handle /*new_value_node*/)
558 LayerParamTreeStore::on_layer_param_changed(synfig::Layer::Handle /*handle*/,synfig::String /*param_name*/)