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));
89 _opacity_label.set_mnemonic_widget(_opacity_hscale);
91 show_all_children();
93 _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_activate), this );
94 _desktop_activated = g_signal_connect ( G_OBJECT (INKSCAPE), "deactivate_desktop", G_CALLBACK (&ObjectCompositeSettings::_on_desktop_deactivate), this );
95 }
97 ObjectCompositeSettings::~ObjectCompositeSettings() {
98 setSubject(NULL);
99 g_signal_handler_disconnect(G_OBJECT(INKSCAPE), _desktop_activated);
100 }
102 void ObjectCompositeSettings::setSubject(StyleSubject *subject) {
103 _subject_changed.disconnect();
104 if (subject) {
105 _subject = subject;
106 _subject_changed = _subject->connectChanged(sigc::mem_fun(*this, &ObjectCompositeSettings::_subjectChanged));
107 _subject->setDesktop(SP_ACTIVE_DESKTOP);
108 }
109 }
111 void
112 ObjectCompositeSettings::_blendBlurValueChanged()
113 {
114 if (!_subject) {
115 return;
116 }
118 SPDesktop *desktop = _subject->getDesktop();
119 if (!desktop) {
120 return;
121 }
122 SPDocument *document = sp_desktop_document (desktop);
124 if (_blocked)
125 return;
126 _blocked = true;
128 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
129 //sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
131 Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
132 double radius;
133 if (bbox) {
134 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y]; // fixme: this is only half the perimeter, is that correct?
135 radius = _fe_cb.get_blur_value() * perimeter / 400;
136 } else {
137 radius = 0;
138 }
140 const Glib::ustring blendmode = _fe_cb.get_blend_mode();
142 //apply created filter to every selected item
143 for (StyleSubject::iterator i = _subject->begin() ; i != _subject->end() ; ++i ) {
144 if (!SP_IS_ITEM(*i)) {
145 continue;
146 }
148 SPItem * item = SP_ITEM(*i);
149 SPStyle *style = SP_OBJECT_STYLE(item);
150 g_assert(style != NULL);
152 if (blendmode != "normal") {
153 SPFilter *filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
154 sp_style_set_property_url(item, "filter", filter, false);
155 } else {
156 sp_style_set_property_url(item, "filter", NULL, false);
157 }
159 if (radius == 0 && item->style->filter.set
160 && filter_is_single_gaussian_blur(SP_FILTER(item->style->getFilter()))) {
161 remove_filter(item, false);
162 }
163 else if (radius != 0) {
164 SPFilter *filter = modify_filter_gaussian_blur_from_item(document, item, radius);
165 sp_style_set_property_url(item, "filter", filter, false);
166 }
168 //request update
169 item->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
170 SP_OBJECT_STYLE_MODIFIED_FLAG ));
171 }
173 DocumentUndo::maybeDone(document, _blur_tag.c_str(), _verb_code,
174 _("Change blur"));
176 // resume interruptibility
177 //sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
179 _blocked = false;
180 }
182 void
183 ObjectCompositeSettings::_opacityValueChanged()
184 {
185 if (!_subject) {
186 return;
187 }
189 SPDesktop *desktop = _subject->getDesktop();
190 if (!desktop) {
191 return;
192 }
194 if (_blocked)
195 return;
196 _blocked = true;
198 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
199 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
200 // (though this only fixes the crash, not the multiple change events)
201 //sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
203 SPCSSAttr *css = sp_repr_css_attr_new ();
205 Inkscape::CSSOStringStream os;
206 os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
207 sp_repr_css_set_property (css, "opacity", os.str().c_str());
209 _subject->setCSS(css);
211 sp_repr_css_attr_unref (css);
213 DocumentUndo::maybeDone(sp_desktop_document (desktop), _opacity_tag.c_str(), _verb_code,
214 _("Change opacity"));
216 // resume interruptibility
217 //sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
219 _blocked = false;
220 }
222 void
223 ObjectCompositeSettings::_subjectChanged() {
224 if (!_subject) {
225 return;
226 }
228 SPDesktop *desktop = _subject->getDesktop();
229 if (!desktop) {
230 return;
231 }
233 if (_blocked)
234 return;
235 _blocked = true;
237 SPStyle *query = sp_style_new (sp_desktop_document(desktop));
238 int result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
240 switch (result) {
241 case QUERY_STYLE_NOTHING:
242 _opacity_hbox.set_sensitive(false);
243 // gtk_widget_set_sensitive (opa, FALSE);
244 break;
245 case QUERY_STYLE_SINGLE:
246 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
247 case QUERY_STYLE_MULTIPLE_SAME:
248 _opacity_hbox.set_sensitive(true);
249 _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
250 break;
251 }
253 //query now for current filter mode and average blurring of selection
254 const int blend_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLEND);
255 switch(blend_result) {
256 case QUERY_STYLE_NOTHING:
257 _fe_cb.set_sensitive(false);
258 break;
259 case QUERY_STYLE_SINGLE:
260 case QUERY_STYLE_MULTIPLE_SAME:
261 _fe_cb.set_blend_mode(query->filter_blend_mode.value);
262 _fe_cb.set_sensitive(true);
263 break;
264 case QUERY_STYLE_MULTIPLE_DIFFERENT:
265 // TODO: set text
266 _fe_cb.set_sensitive(false);
267 break;
268 }
270 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
271 int blur_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLUR);
272 switch (blur_result) {
273 case QUERY_STYLE_NOTHING: //no blurring
274 _fe_cb.set_blur_sensitive(false);
275 break;
276 case QUERY_STYLE_SINGLE:
277 case QUERY_STYLE_MULTIPLE_AVERAGED:
278 case QUERY_STYLE_MULTIPLE_SAME:
279 Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
280 if (bbox) {
281 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y]; // fixme: this is only half the perimeter, is that correct?
282 _fe_cb.set_blur_sensitive(true);
283 //update blur widget value
284 float radius = query->filter_gaussianBlur_deviation.value;
285 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
286 _fe_cb.set_blur_value(percent);
287 }
288 break;
289 }
290 }
292 sp_style_unref(query);
294 _blocked = false;
295 }
297 }
298 }
299 }
301 /*
302 Local Variables:
303 mode:c++
304 c-file-style:"stroustrup"
305 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
306 indent-tabs-mode:nil
307 fill-column:99
308 End:
309 */
310 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :