91e12a7a0c7ab668e17e8cae918cfe879189d059
[synfig.git] / synfig-studio / trunk / src / gtkmm / metadatatreestore.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file metadatatreestore.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "metadatatreestore.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #include "general.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 static MetaDataTreeStore::Model& ModelHack()
55 {
56         static MetaDataTreeStore::Model* model(0);
57         if(!model)model=new MetaDataTreeStore::Model;
58         return *model;
59 }
60
61 MetaDataTreeStore::MetaDataTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
62         Gtk::TreeStore  (ModelHack()),
63         canvas_interface_               (canvas_interface_)
64 {
65         // Connect the signal
66         get_canvas()->signal_meta_data_changed().connect(sigc::mem_fun(*this,&MetaDataTreeStore::meta_data_changed));
67
68         rebuild();
69 }
70
71 MetaDataTreeStore::~MetaDataTreeStore()
72 {
73         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
74                 synfig::info("MetaDataTreeStore::~MetaDataTreeStore(): Deleted");
75 }
76
77 Glib::RefPtr<MetaDataTreeStore>
78 MetaDataTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
79 {
80         return Glib::RefPtr<MetaDataTreeStore>(new MetaDataTreeStore(canvas_interface_));
81 }
82
83 void
84 MetaDataTreeStore::meta_data_changed(synfig::String /*key*/)
85 {
86         rebuild();
87 }
88
89 void
90 MetaDataTreeStore::rebuild()
91 {
92         clear();
93
94         std::list<String> keys(get_canvas()->get_meta_data_keys());
95
96         for(;!keys.empty();keys.pop_front())
97         {
98                 Gtk::TreeRow row(*append());
99                 row[model.key]=keys.front();
100         }
101 }
102
103 void
104 MetaDataTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
105 {
106         if(column>=get_n_columns_vfunc())
107         {
108                 g_warning("KeyframeTreeStore::set_value_impl: Bad column (%d)",column);
109                 return;
110         }
111
112         if(column==model.data.index())
113         {
114                 synfig::String key((Glib::ustring)(*iter)[model.key]);
115                 g_value_init(value.gobj(),G_TYPE_STRING);
116                 g_value_set_string(value.gobj(),get_canvas()->get_meta_data(key).c_str());
117                 return;
118         }
119         else
120                 Gtk::TreeStore::get_value_vfunc(iter,column,value);
121 }
122
123 void
124 MetaDataTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
125 {
126         if(column>=get_n_columns_vfunc())
127         {
128                 g_warning("KeyframeTreeStore::set_value_impl: Bad column (%d)",column);
129                 return;
130         }
131
132         if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
133         {
134                 g_warning("KeyframeTreeStore::set_value_impl: Bad value type");
135                 return;
136         }
137
138         if(column==model.data.index())
139         {
140                 Glib::Value<Glib::ustring> x;
141                 g_value_init(x.gobj(),model.data.type());
142                 g_value_copy(value.gobj(),x.gobj());
143
144                 synfig::String key((Glib::ustring)(*iter)[model.key]);
145                 synfig::String new_data(x.get());
146
147                 get_canvas_interface()->set_meta_data(key,new_data);
148         }
149         else
150                 Gtk::TreeStore::set_value_impl(iter,column, value);
151 }