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 * Abhishek Sharma
9 *
10 * Copyright (C) 2004--2008 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <glibmm/i18n.h>
17 #include "desktop-handles.h"
18 #include "desktop-style.h"
19 #include "document.h"
20 #include "filter-chemistry.h"
21 #include "inkscape.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/icon-names.h"
30 #include "ui/widget/object-composite-settings.h"
31 #include "display/sp-canvas.h"
33 namespace Inkscape {
34 namespace UI {
35 namespace Widget {
37 void ObjectCompositeSettings::_on_desktop_activate(
38 Inkscape::Application */*application*/,
39 SPDesktop *desktop,
40 ObjectCompositeSettings *w
41 ) {
42 if (w->_subject) {
43 w->_subject->setDesktop(desktop);
44 }
45 }
47 void ObjectCompositeSettings::_on_desktop_deactivate(
48 Inkscape::Application */*application*/,
49 SPDesktop */*desktop*/,
50 ObjectCompositeSettings *w
51 ) {
52 if (w->_subject) {
53 w->_subject->setDesktop(NULL);
54 }
55 }
57 ObjectCompositeSettings::ObjectCompositeSettings(unsigned int verb_code, char const *history_prefix, int flags)
58 : _verb_code(verb_code),
59 _blur_tag(Glib::ustring(history_prefix) + ":blur"),
60 _opacity_tag(Glib::ustring(history_prefix) + ":opacity"),
61 _opacity_vbox(false, 0),
62 _opacity_label_box(false, 0),
63 _opacity_label(_("Opacity (%):"), 0.0, 1.0, true),
64 _opacity_adjustment(100.0, 0.0, 100.0, 1.0, 1.0, 0.0),
65 _opacity_hscale(_opacity_adjustment),
66 _opacity_spin_button(_opacity_adjustment, 0.01, 1),
67 _fe_cb(flags),
68 _fe_vbox(false, 0),
69 _fe_alignment(1, 1, 1, 1),
70 _blocked(false)
71 {
72 // Filter Effects
73 pack_start(_fe_vbox, false, false, 2);
74 _fe_alignment.set_padding(0, 0, 4, 0);
75 _fe_alignment.add(_fe_cb);
76 _fe_vbox.pack_start(_fe_alignment, false, false, 0);
77 _fe_cb.signal_blend_blur_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_blendBlurValueChanged));
79 // Opacity
80 pack_start(_opacity_vbox, false, false, 2);
81 _opacity_label_box.pack_start(_opacity_label, false, false, 4);
82 _opacity_vbox.pack_start(_opacity_label_box, false, false, 0);
83 _opacity_vbox.pack_start(_opacity_hbox, false, false, 0);
84 _opacity_hbox.pack_start(_opacity_hscale, true, true, 4);
85 _opacity_hbox.pack_start(_opacity_spin_button, false, false, 0);
86 _opacity_hscale.set_draw_value(false);
87 _opacity_hscale.set_update_policy(Gtk::UPDATE_DELAYED);
88 _opacity_adjustment.signal_value_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_opacityValueChanged));
90 show_all_children();
92 _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_activate), this );
93 _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "deactivate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_deactivate), this );
94 }
96 ObjectCompositeSettings::~ObjectCompositeSettings() {
97 setSubject(NULL);
98 g_signal_handler_disconnect(G_OBJECT(INKSCAPE), _desktop_activated);
99 }
101 void ObjectCompositeSettings::setSubject(StyleSubject *subject) {
102 _subject_changed.disconnect();
103 if (subject) {
104 _subject = subject;
105 _subject_changed = _subject->connectChanged(sigc::mem_fun(*this, &ObjectCompositeSettings::_subjectChanged));
106 _subject->setDesktop(SP_ACTIVE_DESKTOP);
107 }
108 }
110 void
111 ObjectCompositeSettings::_blendBlurValueChanged()
112 {
113 if (!_subject) {
114 return;
115 }
117 SPDesktop *desktop = _subject->getDesktop();
118 if (!desktop) {
119 return;
120 }
121 SPDocument *document = sp_desktop_document (desktop);
123 if (_blocked)
124 return;
125 _blocked = true;
127 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
128 //sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
130 Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
131 double radius;
132 if (bbox) {
133 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y]; // fixme: this is only half the perimeter, is that correct?
134 radius = _fe_cb.get_blur_value() * perimeter / 400;
135 } else {
136 radius = 0;
137 }
139 const Glib::ustring blendmode = _fe_cb.get_blend_mode();
141 //apply created filter to every selected item
142 for (StyleSubject::iterator i = _subject->begin() ; i != _subject->end() ; ++i ) {
143 if (!SP_IS_ITEM(*i)) {
144 continue;
145 }
147 SPItem * item = SP_ITEM(*i);
148 SPStyle *style = SP_OBJECT_STYLE(item);
149 g_assert(style != NULL);
151 if (blendmode != "normal") {
152 SPFilter *filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
153 sp_style_set_property_url(item, "filter", filter, false);
154 } else {
155 sp_style_set_property_url(item, "filter", NULL, false);
156 }
158 if (radius == 0 && item->style->filter.set
159 && filter_is_single_gaussian_blur(SP_FILTER(item->style->getFilter()))) {
160 remove_filter(item, false);
161 }
162 else if (radius != 0) {
163 SPFilter *filter = modify_filter_gaussian_blur_from_item(document, item, radius);
164 sp_style_set_property_url(item, "filter", filter, false);
165 }
167 //request update
168 item->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
169 SP_OBJECT_STYLE_MODIFIED_FLAG ));
170 }
172 DocumentUndo::maybeDone(document, _blur_tag.c_str(), _verb_code,
173 _("Change blur"));
175 // resume interruptibility
176 //sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
178 _blocked = false;
179 }
181 void
182 ObjectCompositeSettings::_opacityValueChanged()
183 {
184 if (!_subject) {
185 return;
186 }
188 SPDesktop *desktop = _subject->getDesktop();
189 if (!desktop) {
190 return;
191 }
193 if (_blocked)
194 return;
195 _blocked = true;
197 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
198 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
199 // (though this only fixes the crash, not the multiple change events)
200 //sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
202 SPCSSAttr *css = sp_repr_css_attr_new ();
204 Inkscape::CSSOStringStream os;
205 os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
206 sp_repr_css_set_property (css, "opacity", os.str().c_str());
208 _subject->setCSS(css);
210 sp_repr_css_attr_unref (css);
212 DocumentUndo::maybeDone(sp_desktop_document (desktop), _opacity_tag.c_str(), _verb_code,
213 _("Change opacity"));
215 // resume interruptibility
216 //sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
218 _blocked = false;
219 }
221 void
222 ObjectCompositeSettings::_subjectChanged() {
223 if (!_subject) {
224 return;
225 }
227 SPDesktop *desktop = _subject->getDesktop();
228 if (!desktop) {
229 return;
230 }
232 if (_blocked)
233 return;
234 _blocked = true;
236 SPStyle *query = sp_style_new (sp_desktop_document(desktop));
237 int result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
239 switch (result) {
240 case QUERY_STYLE_NOTHING:
241 _opacity_hbox.set_sensitive(false);
242 // gtk_widget_set_sensitive (opa, FALSE);
243 break;
244 case QUERY_STYLE_SINGLE:
245 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
246 case QUERY_STYLE_MULTIPLE_SAME:
247 _opacity_hbox.set_sensitive(true);
248 _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
249 break;
250 }
252 //query now for current filter mode and average blurring of selection
253 const int blend_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLEND);
254 switch(blend_result) {
255 case QUERY_STYLE_NOTHING:
256 _fe_cb.set_sensitive(false);
257 break;
258 case QUERY_STYLE_SINGLE:
259 case QUERY_STYLE_MULTIPLE_SAME:
260 _fe_cb.set_blend_mode(query->filter_blend_mode.value);
261 _fe_cb.set_sensitive(true);
262 break;
263 case QUERY_STYLE_MULTIPLE_DIFFERENT:
264 // TODO: set text
265 _fe_cb.set_sensitive(false);
266 break;
267 }
269 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
270 int blur_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLUR);
271 switch (blur_result) {
272 case QUERY_STYLE_NOTHING: //no blurring
273 _fe_cb.set_blur_sensitive(false);
274 break;
275 case QUERY_STYLE_SINGLE:
276 case QUERY_STYLE_MULTIPLE_AVERAGED:
277 case QUERY_STYLE_MULTIPLE_SAME:
278 Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
279 if (bbox) {
280 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y]; // fixme: this is only half the perimeter, is that correct?
281 _fe_cb.set_blur_sensitive(true);
282 //update blur widget value
283 float radius = query->filter_gaussianBlur_deviation.value;
284 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
285 _fe_cb.set_blur_value(percent);
286 }
287 break;
288 }
289 }
291 sp_style_unref(query);
293 _blocked = false;
294 }
296 }
297 }
298 }
300 /*
301 Local Variables:
302 mode:c++
303 c-file-style:"stroustrup"
304 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
305 indent-tabs-mode:nil
306 fill-column:99
307 End:
308 */
309 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :