File: panel.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (513 lines) | stat: -rw-r--r-- 17,284 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// K-3D
// Copyright (c) 1995-2007, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <k3d-i18n-config.h>
#include <k3dsdk/application_plugin_factory.h>
#include <k3dsdk/classes.h>
#include <k3dsdk/gl.h>
#include <k3dsdk/idocument.h>
#include <k3dsdk/idocument_plugin_factory.h>
#include <k3dsdk/inode_collection.h>
#include <k3dsdk/ipipeline.h>
#include <k3dsdk/iuser_interface.h>
#include <k3dsdk/module.h>
#include <k3dsdk/ngui/asynchronous_update.h>
#include <k3dsdk/ngui/button.h>
#include <k3dsdk/ngui/document_state.h>
#include <k3dsdk/ngui/panel.h>
#include <k3dsdk/ngui/toolbar.h>
#include <k3dsdk/ngui/undo_utility.h>
#include <k3dsdk/nodes.h>
#include <k3dsdk/state_change_set.h>
#include <k3dsdk/utility.h>
#include <k3dsdk/utility_gl.h>

#include <gtkmm/box.h>
#include <gtkmm/menu.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/stock.h>
#include <gtkmm/treestore.h>
#include <gtkmm/treeview.h>

#include <boost/assign/list_of.hpp>

// Temporary hack
using namespace k3d::ngui;

namespace module
{

namespace ngui
{

namespace undo_tree
{

namespace detail
{

/////////////////////////////////////////////////////////////////////////////
// implementation

/** \todo This use of the pimpl-idiom is obsolete and doesn't make much sense in the context of a plugin */
class implementation :
	public Gtk::VBox,
	public asynchronous_update
{
	typedef Gtk::VBox base;

public:
	implementation(document_state& DocumentState) :
		base(false, 0),
		m_document_state(DocumentState),
		m_undo_button(0),
		m_redo_button(0)
	{
		toolbar::control* const toolbar = new toolbar::control();

		m_undo_button = new Gtk::Button();
		m_redo_button = new Gtk::Button();

		m_undo_button->add(*Gtk::manage(new Gtk::Image(Gtk::Stock::UNDO, Gtk::ICON_SIZE_SMALL_TOOLBAR)));
		m_redo_button->add(*Gtk::manage(new Gtk::Image(Gtk::Stock::REDO, Gtk::ICON_SIZE_SMALL_TOOLBAR)));

		m_undo_button->signal_clicked().connect(sigc::mem_fun(*this, &implementation::on_undo));
		m_redo_button->signal_clicked().connect(sigc::mem_fun(*this, &implementation::on_redo));

		toolbar->row(0).pack_start(*Gtk::manage(m_undo_button), Gtk::PACK_SHRINK);
		toolbar->row(0).pack_start(*Gtk::manage(m_redo_button), Gtk::PACK_SHRINK);

		Gtk::ScrolledWindow* const scrolled_window = new Gtk::ScrolledWindow();
		scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
		scrolled_window->add(m_view);

		pack_start(*Gtk::manage(toolbar), Gtk::PACK_SHRINK);
		pack_start(*Gtk::manage(scrolled_window), Gtk::PACK_EXPAND_WIDGET);

		m_model = Gtk::TreeStore::create(m_columns);

		m_view.set_model(m_model);
		m_view.set_headers_visible(false);
		m_view.set_reorderable(false);
		m_view.signal_row_activated().connect(sigc::mem_fun(*this, &implementation::on_select_row));

		Gtk::CellRendererText* const cell_text = new Gtk::CellRendererText();

		Gtk::TreeViewColumn* const column = new Gtk::TreeViewColumn;
		column->pack_start(m_columns.saved, false);
		column->pack_start(m_columns.current, false);
		column->pack_start(*manage(cell_text), true);
		column->add_attribute(cell_text->property_text(), m_columns.label);
		column->add_attribute(cell_text->property_strikethrough(), m_columns.strikethrough);

		m_view.append_column(*manage(column));

		m_document_state.document().state_recorder().connect_node_added_signal(sigc::mem_fun(*this, &implementation::on_node_added));
		m_document_state.document().state_recorder().connect_current_node_changed_signal(sigc::mem_fun(*this, &implementation::on_current_node_changed));
		m_document_state.document().state_recorder().connect_last_saved_node_changed_signal(sigc::mem_fun(*this, &implementation::on_last_saved_node_changed));

		schedule_update();
	}

	/// Called by the signal system when the user clicks the undo button
	void on_undo()
	{
		const k3d::istate_recorder::node* current_node = m_document_state.document().state_recorder().current_node();
		return_if_fail(current_node);

		current_node->change_set->undo();
		m_document_state.document().state_recorder().set_current_node(current_node->parent);
		k3d::gl::redraw_all(m_document_state.document(), k3d::gl::irender_viewport::ASYNCHRONOUS);
	}

	/// Called by the signal system when the user clicks the redo button
	void on_redo()
	{
		const k3d::istate_recorder::node* const redo_node = next_redo(m_document_state.document().state_recorder());
		return_if_fail(redo_node);

		redo_node->change_set->redo();
		m_document_state.document().state_recorder().set_current_node(redo_node);
		k3d::gl::redraw_all(m_document_state.document(), k3d::gl::irender_viewport::ASYNCHRONOUS);
	}

	/// Called by the signal system anytime a node is added to the hierarchical undo tree
	void on_node_added(const k3d::istate_recorder::node* const NewNode)
	{
		add_node(NewNode, 0, m_model->children());
	}

	/// Called by the signal system anytime the current node in the hierarchical undo tree changes
	void on_current_node_changed()
	{
		k3d::istate_recorder& state_recorder = m_document_state.document().state_recorder();
		const k3d::istate_recorder::node* const current_node = state_recorder.current_node();
		const k3d::istate_recorder::node* const redo_node = next_redo(state_recorder);
		const parent_nodes_t active_nodes = parent_nodes(current_node);

		set_current_node(current_node, redo_node, active_nodes, m_model->children());

		update_buttons();
	}

	/// Updates the state of the undo/redo buttons to match the current node in the hierarchical undo tree
	void update_buttons()
	{
		k3d::istate_recorder& state_recorder = m_document_state.document().state_recorder();
		const k3d::istate_recorder::node* const current_node = state_recorder.current_node();
		const k3d::istate_recorder::node* const redo_node = next_redo(state_recorder);

		// Setup the undo button ...
		if(current_node)
		{
			m_undo_button->set_sensitive(true);

			const std::string label = k3d::string_cast(boost::format(_("Undo %1%")) % current_node->label);
			m_undo_button->set_tooltip_text(label);
//				m_undo_button->set_label(label);
		}
		else
		{
			m_undo_button->set_sensitive(false);

			const std::string label = _("Can't Undo");
			m_undo_button->set_tooltip_text(label);
//				m_undo_button->set_label(label);
		}

		// Setup the redo menu item ...
		if(redo_node)
		{
			m_redo_button->set_sensitive(true);

			const std::string label = k3d::string_cast(boost::format(_("Redo %1%")) % redo_node->label);
			m_redo_button->set_tooltip_text(label);
//				m_redo_button->set_label(label);
		}
		else
		{
			m_redo_button->set_sensitive(false);

			const std::string label = _("Can't Redo");
			m_redo_button->set_tooltip_text(label);
//				m_redo_button->set_label(label);
		}
	}

	/// Called by the signal system anytime the last saved node in the hierarchical undo tree changes
	void on_last_saved_node_changed()
	{
		k3d::istate_recorder& state_recorder = m_document_state.document().state_recorder();
		const k3d::istate_recorder::node* const last_saved_node = state_recorder.last_saved_node();

		set_last_saved_node(last_saved_node, m_model->children());
	}

	void on_update()
	{
		update_contents();
		update_buttons();
	}

	/// Rebuilds the contents of the control from scratch
	void update_contents()
	{
		k3d::istate_recorder& state_recorder = m_document_state.document().state_recorder();
		const k3d::istate_recorder::node* const current_node = state_recorder.current_node();
		const k3d::istate_recorder::node* const redo_node = next_redo(state_recorder);
		const k3d::istate_recorder::node* const saved_node = state_recorder.last_saved_node();
		const parent_nodes_t active_nodes = parent_nodes(current_node);

		m_model->clear();

		const k3d::istate_recorder::nodes_t root_nodes = state_recorder.root_nodes();
		for(k3d::istate_recorder::nodes_t::const_iterator node = root_nodes.begin(); node != root_nodes.end(); ++node)
			insert_node(current_node, redo_node, saved_node, *node, active_nodes, m_model->children());
	}

	/// Used to efficiently insert a single new undo hierarchy node into the treeview model
	bool add_node(const k3d::istate_recorder::node* const NewNode, const k3d::istate_recorder::node* const ParentNode, const Gtk::TreeNodeChildren& ParentRows)
	{
		if(NewNode->parent == ParentNode)
		{
			Gtk::TreeRow row = *m_model->append(ParentRows);
			row[m_columns.node] = NewNode;
			row[m_columns.label] = NewNode->label;
			row[m_columns.current] = Glib::RefPtr<Gdk::Pixbuf>();
			row[m_columns.saved] = Glib::RefPtr<Gdk::Pixbuf>();
			row[m_columns.strikethrough] = false;

			return true;
		}

		for(Gtk::TreeIter row = ParentRows.begin(); row != ParentRows.end(); ++row)
		{
			if(add_node(NewNode, (*row)[m_columns.node], row->children()))
				return true;
		}

		return false;
	}

	/// Used to efficiently update the treeview model when the undo hierarchy current node has changed
	void set_current_node(const k3d::istate_recorder::node* const CurrentNode, const k3d::istate_recorder::node* const RedoNode, const parent_nodes_t& ActiveNodes, const Gtk::TreeNodeChildren& ParentRows)
	{
		for(Gtk::TreeIter i = ParentRows.begin(); i != ParentRows.end(); ++i)
		{
			Gtk::TreeRow row = *i;

			if(row[m_columns.node] == CurrentNode)
			{
				row[m_columns.current] = m_view.render_icon(Gtk::Stock::UNDO, Gtk::ICON_SIZE_SMALL_TOOLBAR);

				m_view.scroll_to_row(Gtk::TreePath(i), 0.5);
				m_view.expand_to_path(Gtk::TreePath(i));
				m_view.expand_row(Gtk::TreePath(i), false);
			}
			else if(row[m_columns.node] == RedoNode)
			{
				row[m_columns.current] = m_view.render_icon(Gtk::Stock::REDO, Gtk::ICON_SIZE_SMALL_TOOLBAR);
			}
			else
			{
				row[m_columns.current] = Glib::RefPtr<Gdk::Pixbuf>();
			}

			row[m_columns.strikethrough] = std::find(ActiveNodes.begin(), ActiveNodes.end(), row[m_columns.node]) == ActiveNodes.end();

			set_current_node(CurrentNode, RedoNode, ActiveNodes, row.children());
		}
	}

	/// Used to efficiently update the treeview model when the undo hierarchy last saved node has changed
	void set_last_saved_node(const k3d::istate_recorder::node* const LastSavedNode, const Gtk::TreeNodeChildren& ParentRows)
	{
		for(Gtk::TreeIter i = ParentRows.begin(); i != ParentRows.end(); ++i)
		{
			Gtk::TreeRow row = *i;

			if(row[m_columns.node] == LastSavedNode)
			{
				row[m_columns.saved] = m_view.render_icon(Gtk::Stock::SAVE, Gtk::ICON_SIZE_SMALL_TOOLBAR);
			}
			else
			{
				row[m_columns.saved] = Glib::RefPtr<Gdk::Pixbuf>();
			}

			set_last_saved_node(LastSavedNode, row.children());
		}
	}

	/// Used to recursively create the treeview model from scratch
	void insert_node(const k3d::istate_recorder::node* const CurrentNode, const k3d::istate_recorder::node* const RedoNode, const k3d::istate_recorder::node* const SavedNode, k3d::istate_recorder::node* const Node, const parent_nodes_t& ActiveNodes, const Gtk::TreeNodeChildren& ParentRows)
	{
		Gtk::TreeIter i = m_model->append(ParentRows);
		Gtk::TreeRow row = *i;
		row[m_columns.node] = Node;
		row[m_columns.label] = Node->label;

		if(Node == CurrentNode)
		{
			row[m_columns.current] = m_view.render_icon(Gtk::Stock::UNDO, Gtk::ICON_SIZE_SMALL_TOOLBAR);
		}
		else if(Node == RedoNode)
		{
			row[m_columns.current] = m_view.render_icon(Gtk::Stock::REDO, Gtk::ICON_SIZE_SMALL_TOOLBAR);
		}
		else
		{
			row[m_columns.current] = Glib::RefPtr<Gdk::Pixbuf>();
		}

		if(Node == SavedNode)
		{
			row[m_columns.saved] = m_view.render_icon(Gtk::Stock::SAVE, Gtk::ICON_SIZE_SMALL_TOOLBAR);
		}
		else
		{
			row[m_columns.saved] = Glib::RefPtr<Gdk::Pixbuf>();
		}
		row[m_columns.strikethrough] = std::find(ActiveNodes.begin(), ActiveNodes.end(), Node) == ActiveNodes.end();

		for(k3d::istate_recorder::nodes_t::const_iterator node = Node->children.begin(); node != Node->children.end(); ++node)
			insert_node(CurrentNode, RedoNode, SavedNode, *node, ActiveNodes, row.children());

		if(Node == CurrentNode)
		{
			m_view.scroll_to_row(Gtk::TreePath(i), 0.5);
			m_view.expand_to_path(Gtk::TreePath(i));
			m_view.expand_row(Gtk::TreePath(i), false);
		}
	}

	/// Called when the user selects (double-clicks) a row in the treeview control
	void on_select_row(const Gtk::TreePath& Path, Gtk::TreeViewColumn* Column)
	{
		Gtk::TreeRow row = *m_model->get_iter(Path);
		const k3d::istate_recorder::node* const selected_node = row[m_columns.node];

		// Get the current node parents ...
		const parent_nodes_t current_parents = parent_nodes(m_document_state.document().state_recorder().current_node());

		// Get the selected node parents ...
		const parent_nodes_t selected_parents = parent_nodes(selected_node);

		// Remove common nodes from both lists to produce the list of undos and the list of redos to be performed to change the state
		parent_nodes_t undos;
		for(unsigned long i = 0; i != current_parents.size(); ++i)
		{
			if(i < selected_parents.size() && current_parents[i] == selected_parents[i])
				continue;

			undos.push_back(current_parents[i]);
		}
		std::reverse(undos.begin(), undos.end());

		parent_nodes_t redos;
		for(unsigned long i = 0; i != selected_parents.size(); ++i)
		{
			if(i < current_parents.size() && current_parents[i] == selected_parents[i])
				continue;

			redos.push_back(selected_parents[i]);
		}

		// Change from the current to the selected state ...
		m_document_state.document().state_recorder().set_current_node(selected_node);
		for(parent_nodes_t::const_iterator undo = undos.begin(); undo != undos.end(); ++undo)
			(*undo)->change_set->undo();

		for(parent_nodes_t::const_iterator redo = redos.begin(); redo != redos.end(); ++redo)
			(*redo)->change_set->redo();

		k3d::gl::redraw_all(m_document_state.document(), k3d::gl::irender_viewport::ASYNCHRONOUS);
	}

	/// Stores a reference to the owning document
	document_state& m_document_state;

	/// Declares columns for the TreeView data model
	class columns :
		public Gtk::TreeModelColumnRecord
	{
	public:
		columns()
		{
			add(node);
			add(label);
			add(current);
			add(saved);
			add(strikethrough);
		}

		Gtk::TreeModelColumn<const k3d::istate_recorder::node*> node;
		Gtk::TreeModelColumn<Glib::ustring> label;
		Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > current;
		Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > saved;
		Gtk::TreeModelColumn<bool> strikethrough;
	};

	/// Defines columns for the TreeView data model
	columns m_columns;
	/// Provides storage for the TreeView
	Glib::RefPtr<Gtk::TreeStore> m_model;

	Gtk::Button* m_undo_button;
	Gtk::Button* m_redo_button;
	Gtk::TreeView m_view;

	sigc::signal<void, const std::string&, const std::string&> m_command_signal;

	/// Signal that will be emitted whenever this control should grab the panel focus
	sigc::signal<void> m_panel_grab_signal;
};

} // namespace detail

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// panel

class panel :
	public Gtk::VBox,
	public k3d::ngui::panel::control
{
	typedef Gtk::VBox base;

public:
	panel() :
		base(false, 0),
		m_implementation(0)
	{
	}

	~panel()
	{
		delete m_implementation;
	}

	void initialize(document_state& DocumentState)
	{
		m_implementation = new detail::implementation(DocumentState);

		m_implementation->m_undo_button->signal_focus_in_event().connect(sigc::bind_return(sigc::hide(m_implementation->m_panel_grab_signal.make_slot()), false), false);
		m_implementation->m_redo_button->signal_focus_in_event().connect(sigc::bind_return(sigc::hide(m_implementation->m_panel_grab_signal.make_slot()), false), false);
		m_implementation->m_view.signal_focus_in_event().connect(sigc::bind_return(sigc::hide(m_implementation->m_panel_grab_signal.make_slot()), false), false);
		
		pack_start(*m_implementation, Gtk::PACK_EXPAND_WIDGET);
		show_all();
	}

	const k3d::string_t panel_type()
	{
		return get_factory().name();
	}

	sigc::connection connect_focus_signal(const sigc::slot<void>& Slot)
	{
		return m_implementation->m_panel_grab_signal.connect(Slot);
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::application_plugin_factory<panel> factory(
			k3d::uuid(0x0242c7b7, 0x234e7664, 0x3a848fa7, 0x91465b42),
			"NGUIUndoTreePanel",
			_("Provides a panel for displaying the undo tree"),
			"NGUI Panel",
			k3d::iplugin_factory::STABLE,
			boost::assign::map_list_of("ngui:component-type", "panel")("ngui:panel-label", "Undo Tree"));

		return factory;
	}

private:
	detail::implementation* m_implementation;
};

} // namespace undo_tree

} // namespace ngui

} // namespace module

K3D_MODULE_START(Registry)
	Registry.register_factory(module::ngui::undo_tree::panel::get_factory());
K3D_MODULE_END