1 /**
2 * \brief Filter Effects dialog
3 *
4 * Authors:
5 * Nicholas Bishop <nicholasbishop@gmail.org>
6 *
7 * Copyright (C) 2007 Authors
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <gtk/gtktreeview.h>
17 #include <gtkmm/cellrenderertext.h>
18 #include <gtkmm/paned.h>
19 #include <gtkmm/scale.h>
20 #include <gtkmm/scrolledwindow.h>
21 #include <gtkmm/spinbutton.h>
22 #include <gtkmm/stock.h>
23 #include <glibmm/i18n.h>
25 #include "application/application.h"
26 #include "application/editor.h"
27 #include "desktop.h"
28 #include "desktop-handles.h"
29 #include "dialog-manager.h"
30 #include "document.h"
31 #include "filter-chemistry.h"
32 #include "filter-effects-dialog.h"
33 #include "inkscape.h"
34 #include "sp-feblend.h"
35 #include "sp-fecomposite.h"
36 #include "sp-fedisplacementmap.h"
37 #include "sp-femerge.h"
38 #include "sp-filter-primitive.h"
39 #include "sp-gaussian-blur.h"
40 #include "sp-feoffset.h"
41 #include "verbs.h"
42 #include "xml/node.h"
43 #include "xml/repr.h"
44 #include <sstream>
46 #include <iostream>
48 namespace Inkscape {
49 namespace UI {
50 namespace Dialog {
52 Glib::RefPtr<Gtk::Menu> create_popup_menu(Gtk::Widget& parent, sigc::slot<void> dup,
53 sigc::slot<void> rem)
54 {
55 Glib::RefPtr<Gtk::Menu> menu(new Gtk::Menu);
57 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Duplicate"), dup));
58 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
59 menu->append(*mi);
60 mi->signal_activate().connect(rem);
61 mi->show();
62 menu->accelerate(parent);
64 return menu;
65 }
67 static void try_id_change(SPObject* ob, const Glib::ustring& text)
68 {
69 // FIXME: this needs more serious error checking...
70 if(ob && !SP_ACTIVE_DOCUMENT->getObjectById(text.c_str())) {
71 SPException ex;
72 SP_EXCEPTION_INIT(&ex);
73 sp_object_setAttribute(ob, "id", text.c_str(), &ex);
74 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_FILTER_EFFECTS, _("Set object ID"));
75 }
76 }
78 /*** FilterModifier ***/
79 FilterEffectsDialog::FilterModifier::FilterModifier()
80 : _add(Gtk::Stock::ADD)
81 {
82 Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
83 pack_start(*sw);
84 pack_start(_add, false, false);
85 sw->add(_list);
87 _list.set_model(_model);
88 _list.append_column_editable(_("_Filter"), _columns.id);
89 ((Gtk::CellRendererText*)_list.get_column(0)->get_first_cell_renderer())->
90 signal_edited().connect(sigc::mem_fun(*this, &FilterEffectsDialog::FilterModifier::filter_name_edited));
92 sw->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
93 sw->set_shadow_type(Gtk::SHADOW_IN);
94 show_all_children();
95 _add.signal_clicked().connect(sigc::mem_fun(*this, &FilterModifier::add_filter));
96 _list.signal_button_release_event().connect_notify(
97 sigc::mem_fun(*this, &FilterModifier::filter_list_button_release));
98 _menu = create_popup_menu(*this, sigc::mem_fun(*this, &FilterModifier::duplicate_filter),
99 sigc::mem_fun(*this, &FilterModifier::remove_filter));
101 update_filters();
102 }
104 Glib::SignalProxy0<void> FilterEffectsDialog::FilterModifier::signal_selection_changed()
105 {
106 return _list.get_selection()->signal_changed();
107 }
109 SPFilter* FilterEffectsDialog::FilterModifier::get_selected_filter()
110 {
111 if(_list.get_selection()) {
112 Gtk::TreeModel::iterator i = _list.get_selection()->get_selected();
114 if(i)
115 return (*i)[_columns.filter];
116 }
118 return 0;
119 }
121 void FilterEffectsDialog::FilterModifier::select_filter(const SPFilter* filter)
122 {
123 if(filter) {
124 for(Gtk::TreeModel::iterator i = _model->children().begin();
125 i != _model->children().end(); ++i) {
126 if((*i)[_columns.filter] == filter) {
127 _list.get_selection()->select(i);
128 break;
129 }
130 }
131 }
132 }
134 void FilterEffectsDialog::FilterModifier::filter_list_button_release(GdkEventButton* event)
135 {
136 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
137 const bool sensitive = get_selected_filter() != NULL;
138 _menu->items()[0].set_sensitive(sensitive);
139 _menu->items()[1].set_sensitive(sensitive);
140 _menu->popup(event->button, event->time);
141 }
142 }
144 void FilterEffectsDialog::FilterModifier::add_filter()
145 {
146 SPDocument* doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
147 SPFilter* filter = new_filter(doc);
149 update_filters();
151 select_filter(filter);
153 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter"));
154 }
156 void FilterEffectsDialog::FilterModifier::remove_filter()
157 {
158 SPFilter *filter = get_selected_filter();
160 if(filter) {
161 SPDocument* doc = filter->document;
162 sp_repr_unparent(filter->repr);
164 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Remove filter"));
166 update_filters();
167 }
168 }
170 void FilterEffectsDialog::FilterModifier::duplicate_filter()
171 {
172 SPFilter* filter = get_selected_filter();
174 if(filter) {
175 Inkscape::XML::Node* repr = SP_OBJECT_REPR(filter), *parent = repr->parent();
176 repr = repr->duplicate(repr->document());
177 parent->appendChild(repr);
179 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter"));
181 update_filters();
182 }
183 }
185 void FilterEffectsDialog::FilterModifier::filter_name_edited(const Glib::ustring& path, const Glib::ustring& text)
186 {
187 Gtk::TreeModel::iterator i = _model->get_iter(path);
189 if(i)
190 try_id_change((SPObject*)(*i)[_columns.filter], text);
191 }
193 FilterEffectsDialog::CellRendererConnection::CellRendererConnection()
194 : Glib::ObjectBase(typeid(CellRendererConnection)),
195 _primitive(*this, "primitive", 0)
196 {}
198 Glib::PropertyProxy<void*> FilterEffectsDialog::CellRendererConnection::property_primitive()
199 {
200 return _primitive.get_proxy();
201 }
203 int FilterEffectsDialog::CellRendererConnection::input_count(const SPFilterPrimitive* prim)
204 {
205 if(!prim)
206 return 0;
207 else if(SP_IS_FEBLEND(prim) || SP_IS_FECOMPOSITE(prim) || SP_IS_FEDISPLACEMENTMAP(prim))
208 return 2;
209 else if(SP_IS_FEMERGE(prim)) {
210 // Return the number of feMergeNode connections plus an extra one for adding a new input
211 int count = 1;
212 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++count);
213 return count;
214 }
215 else
216 return 1;
217 }
219 void FilterEffectsDialog::CellRendererConnection::set_text_width(const int w)
220 {
221 _text_width = w;
222 }
224 int FilterEffectsDialog::CellRendererConnection::get_text_width() const
225 {
226 return _text_width;
227 }
229 void FilterEffectsDialog::CellRendererConnection::get_size_vfunc(
230 Gtk::Widget& widget, const Gdk::Rectangle* cell_area,
231 int* x_offset, int* y_offset, int* width, int* height) const
232 {
233 PrimitiveList& primlist = dynamic_cast<PrimitiveList&>(widget);
235 if(x_offset)
236 (*x_offset) = 0;
237 if(y_offset)
238 (*y_offset) = 0;
239 if(width)
240 (*width) = size * primlist.primitive_count() + _text_width * 7;
241 if(height) {
242 // Scale the height depending on the number of inputs, unless it's
243 // the first primitive, in which case there are no connections
244 SPFilterPrimitive* prim = (SPFilterPrimitive*)_primitive.get_value();
245 (*height) = size * input_count(prim);
246 }
247 }
249 /*** PrimitiveList ***/
250 FilterEffectsDialog::PrimitiveList::PrimitiveList(FilterEffectsDialog& d)
251 : _dialog(d),
252 _in_drag(0)
253 {
254 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
255 signal_expose_event().connect(sigc::mem_fun(*this, &PrimitiveList::on_expose_signal));
257 _model = Gtk::ListStore::create(_columns);
259 set_reorderable(true);
261 set_model(_model);
262 append_column(_("_Type"), _columns.type);
264 signal_selection_changed().connect(sigc::mem_fun(*this, &PrimitiveList::queue_draw));
266 _connection_cell.set_text_width(init_text());
268 int cols_count = append_column(_("Connections"), _connection_cell);
269 Gtk::TreeViewColumn* col = get_column(cols_count - 1);
270 if(col)
271 col->add_attribute(_connection_cell.property_primitive(), _columns.primitive);
272 }
274 // Sets up a vertical Pango context/layout, and returns the largest
275 // width needed to render the FilterPrimitiveInput labels.
276 int FilterEffectsDialog::PrimitiveList::init_text()
277 {
278 // Set up a vertical context+layout
279 Glib::RefPtr<Pango::Context> context = create_pango_context();
280 const Pango::Matrix matrix = {0, -1, 1, 0, 0, 0};
281 context->set_matrix(matrix);
282 _vertical_layout = Pango::Layout::create(context);
284 int maxfont = 0;
285 for(int i = 0; i < FPInputConverter.end; ++i) {
286 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
287 int fontw, fonth;
288 _vertical_layout->get_pixel_size(fontw, fonth);
289 if(fonth > maxfont)
290 maxfont = fonth;
291 }
293 return maxfont;
294 }
296 Glib::SignalProxy0<void> FilterEffectsDialog::PrimitiveList::signal_selection_changed()
297 {
298 return get_selection()->signal_changed();
299 }
301 /* Add all filter primitives in the current to the list.
302 Keeps the same selection if possible, otherwise selects the first element */
303 void FilterEffectsDialog::PrimitiveList::update()
304 {
305 SPFilter* f = _dialog._filter_modifier.get_selected_filter();
306 const SPFilterPrimitive* active_prim = get_selected();
307 bool active_found = false;
309 _model->clear();
311 if(f) {
312 _dialog._primitive_box.set_sensitive(true);
314 for(SPObject *prim_obj = f->children;
315 prim_obj && SP_IS_FILTER_PRIMITIVE(prim_obj);
316 prim_obj = prim_obj->next) {
317 SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(prim_obj);
318 if(prim) {
319 Gtk::TreeModel::Row row = *_model->append();
320 row[_columns.primitive] = prim;
321 row[_columns.type_id] = FPConverter.get_id_from_key(prim->repr->name());
322 row[_columns.type] = FPConverter.get_label(row[_columns.type_id]);
323 row[_columns.id] = SP_OBJECT_ID(prim);
325 if(prim == active_prim) {
326 get_selection()->select(row);
327 active_found = true;
328 }
329 }
330 }
332 if(!active_found && _model->children().begin())
333 get_selection()->select(_model->children().begin());
334 }
335 else {
336 _dialog._primitive_box.set_sensitive(false);
337 }
338 }
340 void FilterEffectsDialog::PrimitiveList::set_menu(Glib::RefPtr<Gtk::Menu> menu)
341 {
342 _primitive_menu = menu;
343 }
345 SPFilterPrimitive* FilterEffectsDialog::PrimitiveList::get_selected()
346 {
347 if(_dialog._filter_modifier.get_selected_filter()) {
348 Gtk::TreeModel::iterator i = get_selection()->get_selected();
349 if(i)
350 return (*i)[_columns.primitive];
351 }
353 return 0;
354 }
356 void FilterEffectsDialog::PrimitiveList::select(SPFilterPrimitive* prim)
357 {
358 for(Gtk::TreeIter i = _model->children().begin();
359 i != _model->children().end(); ++i) {
360 if((*i)[_columns.primitive] == prim)
361 get_selection()->select(i);
362 }
363 }
367 bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e)
368 {
369 Gdk::Rectangle clip(e->area.x, e->area.y, e->area.width, e->area.height);
370 Glib::RefPtr<Gdk::Window> win = get_bin_window();
371 Glib::RefPtr<Gdk::GC> darkgc = get_style()->get_dark_gc(Gtk::STATE_NORMAL);
373 SPFilterPrimitive* prim = get_selected();
374 int row_count = get_model()->children().size();
376 int fheight = CellRendererConnection::size;
377 Gdk::Rectangle rct, vis;
378 Gtk::TreeIter row = get_model()->children().begin();
379 int text_start_x = 0;
380 if(row) {
381 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
382 get_visible_rect(vis);
383 int vis_x, vis_y;
384 tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
386 text_start_x = rct.get_x() + row_count * fheight;
387 for(int i = 0; i < FPInputConverter.end; ++i) {
388 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
389 const int x = text_start_x + _connection_cell.get_text_width() * (i + 1);
390 get_bin_window()->draw_layout(get_style()->get_text_gc(Gtk::STATE_NORMAL), x, vis_y, _vertical_layout);
391 get_bin_window()->draw_line(darkgc, x, vis_y, x, vis_y + vis.get_height());
392 }
393 }
395 int row_index = 0;
396 for(; row != get_model()->children().end(); ++row, ++row_index) {
397 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
398 const int x = rct.get_x(), y = rct.get_y(), h = rct.get_height();
400 // Check mouse state
401 int mx, my;
402 Gdk::ModifierType mask;
403 get_bin_window()->get_pointer(mx, my, mask);
405 // Outline the bottom of the connection area
406 const int outline_x = x + fheight * (row_count - row_index);
407 get_bin_window()->draw_line(darkgc, x, y + h, outline_x, y + h);
409 // Side outline
410 get_bin_window()->draw_line(darkgc, outline_x, y, outline_x, y + h);
412 std::vector<Gdk::Point> con_poly;
413 int con_drag_y;
414 bool inside;
415 const SPFilterPrimitive* row_prim = (*row)[_columns.primitive];
416 const int inputs = CellRendererConnection::input_count(row_prim);
418 if(SP_IS_FEMERGE(row_prim)) {
419 for(int i = 0; i < inputs; ++i) {
420 inside = do_connection_node(row, i, con_poly, mx, my);
421 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
422 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
423 inside, con_poly);
425 // TODO: draw connections for each of the feMergeNodes
426 }
427 }
428 else {
429 // Draw "in" shape
430 inside = do_connection_node(row, 0, con_poly, mx, my);
431 con_drag_y = con_poly[2].get_y();
432 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
433 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
434 inside, con_poly);
435 // Draw "in" connection
436 if(_in_drag != 1 || row_prim != prim)
437 draw_connection(row, SP_ATTR_IN, text_start_x, outline_x, con_poly[2].get_y(), row_count);
439 if(inputs == 2) {
440 // Draw "in2" shape
441 inside = do_connection_node(row, 1, con_poly, mx, my);
442 if(_in_drag == 2)
443 con_drag_y = con_poly[2].get_y();
444 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
445 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
446 inside, con_poly);
447 // Draw "in2" connection
448 if(_in_drag != 2 || row_prim != prim)
449 draw_connection(row, SP_ATTR_IN2, text_start_x, outline_x, con_poly[2].get_y(), row_count);
450 }
452 // Draw drag connection
453 if(row_prim == prim && _in_drag) {
454 get_bin_window()->draw_line(get_style()->get_black_gc(), outline_x, con_drag_y,
455 mx, con_drag_y);
456 get_bin_window()->draw_line(get_style()->get_black_gc(), mx, con_drag_y, mx, my);
457 }
458 }
459 }
461 return true;
462 }
464 void FilterEffectsDialog::PrimitiveList::draw_connection(const Gtk::TreeIter& input, const SPAttributeEnum attr,
465 const int text_start_x, const int x1, const int y1,
466 const int row_count)
467 {
468 const Gtk::TreeIter res = find_result(input, attr);
469 Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();
471 if(res == input) {
472 // Draw straight connection to a standard input
473 const int tw = _connection_cell.get_text_width();
474 const int src = 1 + (int)FPInputConverter.get_id_from_key(
475 SP_OBJECT_REPR((*res)[_columns.primitive])->attribute((const gchar*)sp_attribute_name(attr)));
476 get_bin_window()->draw_line(gc, x1, y1, text_start_x + tw * src + (int)(tw * 0.5f), y1);
477 }
478 else if(res != _model->children().end()) {
479 Gdk::Rectangle rct;
481 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
482 const int fheight = CellRendererConnection::size;
484 get_cell_area(get_model()->get_path(res), *get_column(1), rct);
485 const int row_index = find_index(res);
486 const int x2 = rct.get_x() + fheight * (row_count - row_index) - fheight / 2;
487 const int y2 = rct.get_y() + rct.get_height();
489 // Draw an 'L'-shaped connection to another filter primitive
490 get_bin_window()->draw_line(gc, x1, y1, x2, y1);
491 get_bin_window()->draw_line(gc, x2, y1, x2, y2);
492 }
493 }
495 // Creates a triangle outline of the connection node and returns true if (x,y) is inside the node
496 bool FilterEffectsDialog::PrimitiveList::do_connection_node(const Gtk::TreeIter& row, const int input,
497 std::vector<Gdk::Point>& points,
498 const int ix, const int iy)
499 {
500 Gdk::Rectangle rct;
501 const int input_count = CellRendererConnection::input_count((*row)[_columns.primitive]);
503 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
504 const int fheight = CellRendererConnection::size;
506 get_cell_area(_model->get_path(row), *get_column(1), rct);
507 const float h = rct.get_height() / input_count;
509 const int x = rct.get_x() + fheight * (_model->children().size() - find_index(row));
510 const int con_w = (int)(fheight * 0.35f);
511 const int con_y = (int)(rct.get_y() + (h / 2) - con_w + (input * h));
512 points.clear();
513 points.push_back(Gdk::Point(x, con_y));
514 points.push_back(Gdk::Point(x, con_y + con_w * 2));
515 points.push_back(Gdk::Point(x - con_w, con_y + con_w));
517 return ix >= x - h && iy >= con_y && ix <= x && iy <= points[1].get_y();
518 }
520 const Gtk::TreeIter FilterEffectsDialog::PrimitiveList::find_result(const Gtk::TreeIter& start,
521 const SPAttributeEnum attr)
522 {
523 SPFilterPrimitive* prim = (*start)[_columns.primitive];
524 Gtk::TreeIter target = _model->children().end();
525 int image;
527 if(attr == SP_ATTR_IN)
528 image = prim->image_in;
529 else if(attr == SP_ATTR_IN2) {
530 if(SP_IS_FEBLEND(prim))
531 image = SP_FEBLEND(prim)->in2;
532 else if(SP_IS_FECOMPOSITE(prim))
533 image = SP_FECOMPOSITE(prim)->in2;
534 /*else if(SP_IS_FEDISPLACEMENTMAP(prim))
535 image = SP_FEDISPLACEMENTMAP(prim)->in2;*/
536 else
537 return target;
538 }
539 else
540 return target;
542 if(image >= 0) {
543 for(Gtk::TreeIter i = _model->children().begin();
544 i != start; ++i) {
545 if(((SPFilterPrimitive*)(*i)[_columns.primitive])->image_out == image)
546 target = i;
547 }
548 return target;
549 }
550 else if(image < -1)
551 return start;
553 return target;
554 }
556 int FilterEffectsDialog::PrimitiveList::find_index(const Gtk::TreeIter& target)
557 {
558 int i = 0;
559 for(Gtk::TreeIter iter = _model->children().begin();
560 iter != target; ++iter, ++i);
561 return i;
562 }
564 bool FilterEffectsDialog::PrimitiveList::on_button_press_event(GdkEventButton* e)
565 {
566 Gtk::TreePath path;
567 Gtk::TreeViewColumn* col;
568 const int x = (int)e->x, y = (int)e->y;
569 int cx, cy;
571 _drag_prim = 0;
573 if(get_path_at_pos(x, y, path, col, cx, cy)) {
574 Gtk::TreeIter iter = _model->get_iter(path);
575 std::vector<Gdk::Point> points;
576 if(do_connection_node(_model->get_iter(path), 0, points, x, y))
577 _in_drag = 1;
578 else if(do_connection_node(_model->get_iter(path), 1, points, x, y))
579 _in_drag = 2;
581 queue_draw();
582 _drag_prim = (*iter)[_columns.primitive];
583 }
585 if(_in_drag) {
586 get_selection()->select(path);
587 return true;
588 }
589 else
590 return Gtk::TreeView::on_button_press_event(e);
591 }
593 bool FilterEffectsDialog::PrimitiveList::on_motion_notify_event(GdkEventMotion* e)
594 {
595 queue_draw();
597 return Gtk::TreeView::on_motion_notify_event(e);
598 }
600 bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* e)
601 {
602 SPFilterPrimitive *prim = get_selected(), *target;
604 if(_in_drag && prim) {
605 Gtk::TreePath path;
606 Gtk::TreeViewColumn* col;
607 int cx, cy;
609 if(get_path_at_pos((int)e->x, (int)e->y, path, col, cx, cy)) {
610 const gchar *in_val = 0;
611 Glib::ustring result;
612 Gtk::TreeIter target_iter = _model->get_iter(path);
613 target = (*target_iter)[_columns.primitive];
615 const int sources_x = CellRendererConnection::size * _model->children().size() +
616 _connection_cell.get_text_width();
618 if(cx > sources_x) {
619 int src = (cx - sources_x) / _connection_cell.get_text_width();
620 if(src < 0)
621 src = 1;
622 else if(src >= FPInputConverter.end)
623 src = FPInputConverter.end - 1;
624 result = FPInputConverter.get_key((FilterPrimitiveInput)src);
625 in_val = result.c_str();
626 }
627 else {
628 // Ensure that the target comes before the selected primitive
629 for(Gtk::TreeIter iter = _model->children().begin();
630 iter != get_selection()->get_selected(); ++iter) {
631 if(iter == target_iter) {
632 Inkscape::XML::Node *repr = SP_OBJECT_REPR(target);
633 // Make sure the target has a result
634 const gchar *gres = repr->attribute("result");
635 if(!gres) {
636 result = "result" + Glib::Ascii::dtostr(SP_FILTER(prim->parent)->_image_number_next);
637 repr->setAttribute("result", result.c_str());
638 in_val = result.c_str();
639 }
640 else
641 in_val = gres;
642 break;
643 }
644 }
645 }
647 if(_in_drag == 1)
648 _dialog.set_attr(SP_ATTR_IN, in_val);
649 else if(_in_drag == 2)
650 _dialog.set_attr(SP_ATTR_IN2, in_val);
651 }
653 _in_drag = 0;
654 queue_draw();
656 _dialog.update_settings_view();
657 }
659 if((e->type == GDK_BUTTON_RELEASE) && (e->button == 3)) {
660 const bool sensitive = get_selected() != NULL;
661 _primitive_menu->items()[0].set_sensitive(sensitive);
662 _primitive_menu->items()[1].set_sensitive(sensitive);
663 _primitive_menu->popup(e->button, e->time);
665 return true;
666 }
667 else
668 return Gtk::TreeView::on_button_release_event(e);
669 }
671 // Checks all of prim's inputs, removes any that use result
672 void check_single_connection(SPFilterPrimitive* prim, const int result)
673 {
674 if(prim && result >= 0) {
676 if(prim->image_in == result)
677 SP_OBJECT_REPR(prim)->setAttribute("in", 0);
679 if(SP_IS_FEBLEND(prim)) {
680 if(SP_FEBLEND(prim)->in2 == result)
681 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
682 }
683 else if(SP_IS_FECOMPOSITE(prim)) {
684 if(SP_FECOMPOSITE(prim)->in2 == result)
685 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
686 }
687 }
688 }
690 // Remove any connections going to/from prim_iter that forward-reference other primitives
691 void FilterEffectsDialog::PrimitiveList::sanitize_connections(const Gtk::TreeIter& prim_iter)
692 {
693 SPFilterPrimitive *prim = (*prim_iter)[_columns.primitive];
694 bool before = true;
696 for(Gtk::TreeIter iter = _model->children().begin();
697 iter != _model->children().end(); ++iter) {
698 if(iter == prim_iter)
699 before = false;
700 else {
701 SPFilterPrimitive* cur_prim = (*iter)[_columns.primitive];
702 if(before)
703 check_single_connection(cur_prim, prim->image_out);
704 else
705 check_single_connection(prim, cur_prim->image_out);
706 }
707 }
708 }
710 // Reorder the filter primitives to match the list order
711 void FilterEffectsDialog::PrimitiveList::on_drag_end(const Glib::RefPtr<Gdk::DragContext>&)
712 {
713 SPFilter* filter = _dialog._filter_modifier.get_selected_filter();
714 int ndx = 0;
716 for(Gtk::TreeModel::iterator iter = _model->children().begin();
717 iter != _model->children().end(); ++iter, ++ndx) {
718 SPFilterPrimitive* prim = (*iter)[_columns.primitive];
719 if(prim) {
720 SP_OBJECT_REPR(prim)->setPosition(ndx);
721 if(_drag_prim == prim) {
722 sanitize_connections(iter);
723 get_selection()->select(iter);
724 }
725 }
726 }
728 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
730 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Reorder filter primitive"));
731 }
733 int FilterEffectsDialog::PrimitiveList::primitive_count() const
734 {
735 return _model->children().size();
736 }
738 /*** SettingsGroup ***/
739 FilterEffectsDialog::SettingsGroup::SettingsGroup()
740 {
741 show();
742 }
744 void FilterEffectsDialog::SettingsGroup::init(FilterEffectsDialog* dlg, Glib::RefPtr<Gtk::SizeGroup> sg)
745 {
746 _dialog = dlg;
747 _dialog->_settings.pack_start(*this, false, false);
748 _sizegroup = sg;
749 }
751 /* Adds a new settings widget using the specified label. The label will be formatted with a colon
752 and all widgets within the setting group are aligned automatically. */
753 void FilterEffectsDialog::SettingsGroup::add_setting_generic(Gtk::Widget& w, const Glib::ustring& label)
754 {
755 Gtk::Label *lbl = Gtk::manage(new Gtk::Label(label + (label == "" ? "" : ":"), Gtk::ALIGN_LEFT));
756 Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
757 hb->set_spacing(12);
758 hb->pack_start(*lbl, false, false);
759 hb->pack_start(w);
760 pack_start(*hb);
762 _sizegroup->add_widget(*lbl);
764 hb->show();
765 lbl->show();
767 w.show();
768 }
770 /* For SpinSlider settings */
771 void FilterEffectsDialog::SettingsGroup::add_setting(SpinSlider& ss, const SPAttributeEnum attr,
772 const Glib::ustring& label)
773 {
774 add_setting_generic(ss, label);
775 ss.signal_value_changed().connect(
776 sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_spinslider), attr, &ss));
777 }
779 /* For subgroups of settings */
780 void FilterEffectsDialog::SettingsGroup::add_setting(std::vector<Gtk::Widget*>& w, const Glib::ustring& label)
781 {
782 Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
783 for(unsigned int i = 0; i < w.size(); ++i)
784 hb->pack_start(*w[i]);
785 hb->set_spacing(12);
786 add_setting_generic(*hb, label);
787 }
789 /*** ConvolveMatrix ***/
790 FilterEffectsDialog::ConvolveMatrixColumns::ConvolveMatrixColumns()
791 {
792 cols.resize(5);
793 for(unsigned i = 0; i < cols.size(); ++i)
794 add(cols[i]);
795 }
797 FilterEffectsDialog::ConvolveMatrix::ConvolveMatrix()
798 {
799 _model = Gtk::ListStore::create(_columns);
800 set_model(_model);
801 set_headers_visible(false);
802 }
804 sigc::signal<void>& FilterEffectsDialog::ConvolveMatrix::signal_changed()
805 {
806 return _signal_changed;
807 }
809 Glib::ustring FilterEffectsDialog::ConvolveMatrix::get_value() const
810 {
811 std::ostringstream os;
813 for(Gtk::TreeIter iter = _model->children().begin();
814 iter != _model->children().end(); ++iter) {
815 for(unsigned c = 0; c < get_columns().size(); ++c) {
816 os << (*iter)[_columns.cols[c]] << " ";
817 }
818 }
820 return os.str();
821 }
823 void FilterEffectsDialog::ConvolveMatrix::update(SPFeConvolveMatrix* conv)
824 {
825 if(conv) {
826 int cols, rows;
828 cols = (int)conv->order.getNumber();
829 if(cols > 5)
830 cols = 5;
831 rows = conv->order.optNumber_set ? (int)conv->order.getOptNumber() : cols;
833 update(conv, cols, rows);
834 }
835 }
837 void FilterEffectsDialog::ConvolveMatrix::update(SPFeConvolveMatrix* conv, const int rows, const int cols)
838 {
839 _model->clear();
841 remove_all_columns();
843 if(conv) {
844 int ndx = 0;
846 for(int i = 0; i < cols; ++i) {
847 append_column_numeric_editable("", _columns.cols[i], "%.2f");
848 dynamic_cast<Gtk::CellRendererText*>(get_column(i)->get_first_cell_renderer())->signal_edited().connect(
849 sigc::mem_fun(*this, &ConvolveMatrix::rebind));
850 }
852 for(int r = 0; r < rows; ++r) {
853 Gtk::TreeRow row = *(_model->append());
854 for(int c = 0; c < cols; ++c, ++ndx)
855 row[_columns.cols[c]] = ndx < (int)conv->kernelMatrix.size() ? conv->kernelMatrix[ndx] : 0;
856 }
857 }
858 }
860 void FilterEffectsDialog::ConvolveMatrix::rebind(const Glib::ustring&, const Glib::ustring&)
861 {
862 _signal_changed();
863 }
865 /*** FilterEffectsDialog ***/
867 FilterEffectsDialog::FilterEffectsDialog()
868 : Dialog ("dialogs.filtereffects", SP_VERB_DIALOG_FILTER_EFFECTS),
869 _primitive_list(*this),
870 _add_primitive_type(FPConverter),
871 _add_primitive(Gtk::Stock::ADD),
872 _settings_labels(Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL)),
873 _empty_settings(_("No primitive selected"), Gtk::ALIGN_LEFT),
874 // TODO: Find better range/climb-rate/digits values for the SpinSliders,
875 // many of the current values are just guesses
876 _blend_mode(BlendModeConverter),
877 _composite_operator(CompositeOperatorConverter),
878 _composite_k1(0, -10, 10, 1, 0.01, 1),
879 _composite_k2(0, -10, 10, 1, 0.01, 1),
880 _composite_k3(0, -10, 10, 1, 0.01, 1),
881 _composite_k4(0, -10, 10, 1, 0.01, 1),
882 _convolve_orderx(1, 0),
883 _convolve_ordery(1, 0),
884 _convolve_divisor(1, 0.01, 10, 1, 0.01, 1),
885 _convolve_bias(0, -10, 10, 1, 0.01, 1),
886 _gaussianblur_stddeviation(1, 0, 100, 1, 0.01, 1),
887 _morphology_radius(1, 0, 100, 1, 0.01, 1),
888 _offset_dx(0, -100, 100, 1, 0.01, 1),
889 _offset_dy(0, -100, 100, 1, 0.01, 1),
890 _turbulence_basefrequency(1, 0, 100, 1, 0.01, 1),
891 _turbulence_numoctaves(1, 1, 10, 1, 1, 0),
892 _turbulence_seed(1, 0, 100, 1, 0.01, 1),
893 _turbulence_stitchtiles(_("Stitch Tiles")),
894 _turbulence_fractalnoise(_turbulence_type, _("Fractal Noise")),
895 _turbulence_turbulence(_turbulence_type, _("Turbulence"))
896 {
897 // Initialize widget hierarchy
898 Gtk::HPaned* hpaned = Gtk::manage(new Gtk::HPaned);
899 Gtk::ScrolledWindow* sw_prims = Gtk::manage(new Gtk::ScrolledWindow);
900 Gtk::HBox* hb_prims = Gtk::manage(new Gtk::HBox);
901 Gtk::Frame* fr_settings = Gtk::manage(new Gtk::Frame(_("<b>Settings</b>")));
902 Gtk::Alignment* al_settings = Gtk::manage(new Gtk::Alignment);
903 get_vbox()->add(*hpaned);
904 hpaned->pack1(_filter_modifier);
905 hpaned->pack2(_primitive_box);
906 _primitive_box.pack_start(*sw_prims);
907 _primitive_box.pack_start(*hb_prims, false, false);
908 sw_prims->add(_primitive_list);
909 hb_prims->pack_end(_add_primitive, false, false);
910 hb_prims->pack_end(_add_primitive_type, false, false);
911 get_vbox()->pack_start(*fr_settings, false, false);
912 fr_settings->add(*al_settings);
913 al_settings->add(_settings);
915 _primitive_list.signal_selection_changed().connect(
916 sigc::mem_fun(*this, &FilterEffectsDialog::update_settings_view));
917 _filter_modifier.signal_selection_changed().connect(
918 sigc::mem_fun(_primitive_list, &PrimitiveList::update));
920 sw_prims->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
921 sw_prims->set_shadow_type(Gtk::SHADOW_IN);
922 al_settings->set_padding(0, 0, 12, 0);
923 fr_settings->set_shadow_type(Gtk::SHADOW_NONE);
924 ((Gtk::Label*)fr_settings->get_label_widget())->set_use_markup();
925 _add_primitive.signal_clicked().connect(sigc::mem_fun(*this, &FilterEffectsDialog::add_primitive));
926 _primitive_list.set_menu(create_popup_menu(*this, sigc::mem_fun(*this, &FilterEffectsDialog::duplicate_primitive),
927 sigc::mem_fun(*this, &FilterEffectsDialog::remove_primitive)));
928 _settings_labels->set_ignore_hidden(true);
930 show_all_children();
931 init_settings_widgets();
932 _primitive_list.update();
933 update_settings_view();
934 }
936 FilterEffectsDialog::~FilterEffectsDialog()
937 {
938 }
940 void FilterEffectsDialog::init_settings_widgets()
941 {
942 _empty_settings.set_sensitive(false);
943 _settings.pack_start(_empty_settings);
945 _blend.init(this, _settings_labels);
946 _blend.add_setting(_blend_mode, SP_ATTR_MODE, _("Mode"));
948 _colormatrix.init(this, _settings_labels);
949 //_colormatrix.add_setting(_colormatrix_type, _("Type"));
951 _componenttransfer.init(this, _settings_labels);
953 _composite.init(this, _settings_labels);
954 _composite.add_setting(_composite_operator, SP_ATTR_OPERATOR, _("Operator"));
955 _composite.add_setting(_composite_k1, SP_ATTR_K1, _("K1"));
956 _composite.add_setting(_composite_k2, SP_ATTR_K2, _("K2"));
957 _composite.add_setting(_composite_k3, SP_ATTR_K3, _("K3"));
958 _composite.add_setting(_composite_k4, SP_ATTR_K4, _("K4"));
960 _convolvematrix.init(this, _settings_labels);
961 _convolvematrix.add_setting_generic(_convolve_orderx, _("Rows"));
962 _convolve_orderx.set_range(1, 5);
963 _convolve_orderx.get_adjustment()->set_step_increment(1);
964 _convolve_orderx.signal_value_changed().connect(
965 sigc::bind(sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_special), SP_ATTR_ORDER));
966 _convolvematrix.add_setting_generic(_convolve_ordery, _("Columns"));
967 _convolve_ordery.set_range(1, 5);
968 _convolve_ordery.get_adjustment()->set_step_increment(1);
969 _convolve_ordery.signal_value_changed().connect(
970 sigc::bind(sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_special), SP_ATTR_ORDER));
971 Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
972 sw->add(_convolve_kernelmatrix);
973 sw->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
974 sw->set_shadow_type(Gtk::SHADOW_IN);
975 _convolvematrix.add_setting_generic(*sw, _("Kernel"));
976 _convolve_kernelmatrix.signal_changed().connect(
977 sigc::bind(sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_special), SP_ATTR_KERNELMATRIX));
978 //_convolvematrix.add_setting(_convolve_divisor, SP_ATTR_DIVISOR, _("Divisor"));
979 //_convolvematrix.add_setting(_convolve_bias, SP_ATTR_BIAS, _("Bias"));
981 _diffuselighting.init(this, _settings_labels);
983 _displacementmap.init(this, _settings_labels);
985 _flood.init(this, _settings_labels);
987 _gaussianblur.init(this, _settings_labels);
988 _gaussianblur.add_setting(_gaussianblur_stddeviation, SP_ATTR_STDDEVIATION, _("Standard Deviation"));
990 _image.init(this, _settings_labels);
992 _merge.init(this, _settings_labels);
994 _morphology.init(this, _settings_labels);
995 //_morphology.add_setting(_morphology_operator, _("Operator"));
996 //_morphology.add_setting(_morphology_radius, _("Radius"));
998 _offset.init(this, _settings_labels);
999 _offset.add_setting(_offset_dx, SP_ATTR_DX, _("Delta X"));
1000 _offset.add_setting(_offset_dy, SP_ATTR_DY, _("Delta Y"));
1002 _specularlighting.init(this, _settings_labels);
1004 _tile.init(this, _settings_labels);
1006 _turbulence.init(this, _settings_labels);
1007 std::vector<Gtk::Widget*> trb_grp;
1008 trb_grp.push_back(&_turbulence_fractalnoise);
1009 trb_grp.push_back(&_turbulence_turbulence);
1010 _turbulence.add_setting(trb_grp);
1011 /*_turbulence.add_setting(_turbulence_numoctaves, _("Octaves"));
1012 _turbulence.add_setting(_turbulence_basefrequency, _("Base Frequency"));
1013 _turbulence.add_setting(_turbulence_seed, _("Seed"));
1014 _turbulence.add_setting(_turbulence_stitchtiles);*/
1015 }
1017 void FilterEffectsDialog::add_primitive()
1018 {
1019 SPFilter* filter = _filter_modifier.get_selected_filter();
1021 if(filter) {
1022 SPFilterPrimitive* prim = filter_add_primitive(filter, _add_primitive_type.get_active_data()->id);
1024 _primitive_list.update();
1025 _primitive_list.select(prim);
1027 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter primitive"));
1028 }
1029 }
1031 void FilterEffectsDialog::remove_primitive()
1032 {
1033 SPFilterPrimitive* prim = _primitive_list.get_selected();
1035 if(prim) {
1036 sp_repr_unparent(prim->repr);
1038 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_FILTER_EFFECTS,
1039 _("Remove filter primitive"));
1041 _primitive_list.update();
1042 }
1043 }
1045 void FilterEffectsDialog::duplicate_primitive()
1046 {
1047 SPFilter* filter = _filter_modifier.get_selected_filter();
1048 SPFilterPrimitive* origprim = _primitive_list.get_selected();
1050 if(filter && origprim) {
1051 Inkscape::XML::Node *repr;
1052 repr = SP_OBJECT_REPR(origprim)->duplicate(SP_OBJECT_REPR(origprim)->document());
1053 SP_OBJECT_REPR(filter)->appendChild(repr);
1055 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter primitive"));
1057 _primitive_list.update();
1058 }
1059 }
1061 void FilterEffectsDialog::set_attr_spinslider(const SPAttributeEnum attr, const SpinSlider* input)
1062 {
1063 if(input->is_sensitive()) {
1064 std::ostringstream os;
1065 os << input->get_value();
1066 set_attr(attr, os.str().c_str());
1067 }
1068 }
1070 void FilterEffectsDialog::set_attr_special(const SPAttributeEnum attr)
1071 {
1072 Glib::ustring val;
1073 std::ostringstream os;
1075 switch(attr) {
1076 case SP_ATTR_ORDER:
1077 {
1078 int x = (int)(_convolve_orderx.get_value() + 0.5f);
1079 int y = (int)(_convolve_ordery.get_value() + 0.5f);
1080 os << x << " " << y;
1081 val = os.str();
1082 _convolve_kernelmatrix.update(SP_FECONVOLVEMATRIX(_primitive_list.get_selected()), x, y);
1083 break;
1084 }
1085 case SP_ATTR_KERNELMATRIX:
1086 val = _convolve_kernelmatrix.get_value();
1087 break;
1088 default:
1089 return;
1090 }
1092 set_attr(attr, val.c_str());
1093 }
1095 void FilterEffectsDialog::set_attr(const SPAttributeEnum attr, const gchar* val)
1096 {
1097 SPFilter *filter = _filter_modifier.get_selected_filter();
1098 SPFilterPrimitive* prim = _primitive_list.get_selected();
1100 if(filter && prim) {
1101 update_settings_sensitivity();
1103 SP_OBJECT_REPR(prim)->setAttribute((gchar*)sp_attribute_name(attr), val);
1104 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1106 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Set filter primitive attribute"));
1107 }
1108 }
1110 void FilterEffectsDialog::update_settings_view()
1111 {
1112 SPFilterPrimitive* prim = _primitive_list.get_selected();
1114 // Hide all the settings
1115 _settings.hide_all();
1116 _settings.show();
1118 _settings.set_sensitive(false);
1119 _empty_settings.show();
1121 if(prim) {
1122 const NR::FilterPrimitiveType tid = FPConverter.get_id_from_key(prim->repr->name());
1124 if(tid == NR::NR_FILTER_BLEND) {
1125 _blend.show_all();
1126 const gchar* val = prim->repr->attribute("mode");
1127 if(val)
1128 _blend_mode.set_active(BlendModeConverter.get_id_from_key(val));
1129 }
1130 else if(tid == NR::NR_FILTER_COLORMATRIX)
1131 _colormatrix.show_all();
1132 else if(tid == NR::NR_FILTER_COMPONENTTRANSFER)
1133 _componenttransfer.show_all();
1134 else if(tid == NR::NR_FILTER_COMPOSITE) {
1135 _composite.show_all();
1136 SPFeComposite* comp = SP_FECOMPOSITE(prim);
1137 _composite_operator.set_active(comp->composite_operator);
1138 _composite_k1.set_value(comp->k1);
1139 _composite_k2.set_value(comp->k2);
1140 _composite_k3.set_value(comp->k3);
1141 _composite_k4.set_value(comp->k4);
1142 }
1143 else if(tid == NR::NR_FILTER_CONVOLVEMATRIX) {
1144 _convolvematrix.show_all();
1145 SPFeConvolveMatrix* conv = SP_FECONVOLVEMATRIX(prim);
1146 _convolve_orderx.set_value(conv->order.getNumber());
1147 _convolve_ordery.set_value(conv->order.optNumber_set ? conv->order.getOptNumber() : conv->order.getNumber());
1148 _convolve_kernelmatrix.update(conv);
1149 }
1150 else if(tid == NR::NR_FILTER_DIFFUSELIGHTING)
1151 _diffuselighting.show_all();
1152 else if(tid == NR::NR_FILTER_DISPLACEMENTMAP) {
1153 _displacementmap.show_all();
1154 }
1155 else if(tid == NR::NR_FILTER_FLOOD)
1156 _flood.show_all();
1157 else if(tid == NR::NR_FILTER_GAUSSIANBLUR) {
1158 _gaussianblur.show_all();
1159 _gaussianblur_stddeviation.set_value(((SPGaussianBlur*)prim)->stdDeviation.getNumber());
1160 }
1161 else if(tid == NR::NR_FILTER_IMAGE)
1162 _image.show_all();
1163 else if(tid == NR::NR_FILTER_MERGE)
1164 _merge.show_all();
1165 else if(tid == NR::NR_FILTER_MORPHOLOGY)
1166 _morphology.show_all();
1167 else if(tid == NR::NR_FILTER_OFFSET) {
1168 _offset.show_all();
1169 _offset_dx.set_value(((SPFeOffset*)prim)->dx);
1170 _offset_dy.set_value(((SPFeOffset*)prim)->dy);
1171 }
1172 else if(tid == NR::NR_FILTER_SPECULARLIGHTING)
1173 _specularlighting.show_all();
1174 else if(tid == NR::NR_FILTER_TILE)
1175 _tile.show_all();
1176 else if(tid == NR::NR_FILTER_TURBULENCE)
1177 _turbulence.show_all();
1179 _settings.set_sensitive(true);
1180 _empty_settings.hide();
1181 }
1183 update_settings_sensitivity();
1184 }
1186 void FilterEffectsDialog::update_settings_sensitivity()
1187 {
1188 const bool use_k = _composite_operator.get_active_data()->id == COMPOSITE_ARITHMETIC;
1189 _composite_k1.set_sensitive(use_k);
1190 _composite_k2.set_sensitive(use_k);
1191 _composite_k3.set_sensitive(use_k);
1192 _composite_k4.set_sensitive(use_k);
1193 }
1195 } // namespace Dialog
1196 } // namespace UI
1197 } // namespace Inkscape
1199 /*
1200 Local Variables:
1201 mode:c++
1202 c-file-style:"stroustrup"
1203 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1204 indent-tabs-mode:nil
1205 fill-column:99
1206 End:
1207 */
1208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :