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
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 "childrentreestore.h"
33 #include "iconcontroler.h"
34 #include <gtkmm/button.h>
35 #include <synfig/paramdesc.h>
38 class Profiler : private etl::clock
40 const std::string name;
42 Profiler(const std::string& name):name(name) { reset(); }
43 ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
48 /* === U S I N G =========================================================== */
52 using namespace synfig;
53 using namespace studio;
55 /* === M A C R O S ========================================================= */
57 /* === G L O B A L S ======================================================= */
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 static ChildrenTreeStore::Model& ModelHack()
65 static ChildrenTreeStore::Model* model(0);
66 if(!model)model=new ChildrenTreeStore::Model;
70 ChildrenTreeStore::ChildrenTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
71 Gtk::TreeStore (ModelHack()),
72 CanvasTreeStore (canvas_interface_)
75 canvas_row[model.label]=_("Canvases");
76 canvas_row[model.is_canvas] = false;
77 canvas_row[model.is_value_node] = false;
79 value_node_row=*append();
80 value_node_row[model.label]=_("ValueBase Nodes");
81 value_node_row[model.is_canvas] = false;
82 value_node_row[model.is_value_node] = false;
84 // Connect all the signals
85 canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_changed));
86 canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_added));
87 canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_deleted));
88 canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_replaced));
89 canvas_interface()->signal_canvas_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_added));
90 canvas_interface()->signal_canvas_removed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_removed));
95 ChildrenTreeStore::~ChildrenTreeStore()
99 Glib::RefPtr<ChildrenTreeStore>
100 ChildrenTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
102 return Glib::RefPtr<ChildrenTreeStore>(new ChildrenTreeStore(canvas_interface_));
106 ChildrenTreeStore::rebuild()
108 Profiler profiler("ChildrenTreeStore::rebuild()");
109 rebuild_value_nodes();
114 ChildrenTreeStore::refresh()
116 Profiler profiler("ChildrenTreeStore::refresh()");
117 refresh_value_nodes();
122 ChildrenTreeStore::rebuild_value_nodes()
124 Gtk::TreeModel::Children children(value_node_row.children());
126 while(!children.empty())erase(children.begin());
128 clear_changed_queue();
131 canvas_interface()->get_canvas()->value_node_list().rbegin(), canvas_interface()->get_canvas()->value_node_list().rend(),
132 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_value_node_added)
137 ChildrenTreeStore::refresh_value_nodes()
139 Gtk::TreeModel::Children children(value_node_row.children());
141 Gtk::TreeModel::Children::iterator iter;
143 if(!children.empty())
144 for(iter = children.begin(); iter != children.end(); ++iter)
146 Gtk::TreeRow row=*iter;
153 ChildrenTreeStore::rebuild_canvases()
155 Gtk::TreeModel::Children children(canvas_row.children());
157 while(!children.empty())erase(children.begin());
160 canvas_interface()->get_canvas()->children().rbegin(), canvas_interface()->get_canvas()->children().rend(),
161 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_canvas_added)
166 ChildrenTreeStore::refresh_canvases()
172 ChildrenTreeStore::refresh_row(Gtk::TreeModel::Row &row, bool do_children)
174 CanvasTreeStore::refresh_row(row,false);
176 if((bool)row[model.is_value_node])
178 changed_set_.erase(row[model.value_node]);
184 ChildrenTreeStore::on_canvas_added(Canvas::Handle canvas)
186 Gtk::TreeRow row = *(prepend(canvas_row.children()));
188 row[model.icon] = Gtk::Button().render_icon(Gtk::StockID("synfig-canvas"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
189 row[model.id] = canvas->get_id();
190 row[model.name] = canvas->get_name();
192 if(!canvas->get_id().empty())
193 row[model.label] = canvas->get_id();
195 if(!canvas->get_name().empty())
196 row[model.label] = canvas->get_name();
198 row[model.label] = _("[Unnamed]");
200 row[model.canvas] = canvas;
201 row[model.type] = _("Canvas");
202 //row[model.is_canvas] = true;
203 //row[model.is_value_node] = false;
207 ChildrenTreeStore::on_canvas_removed(Canvas::Handle canvas)
213 ChildrenTreeStore::on_value_node_added(ValueNode::Handle value_node)
215 // if(value_node->get_id().find("Unnamed")!=String::npos)
218 Gtk::TreeRow row = *prepend(value_node_row.children());
220 set_row(row,synfigapp::ValueDesc(canvas_interface()->get_canvas(),value_node->get_id()),false);
224 ChildrenTreeStore::on_value_node_deleted(etl::handle<ValueNode> value_node)
229 if(find_first_value_node(value_node,iter))
233 //rebuild_value_nodes();
237 ChildrenTreeStore::execute_changed_value_nodes()
239 Profiler profiler("ChildrenTreeStore::execute_changed_value_nodes()");
241 if(!replaced_set_.empty())
242 rebuild_value_nodes();
247 while(!changed_set_.empty())
249 ValueNode::Handle value_node(*changed_set_.begin());
250 changed_set_.erase(value_node);
259 if(!value_node->is_exported() && find_first_value_node(value_node,iter))
261 rebuild_value_nodes();
265 if(value_node->is_exported() && find_first_value_node(value_node,iter)) do
267 Gtk::TreeRow row(*iter);
270 }while(find_next_value_node(value_node,iter));
274 refresh_value_nodes();
281 rebuild_value_nodes();
285 // If we are taking too long...
288 refresh_value_nodes();
297 ChildrenTreeStore::on_value_node_changed(etl::handle<ValueNode> value_node)
300 if(value_node->get_name()=="constant" || !value_node->is_exported())
302 changed_connection.disconnect();
303 // if(!execute_changed_queued())
304 // changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
305 changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
307 changed_set_.insert(value_node);
313 while(find_next_value_node(value_node,iter))
315 Gtk::TreeRow row(*iter);
321 refresh_value_nodes();
326 rebuild_value_nodes();
332 ChildrenTreeStore::on_value_node_replaced(synfig::ValueNode::Handle replaced_value_node,synfig::ValueNode::Handle new_value_node)
334 changed_connection.disconnect();
335 //if(!execute_changed_queued())
336 // changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
337 changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
339 replaced_set_.insert(replaced_value_node);
343 ChildrenTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
345 if(column>=get_n_columns_vfunc())
347 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
351 if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
353 g_warning("LayerTreeStore::set_value_impl: Bad value type");
359 if(column==model.value.index())
361 Glib::Value<synfig::ValueBase> x;
362 g_value_init(x.gobj(),model.value.type());
363 g_value_copy(value.gobj(),x.gobj());
365 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
368 canvas_interface()->change_value(value_desc,x.get());
369 row_changed(get_path(*iter),*iter);
375 CanvasTreeStore::set_value_impl(iter,column, value);
377 catch(std::exception x)