Code

Merge and cleanup of GSoC C++-ification project.
[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 "preferences.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  desktop What should be edited.
52 */
53 void
54 BlurEdge::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *desktop, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
55 {
56     Inkscape::Selection * selection     = static_cast<SPDesktop *>(desktop)->selection;
58     float width = module->get_param_float("blur-width");
59     int   steps = module->get_param_int("num-steps");
61     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
62     double old_offset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
64     using Inkscape::Util::GSListConstIterator;
65     // TODO need to properly refcount the items, at least
66     std::list<SPItem *> items;
67     items.insert<GSListConstIterator<SPItem *> >(items.end(), selection->itemList(), NULL);
68     selection->clear();
70     for(std::list<SPItem *>::iterator item = items.begin();
71             item != items.end(); item++) {
72         SPItem * spitem = *item;
74         std::vector<Inkscape::XML::Node *> new_items(steps);
75         Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
76         Inkscape::XML::Node * new_group = xml_doc->createElement("svg:g");
77         (SP_OBJECT_REPR(spitem)->parent())->appendChild(new_group);
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(xml_doc);
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(static_cast<SPDesktop *>(desktop));
97             if (offset < 0.0) {
98                 /* Doing an inset here folks */
99                 offset *= -1.0;
100                 prefs->setDouble("/options/defaultoffsetwidth/value", offset);
101                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_INSET)->get_action(desktop), NULL);
102             } else if (offset > 0.0) {
103                 prefs->setDouble("/options/defaultoffsetwidth/value", offset);
104                 sp_action_perform(Inkscape::Verb::get(SP_VERB_SELECTION_OFFSET)->get_action(desktop), NULL);
105             }
107             selection->clear();
108         }
110         Inkscape::GC::release(new_group);
111     }
113     prefs->setDouble("/options/defaultoffsetwidth/value", old_offset);
115     selection->clear();
116     selection->add(items.begin(), items.end());
118     return;
121 Gtk::Widget *
122 BlurEdge::prefs_effect(Inkscape::Extension::Effect * module, Inkscape::UI::View::View * /*view*/, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
124     return module->autogui(NULL, NULL, changeSignal);
127 #include "clear-n_.h"
129 void
130 BlurEdge::init (void)
132     Inkscape::Extension::build_from_mem(
133         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
134             "<name>" N_("Inset/Outset Halo") "</name>\n"
135             "<id>org.inkscape.effect.bluredge</id>\n"
136             "<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"
137             "<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"
138             "<effect>\n"
139                 "<object-type>all</object-type>\n"
140                 "<effects-menu>\n"
141                     "<submenu name=\"" N_("Generate from Path") "\" />\n"
142                 "</effects-menu>\n"
143             "</effect>\n"
144         "</inkscape-extension>\n" , new BlurEdge());
145     return;
148 }; /* namespace Internal */
149 }; /* namespace Extension */
150 }; /* namespace Inkscape */
152 /*
153   Local Variables:
154   mode:c++
155   c-file-style:"stroustrup"
156   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157   indent-tabs-mode:nil
158   fill-column:99
159   End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :