Code

3f230439a4e868352fb21412d30b082ccf6be663
[inkscape.git] / src / extension / internal / bluredge.cpp
1 /**
2     \file bluredge.cpp
3  
4     A plug-in to add an effect to blur the edges of an object. 
5 */
6 /*
7  * Authors:
8  *   Ted Gould <ted@gould.cx>
9  *
10  * Copyright (C) 2005 Authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include "desktop.h"
16 #include "selection.h"
17 #include "helper/action.h"
18 #include "prefs-utils.h"
19 #include "path-chemistry.h"
20 #include "sp-item.h"
22 #include "util/glib-list-iterators.h"
24 #include "extension/effect.h"
25 #include "extension/system.h"
28 #include "bluredge.h"
30 namespace Inkscape {
31 namespace Extension {
32 namespace Internal {
35 /**
36     \brief  A function to allocated anything -- just an example here
37     \param  module  Unused
38     \return Whether the load was sucessful
39 */
40 bool
41 BlurEdge::load (Inkscape::Extension::Extension *module)
42 {
43     // std::cout << "Hey, I'm Blur Edge, I'm loading!" << std::endl;
44     return TRUE;
45 }
47 /**
48     \brief  This actually blurs the edge.
49     \param  module   The effect that was called (unused)
50     \param  document What should be edited.
51 */
52 void
53 BlurEdge::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
54 {
55     Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;
57     float width = module->get_param_float("blur-width");
58     int   steps = module->get_param_int("num-steps");
60     double old_offset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
62     using Inkscape::Util::GSListConstIterator;
63     // TODO need to properly refcount the items, at least
64     std::list<SPItem *> items;
65     items.insert<GSListConstIterator<SPItem *> >(items.end(), selection->itemList(), NULL);
66     selection->clear();
68     std::list<SPItem *> new_items;
69     for(std::list<SPItem *>::iterator item = items.begin();
70             item != items.end(); item++) {
71         SPItem * spitem = *item;
73         Inkscape::XML::Node * new_items[steps];
74         Inkscape::XML::Node * new_group = sp_repr_new("svg:g");
75         (SP_OBJECT_REPR(spitem)->parent())->appendChild(new_group);
76         /** \todo  Need to figure out how to get from XML::Node to SPItem */
77         /* new_items.push_back(); */
79         double orig_opacity = sp_repr_css_double_property(sp_repr_css_attr(SP_OBJECT_REPR(spitem), "style"), "opacity", 1.0);
80         char opacity_string[64];
81         g_ascii_formatd(opacity_string, sizeof(opacity_string), "%f",
82                         orig_opacity / (steps));
84         for (int i = 0; i < steps; i++) {
85             double offset = (width / (float)(steps - 1) * (float)i) - (width / 2.0);
87             new_items[i] = (SP_OBJECT_REPR(spitem))->duplicate();
89             SPCSSAttr * css = sp_repr_css_attr(new_items[i], "style");
90             sp_repr_css_set_property(css, "opacity", opacity_string);
91             sp_repr_css_change(new_items[i], css, "style");
93             new_group->appendChild(new_items[i]);
94             selection->add(new_items[i]);
95             sp_selected_path_to_curves();
97             if (offset < 0.0) {
98                 /* Doing an inset here folks */
99                 offset *= -1.0;
100                 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
101                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_INSET)->get_action(document), NULL);
102             } else if (offset == 0.0) {
103             } else {
104                 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
105                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_OFFSET)->get_action(document), NULL);
106             }
108             selection->clear();
109         }
111     }
113     prefs_set_double_attribute("options.defaultoffsetwidth", "value", old_offset);
115     selection->clear();
116     selection->add(items.begin(), items.end());
117     selection->add(new_items.begin(), new_items.end());
119     return;
122 Gtk::Widget *
123 BlurEdge::prefs_effect(Inkscape::Extension::Effect * module, Inkscape::UI::View::View * view)
125     return module->autogui();
128 void
129 BlurEdge::init (void)
131     Inkscape::Extension::build_from_mem(
132         "<inkscape-extension>\n"
133             "<name>Blur Edge</name>\n"
134             "<id>org.inkscape.effect.bluredge</id>\n"
135             "<param name=\"blur-width\" gui-text=\"Blur Width\" type=\"float\" min=\"1.0\" max=\"50.0\">1.0</param>\n"
136             "<param name=\"num-steps\" gui-text=\"Number of Steps\" type=\"int\" min=\"5\" max=\"100\">11</param>\n"
137             "<effect>\n"
138                 "<object-type>all</object-type>\n"
139             "</effect>\n"
140         "</inkscape-extension>\n" , new BlurEdge());
141     return;
144 }; /* namespace Internal */
145 }; /* namespace Extension */
146 }; /* namespace Inkscape */
148 /*
149   Local Variables:
150   mode:c++
151   c-file-style:"stroustrup"
152   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
153   indent-tabs-mode:nil
154   fill-column:99
155   End:
156 */
157 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :