Code

1e277fa0dcc6cfdfbde4395aa1453bcb2d8f23c6
[inkscape.git] / src / ui / widget / object-composite-settings.cpp
1 /*
2  * A widget for controlling object compositing (filter, opacity, etc.)
3  *
4  * Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   Gustav Broberg <broberg@kth.se>
7  *
8  * Copyright (C) 2004--2007 Authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include <glibmm/i18n.h>
15 #include "desktop-handles.h"
16 #include "desktop-style.h"
17 #include "document.h"
18 #include "filter-chemistry.h"
19 #include "inkscape.h"
20 #include "inkscape-stock.h"
21 #include "selection.h"
22 #include "style.h"
23 #include "sp-item.h"
24 #include "svg/css-ostringstream.h"
25 #include "verbs.h"
26 #include "xml/repr.h"
27 #include "widgets/icon.h"
28 #include "ui/widget/object-composite-settings.h"
29 #include "display/sp-canvas.h"
31 namespace Inkscape {
32 namespace UI {
33 namespace Widget {
35 void ObjectCompositeSettings::_on_desktop_switch(
36     Inkscape::Application */*application*/,
37     SPDesktop *desktop,
38     ObjectCompositeSettings *w
39 ) {
40     if (w->_subject) {
41         w->_subject->setDesktop(desktop);
42     }
43 }
45 ObjectCompositeSettings::ObjectCompositeSettings(unsigned int verb_code, char const *history_prefix, int flags)
46 : _verb_code(verb_code),
47   _blur_tag(Glib::ustring(history_prefix) + ":blur"),
48   _opacity_tag(Glib::ustring(history_prefix) + ":opacity"),
49   _opacity_vbox(false, 0),
50   _opacity_label_box(false, 0),
51   _opacity_label(_("Opacity, %"), 0.0, 1.0, true),
52   _opacity_adjustment(100.0, 0.0, 100.0, 1.0, 1.0, 0.0),
53   _opacity_hscale(_opacity_adjustment),
54   _opacity_spin_button(_opacity_adjustment, 0.01, 1),
55   _fe_cb(flags),
56   _fe_vbox(false, 0),
57   _fe_alignment(1, 1, 1, 1),
58   _blocked(false)
59 {
60     // Filter Effects
61     pack_start(_fe_vbox, false, false, 2);
62     _fe_alignment.set_padding(0, 0, 4, 0);
63     _fe_alignment.add(_fe_cb);
64     _fe_vbox.pack_start(_fe_alignment, false, false, 0);
65     _fe_cb.signal_blend_blur_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_blendBlurValueChanged));
67     // Opacity
68     pack_start(_opacity_vbox, false, false, 2);
69     _opacity_label_box.pack_start(_opacity_label, false, false, 4);
70     _opacity_vbox.pack_start(_opacity_label_box, false, false, 0);
71     _opacity_vbox.pack_start(_opacity_hbox, false, false, 0);
72     _opacity_hbox.pack_start(_opacity_hscale, true, true, 4);
73     _opacity_hbox.pack_start(_opacity_spin_button, false, false, 0);
74     _opacity_hscale.set_draw_value(false);
75     _opacity_adjustment.signal_value_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_opacityValueChanged));
77     show_all_children();
79     _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_switch), this );
80 }
82 ObjectCompositeSettings::~ObjectCompositeSettings() {
83     setSubject(NULL);
84     g_signal_handler_disconnect(G_OBJECT(INKSCAPE), _desktop_activated);
85 }
87 void ObjectCompositeSettings::setSubject(StyleSubject *subject) {
88     _subject_changed.disconnect();
89     if (subject) {
90         _subject = subject;
91         _subject_changed = _subject->connectChanged(sigc::mem_fun(*this, &ObjectCompositeSettings::_subjectChanged));
92         _subject->setDesktop(SP_ACTIVE_DESKTOP);
93     }
94 }
96 void
97 ObjectCompositeSettings::_blendBlurValueChanged()
98 {
99     if (!_subject) {
100         return;
101     }
103     SPDesktop *desktop = _subject->getDesktop();
104     if (!desktop) {
105         return;
106     }
108     if (_blocked)
109         return;
110     _blocked = true;
112     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
113     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
115     NR::Maybe<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
116     double radius;
117     if (bbox) {
118         double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
119         radius = _fe_cb.get_blur_value() * perimeter / 400;
120     } else {
121         radius = 0;
122     }
124     const Glib::ustring blendmode = _fe_cb.get_blend_mode();
126     SPFilter *filter = 0;
127     const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
129     if(blendmode != "filter" || filter) {
130         SPDocument *document = sp_desktop_document (desktop);
132         //apply created filter to every selected item
133         for (StyleSubject::iterator i = _subject->begin() ; i != _subject->end() ; ++i ) {
134             if (!SP_IS_ITEM(*i)) {
135                 continue;
136             }
138             SPItem * item = SP_ITEM(*i);
139             SPStyle *style = SP_OBJECT_STYLE(item);
140             g_assert(style != NULL);
142             if(remfilter) {
143                 remove_filter (item, false);
144             }
145             else {
146                 if(blendmode != "filter")
147                     filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
148                 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
149             }
151             //request update
152             SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
153                                                     SP_OBJECT_STYLE_MODIFIED_FLAG ));
154         }
155     }
157     sp_document_maybe_done (sp_desktop_document (desktop), _blur_tag.c_str(), _verb_code, _("Change blur"));
159     // resume interruptibility
160     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
162     _blocked = false;
165 void
166 ObjectCompositeSettings::_opacityValueChanged()
168     if (!_subject) {
169         return;
170     }
172     SPDesktop *desktop = _subject->getDesktop();
173     if (!desktop) {
174         return;
175     }
177     if (_blocked)
178         return;
179     _blocked = true;
181     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
182     // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
183     // (though this only fixes the crash, not the multiple change events)
184     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
186     SPCSSAttr *css = sp_repr_css_attr_new ();
188     Inkscape::CSSOStringStream os;
189     os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
190     sp_repr_css_set_property (css, "opacity", os.str().c_str());
192     _subject->setCSS(css);
194     sp_repr_css_attr_unref (css);
196     sp_document_maybe_done (sp_desktop_document (desktop), _opacity_tag.c_str(), _verb_code,
197                             _("Change opacity"));
199     // resume interruptibility
200     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
202     _blocked = false;
205 void
206 ObjectCompositeSettings::_subjectChanged() {
207     if (!_subject) {
208         return;
209     }
211     SPDesktop *desktop = _subject->getDesktop();
212     if (!desktop) {
213         return;
214     }
216     if (_blocked)
217         return;
218     _blocked = true;
220     SPStyle *query = sp_style_new (sp_desktop_document(desktop));
221     int result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
223     switch (result) {
224         case QUERY_STYLE_NOTHING:
225             _opacity_hbox.set_sensitive(false);
226             // gtk_widget_set_sensitive (opa, FALSE);
227             break;
228         case QUERY_STYLE_SINGLE:
229         case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
230         case QUERY_STYLE_MULTIPLE_SAME:
231             _opacity_hbox.set_sensitive(true);
232             _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
233             break;
234     }
236     //query now for current filter mode and average blurring of selection
237     const int blend_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLEND);
238     switch(blend_result) {
239         case QUERY_STYLE_NOTHING:
240             _fe_cb.set_sensitive(false);
241             break;
242         case QUERY_STYLE_SINGLE:
243         case QUERY_STYLE_MULTIPLE_SAME:
244             _fe_cb.set_blend_mode(query->filter_blend_mode.value);
245             _fe_cb.set_sensitive(true);
246             break;
247         case QUERY_STYLE_MULTIPLE_DIFFERENT:
248             // TODO: set text
249             _fe_cb.set_sensitive(false);
250             break;
251     }
253     if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
254         int blur_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLUR);
255         switch (blur_result) {
256             case QUERY_STYLE_NOTHING: //no blurring
257                 _fe_cb.set_blur_sensitive(false);
258                 break;
259             case QUERY_STYLE_SINGLE:
260             case QUERY_STYLE_MULTIPLE_AVERAGED:
261             case QUERY_STYLE_MULTIPLE_SAME:
262                 NR::Maybe<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
263                 if (bbox) {
264                     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
265                     _fe_cb.set_blur_sensitive(true);
266                     //update blur widget value
267                     float radius = query->filter_gaussianBlur_deviation.value;
268                     float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
269                     _fe_cb.set_blur_value(percent);
270                 }
271                 break;
272         }
273     }
275     sp_style_unref(query);
277     _blocked = false;
284 /*
285   Local Variables:
286   mode:c++
287   c-file-style:"stroustrup"
288   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
289   indent-tabs-mode:nil
290   fill-column:99
291   End:
292 */
293 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :