Code

r16677@shi: ted | 2007-10-17 19:31:04 -0700
[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 <vector>
16 #include "desktop.h"
17 #include "selection.h"
18 #include "helper/action.h"
19 #include "prefs-utils.h"
20 #include "path-chemistry.h"
21 #include "sp-item.h"
23 #include "util/glib-list-iterators.h"
25 #include "extension/effect.h"
26 #include "extension/system.h"
29 #include "bluredge.h"
31 namespace Inkscape {
32 namespace Extension {
33 namespace Internal {
36 /**
37     \brief  A function to allocated anything -- just an example here
38     \param  module  Unused
39     \return Whether the load was sucessful
40 */
41 bool
42 BlurEdge::load (Inkscape::Extension::Extension *module)
43 {
44     // std::cout << "Hey, I'm Blur Edge, I'm loading!" << std::endl;
45     return TRUE;
46 }
48 /**
49     \brief  This actually blurs the edge.
50     \param  module   The effect that was called (unused)
51     \param  document What should be edited.
52 */
53 void
54 BlurEdge::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
55 {
56     Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;
58     float width = module->get_param_float("blur-width");
59     int   steps = module->get_param_int("num-steps");
61     double old_offset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
63     using Inkscape::Util::GSListConstIterator;
64     // TODO need to properly refcount the items, at least
65     std::list<SPItem *> items;
66     items.insert<GSListConstIterator<SPItem *> >(items.end(), selection->itemList(), NULL);
67     selection->clear();
69     for(std::list<SPItem *>::iterator item = items.begin();
70             item != items.end(); item++) {
71         SPItem * spitem = *item;
73         std::vector<Inkscape::XML::Node *> new_items(steps);
74         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document->doc());
75         Inkscape::XML::Node * new_group = xml_doc->createElement("svg:g");
76         (SP_OBJECT_REPR(spitem)->parent())->appendChild(new_group);
78         double orig_opacity = sp_repr_css_double_property(sp_repr_css_attr(SP_OBJECT_REPR(spitem), "style"), "opacity", 1.0);
79         char opacity_string[64];
80         g_ascii_formatd(opacity_string, sizeof(opacity_string), "%f",
81                         orig_opacity / (steps));
83         for (int i = 0; i < steps; i++) {
84             double offset = (width / (float)(steps - 1) * (float)i) - (width / 2.0);
86             new_items[i] = (SP_OBJECT_REPR(spitem))->duplicate(xml_doc);
88             SPCSSAttr * css = sp_repr_css_attr(new_items[i], "style");
89             sp_repr_css_set_property(css, "opacity", opacity_string);
90             sp_repr_css_change(new_items[i], css, "style");
92             new_group->appendChild(new_items[i]);
93             selection->add(new_items[i]);
94             sp_selected_path_to_curves();
96             if (offset < 0.0) {
97                 /* Doing an inset here folks */
98                 offset *= -1.0;
99                 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
100                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_INSET)->get_action(document), NULL);
101             } else if (offset > 0.0) {
102                 prefs_set_double_attribute("options.defaultoffsetwidth", "value", offset);
103                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_OFFSET)->get_action(document), NULL);
104             }
106             selection->clear();
107         }
109     }
111     prefs_set_double_attribute("options.defaultoffsetwidth", "value", old_offset);
113     selection->clear();
114     selection->add(items.begin(), items.end());
116     return;
119 Gtk::Widget *
120 BlurEdge::prefs_effect(Inkscape::Extension::Effect * module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
122     return module->autogui(NULL, NULL, changeSignal);
125 #include "clear-n_.h"
127 void
128 BlurEdge::init (void)
130     Inkscape::Extension::build_from_mem(
131         "<inkscape-extension>\n"
132             "<name>" N_("Inset/Outset Halo") "</name>\n"
133             "<id>org.inkscape.effect.bluredge</id>\n"
134             "<param name=\"blur-width\" gui-text=\"" N_("Width") "\" gui-description=\"" N_("Width in px of the halo") "\" scope=\"document\" type=\"float\" min=\"1.0\" max=\"50.0\">1.0</param>\n"
135             "<param name=\"num-steps\" gui-text=\"" N_("Number of steps") "\" gui-description=\"" N_("Number of inset/outset copies of the object to make") "\" scope=\"document\" type=\"int\" min=\"5\" max=\"100\">11</param>\n"
136             "<effect>\n"
137                 "<object-type>all</object-type>\n"
138                 "<effects-menu>\n"
139                     "<submenu name=\"" N_("Generate from Path") "\" />\n"
140                 "</effects-menu>\n"
141             "</effect>\n"
142         "</inkscape-extension>\n" , new BlurEdge());
143     return;
146 }; /* namespace Internal */
147 }; /* namespace Extension */
148 }; /* namespace Inkscape */
150 /*
151   Local Variables:
152   mode:c++
153   c-file-style:"stroustrup"
154   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155   indent-tabs-mode:nil
156   fill-column:99
157   End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :