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 * Niko Kiirala <niko@kiirala.com>
8 *
9 * Copyright (C) 2004--2008 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include <glibmm/i18n.h>
16 #include "desktop-handles.h"
17 #include "desktop-style.h"
18 #include "document.h"
19 #include "filter-chemistry.h"
20 #include "inkscape.h"
21 #include "inkscape-stock.h"
22 #include "selection.h"
23 #include "style.h"
24 #include "sp-item.h"
25 #include "svg/css-ostringstream.h"
26 #include "verbs.h"
27 #include "xml/repr.h"
28 #include "widgets/icon.h"
29 #include "ui/widget/object-composite-settings.h"
30 #include "display/sp-canvas.h"
32 namespace Inkscape {
33 namespace UI {
34 namespace Widget {
36 void ObjectCompositeSettings::_on_desktop_switch(
37 Inkscape::Application */*application*/,
38 SPDesktop *desktop,
39 ObjectCompositeSettings *w
40 ) {
41 if (w->_subject) {
42 w->_subject->setDesktop(desktop);
43 }
44 }
46 ObjectCompositeSettings::ObjectCompositeSettings(unsigned int verb_code, char const *history_prefix, int flags)
47 : _verb_code(verb_code),
48 _blur_tag(Glib::ustring(history_prefix) + ":blur"),
49 _opacity_tag(Glib::ustring(history_prefix) + ":opacity"),
50 _opacity_vbox(false, 0),
51 _opacity_label_box(false, 0),
52 _opacity_label(_("Opacity, %"), 0.0, 1.0, true),
53 _opacity_adjustment(100.0, 0.0, 100.0, 1.0, 1.0, 0.0),
54 _opacity_hscale(_opacity_adjustment),
55 _opacity_spin_button(_opacity_adjustment, 0.01, 1),
56 _fe_cb(flags),
57 _fe_vbox(false, 0),
58 _fe_alignment(1, 1, 1, 1),
59 _blocked(false)
60 {
61 // Filter Effects
62 pack_start(_fe_vbox, false, false, 2);
63 _fe_alignment.set_padding(0, 0, 4, 0);
64 _fe_alignment.add(_fe_cb);
65 _fe_vbox.pack_start(_fe_alignment, false, false, 0);
66 _fe_cb.signal_blend_blur_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_blendBlurValueChanged));
68 // Opacity
69 pack_start(_opacity_vbox, false, false, 2);
70 _opacity_label_box.pack_start(_opacity_label, false, false, 4);
71 _opacity_vbox.pack_start(_opacity_label_box, false, false, 0);
72 _opacity_vbox.pack_start(_opacity_hbox, false, false, 0);
73 _opacity_hbox.pack_start(_opacity_hscale, true, true, 4);
74 _opacity_hbox.pack_start(_opacity_spin_button, false, false, 0);
75 _opacity_hscale.set_draw_value(false);
76 _opacity_adjustment.signal_value_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_opacityValueChanged));
78 show_all_children();
80 _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_switch), this );
81 }
83 ObjectCompositeSettings::~ObjectCompositeSettings() {
84 setSubject(NULL);
85 g_signal_handler_disconnect(G_OBJECT(INKSCAPE), _desktop_activated);
86 }
88 void ObjectCompositeSettings::setSubject(StyleSubject *subject) {
89 _subject_changed.disconnect();
90 if (subject) {
91 _subject = subject;
92 _subject_changed = _subject->connectChanged(sigc::mem_fun(*this, &ObjectCompositeSettings::_subjectChanged));
93 _subject->setDesktop(SP_ACTIVE_DESKTOP);
94 }
95 }
97 void
98 ObjectCompositeSettings::_blendBlurValueChanged()
99 {
100 if (!_subject) {
101 return;
102 }
104 SPDesktop *desktop = _subject->getDesktop();
105 if (!desktop) {
106 return;
107 }
108 SPDocument *document = sp_desktop_document (desktop);
110 if (_blocked)
111 return;
112 _blocked = true;
114 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
115 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
117 boost::optional<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
118 double radius;
119 if (bbox) {
120 double perimeter = bbox->extent(Geom::X) + bbox->extent(Geom::Y);
121 radius = _fe_cb.get_blur_value() * perimeter / 400;
122 } else {
123 radius = 0;
124 }
126 const Glib::ustring blendmode = _fe_cb.get_blend_mode();
128 //apply created filter to every selected item
129 for (StyleSubject::iterator i = _subject->begin() ; i != _subject->end() ; ++i ) {
130 if (!SP_IS_ITEM(*i)) {
131 continue;
132 }
134 SPItem * item = SP_ITEM(*i);
135 SPStyle *style = SP_OBJECT_STYLE(item);
136 g_assert(style != NULL);
138 if (blendmode != "normal") {
139 SPFilter *filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
140 sp_style_set_property_url(item, "filter", filter, false);
141 }
143 if (radius == 0 && item->style->filter.set
144 && filter_is_single_gaussian_blur(SP_FILTER(item->style->getFilter()))) {
145 remove_filter(item, false);
146 }
147 else if (radius != 0) {
148 SPFilter *filter = modify_filter_gaussian_blur_from_item(document, item, radius);
149 sp_style_set_property_url(item, "filter", filter, false);
150 }
152 //request update
153 item->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
154 SP_OBJECT_STYLE_MODIFIED_FLAG ));
155 }
157 sp_document_maybe_done (document, _blur_tag.c_str(), _verb_code,
158 _("Change blur"));
160 // resume interruptibility
161 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
163 _blocked = false;
164 }
166 void
167 ObjectCompositeSettings::_opacityValueChanged()
168 {
169 if (!_subject) {
170 return;
171 }
173 SPDesktop *desktop = _subject->getDesktop();
174 if (!desktop) {
175 return;
176 }
178 if (_blocked)
179 return;
180 _blocked = true;
182 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
183 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
184 // (though this only fixes the crash, not the multiple change events)
185 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
187 SPCSSAttr *css = sp_repr_css_attr_new ();
189 Inkscape::CSSOStringStream os;
190 os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
191 sp_repr_css_set_property (css, "opacity", os.str().c_str());
193 _subject->setCSS(css);
195 sp_repr_css_attr_unref (css);
197 sp_document_maybe_done (sp_desktop_document (desktop), _opacity_tag.c_str(), _verb_code,
198 _("Change opacity"));
200 // resume interruptibility
201 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
203 _blocked = false;
204 }
206 void
207 ObjectCompositeSettings::_subjectChanged() {
208 if (!_subject) {
209 return;
210 }
212 SPDesktop *desktop = _subject->getDesktop();
213 if (!desktop) {
214 return;
215 }
217 if (_blocked)
218 return;
219 _blocked = true;
221 SPStyle *query = sp_style_new (sp_desktop_document(desktop));
222 int result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
224 switch (result) {
225 case QUERY_STYLE_NOTHING:
226 _opacity_hbox.set_sensitive(false);
227 // gtk_widget_set_sensitive (opa, FALSE);
228 break;
229 case QUERY_STYLE_SINGLE:
230 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
231 case QUERY_STYLE_MULTIPLE_SAME:
232 _opacity_hbox.set_sensitive(true);
233 _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
234 break;
235 }
237 //query now for current filter mode and average blurring of selection
238 const int blend_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLEND);
239 switch(blend_result) {
240 case QUERY_STYLE_NOTHING:
241 _fe_cb.set_sensitive(false);
242 break;
243 case QUERY_STYLE_SINGLE:
244 case QUERY_STYLE_MULTIPLE_SAME:
245 _fe_cb.set_blend_mode(query->filter_blend_mode.value);
246 _fe_cb.set_sensitive(true);
247 break;
248 case QUERY_STYLE_MULTIPLE_DIFFERENT:
249 // TODO: set text
250 _fe_cb.set_sensitive(false);
251 break;
252 }
254 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
255 int blur_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLUR);
256 switch (blur_result) {
257 case QUERY_STYLE_NOTHING: //no blurring
258 _fe_cb.set_blur_sensitive(false);
259 break;
260 case QUERY_STYLE_SINGLE:
261 case QUERY_STYLE_MULTIPLE_AVERAGED:
262 case QUERY_STYLE_MULTIPLE_SAME:
263 boost::optional<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
264 if (bbox) {
265 double perimeter = bbox->extent(Geom::X) + bbox->extent(Geom::Y);
266 _fe_cb.set_blur_sensitive(true);
267 //update blur widget value
268 float radius = query->filter_gaussianBlur_deviation.value;
269 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
270 _fe_cb.set_blur_value(percent);
271 }
272 break;
273 }
274 }
276 sp_style_unref(query);
278 _blocked = false;
279 }
281 }
282 }
283 }
285 /*
286 Local Variables:
287 mode:c++
288 c-file-style:"stroustrup"
289 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
290 indent-tabs-mode:nil
291 fill-column:99
292 End:
293 */
294 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :