Make the keyframe panel's description column sortable.
[synfig.git] / synfig-studio / trunk / src / gtkmm / keyframetree.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframetree.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 "keyframetree.h"
33 #include "cellrenderer_time.h"
34 #include <gtkmm/treemodelsort.h>
35 #include <ETL/misc>
36
37 #include "general.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 KeyframeTree::KeyframeTree()
57 {
58         const KeyframeTreeStore::Model model;
59
60         {
61                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time")) );
62
63                 cell_renderer_time = Gtk::manage( new CellRenderer_Time() );
64                 column->pack_start(*cell_renderer_time,true);
65                 column->add_attribute(cell_renderer_time->property_time(), model.time);
66                 cell_renderer_time->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_time));
67
68                 column->set_reorderable();
69                 column->set_resizable();
70                 column->set_clickable();
71                 column->set_sort_column(model.time);
72
73                 append_column(*column);
74         }
75         {
76                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Length")) );
77
78                 cell_renderer_time_delta = Gtk::manage( new CellRenderer_Time() );
79                 column->pack_start(*cell_renderer_time_delta,true);
80                 column->add_attribute(cell_renderer_time_delta->property_time(), model.time_delta);
81                 cell_renderer_time_delta->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_time_delta));
82
83                 column->set_reorderable();
84                 column->set_resizable();
85                 column->set_clickable(false);
86                 // column->set_sort_column(model.time_delta);
87
88                 append_column(*column);
89         }
90         {
91                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
92
93                 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
94                 column->pack_start(*cell_renderer_jump,true);
95                 cell_renderer_jump->property_text()=_("(JMP)");
96                 cell_renderer_jump->property_foreground()="#003a7f";
97
98                 column->set_reorderable();
99                 column->set_resizable();
100                 column->set_clickable(false);
101
102                 append_column(*column);
103         }
104         // append_column_editable(_("Description"),model.description);
105         {
106                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Description")) );
107
108                 cell_renderer_description=Gtk::manage(new Gtk::CellRendererText());
109                 column->pack_start(*cell_renderer_description,true);
110                 column->add_attribute(cell_renderer_description->property_text(), model.description);
111                 cell_renderer_description->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_description));
112
113                 column->set_reorderable();
114                 column->set_resizable();
115                 column->set_clickable();
116                 column->set_sort_column(model.description);
117
118                 append_column(*column);
119         }
120
121         set_enable_search(true);
122         set_search_column(model.description);
123
124         // This makes things easier to read.
125         set_rules_hint();
126
127         // Make us more sensitive to several events
128         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
129 }
130
131 KeyframeTree::~KeyframeTree()
132 {
133         synfig::info("KeyframeTree::~KeyframeTree(): deleted");
134 }
135
136 void
137 KeyframeTree::on_rend_desc_changed()
138 {
139         cell_renderer_time->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
140         queue_draw();
141 }
142
143 void
144 KeyframeTree::set_model(Glib::RefPtr<KeyframeTreeStore> keyframe_tree_store)
145 {
146         keyframe_tree_store_=keyframe_tree_store;
147         KeyframeTreeStore::Model model;
148
149         if(true)
150         {
151                 Glib::RefPtr<Gtk::TreeModelSort> sorted_store(Gtk::TreeModelSort::create(keyframe_tree_store_));
152                 sorted_store->set_default_sort_func(sigc::ptr_fun(&studio::KeyframeTreeStore::time_sorter));
153                 sorted_store->set_sort_column(model.time.index(), Gtk::SORT_ASCENDING);
154                 sorted_store->set_sort_func(model.time,                 sigc::ptr_fun(&studio::KeyframeTreeStore::time_sorter));
155                 sorted_store->set_sort_func(model.description,  sigc::ptr_fun(&studio::KeyframeTreeStore::description_sorter));
156                 Gtk::TreeView::set_model(sorted_store);
157         }
158         else
159                 Gtk::TreeView::set_model(keyframe_tree_store);
160
161         keyframe_tree_store_->canvas_interface()->signal_rend_desc_changed().connect(
162                 sigc::mem_fun(
163                         *this,
164                         &studio::KeyframeTree::on_rend_desc_changed
165                 )
166         );
167         cell_renderer_time->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
168         cell_renderer_time_delta->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
169 }
170
171 void
172 KeyframeTree::set_editable(bool x)
173 {
174         editable_=x;
175
176         if(editable_)
177         {
178                 cell_renderer_time->property_editable()=true;
179                 cell_renderer_time_delta->property_editable()=true;
180                 cell_renderer_description->property_editable()=true;
181         }
182         else
183         {
184                 cell_renderer_time->property_editable()=false;
185                 cell_renderer_time_delta->property_editable()=false;
186                 cell_renderer_description->property_editable()=false;
187         }
188 }
189
190 void
191 KeyframeTree::on_edited_time(const Glib::ustring&path_string,synfig::Time time)
192 {
193         Gtk::TreePath path(path_string);
194
195         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
196
197         synfig::Keyframe keyframe(row[model.keyframe]);
198         if(time!=keyframe.get_time())
199         {
200                 row[model.time]=time;
201                 //keyframe.set_time(time);
202                 //signal_edited_time()(keyframe,time);
203                 //signal_edited()(keyframe);
204         }
205 }
206
207 void
208 KeyframeTree::on_edited_time_delta(const Glib::ustring&path_string,synfig::Time time)
209 {
210         Gtk::TreePath path(path_string);
211
212         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
213
214         if(row)row[model.time_delta]=time;
215 }
216
217 void
218 KeyframeTree::on_edited_description(const Glib::ustring&path_string,const Glib::ustring &desc)
219 {
220         Gtk::TreePath path(path_string);
221
222         const Gtk::TreeRow row = *(get_model()->get_iter(path));
223
224         const synfig::String description(desc);
225         synfig::Keyframe keyframe(row[model.keyframe]);
226         if(description!=keyframe.get_description())
227         {
228                 row[model.description]=desc;
229                 keyframe.set_description(description);
230                 signal_edited_description()(keyframe,description);
231                 signal_edited()(keyframe);
232         }
233 }
234
235 bool
236 KeyframeTree::on_event(GdkEvent *event)
237 {
238     switch(event->type)
239     {
240         case GDK_BUTTON_PRESS:
241                 {
242                         Gtk::TreeModel::Path path;
243                         Gtk::TreeViewColumn *column;
244                         int cell_x, cell_y;
245                         int wx(round_to_int(event->button.x)),wy(round_to_int(event->button.y));
246                         //tree_to_widget_coords (,, wx, wy);
247                         if(!get_path_at_pos(
248                                 wx,wy,  // x, y
249                                 path, // TreeModel::Path&
250                                 column, //TreeViewColumn*&
251                                 cell_x,cell_y //int&cell_x,int&cell_y
252                                 )
253                         ) break;
254                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
255
256                         signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
257                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
258                         {
259                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
260                         }
261                 }
262                 break;
263         case GDK_2BUTTON_PRESS:
264                 {
265                         Gtk::TreeModel::Path path;
266                         Gtk::TreeViewColumn *column;
267                         int cell_x, cell_y;
268                         if(!get_path_at_pos(
269                                 int(event->button.x),int(event->button.y),      // x, y
270                                 path, // TreeModel::Path&
271                                 column, //TreeViewColumn*&
272                                 cell_x,cell_y //int&cell_x,int&cell_y
273                                 )
274                         ) break;
275                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
276
277                         {
278                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
279                                 return true;
280                         }
281                 }
282                 break;
283
284         case GDK_BUTTON_RELEASE:
285                 break;
286         default:
287                 break;
288         }
289         return false;
290 }