1 /* === S Y N F I G ========================================================= */
2 /*! \file childrentreestore.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 "childrentreestore.h"
34 #include "iconcontroler.h"
35 #include <gtkmm/button.h>
36 #include <synfig/paramdesc.h>
39 class Profiler : private etl::clock
41 const std::string name;
43 Profiler(const std::string& name):name(name) { reset(); }
44 ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
49 /* === U S I N G =========================================================== */
53 using namespace synfig;
54 using namespace studio;
56 /* === M A C R O S ========================================================= */
58 /* === G L O B A L S ======================================================= */
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 static ChildrenTreeStore::Model& ModelHack()
66 static ChildrenTreeStore::Model* model(0);
67 if(!model)model=new ChildrenTreeStore::Model;
71 ChildrenTreeStore::ChildrenTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
72 Gtk::TreeStore (ModelHack()),
73 CanvasTreeStore (canvas_interface_)
76 canvas_row[model.label]=_("Canvases");
77 canvas_row[model.is_canvas] = false;
78 canvas_row[model.is_value_node] = false;
80 value_node_row=*append();
81 value_node_row[model.label]=_("ValueBase Nodes");
82 value_node_row[model.is_canvas] = false;
83 value_node_row[model.is_value_node] = false;
85 // Connect all the signals
86 canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_changed));
87 canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_added));
88 canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_deleted));
89 canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_replaced));
90 canvas_interface()->signal_canvas_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_added));
91 canvas_interface()->signal_canvas_removed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_removed));
96 ChildrenTreeStore::~ChildrenTreeStore()
100 Glib::RefPtr<ChildrenTreeStore>
101 ChildrenTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
103 return Glib::RefPtr<ChildrenTreeStore>(new ChildrenTreeStore(canvas_interface_));
107 ChildrenTreeStore::rebuild()
109 Profiler profiler("ChildrenTreeStore::rebuild()");
110 rebuild_value_nodes();
115 ChildrenTreeStore::refresh()
117 Profiler profiler("ChildrenTreeStore::refresh()");
118 refresh_value_nodes();
123 ChildrenTreeStore::rebuild_value_nodes()
125 Gtk::TreeModel::Children children(value_node_row.children());
127 while(!children.empty())erase(children.begin());
129 clear_changed_queue();
132 canvas_interface()->get_canvas()->value_node_list().rbegin(), canvas_interface()->get_canvas()->value_node_list().rend(),
133 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_value_node_added)
138 ChildrenTreeStore::refresh_value_nodes()
140 Gtk::TreeModel::Children children(value_node_row.children());
142 Gtk::TreeModel::Children::iterator iter;
144 if(!children.empty())
145 for(iter = children.begin(); iter != children.end(); ++iter)
147 Gtk::TreeRow row=*iter;
154 ChildrenTreeStore::rebuild_canvases()
156 Gtk::TreeModel::Children children(canvas_row.children());
158 while(!children.empty())erase(children.begin());
161 canvas_interface()->get_canvas()->children().rbegin(), canvas_interface()->get_canvas()->children().rend(),
162 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_canvas_added)
167 ChildrenTreeStore::refresh_canvases()
173 ChildrenTreeStore::refresh_row(Gtk::TreeModel::Row &row, bool /*do_children*/)
175 CanvasTreeStore::refresh_row(row,false);
177 if((bool)row[model.is_value_node])
179 changed_set_.erase(row[model.value_node]);
185 ChildrenTreeStore::on_canvas_added(Canvas::Handle canvas)
187 Gtk::TreeRow row = *(prepend(canvas_row.children()));
189 row[model.icon] = Gtk::Button().render_icon(Gtk::StockID("synfig-canvas"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
190 row[model.id] = canvas->get_id();
191 row[model.name] = canvas->get_name();
193 if(!canvas->get_id().empty())
194 row[model.label] = canvas->get_id();
196 if(!canvas->get_name().empty())
197 row[model.label] = canvas->get_name();
199 row[model.label] = _("[Unnamed]");
201 row[model.canvas] = canvas;
202 row[model.type] = _("Canvas");
203 //row[model.is_canvas] = true;
204 //row[model.is_value_node] = false;
208 ChildrenTreeStore::on_canvas_removed(Canvas::Handle /*canvas*/)
214 ChildrenTreeStore::on_value_node_added(ValueNode::Handle value_node)
216 // if(value_node->get_id().find("Unnamed")!=String::npos)
219 Gtk::TreeRow row = *prepend(value_node_row.children());
221 set_row(row,synfigapp::ValueDesc(canvas_interface()->get_canvas(),value_node->get_id()),false);
225 ChildrenTreeStore::on_value_node_deleted(etl::handle<ValueNode> value_node)
230 if(find_first_value_node(value_node,iter))
234 //rebuild_value_nodes();
238 ChildrenTreeStore::execute_changed_value_nodes()
240 Profiler profiler("ChildrenTreeStore::execute_changed_value_nodes()");
242 if(!replaced_set_.empty())
243 rebuild_value_nodes();
248 while(!changed_set_.empty())
250 ValueNode::Handle value_node(*changed_set_.begin());
251 changed_set_.erase(value_node);
260 if(!value_node->is_exported() && find_first_value_node(value_node,iter))
262 rebuild_value_nodes();
266 if(value_node->is_exported() && find_first_value_node(value_node,iter)) do
268 Gtk::TreeRow row(*iter);
271 }while(find_next_value_node(value_node,iter));
275 refresh_value_nodes();
282 rebuild_value_nodes();
286 // If we are taking too long...
289 refresh_value_nodes();
298 ChildrenTreeStore::on_value_node_changed(etl::handle<ValueNode> value_node)
301 if(!value_node->is_exported())
303 changed_connection.disconnect();
304 // if(!execute_changed_queued())
305 // changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
306 changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
308 changed_set_.insert(value_node);
314 while(find_next_value_node(value_node,iter))
316 Gtk::TreeRow row(*iter);
322 refresh_value_nodes();
327 rebuild_value_nodes();
333 ChildrenTreeStore::on_value_node_replaced(synfig::ValueNode::Handle replaced_value_node,synfig::ValueNode::Handle /*new_value_node*/)
335 changed_connection.disconnect();
336 //if(!execute_changed_queued())
337 // changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
338 changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
340 replaced_set_.insert(replaced_value_node);
344 ChildrenTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
346 if(column>=get_n_columns_vfunc())
348 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
352 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
354 g_warning("LayerTreeStore::set_value_impl: Bad value type");
360 if(column==model.value.index())
362 Glib::Value<synfig::ValueBase> x;
363 g_value_init(x.gobj(),model.value.type());
364 g_value_copy(value.gobj(),x.gobj());
366 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
369 canvas_interface()->change_value(value_desc,x.get());
370 row_changed(get_path(*iter),*iter);
376 CanvasTreeStore::set_value_impl(iter,column, value);
378 catch(std::exception x)