9a0efc536d1b0684ebea8a1e13c470bc9a2afdc8
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 using namespace NR;
50 namespace Inkscape {
51 namespace UI {
52 namespace Dialog {
54 /* Displays/Edits the kernel matrix for feConvolveMatrix */
55 class FilterEffectsDialog::ConvolveMatrix : public Gtk::TreeView, public AttrWidget
56 {
57 public:
58 ConvolveMatrix(const SPAttributeEnum a)
59 : AttrWidget(a)
60 {
61 _model = Gtk::ListStore::create(_columns);
62 set_model(_model);
63 set_headers_visible(false);
64 }
66 Glib::ustring get_as_attribute() const
67 {
68 std::ostringstream os;
70 for(Gtk::TreeIter iter = _model->children().begin();
71 iter != _model->children().end(); ++iter) {
72 for(unsigned c = 0; c < get_columns().size(); ++c) {
73 os << (*iter)[_columns.cols[c]] << " ";
74 }
75 }
77 return os.str();
78 }
80 void set_from_attribute(SPObject* o)
81 {
82 update(SP_FECONVOLVEMATRIX(o));
83 }
85 sigc::signal<void>& signal_changed()
86 {
87 return _signal_changed;
88 }
90 void update_direct(FilterEffectsDialog* d)
91 {
92 update(SP_FECONVOLVEMATRIX(d->_primitive_list.get_selected()));
93 }
94 private:
95 class ConvolveMatrixColumns : public Gtk::TreeModel::ColumnRecord
96 {
97 public:
98 ConvolveMatrixColumns()
99 {
100 cols.resize(5);
101 for(unsigned i = 0; i < cols.size(); ++i)
102 add(cols[i]);
103 }
104 std::vector<Gtk::TreeModelColumn<double> > cols;
105 };
107 void update(SPFeConvolveMatrix* conv)
108 {
109 if(conv) {
110 int cols, rows;
112 cols = (int)conv->order.getNumber();
113 if(cols > 5)
114 cols = 5;
115 rows = conv->order.optNumber_set ? (int)conv->order.getOptNumber() : cols;
117 update(conv, cols, rows);
118 }
119 }
121 void update(SPFeConvolveMatrix* conv, const int rows, const int cols)
122 {
123 _model->clear();
125 remove_all_columns();
127 if(conv) {
128 int ndx = 0;
130 for(int i = 0; i < cols; ++i) {
131 append_column_numeric_editable("", _columns.cols[i], "%.2f");
132 dynamic_cast<Gtk::CellRendererText*>(get_column(i)->get_first_cell_renderer())->signal_edited().connect(
133 sigc::mem_fun(*this, &ConvolveMatrix::rebind));
134 }
136 for(int r = 0; r < rows; ++r) {
137 Gtk::TreeRow row = *(_model->append());
138 for(int c = 0; c < cols; ++c, ++ndx)
139 row[_columns.cols[c]] = ndx < (int)conv->kernelMatrix.size() ? conv->kernelMatrix[ndx] : 0;
140 }
141 }
142 }
144 void rebind(const Glib::ustring&, const Glib::ustring&)
145 {
146 _signal_changed();
147 }
149 Glib::RefPtr<Gtk::ListStore> _model;
150 ConvolveMatrixColumns _columns;
151 sigc::signal<void> _signal_changed;
152 };
154 class FilterEffectsDialog::Settings
155 {
156 public:
157 Settings(FilterEffectsDialog& d)
158 : _dialog(d)
159 {
160 _sizegroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);
161 _sizegroup->set_ignore_hidden();
163 for(int i = 0; i < NR_FILTER_ENDPRIMITIVETYPE; ++i) {
164 _dialog._settings_box.add(_groups[i]);
165 }
166 }
168 // Show the active settings group and update all the AttrWidgets with new values
169 void show_and_update(const NR::FilterPrimitiveType t)
170 {
171 type(t);
172 _groups[t].show_all();
174 SPObject* ob = _dialog._primitive_list.get_selected();
176 for(unsigned i = 0; i < _attrwidgets[_current_type].size(); ++i)
177 _attrwidgets[_current_type][i]->set_from_attribute(ob);
178 }
180 void type(const NR::FilterPrimitiveType t)
181 {
182 _current_type = t;
183 }
185 void add(Gtk::ColorButton& cb, const SPAttributeEnum attr, const Glib::ustring& label)
186 {
187 //generic_add(cb, label);
188 //cb.signal_color_set().connect(
189 // sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_color), attr, &cb));
190 }
192 // ConvolveMatrix
193 ConvolveMatrix* add(const SPAttributeEnum attr, const Glib::ustring& label)
194 {
195 ConvolveMatrix* conv = new ConvolveMatrix(attr);
196 add_widget(*conv, label);
197 _attrwidgets[_current_type].push_back(conv);
198 conv->signal_changed().connect(
199 sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_direct), attr, conv));
200 return conv;
201 }
203 // SpinSlider
204 SpinSlider* add(const SPAttributeEnum attr, const Glib::ustring& label,
205 const double lo, const double hi, const double step_inc, const double climb, const int digits)
206 {
207 SpinSlider* spinslider = new SpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
208 add_widget(*spinslider, label);
209 _attrwidgets[_current_type].push_back(spinslider);
210 spinslider->signal_value_changed().connect(
211 sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_direct), attr, spinslider));
212 return spinslider;
213 }
215 // DualSpinSlider
216 DualSpinSlider* add(const SPAttributeEnum attr, const Glib::ustring& label1, const Glib::ustring& label2,
217 const double lo, const double hi, const double step_inc, const double climb, const int digits)
218 {
219 DualSpinSlider* dss = new DualSpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
220 add_widget(dss->get_spinslider1(), label1);
221 add_widget(dss->get_spinslider2(), label2);
222 _attrwidgets[_current_type].push_back(dss);
223 dss->signal_value_changed().connect(
224 sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_direct), attr, dss));
225 return dss;
226 }
228 // ComboBoxEnum
229 template<typename T> ComboBoxEnum<T>* add(const SPAttributeEnum attr,
230 const Glib::ustring& label,
231 const Util::EnumDataConverter<T>& conv)
232 {
233 ComboBoxEnum<T>* combo = new ComboBoxEnum<T>(conv, attr);
234 add_widget(*combo, label);
235 _attrwidgets[_current_type].push_back(combo);
236 combo->signal_changed().connect(
237 sigc::bind(sigc::mem_fun(_dialog, &FilterEffectsDialog::set_attr_direct), attr, combo));
238 return combo;
239 }
240 private:
241 /* Adds a new settings widget using the specified label. The label will be formatted with a colon
242 and all widgets within the setting group are aligned automatically. */
243 void add_widget(Gtk::Widget& w, const Glib::ustring& label)
244 {
245 Gtk::Label *lbl = Gtk::manage(new Gtk::Label(label + (label == "" ? "" : ":"), Gtk::ALIGN_LEFT));
246 Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
247 hb->set_spacing(12);
248 hb->pack_start(*lbl, false, false);
249 hb->pack_start(w);
250 _groups[_current_type].pack_start(*hb);
252 _sizegroup->add_widget(*lbl);
254 hb->show();
255 lbl->show();
257 w.show();
258 }
260 Gtk::VBox _groups[NR::NR_FILTER_ENDPRIMITIVETYPE];
261 Glib::RefPtr<Gtk::SizeGroup> _sizegroup;
263 FilterEffectsDialog& _dialog;
264 std::vector<AttrWidget*> _attrwidgets[NR::NR_FILTER_ENDPRIMITIVETYPE];
265 NR::FilterPrimitiveType _current_type;
266 };
268 Glib::RefPtr<Gtk::Menu> create_popup_menu(Gtk::Widget& parent, sigc::slot<void> dup,
269 sigc::slot<void> rem)
270 {
271 Glib::RefPtr<Gtk::Menu> menu(new Gtk::Menu);
273 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Duplicate"), dup));
274 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
275 menu->append(*mi);
276 mi->signal_activate().connect(rem);
277 mi->show();
278 menu->accelerate(parent);
280 return menu;
281 }
283 static void try_id_change(SPObject* ob, const Glib::ustring& text)
284 {
285 // FIXME: this needs more serious error checking...
286 if(ob && !SP_ACTIVE_DOCUMENT->getObjectById(text.c_str())) {
287 SPException ex;
288 SP_EXCEPTION_INIT(&ex);
289 sp_object_setAttribute(ob, "id", text.c_str(), &ex);
290 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_FILTER_EFFECTS, _("Set object ID"));
291 }
292 }
294 /*** FilterModifier ***/
295 FilterEffectsDialog::FilterModifier::FilterModifier()
296 : _add(Gtk::Stock::ADD)
297 {
298 Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
299 pack_start(*sw);
300 pack_start(_add, false, false);
301 sw->add(_list);
303 _list.set_model(_model);
304 _list.append_column_editable(_("_Filter"), _columns.id);
305 ((Gtk::CellRendererText*)_list.get_column(0)->get_first_cell_renderer())->
306 signal_edited().connect(sigc::mem_fun(*this, &FilterEffectsDialog::FilterModifier::filter_name_edited));
308 sw->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
309 sw->set_shadow_type(Gtk::SHADOW_IN);
310 show_all_children();
311 _add.signal_clicked().connect(sigc::mem_fun(*this, &FilterModifier::add_filter));
312 _list.signal_button_release_event().connect_notify(
313 sigc::mem_fun(*this, &FilterModifier::filter_list_button_release));
314 _menu = create_popup_menu(*this, sigc::mem_fun(*this, &FilterModifier::duplicate_filter),
315 sigc::mem_fun(*this, &FilterModifier::remove_filter));
317 update_filters();
318 }
320 Glib::SignalProxy0<void> FilterEffectsDialog::FilterModifier::signal_selection_changed()
321 {
322 return _list.get_selection()->signal_changed();
323 }
325 SPFilter* FilterEffectsDialog::FilterModifier::get_selected_filter()
326 {
327 if(_list.get_selection()) {
328 Gtk::TreeModel::iterator i = _list.get_selection()->get_selected();
330 if(i)
331 return (*i)[_columns.filter];
332 }
334 return 0;
335 }
337 void FilterEffectsDialog::FilterModifier::select_filter(const SPFilter* filter)
338 {
339 if(filter) {
340 for(Gtk::TreeModel::iterator i = _model->children().begin();
341 i != _model->children().end(); ++i) {
342 if((*i)[_columns.filter] == filter) {
343 _list.get_selection()->select(i);
344 break;
345 }
346 }
347 }
348 }
350 void FilterEffectsDialog::FilterModifier::filter_list_button_release(GdkEventButton* event)
351 {
352 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
353 const bool sensitive = get_selected_filter() != NULL;
354 _menu->items()[0].set_sensitive(sensitive);
355 _menu->items()[1].set_sensitive(sensitive);
356 _menu->popup(event->button, event->time);
357 }
358 }
360 void FilterEffectsDialog::FilterModifier::add_filter()
361 {
362 SPDocument* doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
363 SPFilter* filter = new_filter(doc);
365 update_filters();
367 select_filter(filter);
369 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter"));
370 }
372 void FilterEffectsDialog::FilterModifier::remove_filter()
373 {
374 SPFilter *filter = get_selected_filter();
376 if(filter) {
377 SPDocument* doc = filter->document;
378 sp_repr_unparent(filter->repr);
380 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Remove filter"));
382 update_filters();
383 }
384 }
386 void FilterEffectsDialog::FilterModifier::duplicate_filter()
387 {
388 SPFilter* filter = get_selected_filter();
390 if(filter) {
391 Inkscape::XML::Node* repr = SP_OBJECT_REPR(filter), *parent = repr->parent();
392 repr = repr->duplicate(repr->document());
393 parent->appendChild(repr);
395 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter"));
397 update_filters();
398 }
399 }
401 void FilterEffectsDialog::FilterModifier::filter_name_edited(const Glib::ustring& path, const Glib::ustring& text)
402 {
403 Gtk::TreeModel::iterator i = _model->get_iter(path);
405 if(i)
406 try_id_change((SPObject*)(*i)[_columns.filter], text);
407 }
409 FilterEffectsDialog::CellRendererConnection::CellRendererConnection()
410 : Glib::ObjectBase(typeid(CellRendererConnection)),
411 _primitive(*this, "primitive", 0)
412 {}
414 Glib::PropertyProxy<void*> FilterEffectsDialog::CellRendererConnection::property_primitive()
415 {
416 return _primitive.get_proxy();
417 }
419 int FilterEffectsDialog::CellRendererConnection::input_count(const SPFilterPrimitive* prim)
420 {
421 if(!prim)
422 return 0;
423 else if(SP_IS_FEBLEND(prim) || SP_IS_FECOMPOSITE(prim) || SP_IS_FEDISPLACEMENTMAP(prim))
424 return 2;
425 else if(SP_IS_FEMERGE(prim)) {
426 // Return the number of feMergeNode connections plus an extra one for adding a new input
427 int count = 1;
428 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++count);
429 return count;
430 }
431 else
432 return 1;
433 }
435 void FilterEffectsDialog::CellRendererConnection::set_text_width(const int w)
436 {
437 _text_width = w;
438 }
440 int FilterEffectsDialog::CellRendererConnection::get_text_width() const
441 {
442 return _text_width;
443 }
445 void FilterEffectsDialog::CellRendererConnection::get_size_vfunc(
446 Gtk::Widget& widget, const Gdk::Rectangle* cell_area,
447 int* x_offset, int* y_offset, int* width, int* height) const
448 {
449 PrimitiveList& primlist = dynamic_cast<PrimitiveList&>(widget);
451 if(x_offset)
452 (*x_offset) = 0;
453 if(y_offset)
454 (*y_offset) = 0;
455 if(width)
456 (*width) = size * primlist.primitive_count() + _text_width * 7;
457 if(height) {
458 // Scale the height depending on the number of inputs, unless it's
459 // the first primitive, in which case there are no connections
460 SPFilterPrimitive* prim = (SPFilterPrimitive*)_primitive.get_value();
461 (*height) = size * input_count(prim);
462 }
463 }
465 /*** PrimitiveList ***/
466 FilterEffectsDialog::PrimitiveList::PrimitiveList(FilterEffectsDialog& d)
467 : _dialog(d),
468 _in_drag(0)
469 {
470 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
471 signal_expose_event().connect(sigc::mem_fun(*this, &PrimitiveList::on_expose_signal));
473 _model = Gtk::ListStore::create(_columns);
475 set_reorderable(true);
477 set_model(_model);
478 append_column(_("_Type"), _columns.type);
480 signal_selection_changed().connect(sigc::mem_fun(*this, &PrimitiveList::queue_draw));
482 _connection_cell.set_text_width(init_text());
484 int cols_count = append_column(_("Connections"), _connection_cell);
485 Gtk::TreeViewColumn* col = get_column(cols_count - 1);
486 if(col)
487 col->add_attribute(_connection_cell.property_primitive(), _columns.primitive);
488 }
490 // Sets up a vertical Pango context/layout, and returns the largest
491 // width needed to render the FilterPrimitiveInput labels.
492 int FilterEffectsDialog::PrimitiveList::init_text()
493 {
494 // Set up a vertical context+layout
495 Glib::RefPtr<Pango::Context> context = create_pango_context();
496 const Pango::Matrix matrix = {0, -1, 1, 0, 0, 0};
497 context->set_matrix(matrix);
498 _vertical_layout = Pango::Layout::create(context);
500 int maxfont = 0;
501 for(int i = 0; i < FPInputConverter.end; ++i) {
502 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
503 int fontw, fonth;
504 _vertical_layout->get_pixel_size(fontw, fonth);
505 if(fonth > maxfont)
506 maxfont = fonth;
507 }
509 return maxfont;
510 }
512 Glib::SignalProxy0<void> FilterEffectsDialog::PrimitiveList::signal_selection_changed()
513 {
514 return get_selection()->signal_changed();
515 }
517 /* Add all filter primitives in the current to the list.
518 Keeps the same selection if possible, otherwise selects the first element */
519 void FilterEffectsDialog::PrimitiveList::update()
520 {
521 SPFilter* f = _dialog._filter_modifier.get_selected_filter();
522 const SPFilterPrimitive* active_prim = get_selected();
523 bool active_found = false;
525 _model->clear();
527 if(f) {
528 _dialog._primitive_box.set_sensitive(true);
530 for(SPObject *prim_obj = f->children;
531 prim_obj && SP_IS_FILTER_PRIMITIVE(prim_obj);
532 prim_obj = prim_obj->next) {
533 SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(prim_obj);
534 if(prim) {
535 Gtk::TreeModel::Row row = *_model->append();
536 row[_columns.primitive] = prim;
537 row[_columns.type_id] = FPConverter.get_id_from_key(prim->repr->name());
538 row[_columns.type] = FPConverter.get_label(row[_columns.type_id]);
539 row[_columns.id] = SP_OBJECT_ID(prim);
541 if(prim == active_prim) {
542 get_selection()->select(row);
543 active_found = true;
544 }
545 }
546 }
548 if(!active_found && _model->children().begin())
549 get_selection()->select(_model->children().begin());
550 }
551 else {
552 _dialog._primitive_box.set_sensitive(false);
553 }
554 }
556 void FilterEffectsDialog::PrimitiveList::set_menu(Glib::RefPtr<Gtk::Menu> menu)
557 {
558 _primitive_menu = menu;
559 }
561 SPFilterPrimitive* FilterEffectsDialog::PrimitiveList::get_selected()
562 {
563 if(_dialog._filter_modifier.get_selected_filter()) {
564 Gtk::TreeModel::iterator i = get_selection()->get_selected();
565 if(i)
566 return (*i)[_columns.primitive];
567 }
569 return 0;
570 }
572 void FilterEffectsDialog::PrimitiveList::select(SPFilterPrimitive* prim)
573 {
574 for(Gtk::TreeIter i = _model->children().begin();
575 i != _model->children().end(); ++i) {
576 if((*i)[_columns.primitive] == prim)
577 get_selection()->select(i);
578 }
579 }
583 bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e)
584 {
585 Gdk::Rectangle clip(e->area.x, e->area.y, e->area.width, e->area.height);
586 Glib::RefPtr<Gdk::Window> win = get_bin_window();
587 Glib::RefPtr<Gdk::GC> darkgc = get_style()->get_dark_gc(Gtk::STATE_NORMAL);
589 SPFilterPrimitive* prim = get_selected();
590 int row_count = get_model()->children().size();
592 int fheight = CellRendererConnection::size;
593 Gdk::Rectangle rct, vis;
594 Gtk::TreeIter row = get_model()->children().begin();
595 int text_start_x = 0;
596 if(row) {
597 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
598 get_visible_rect(vis);
599 int vis_x, vis_y;
600 tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
602 text_start_x = rct.get_x() + row_count * fheight;
603 for(int i = 0; i < FPInputConverter.end; ++i) {
604 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
605 const int x = text_start_x + _connection_cell.get_text_width() * (i + 1);
606 get_bin_window()->draw_layout(get_style()->get_text_gc(Gtk::STATE_NORMAL), x, vis_y, _vertical_layout);
607 get_bin_window()->draw_line(darkgc, x, vis_y, x, vis_y + vis.get_height());
608 }
609 }
611 int row_index = 0;
612 for(; row != get_model()->children().end(); ++row, ++row_index) {
613 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
614 const int x = rct.get_x(), y = rct.get_y(), h = rct.get_height();
616 // Check mouse state
617 int mx, my;
618 Gdk::ModifierType mask;
619 get_bin_window()->get_pointer(mx, my, mask);
621 // Outline the bottom of the connection area
622 const int outline_x = x + fheight * (row_count - row_index);
623 get_bin_window()->draw_line(darkgc, x, y + h, outline_x, y + h);
625 // Side outline
626 get_bin_window()->draw_line(darkgc, outline_x, y, outline_x, y + h);
628 std::vector<Gdk::Point> con_poly;
629 int con_drag_y;
630 bool inside;
631 const SPFilterPrimitive* row_prim = (*row)[_columns.primitive];
632 const int inputs = CellRendererConnection::input_count(row_prim);
634 if(SP_IS_FEMERGE(row_prim)) {
635 for(int i = 0; i < inputs; ++i) {
636 inside = do_connection_node(row, i, con_poly, mx, my);
637 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
638 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
639 inside, con_poly);
641 // TODO: draw connections for each of the feMergeNodes
642 }
643 }
644 else {
645 // Draw "in" shape
646 inside = do_connection_node(row, 0, con_poly, mx, my);
647 con_drag_y = con_poly[2].get_y();
648 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
649 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
650 inside, con_poly);
651 // Draw "in" connection
652 if(_in_drag != 1 || row_prim != prim)
653 draw_connection(row, SP_ATTR_IN, text_start_x, outline_x, con_poly[2].get_y(), row_count);
655 if(inputs == 2) {
656 // Draw "in2" shape
657 inside = do_connection_node(row, 1, con_poly, mx, my);
658 if(_in_drag == 2)
659 con_drag_y = con_poly[2].get_y();
660 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
661 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
662 inside, con_poly);
663 // Draw "in2" connection
664 if(_in_drag != 2 || row_prim != prim)
665 draw_connection(row, SP_ATTR_IN2, text_start_x, outline_x, con_poly[2].get_y(), row_count);
666 }
668 // Draw drag connection
669 if(row_prim == prim && _in_drag) {
670 get_bin_window()->draw_line(get_style()->get_black_gc(), outline_x, con_drag_y,
671 mx, con_drag_y);
672 get_bin_window()->draw_line(get_style()->get_black_gc(), mx, con_drag_y, mx, my);
673 }
674 }
675 }
677 return true;
678 }
680 void FilterEffectsDialog::PrimitiveList::draw_connection(const Gtk::TreeIter& input, const SPAttributeEnum attr,
681 const int text_start_x, const int x1, const int y1,
682 const int row_count)
683 {
684 const Gtk::TreeIter res = find_result(input, attr);
685 Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();
687 if(res == input) {
688 // Draw straight connection to a standard input
689 const int tw = _connection_cell.get_text_width();
690 const int src = 1 + (int)FPInputConverter.get_id_from_key(
691 SP_OBJECT_REPR((*res)[_columns.primitive])->attribute((const gchar*)sp_attribute_name(attr)));
692 get_bin_window()->draw_line(gc, x1, y1, text_start_x + tw * src + (int)(tw * 0.5f), y1);
693 }
694 else if(res != _model->children().end()) {
695 Gdk::Rectangle rct;
697 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
698 const int fheight = CellRendererConnection::size;
700 get_cell_area(get_model()->get_path(res), *get_column(1), rct);
701 const int row_index = find_index(res);
702 const int x2 = rct.get_x() + fheight * (row_count - row_index) - fheight / 2;
703 const int y2 = rct.get_y() + rct.get_height();
705 // Draw an 'L'-shaped connection to another filter primitive
706 get_bin_window()->draw_line(gc, x1, y1, x2, y1);
707 get_bin_window()->draw_line(gc, x2, y1, x2, y2);
708 }
709 }
711 // Creates a triangle outline of the connection node and returns true if (x,y) is inside the node
712 bool FilterEffectsDialog::PrimitiveList::do_connection_node(const Gtk::TreeIter& row, const int input,
713 std::vector<Gdk::Point>& points,
714 const int ix, const int iy)
715 {
716 Gdk::Rectangle rct;
717 const int input_count = CellRendererConnection::input_count((*row)[_columns.primitive]);
719 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
720 const int fheight = CellRendererConnection::size;
722 get_cell_area(_model->get_path(row), *get_column(1), rct);
723 const float h = rct.get_height() / input_count;
725 const int x = rct.get_x() + fheight * (_model->children().size() - find_index(row));
726 const int con_w = (int)(fheight * 0.35f);
727 const int con_y = (int)(rct.get_y() + (h / 2) - con_w + (input * h));
728 points.clear();
729 points.push_back(Gdk::Point(x, con_y));
730 points.push_back(Gdk::Point(x, con_y + con_w * 2));
731 points.push_back(Gdk::Point(x - con_w, con_y + con_w));
733 return ix >= x - h && iy >= con_y && ix <= x && iy <= points[1].get_y();
734 }
736 const Gtk::TreeIter FilterEffectsDialog::PrimitiveList::find_result(const Gtk::TreeIter& start,
737 const SPAttributeEnum attr)
738 {
739 SPFilterPrimitive* prim = (*start)[_columns.primitive];
740 Gtk::TreeIter target = _model->children().end();
741 int image;
743 if(attr == SP_ATTR_IN)
744 image = prim->image_in;
745 else if(attr == SP_ATTR_IN2) {
746 if(SP_IS_FEBLEND(prim))
747 image = SP_FEBLEND(prim)->in2;
748 else if(SP_IS_FECOMPOSITE(prim))
749 image = SP_FECOMPOSITE(prim)->in2;
750 /*else if(SP_IS_FEDISPLACEMENTMAP(prim))
751 image = SP_FEDISPLACEMENTMAP(prim)->in2;*/
752 else
753 return target;
754 }
755 else
756 return target;
758 if(image >= 0) {
759 for(Gtk::TreeIter i = _model->children().begin();
760 i != start; ++i) {
761 if(((SPFilterPrimitive*)(*i)[_columns.primitive])->image_out == image)
762 target = i;
763 }
764 return target;
765 }
766 else if(image < -1)
767 return start;
769 return target;
770 }
772 int FilterEffectsDialog::PrimitiveList::find_index(const Gtk::TreeIter& target)
773 {
774 int i = 0;
775 for(Gtk::TreeIter iter = _model->children().begin();
776 iter != target; ++iter, ++i);
777 return i;
778 }
780 bool FilterEffectsDialog::PrimitiveList::on_button_press_event(GdkEventButton* e)
781 {
782 Gtk::TreePath path;
783 Gtk::TreeViewColumn* col;
784 const int x = (int)e->x, y = (int)e->y;
785 int cx, cy;
787 _drag_prim = 0;
789 if(get_path_at_pos(x, y, path, col, cx, cy)) {
790 Gtk::TreeIter iter = _model->get_iter(path);
791 std::vector<Gdk::Point> points;
792 if(do_connection_node(_model->get_iter(path), 0, points, x, y))
793 _in_drag = 1;
794 else if(do_connection_node(_model->get_iter(path), 1, points, x, y))
795 _in_drag = 2;
797 queue_draw();
798 _drag_prim = (*iter)[_columns.primitive];
799 }
801 if(_in_drag) {
802 get_selection()->select(path);
803 return true;
804 }
805 else
806 return Gtk::TreeView::on_button_press_event(e);
807 }
809 bool FilterEffectsDialog::PrimitiveList::on_motion_notify_event(GdkEventMotion* e)
810 {
811 queue_draw();
813 return Gtk::TreeView::on_motion_notify_event(e);
814 }
816 bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* e)
817 {
818 SPFilterPrimitive *prim = get_selected(), *target;
820 if(_in_drag && prim) {
821 Gtk::TreePath path;
822 Gtk::TreeViewColumn* col;
823 int cx, cy;
825 if(get_path_at_pos((int)e->x, (int)e->y, path, col, cx, cy)) {
826 const gchar *in_val = 0;
827 Glib::ustring result;
828 Gtk::TreeIter target_iter = _model->get_iter(path);
829 target = (*target_iter)[_columns.primitive];
831 const int sources_x = CellRendererConnection::size * _model->children().size() +
832 _connection_cell.get_text_width();
834 if(cx > sources_x) {
835 int src = (cx - sources_x) / _connection_cell.get_text_width();
836 if(src < 0)
837 src = 1;
838 else if(src >= FPInputConverter.end)
839 src = FPInputConverter.end - 1;
840 result = FPInputConverter.get_key((FilterPrimitiveInput)src);
841 in_val = result.c_str();
842 }
843 else {
844 // Ensure that the target comes before the selected primitive
845 for(Gtk::TreeIter iter = _model->children().begin();
846 iter != get_selection()->get_selected(); ++iter) {
847 if(iter == target_iter) {
848 Inkscape::XML::Node *repr = SP_OBJECT_REPR(target);
849 // Make sure the target has a result
850 const gchar *gres = repr->attribute("result");
851 if(!gres) {
852 result = "result" + Glib::Ascii::dtostr(SP_FILTER(prim->parent)->_image_number_next);
853 repr->setAttribute("result", result.c_str());
854 in_val = result.c_str();
855 }
856 else
857 in_val = gres;
858 break;
859 }
860 }
861 }
863 if(_in_drag == 1)
864 _dialog.set_attr(SP_ATTR_IN, in_val);
865 else if(_in_drag == 2)
866 _dialog.set_attr(SP_ATTR_IN2, in_val);
867 }
869 _in_drag = 0;
870 queue_draw();
872 _dialog.update_settings_view();
873 }
875 if((e->type == GDK_BUTTON_RELEASE) && (e->button == 3)) {
876 const bool sensitive = get_selected() != NULL;
877 _primitive_menu->items()[0].set_sensitive(sensitive);
878 _primitive_menu->items()[1].set_sensitive(sensitive);
879 _primitive_menu->popup(e->button, e->time);
881 return true;
882 }
883 else
884 return Gtk::TreeView::on_button_release_event(e);
885 }
887 // Checks all of prim's inputs, removes any that use result
888 void check_single_connection(SPFilterPrimitive* prim, const int result)
889 {
890 if(prim && result >= 0) {
892 if(prim->image_in == result)
893 SP_OBJECT_REPR(prim)->setAttribute("in", 0);
895 if(SP_IS_FEBLEND(prim)) {
896 if(SP_FEBLEND(prim)->in2 == result)
897 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
898 }
899 else if(SP_IS_FECOMPOSITE(prim)) {
900 if(SP_FECOMPOSITE(prim)->in2 == result)
901 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
902 }
903 }
904 }
906 // Remove any connections going to/from prim_iter that forward-reference other primitives
907 void FilterEffectsDialog::PrimitiveList::sanitize_connections(const Gtk::TreeIter& prim_iter)
908 {
909 SPFilterPrimitive *prim = (*prim_iter)[_columns.primitive];
910 bool before = true;
912 for(Gtk::TreeIter iter = _model->children().begin();
913 iter != _model->children().end(); ++iter) {
914 if(iter == prim_iter)
915 before = false;
916 else {
917 SPFilterPrimitive* cur_prim = (*iter)[_columns.primitive];
918 if(before)
919 check_single_connection(cur_prim, prim->image_out);
920 else
921 check_single_connection(prim, cur_prim->image_out);
922 }
923 }
924 }
926 // Reorder the filter primitives to match the list order
927 void FilterEffectsDialog::PrimitiveList::on_drag_end(const Glib::RefPtr<Gdk::DragContext>&)
928 {
929 SPFilter* filter = _dialog._filter_modifier.get_selected_filter();
930 int ndx = 0;
932 for(Gtk::TreeModel::iterator iter = _model->children().begin();
933 iter != _model->children().end(); ++iter, ++ndx) {
934 SPFilterPrimitive* prim = (*iter)[_columns.primitive];
935 if(prim) {
936 SP_OBJECT_REPR(prim)->setPosition(ndx);
937 if(_drag_prim == prim) {
938 sanitize_connections(iter);
939 get_selection()->select(iter);
940 }
941 }
942 }
944 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
946 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Reorder filter primitive"));
947 }
949 int FilterEffectsDialog::PrimitiveList::primitive_count() const
950 {
951 return _model->children().size();
952 }
954 /*** FilterEffectsDialog ***/
956 FilterEffectsDialog::FilterEffectsDialog()
957 : Dialog ("dialogs.filtereffects", SP_VERB_DIALOG_FILTER_EFFECTS),
958 _primitive_list(*this),
959 _add_primitive_type(FPConverter),
960 _add_primitive(Gtk::Stock::ADD),
961 _empty_settings(_("No primitive selected"), Gtk::ALIGN_LEFT)
962 {
963 _settings = new Settings(*this);
965 // Initialize widget hierarchy
966 Gtk::HPaned* hpaned = Gtk::manage(new Gtk::HPaned);
967 Gtk::ScrolledWindow* sw_prims = Gtk::manage(new Gtk::ScrolledWindow);
968 Gtk::HBox* hb_prims = Gtk::manage(new Gtk::HBox);
969 Gtk::Frame* fr_settings = Gtk::manage(new Gtk::Frame(_("<b>Settings</b>")));
970 Gtk::Alignment* al_settings = Gtk::manage(new Gtk::Alignment);
971 get_vbox()->add(*hpaned);
972 hpaned->pack1(_filter_modifier);
973 hpaned->pack2(_primitive_box);
974 _primitive_box.pack_start(*sw_prims);
975 _primitive_box.pack_start(*hb_prims, false, false);
976 sw_prims->add(_primitive_list);
977 hb_prims->pack_end(_add_primitive, false, false);
978 hb_prims->pack_end(_add_primitive_type, false, false);
979 get_vbox()->pack_start(*fr_settings, false, false);
980 fr_settings->add(*al_settings);
981 al_settings->add(_settings_box);
983 _primitive_list.signal_selection_changed().connect(
984 sigc::mem_fun(*this, &FilterEffectsDialog::update_settings_view));
985 _filter_modifier.signal_selection_changed().connect(
986 sigc::mem_fun(_primitive_list, &PrimitiveList::update));
988 sw_prims->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
989 sw_prims->set_shadow_type(Gtk::SHADOW_IN);
990 al_settings->set_padding(0, 0, 12, 0);
991 fr_settings->set_shadow_type(Gtk::SHADOW_NONE);
992 ((Gtk::Label*)fr_settings->get_label_widget())->set_use_markup();
993 _add_primitive.signal_clicked().connect(sigc::mem_fun(*this, &FilterEffectsDialog::add_primitive));
994 _primitive_list.set_menu(create_popup_menu(*this, sigc::mem_fun(*this, &FilterEffectsDialog::duplicate_primitive),
995 sigc::mem_fun(*this, &FilterEffectsDialog::remove_primitive)));
997 show_all_children();
998 init_settings_widgets();
999 _primitive_list.update();
1000 update_settings_view();
1001 }
1003 FilterEffectsDialog::~FilterEffectsDialog()
1004 {
1005 delete _settings;
1006 }
1008 void FilterEffectsDialog::init_settings_widgets()
1009 {
1010 // TODO: Find better range/climb-rate/digits values for the SpinSliders,
1011 // most of the current values are complete guesses!
1013 _empty_settings.set_sensitive(false);
1014 _settings_box.pack_start(_empty_settings);
1016 _settings->type(NR_FILTER_BLEND);
1017 _settings->add(SP_ATTR_MODE, _("Mode"), BlendModeConverter);
1019 _settings->type(NR_FILTER_COMPOSITE);
1020 _settings->add(SP_ATTR_OPERATOR, _("Operator"), CompositeOperatorConverter);
1021 _k1 = _settings->add(SP_ATTR_K1, _("K1"), -10, 10, 1, 0.01, 1);
1022 _k2 = _settings->add(SP_ATTR_K2, _("K2"), -10, 10, 1, 0.01, 1);
1023 _k3 = _settings->add(SP_ATTR_K3, _("K3"), -10, 10, 1, 0.01, 1);
1024 _k4 = _settings->add(SP_ATTR_K4, _("K4"), -10, 10, 1, 0.01, 1);
1026 _settings->type(NR_FILTER_CONVOLVEMATRIX);
1027 DualSpinSlider* order = _settings->add(SP_ATTR_ORDER, _("Rows"), _("Columns"), 1, 5, 1, 1, 0);
1028 ConvolveMatrix* convmat = _settings->add(SP_ATTR_KERNELMATRIX, _("Kernel"));
1029 order->signal_value_changed().connect(
1030 sigc::bind(sigc::mem_fun(*convmat, &ConvolveMatrix::update_direct), this));
1031 order->set_update_policy(Gtk::UPDATE_DISCONTINUOUS);
1032 _settings->add(SP_ATTR_DIVISOR, _("Divisor"), 0.01, 10, 1, 0.01, 1);
1033 _settings->add(SP_ATTR_BIAS, _("Bias"), -10, 10, 1, 0.01, 1);
1035 _settings->type(NR_FILTER_GAUSSIANBLUR);
1036 _settings->add(SP_ATTR_STDDEVIATION, _("Standard Deviation X"), _("Standard Deviation Y"), 0, 100, 1, 0.01, 1);
1038 _settings->type(NR_FILTER_OFFSET);
1039 _settings->add(SP_ATTR_DX, _("Delta X"), -100, 100, 1, 0.01, 1);
1040 _settings->add(SP_ATTR_DY, _("Delta Y"), -100, 100, 1, 0.01, 1);
1042 _settings->type(NR_FILTER_SPECULARLIGHTING);
1043 //_settings->add(_specular_color, SP_PROP_LIGHTING_COLOR, _("Specular Color"));
1044 _settings->add(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1045 _settings->add(SP_ATTR_SPECULARCONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1046 _settings->add(SP_ATTR_SPECULAREXPONENT, _("Exponent"), 1, 128, 1, 0.01, 1);
1048 _settings->type(NR_FILTER_TURBULENCE);
1049 /*std::vector<Gtk::Widget*> trb_grp;
1050 trb_grp.push_back(&_turbulence_fractalnoise);
1051 trb_grp.push_back(&_turbulence_turbulence);
1052 _settings->add(trb_grp);
1053 _turbulence.add_setting(_turbulence_numoctaves, _("Octaves"));
1054 _turbulence.add_setting(_turbulence_basefrequency, _("Base Frequency"));
1055 _turbulence.add_setting(_turbulence_seed, _("Seed"));
1056 _turbulence.add_setting(_turbulence_stitchtiles);*/
1057 }
1059 void FilterEffectsDialog::add_primitive()
1060 {
1061 SPFilter* filter = _filter_modifier.get_selected_filter();
1063 if(filter) {
1064 SPFilterPrimitive* prim = filter_add_primitive(filter, _add_primitive_type.get_active_data()->id);
1066 _primitive_list.update();
1067 _primitive_list.select(prim);
1069 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter primitive"));
1070 }
1071 }
1073 void FilterEffectsDialog::remove_primitive()
1074 {
1075 SPFilterPrimitive* prim = _primitive_list.get_selected();
1077 if(prim) {
1078 sp_repr_unparent(prim->repr);
1080 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_FILTER_EFFECTS,
1081 _("Remove filter primitive"));
1083 _primitive_list.update();
1084 }
1085 }
1087 void FilterEffectsDialog::duplicate_primitive()
1088 {
1089 SPFilter* filter = _filter_modifier.get_selected_filter();
1090 SPFilterPrimitive* origprim = _primitive_list.get_selected();
1092 if(filter && origprim) {
1093 Inkscape::XML::Node *repr;
1094 repr = SP_OBJECT_REPR(origprim)->duplicate(SP_OBJECT_REPR(origprim)->document());
1095 SP_OBJECT_REPR(filter)->appendChild(repr);
1097 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter primitive"));
1099 _primitive_list.update();
1100 }
1101 }
1103 void FilterEffectsDialog::set_attr_color(const SPAttributeEnum attr, const Gtk::ColorButton* input)
1104 {
1105 if(input->is_sensitive()) {
1106 std::ostringstream os;
1107 const Gdk::Color c = input->get_color();
1108 const int r = 255 * c.get_red() / 65535, g = 255 * c.get_green() / 65535, b = 255 * c.get_blue() / 65535;
1109 os << "rgb(" << r << "," << g << "," << b << ")";
1110 set_attr(attr, os.str().c_str());
1111 }
1112 }
1114 void FilterEffectsDialog::set_attr_direct(const SPAttributeEnum attr, const AttrWidget* input)
1115 {
1116 set_attr(attr, input->get_as_attribute().c_str());
1117 }
1119 void FilterEffectsDialog::set_attr(const SPAttributeEnum attr, const gchar* val)
1120 {
1121 SPFilter *filter = _filter_modifier.get_selected_filter();
1122 SPFilterPrimitive* prim = _primitive_list.get_selected();
1124 if(filter && prim) {
1125 update_settings_sensitivity();
1127 SP_OBJECT_REPR(prim)->setAttribute((gchar*)sp_attribute_name(attr), val);
1128 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1130 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Set filter primitive attribute"));
1131 }
1132 }
1134 void FilterEffectsDialog::update_settings_view()
1135 {
1136 SPFilterPrimitive* prim = _primitive_list.get_selected();
1138 // Hide all the settings
1139 _settings_box.hide_all();
1140 _settings_box.show();
1142 _settings_box.set_sensitive(false);
1143 _empty_settings.show();
1145 if(prim) {
1146 const FilterPrimitiveType tid = FPConverter.get_id_from_key(prim->repr->name());
1148 _settings->show_and_update(tid);
1150 _settings_box.set_sensitive(true);
1151 _empty_settings.hide();
1152 }
1154 update_settings_sensitivity();
1155 }
1157 void FilterEffectsDialog::update_settings_sensitivity()
1158 {
1159 SPFilterPrimitive* prim = _primitive_list.get_selected();
1160 const bool use_k = SP_IS_FECOMPOSITE(prim) && SP_FECOMPOSITE(prim)->composite_operator == COMPOSITE_ARITHMETIC;
1161 _k1->set_sensitive(use_k);
1162 _k2->set_sensitive(use_k);
1163 _k3->set_sensitive(use_k);
1164 _k4->set_sensitive(use_k);
1165 }
1167 } // namespace Dialog
1168 } // namespace UI
1169 } // namespace Inkscape
1171 /*
1172 Local Variables:
1173 mode:c++
1174 c-file-style:"stroustrup"
1175 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1176 indent-tabs-mode:nil
1177 fill-column:99
1178 End:
1179 */
1180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :