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 }
107 SPDocument *document = sp_desktop_document (desktop);
109 if (_blocked)
110 return;
111 _blocked = true;
113 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
114 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
116 NR::Maybe<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
117 double radius;
118 if (bbox) {
119 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
120 radius = _fe_cb.get_blur_value() * perimeter / 400;
121 } else {
122 radius = 0;
123 }
125 //const Glib::ustring blendmode = _fe_cb.get_blend_mode();
127 //apply created filter to every selected item
128 for (StyleSubject::iterator i = _subject->begin() ; i != _subject->end() ; ++i ) {
129 if (!SP_IS_ITEM(*i)) {
130 continue;
131 }
133 SPItem * item = SP_ITEM(*i);
134 SPStyle *style = SP_OBJECT_STYLE(item);
135 g_assert(style != NULL);
137 if (radius == 0 && item->style->filter.set
138 && filter_is_single_gaussian_blur(SP_FILTER(item->style->getFilter()))) {
139 remove_filter(item, false);
140 }
141 else {
142 SPFilter *filter = modify_filter_gaussian_blur_from_item(document, item, radius);
143 sp_style_set_property_url(item, "filter", filter, false);
144 }
146 //request update
147 item->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
148 SP_OBJECT_STYLE_MODIFIED_FLAG ));
149 }
151 sp_document_maybe_done (document, _blur_tag.c_str(), _verb_code,
152 _("Change blur"));
154 // resume interruptibility
155 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
157 _blocked = false;
158 }
160 void
161 ObjectCompositeSettings::_opacityValueChanged()
162 {
163 if (!_subject) {
164 return;
165 }
167 SPDesktop *desktop = _subject->getDesktop();
168 if (!desktop) {
169 return;
170 }
172 if (_blocked)
173 return;
174 _blocked = true;
176 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
177 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
178 // (though this only fixes the crash, not the multiple change events)
179 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
181 SPCSSAttr *css = sp_repr_css_attr_new ();
183 Inkscape::CSSOStringStream os;
184 os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
185 sp_repr_css_set_property (css, "opacity", os.str().c_str());
187 _subject->setCSS(css);
189 sp_repr_css_attr_unref (css);
191 sp_document_maybe_done (sp_desktop_document (desktop), _opacity_tag.c_str(), _verb_code,
192 _("Change opacity"));
194 // resume interruptibility
195 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
197 _blocked = false;
198 }
200 void
201 ObjectCompositeSettings::_subjectChanged() {
202 if (!_subject) {
203 return;
204 }
206 SPDesktop *desktop = _subject->getDesktop();
207 if (!desktop) {
208 return;
209 }
211 if (_blocked)
212 return;
213 _blocked = true;
215 SPStyle *query = sp_style_new (sp_desktop_document(desktop));
216 int result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
218 switch (result) {
219 case QUERY_STYLE_NOTHING:
220 _opacity_hbox.set_sensitive(false);
221 // gtk_widget_set_sensitive (opa, FALSE);
222 break;
223 case QUERY_STYLE_SINGLE:
224 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
225 case QUERY_STYLE_MULTIPLE_SAME:
226 _opacity_hbox.set_sensitive(true);
227 _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
228 break;
229 }
231 //query now for current filter mode and average blurring of selection
232 const int blend_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLEND);
233 switch(blend_result) {
234 case QUERY_STYLE_NOTHING:
235 _fe_cb.set_sensitive(false);
236 break;
237 case QUERY_STYLE_SINGLE:
238 case QUERY_STYLE_MULTIPLE_SAME:
239 _fe_cb.set_blend_mode(query->filter_blend_mode.value);
240 _fe_cb.set_sensitive(true);
241 break;
242 case QUERY_STYLE_MULTIPLE_DIFFERENT:
243 // TODO: set text
244 _fe_cb.set_sensitive(false);
245 break;
246 }
248 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
249 int blur_result = _subject->queryStyle(query, QUERY_STYLE_PROPERTY_BLUR);
250 switch (blur_result) {
251 case QUERY_STYLE_NOTHING: //no blurring
252 _fe_cb.set_blur_sensitive(false);
253 break;
254 case QUERY_STYLE_SINGLE:
255 case QUERY_STYLE_MULTIPLE_AVERAGED:
256 case QUERY_STYLE_MULTIPLE_SAME:
257 NR::Maybe<NR::Rect> bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
258 if (bbox) {
259 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
260 _fe_cb.set_blur_sensitive(true);
261 //update blur widget value
262 float radius = query->filter_gaussianBlur_deviation.value;
263 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
264 _fe_cb.set_blur_value(percent);
265 }
266 break;
267 }
268 }
270 sp_style_unref(query);
272 _blocked = false;
273 }
275 }
276 }
277 }
279 /*
280 Local Variables:
281 mode:c++
282 c-file-style:"stroustrup"
283 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
284 indent-tabs-mode:nil
285 fill-column:99
286 End:
287 */
288 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :