summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/object-composite-settings.cpp
blob: 9331e71a1bff3ee7e624136bb9ccf79c02c40caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * A widget for controlling object compositing (filter, opacity, etc.)
 *
 * Authors:
 *   Bryce W. Harrington <bryce@bryceharrington.org>
 *   Gustav Broberg <broberg@kth.se>
 *   Niko Kiirala <niko@kiirala.com>
 *   Abhishek Sharma
 *
 * Copyright (C) 2004--2008 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "object-composite-settings.h"

#include <utility>

#include "desktop.h"
#include "desktop-style.h"
#include "document.h"
#include "document-undo.h"
#include "filter-chemistry.h"
#include "inkscape.h"
#include "style.h"

#include "object/filters/blend.h"
#include "svg/css-ostringstream.h"
#include "ui/widget/style-subject.h"

constexpr double BLUR_MULTIPLIER = 4.0;

namespace Inkscape {
namespace UI {
namespace Widget {

ObjectCompositeSettings::ObjectCompositeSettings(Glib::ustring icon_name, char const *history_prefix, int flags)
: Gtk::Box(Gtk::ORIENTATION_VERTICAL),
  _icon_name(std::move(icon_name)),
  _blend_tag(Glib::ustring(history_prefix) + ":blend"),
  _blur_tag(Glib::ustring(history_prefix) + ":blur"),
  _opacity_tag(Glib::ustring(history_prefix) + ":opacity"),
  _isolation_tag(Glib::ustring(history_prefix) + ":isolation"),
  _filter_modifier(flags),
  _blocked(false)
{
    set_name( "ObjectCompositeSettings");

    // Filter Effects
    pack_start(_filter_modifier, false, false, 2);

    _filter_modifier.signal_blend_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_blendBlurValueChanged));
    _filter_modifier.signal_blur_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_blendBlurValueChanged));
    _filter_modifier.signal_opacity_changed().connect(sigc::mem_fun(*this, &ObjectCompositeSettings::_opacityValueChanged));
    _filter_modifier.signal_isolation_changed().connect(
        sigc::mem_fun(*this, &ObjectCompositeSettings::_isolationValueChanged));

    show_all_children();
}

ObjectCompositeSettings::~ObjectCompositeSettings() {
    setSubject(nullptr);
}

void ObjectCompositeSettings::setSubject(StyleSubject *subject) {
    _subject_changed.disconnect();
    if (subject) {
        _subject = subject;
        _subject_changed = _subject->connectChanged(sigc::mem_fun(*this, &ObjectCompositeSettings::_subjectChanged));
    }
}

// We get away with sharing one callback for blend and blur as this is used by
//  * the Layers dialog where only one layer can be selected at a time,
//  * the Fill and Stroke dialog where only blur is used.
// If both blend and blur are used in a dialog where more than one object can
// be selected then this should be split into separate functions for blend and
// blur (like in the Objects dialog).
void
ObjectCompositeSettings::_blendBlurValueChanged()
{
    if (!_subject) {
        return;
    }

    SPDesktop *desktop = _subject->getDesktop();
    if (!desktop) {
        return;
    }
    SPDocument *document = desktop->getDocument();

    if (_blocked)
        return;
    _blocked = true;

    Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
    double radius;
    if (bbox) {
        double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y];   // fixme: this is only half the perimeter, is that correct?
        double blur_value = _filter_modifier.get_blur_value() / 100.0;
        radius = blur_value * blur_value * perimeter / BLUR_MULTIPLIER;
    } else {
        radius = 0;
    }

    //apply created filter to every selected item
    std::vector<SPObject*> sel = _subject->list();
    for (auto i : sel) {
        if (!SP_IS_ITEM(i)) {
            continue;
        }
        SPItem * item = SP_ITEM(i);
        SPStyle *style = item->style;
        g_assert(style != nullptr);
        bool change_blend = (item->style->mix_blend_mode.set ? item->style->mix_blend_mode.value : SP_CSS_BLEND_NORMAL) != _filter_modifier.get_blend_mode();
        // < 1.0 filter based blend removal
        if (!item->style->mix_blend_mode.set && item->style->filter.set && item->style->getFilter()) {
            remove_filter_legacy_blend(item);
        }
        item->style->mix_blend_mode.set = TRUE;
        if (item->style->isolation.value == SP_CSS_ISOLATION_ISOLATE) {
            item->style->mix_blend_mode.value = SP_CSS_BLEND_NORMAL;
        } else { 
            item->style->mix_blend_mode.value = _filter_modifier.get_blend_mode();
        }

        if (radius == 0 && item->style->filter.set && item->style->getFilter()
            && filter_is_single_gaussian_blur(item->style->getFilter())) {
            remove_filter(item, false);
        } else if (radius != 0) {
            SPFilter *filter = modify_filter_gaussian_blur_from_item(document, item, radius);
            filter->update_filter_region(item);
            sp_style_set_property_url(item, "filter", filter, false);
        } 
        if (change_blend) { //we do blend so we need update display style
            item->updateRepr(SP_OBJECT_WRITE_NO_CHILDREN | SP_OBJECT_WRITE_EXT);
        } else {
            item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
        }
    }

    DocumentUndo::maybeDone(document, _blur_tag.c_str(), _("Change blur/blend filter"), _icon_name);

    _blocked = false;
}

void
ObjectCompositeSettings::_opacityValueChanged()
{
    if (!_subject) {
        return;
    }

    SPDesktop *desktop = _subject->getDesktop();
    if (!desktop) {
        return;
    }

    if (_blocked)
        return;
    _blocked = true;

    SPCSSAttr *css = sp_repr_css_attr_new ();

    Inkscape::CSSOStringStream os;
    os << CLAMP (_filter_modifier.get_opacity_value() / 100, 0.0, 1.0);
    sp_repr_css_set_property (css, "opacity", os.str().c_str());

    _subject->setCSS(css);

    sp_repr_css_attr_unref (css);

    DocumentUndo::maybeDone(desktop->getDocument(), _opacity_tag.c_str(), _("Change opacity"), _icon_name);

    _blocked = false;
}

void ObjectCompositeSettings::_isolationValueChanged()
{
    if (!_subject) {
        return;
    }

    SPDesktop *desktop = _subject->getDesktop();
    if (!desktop) {
        return;
    }

    if (_blocked)
        return;
    _blocked = true;

    for (auto item : _subject->list()) {
        item->style->isolation.set = TRUE;
        item->style->isolation.value = _filter_modifier.get_isolation_mode();
        if (item->style->isolation.value == SP_CSS_ISOLATION_ISOLATE) {
            item->style->mix_blend_mode.set = TRUE;
            item->style->mix_blend_mode.value = SP_CSS_BLEND_NORMAL;
        }
        item->updateRepr(SP_OBJECT_WRITE_NO_CHILDREN | SP_OBJECT_WRITE_EXT);
    }

    DocumentUndo::maybeDone(desktop->getDocument(), _isolation_tag.c_str(), _("Change isolation"), _icon_name);

    _blocked = false;
}

void
ObjectCompositeSettings::_subjectChanged() {
    if (!_subject) {
        return;
    }

    SPDesktop *desktop = _subject->getDesktop();
    if (!desktop) {
        return;
    }

    if (_blocked)
        return;
    _blocked = true;
    SPStyle query(desktop->getDocument());
    int result = _subject->queryStyle(&query, QUERY_STYLE_PROPERTY_MASTEROPACITY);

    switch (result) {
        case QUERY_STYLE_NOTHING:
            break;
        case QUERY_STYLE_SINGLE:
        case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
        case QUERY_STYLE_MULTIPLE_SAME:
            _filter_modifier.set_opacity_value(100 * SP_SCALE24_TO_FLOAT(query.opacity.value));
            break;
    }

    //query now for current filter mode and average blurring of selection
    const int isolation_result = _subject->queryStyle(&query, QUERY_STYLE_PROPERTY_ISOLATION);
    switch (isolation_result) {
        case QUERY_STYLE_NOTHING:
            _filter_modifier.set_isolation_mode(SP_CSS_ISOLATION_AUTO, false);
            break;
        case QUERY_STYLE_SINGLE:
        case QUERY_STYLE_MULTIPLE_SAME:
            _filter_modifier.set_isolation_mode(query.isolation.value, true); // here dont work mix_blend_mode.set
            break;
        case QUERY_STYLE_MULTIPLE_DIFFERENT:
            _filter_modifier.set_isolation_mode(SP_CSS_ISOLATION_AUTO, false);
            // TODO: set text
            break;
    }

    // query now for current filter mode and average blurring of selection
    const int blend_result = _subject->queryStyle(&query, QUERY_STYLE_PROPERTY_BLEND);
    switch(blend_result) {
        case QUERY_STYLE_NOTHING:
            _filter_modifier.set_blend_mode(SP_CSS_BLEND_NORMAL, false);
            break;
        case QUERY_STYLE_SINGLE:
        case QUERY_STYLE_MULTIPLE_SAME:
            _filter_modifier.set_blend_mode(query.mix_blend_mode.value, true); // here dont work mix_blend_mode.set
            break;
        case QUERY_STYLE_MULTIPLE_DIFFERENT:
            _filter_modifier.set_blend_mode(SP_CSS_BLEND_NORMAL, false);
            break;
    }

    int blur_result = _subject->queryStyle(&query, QUERY_STYLE_PROPERTY_BLUR);
    switch (blur_result) {
        case QUERY_STYLE_NOTHING: // no blurring
            _filter_modifier.set_blur_value(0);
            break;
        case QUERY_STYLE_SINGLE:
        case QUERY_STYLE_MULTIPLE_AVERAGED:
        case QUERY_STYLE_MULTIPLE_SAME:
            Geom::OptRect bbox = _subject->getBounds(SPItem::GEOMETRIC_BBOX);
            if (bbox) {
                double perimeter =
                    bbox->dimensions()[Geom::X] +
                    bbox->dimensions()[Geom::Y]; // fixme: this is only half the perimeter, is that correct?
                // update blur widget value
                float radius = query.filter_gaussianBlur_deviation.value;
                float percent = std::sqrt(radius * BLUR_MULTIPLIER / perimeter) * 100;
                _filter_modifier.set_blur_value(percent);
            }
            break;
    }

    // If we have nothing selected, disable dialog.
    if (result       == QUERY_STYLE_NOTHING &&
        blend_result == QUERY_STYLE_NOTHING ) {
        _filter_modifier.set_sensitive( false );
    } else {
        _filter_modifier.set_sensitive( true );
    }

    _blocked = false;
}

}
}
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :