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
9 ** Copyright (c) 2007, 2008 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 "layerparamtreestore.h"
34 #include "iconcontroller.h"
35 #include <gtkmm/button.h>
36 #include <synfig/paramdesc.h>
37 #include "layertree.h"
38 #include <synfigapp/action_system.h>
39 #include <synfigapp/instance.h>
47 /* === U S I N G =========================================================== */
51 using namespace synfig;
52 using namespace studio;
54 /* === M A C R O S ========================================================= */
56 class Profiler : private etl::clock
58 const std::string name;
60 Profiler(const std::string& name):name(name) { reset(); }
61 ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
64 /* === G L O B A L S ======================================================= */
66 /* === P R O C E D U R E S ================================================= */
68 /* === M E T H O D S ======================================================= */
70 static LayerParamTreeStore::Model& ModelHack()
72 static LayerParamTreeStore::Model* model(0);
73 if(!model)model=new LayerParamTreeStore::Model;
77 LayerParamTreeStore::LayerParamTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_,LayerTree* layer_tree):
78 Gtk::TreeStore (ModelHack()),
79 CanvasTreeStore (canvas_interface_),
80 layer_tree (layer_tree)
83 // Connect all the signals
84 canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_changed));
85 canvas_interface()->signal_value_node_renamed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_renamed));
86 canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_added));
87 canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_deleted));
88 canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_replaced));
89 canvas_interface()->signal_layer_param_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_layer_param_changed));
91 canvas_interface()->signal_value_node_child_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_added));
92 canvas_interface()->signal_value_node_child_removed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_removed));
95 layer_tree->get_selection()->signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_rebuild));
97 signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_refresh));
101 LayerParamTreeStore::~LayerParamTreeStore()
103 while(!changed_connection_list.empty())
105 changed_connection_list.back().disconnect();
106 changed_connection_list.pop_back();
109 if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
110 synfig::info("LayerParamTreeStore::~LayerParamTreeStore(): Deleted");
113 Glib::RefPtr<LayerParamTreeStore>
114 LayerParamTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_, LayerTree*layer_tree)
116 return Glib::RefPtr<LayerParamTreeStore>(new LayerParamTreeStore(canvas_interface_,layer_tree));
122 LayerParamTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
126 synfig::error("LayerParamTreeStore::get_value_vfunc(): Bad column!");
130 /* if(column==model.label.index())
132 synfig::Layer::Handle layer((*iter)[model.layer]);
136 Glib::Value<Glib::ustring> x;
137 g_value_init(x.gobj(),x.value_type());
139 x.set(layer->get_non_empty_description());
141 g_value_init(value.gobj(),x.value_type());
142 g_value_copy(x.gobj(),value.gobj());
146 if(column==model.label.index())
148 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
151 if(!(*iter)[model.is_toplevel])
152 return CanvasTreeStore::get_value_vfunc(iter,column,value);
153 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
154 label=param_desc.get_local_name();
156 if(!(*iter)[model.is_inconsistent])
157 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
159 label+=strprintf(" (%s)",value_desc.get_value_node()->get_id().c_str());
162 Glib::Value<Glib::ustring> x;
163 g_value_init(x.gobj(),x.value_type());
167 g_value_init(value.gobj(),x.value_type());
168 g_value_copy(x.gobj(),value.gobj());
171 if(column==model.is_toplevel.index())
174 g_value_init(x.gobj(),x.value_type());
176 TreeModel::Path path(get_path(iter));
178 x.set(path.get_depth()<=1);
180 g_value_init(value.gobj(),x.value_type());
181 g_value_copy(x.gobj(),value.gobj());
184 if(column==model.is_inconsistent.index())
186 if((*iter)[model.is_toplevel])
188 CanvasTreeStore::get_value_vfunc(iter,column,value);
193 g_value_init(x.gobj(),x.value_type());
197 g_value_init(value.gobj(),x.value_type());
198 g_value_copy(x.gobj(),value.gobj());
201 CanvasTreeStore::get_value_vfunc(iter,column,value);
207 LayerParamTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
209 //if(!iterator_sane(row))
212 if(column>=get_n_columns_vfunc())
214 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
218 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
220 g_warning("LayerTreeStore::set_value_impl: Bad value type");
226 if(column==model.value.index())
228 Glib::Value<synfig::ValueBase> x;
229 g_value_init(x.gobj(),model.value.type());
230 g_value_copy(value.gobj(),x.gobj());
232 if((bool)(*iter)[model.is_toplevel])
234 synfigapp::Action::PassiveGrouper group(canvas_interface()->get_instance().get(),_("Set Layer Params"));
236 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
238 LayerList::iterator iter2(layer_list.begin());
240 for(;iter2!=layer_list.end();++iter2)
242 if(!canvas_interface()->change_value(synfigapp::ValueDesc(*iter2,param_desc.get_name()),x.get()))
246 App::dialog_error_blocking(_("Error"),_("Unable to set all layer parameters."));
254 canvas_interface()->change_value((*iter)[model.value_desc],x.get());
260 if(column==model.active.index())
262 synfig::Layer::Handle layer((*iter)[model.layer]);
267 g_value_init(x.gobj(),model.active.type());
268 g_value_copy(value.gobj(),x.gobj());
270 synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
275 action->set_param("canvas",canvas_interface()->get_canvas());
276 action->set_param("canvas_interface",canvas_interface());
277 action->set_param("layer",layer);
278 action->set_param("new_status",bool(x.get()));
280 canvas_interface()->get_instance()->perform_action(action);
285 CanvasTreeStore::set_value_impl(iter,column, value);
287 catch(std::exception x)
303 LayerParamTreeStore::rebuild()
305 // Profiler profiler("LayerParamTreeStore::rebuild()");
306 if(queued)queued=false;
308 layer_list=layer_tree->get_selected_layers();
310 if(layer_list.size()<=0)
313 // Get rid of all the connections,
314 // and clear the connection map.
315 //while(!connection_map.empty())connection_map.begin()->second.disconnect(),connection_map.erase(connection_map.begin());
316 while(!changed_connection_list.empty())
318 changed_connection_list.back().disconnect();
319 changed_connection_list.pop_back();
322 struct REBUILD_HELPER
326 static ParamVocab::iterator find_param_desc(ParamVocab& vocab, const synfig::String& x)
328 ParamVocab::iterator iter;
330 for(iter=vocab.begin();iter!=vocab.end();++iter)
331 if(iter->get_name()==x)
336 void process_vocab(ParamVocab x)
338 ParamVocab::iterator iter;
340 for(iter=vocab.begin();iter!=vocab.end();++iter)
342 ParamVocab::iterator iter2(find_param_desc(x,iter->get_name()));
345 // remove it and start over
358 LayerList::iterator iter(layer_list.begin());
359 rebuild_helper.vocab=(*iter)->get_param_vocab();
361 for(++iter;iter!=layer_list.end();++iter)
363 rebuild_helper.process_vocab((*iter)->get_param_vocab());
364 changed_connection_list.push_back(
365 (*iter)->signal_changed().connect(
368 &LayerParamTreeStore::changed
375 ParamVocab::iterator iter;
376 for(iter=rebuild_helper.vocab.begin();iter!=rebuild_helper.vocab.end();++iter)
378 if(iter->get_hidden())
382 if(iter->get_animation_only())
384 int length(layer_list.front()->get_canvas()->rend_desc().get_frame_end()-layer_list.front()->get_canvas()->rend_desc().get_frame_start());
389 Gtk::TreeRow row(*(append()));
390 synfigapp::ValueDesc value_desc(layer_list.front(),iter->get_name());
391 CanvasTreeStore::set_row(row,value_desc);
392 if(value_desc.is_value_node())
394 changed_connection_list.push_back(
395 value_desc.get_value_node()->signal_changed().connect(
398 &LayerParamTreeStore::changed
403 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
405 Canvas::Handle canvas_handle = value_desc.get_value().get(Canvas::Handle());
406 if(canvas_handle) changed_connection_list.push_back(
407 canvas_handle->signal_changed().connect(
410 &LayerParamTreeStore::changed
415 //row[model.label] = iter->get_local_name();
416 row[model.param_desc] = *iter;
417 row[model.canvas] = layer_list.front()->get_canvas();
418 row[model.is_inconsistent] = false;
419 //row[model.is_toplevel] = true;
422 LayerList::iterator iter2(layer_list.begin());
423 ValueBase value((*iter2)->get_param(iter->get_name()));
424 for(++iter2;iter2!=layer_list.end();++iter2)
426 if(value!=((*iter2)->get_param(iter->get_name())))
428 row[model.is_inconsistent] = true;
429 while(!row.children().empty() && erase(row.children().begin()))
438 LayerParamTreeStore::queue_refresh()
443 queue_connection.disconnect();
444 queue_connection=Glib::signal_timeout().connect(
446 sigc::mem_fun(*this,&LayerParamTreeStore::refresh),
454 LayerParamTreeStore::queue_rebuild()
459 queue_connection.disconnect();
460 queue_connection=Glib::signal_timeout().connect(
462 sigc::mem_fun(*this,&LayerParamTreeStore::rebuild),
470 LayerParamTreeStore::refresh()
472 if(queued)queued=false;
474 Gtk::TreeModel::Children children_(children());
476 Gtk::TreeModel::Children::iterator iter;
478 if(!children_.empty())
479 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
481 Gtk::TreeRow row=*iter;
487 LayerParamTreeStore::refresh_row(Gtk::TreeModel::Row &row)
489 if(row[model.is_toplevel])
491 row[model.is_inconsistent] = false;
492 ParamDesc param_desc(row[model.param_desc]);
494 LayerList::iterator iter2(layer_list.begin());
495 ValueBase value((*iter2)->get_param(param_desc.get_name()));
496 for(++iter2;iter2!=layer_list.end();++iter2)
498 if(value!=((*iter2)->get_param(param_desc.get_name())))
500 row[model.is_inconsistent] = true;
501 while(!row.children().empty() && erase(row.children().begin()))
508 //handle<ValueNode> value_node=row[model.value_node];
511 CanvasTreeStore::refresh_row(row);
517 LayerParamTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc)
519 Gtk::TreeModel::Children children = row.children();
520 while(!children.empty() && erase(children.begin()))
523 CanvasTreeStore::set_row(row,value_desc);
527 LayerParamTreeStore::on_value_node_added(synfig::ValueNode::Handle /*value_node*/)
533 LayerParamTreeStore::on_value_node_deleted(synfig::ValueNode::Handle /*value_node*/)
539 LayerParamTreeStore::on_value_node_child_added(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
545 LayerParamTreeStore::on_value_node_child_removed(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
551 LayerParamTreeStore::on_value_node_changed(synfig::ValueNode::Handle /*value_node*/)
557 LayerParamTreeStore::on_value_node_renamed(synfig::ValueNode::Handle /*value_node*/)
563 LayerParamTreeStore::on_value_node_replaced(synfig::ValueNode::Handle /*replaced_value_node*/,synfig::ValueNode::Handle /*new_value_node*/)
569 LayerParamTreeStore::on_layer_param_changed(synfig::Layer::Handle /*handle*/,synfig::String /*param_name*/)