Documentation for target_scanline.h
[synfig.git] / synfig-studio / src / gui / trees / childrentreestore.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file childrentreestore.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "trees/childrentreestore.h"
34 #include "iconcontroller.h"
35 #include <gtkmm/button.h>
36 #include <synfig/paramdesc.h>
37 #include <ETL/clock>
38
39 #include "general.h"
40
41 class Profiler : private etl::clock
42 {
43         const std::string name;
44 public:
45         Profiler(const std::string& name):name(name) { reset(); }
46         ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
47 };
48
49 #endif
50
51 /* === U S I N G =========================================================== */
52
53 using namespace std;
54 using namespace etl;
55 using namespace synfig;
56 using namespace studio;
57
58 /* === M A C R O S ========================================================= */
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 static ChildrenTreeStore::Model& ModelHack()
67 {
68         static ChildrenTreeStore::Model* model(0);
69         if(!model)model=new ChildrenTreeStore::Model;
70         return *model;
71 }
72
73 ChildrenTreeStore::ChildrenTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
74         Gtk::TreeStore                  (ModelHack()),
75         CanvasTreeStore                 (canvas_interface_)
76 {
77         canvas_row=*append();
78         canvas_row[model.label]=_("Canvases");
79         canvas_row[model.is_canvas] = false;
80         canvas_row[model.is_value_node] = false;
81
82         value_node_row=*append();
83         value_node_row[model.label]=_("ValueBase Nodes");
84         value_node_row[model.is_canvas] = false;
85         value_node_row[model.is_value_node] = false;
86
87         // Connect all the signals
88         canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_changed));
89         canvas_interface()->signal_value_node_renamed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_renamed));
90         canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_added));
91         canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_deleted));
92         canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_value_node_replaced));
93         canvas_interface()->signal_canvas_added().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_added));
94         canvas_interface()->signal_canvas_removed().connect(sigc::mem_fun(*this,&studio::ChildrenTreeStore::on_canvas_removed));
95
96         rebuild();
97 }
98
99 ChildrenTreeStore::~ChildrenTreeStore()
100 {
101 }
102
103 Glib::RefPtr<ChildrenTreeStore>
104 ChildrenTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
105 {
106         return Glib::RefPtr<ChildrenTreeStore>(new ChildrenTreeStore(canvas_interface_));
107 }
108
109 void
110 ChildrenTreeStore::rebuild()
111 {
112         // Profiler profiler("ChildrenTreeStore::rebuild()");
113         rebuild_value_nodes();
114         rebuild_canvases();
115 }
116
117 void
118 ChildrenTreeStore::refresh()
119 {
120         // Profiler profiler("ChildrenTreeStore::refresh()");
121         refresh_value_nodes();
122         refresh_canvases();
123 }
124
125 void
126 ChildrenTreeStore::rebuild_value_nodes()
127 {
128         Gtk::TreeModel::Children children(value_node_row.children());
129
130         while(!children.empty())erase(children.begin());
131
132         clear_changed_queue();
133
134         std::for_each(
135                 canvas_interface()->get_canvas()->value_node_list().rbegin(), canvas_interface()->get_canvas()->value_node_list().rend(),
136                 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_value_node_added)
137         );
138 }
139
140 void
141 ChildrenTreeStore::refresh_value_nodes()
142 {
143         Gtk::TreeModel::Children children(value_node_row.children());
144
145         Gtk::TreeModel::Children::iterator iter;
146
147         if(!children.empty())
148                 for(iter = children.begin(); iter != children.end(); ++iter)
149                 {
150                         Gtk::TreeRow row=*iter;
151                         refresh_row(row);
152                 }
153 }
154
155 void
156 ChildrenTreeStore::rebuild_canvases()
157 {
158         Gtk::TreeModel::Children children(canvas_row.children());
159
160         while(!children.empty())erase(children.begin());
161
162         std::for_each(
163                 canvas_interface()->get_canvas()->children().rbegin(), canvas_interface()->get_canvas()->children().rend(),
164                 sigc::mem_fun(*this, &studio::ChildrenTreeStore::on_canvas_added)
165         );
166 }
167
168 void
169 ChildrenTreeStore::refresh_canvases()
170 {
171         rebuild_canvases();
172 }
173
174 void
175 ChildrenTreeStore::refresh_row(Gtk::TreeModel::Row &row, bool /*do_children*/)
176 {
177         CanvasTreeStore::refresh_row(row,false);
178
179         if((bool)row[model.is_value_node])
180         {
181                 changed_set_.erase(row[model.value_node]);
182         }
183
184 }
185
186 void
187 ChildrenTreeStore::on_canvas_added(synfig::Canvas::Handle canvas)
188 {
189         Gtk::TreeRow row = *(prepend(canvas_row.children()));
190
191         row[model.icon] = Gtk::Button().render_icon(Gtk::StockID("synfig-canvas"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
192         row[model.id] = canvas->get_id();
193         row[model.name] = canvas->get_name();
194
195         if(!canvas->get_id().empty())
196                 row[model.label] = canvas->get_id();
197         else
198         if(!canvas->get_name().empty())
199                 row[model.label] = canvas->get_name();
200         else
201                 row[model.label] = _("[Unnamed]");
202
203         row[model.canvas] = canvas;
204         row[model.type] = _("Canvas");
205         //row[model.is_canvas] = true;
206         //row[model.is_value_node] = false;
207 }
208
209 void
210 ChildrenTreeStore::on_canvas_removed(synfig::Canvas::Handle /*canvas*/)
211 {
212         rebuild_canvases();
213 }
214
215 void
216 ChildrenTreeStore::on_value_node_added(synfig::ValueNode::Handle value_node)
217 {
218 //      if(value_node->get_id().find("Unnamed")!=String::npos)
219 //              return;
220
221         Gtk::TreeRow row = *prepend(value_node_row.children());
222
223         set_row(row,synfigapp::ValueDesc(canvas_interface()->get_canvas(),value_node->get_id()),false);
224 }
225
226 void
227 ChildrenTreeStore::on_value_node_deleted(synfig::ValueNode::Handle value_node)
228 {
229         Gtk::TreeIter iter;
230         //int i(0);
231
232         if(find_first_value_node(value_node,iter))
233         {
234                 erase(iter);
235         }
236         //rebuild_value_nodes();
237 }
238
239 bool
240 ChildrenTreeStore::execute_changed_value_nodes()
241 {
242         // Profiler profiler("ChildrenTreeStore::execute_changed_value_nodes()");
243         if(!replaced_set_.empty())
244                 rebuild_value_nodes();
245
246         etl::clock timer;
247         timer.reset();
248
249         while(!changed_set_.empty())
250         {
251                 ValueNode::Handle value_node(*changed_set_.begin());
252                 changed_set_.erase(value_node);
253
254                 Gtk::TreeIter iter;
255
256                 try
257                 {
258                         Gtk::TreeIter iter;
259                         int i(0);
260
261                         if(!value_node->is_exported() && find_first_value_node(value_node,iter))
262                         {
263                                 rebuild_value_nodes();
264                                 continue;
265                         }
266
267                         if(value_node->is_exported() && find_first_value_node(value_node,iter)) do
268                         {
269                                 Gtk::TreeRow row(*iter);
270                                 i++;
271                                 refresh_row(row);
272                         }while(find_next_value_node(value_node,iter));
273
274                         if(!i)
275                         {
276                                 refresh_value_nodes();
277                                 return false;
278                         }
279
280                 }
281                 catch(...)
282                 {
283                         rebuild_value_nodes();
284                         return false;
285                 }
286
287                 // If we are taking too long...
288                 if(timer()>4)
289                 {
290                         refresh_value_nodes();
291                         return false;
292                 }
293         }
294
295         return false;
296 }
297
298 void
299 ChildrenTreeStore::on_value_node_changed(synfig::ValueNode::Handle value_node)
300 {
301
302         if(!value_node->is_exported())
303                 return;
304         changed_connection.disconnect();
305 //      if(!execute_changed_queued())
306 //              changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
307         changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
308
309         changed_set_.insert(value_node);
310         /*
311         try
312         {
313                 Gtk::TreeIter iter;
314                 int i(0);
315                 while(find_next_value_node(value_node,iter))
316                 {
317                         Gtk::TreeRow row(*iter);
318                         i++;
319                         refresh_row(row);
320                 }
321                 if(!i)
322                 {
323                         refresh_value_nodes();
324                 }
325         }
326         catch(...)
327         {
328                 rebuild_value_nodes();
329         }
330         */
331 }
332
333 void
334 ChildrenTreeStore::on_value_node_renamed(synfig::ValueNode::Handle value_node __attribute__ ((unused)))
335 {
336         rebuild_value_nodes();
337 }
338
339 void
340 ChildrenTreeStore::on_value_node_replaced(synfig::ValueNode::Handle replaced_value_node,synfig::ValueNode::Handle /*new_value_node*/)
341 {
342         changed_connection.disconnect();
343         //if(!execute_changed_queued())
344 //              changed_connection=Glib::signal_idle().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes));
345                 changed_connection=Glib::signal_timeout().connect(sigc::mem_fun(*this,&ChildrenTreeStore::execute_changed_value_nodes),150);
346
347         replaced_set_.insert(replaced_value_node);
348 }
349
350 void
351 ChildrenTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
352 {
353         if(column>=get_n_columns_vfunc())
354         {
355                 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
356                 return;
357         }
358
359         if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
360         {
361                 g_warning("LayerTreeStore::set_value_impl: Bad value type");
362                 return;
363         }
364
365         try
366         {
367                 if(column==model.value.index())
368                 {
369                         Glib::Value<synfig::ValueBase> x;
370                         g_value_init(x.gobj(),model.value.type());
371                         g_value_copy(value.gobj(),x.gobj());
372
373                         synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
374                         if(value_desc)
375                         {
376                                 canvas_interface()->change_value(value_desc,x.get());
377                                 row_changed(get_path(*iter),*iter);
378                         }
379
380                         return;
381                 }
382                 else
383                         CanvasTreeStore::set_value_impl(iter,column, value);
384         }
385         catch(std::exception x)
386         {
387                 g_warning("%s", x.what());
388         }
389 }