Code

NR:: to Geom:: for most of src/extension/
[inkscape.git] / src / extension / internal / grid.cpp
1 /**
2     \file grid.cpp
4     A plug-in to add a grid creation effect into Inkscape.
5 */
6 /*
7  * Copyright (C) 2004-2005  Ted Gould <ted@gould.cx>
8  * Copyright (C) 2007  MenTaLguY <mental@rydia.net>
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include <gtkmm/box.h>
14 #include <gtkmm/adjustment.h>
15 #include <gtkmm/spinbutton.h>
17 #include "desktop.h"
18 #include "desktop-handles.h"
19 #include "selection.h"
20 #include "sp-object.h"
21 #include "util/glib-list-iterators.h"
23 #include "svg/path-string.h"
25 #include "extension/effect.h"
26 #include "extension/system.h"
29 #include "grid.h"
31 namespace Inkscape {
32 namespace Extension {
33 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 Grid::load (Inkscape::Extension::Extension */*module*/)
42 {
43     // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
44     return TRUE;
45 }
47 namespace {
49 Glib::ustring build_lines(int axis, Geom::Rect bounding_area,
50                           float offset, float spacing)
51 {
52     Geom::Point point_offset(0.0, 0.0);
53     point_offset[axis] = offset;
55     SVG::PathString path_data;
56     for (Geom::Point start_point = bounding_area.min();
57             start_point[axis] + offset <= (bounding_area.max())[axis];
58             start_point[axis] += spacing) {
59         Geom::Point end_point = start_point;
60         end_point[1-axis] = (bounding_area.max())[1-axis];
62         path_data.moveTo(start_point + point_offset)
63                  .lineTo(end_point + point_offset);
64     }
66     return path_data;
67 }
69 }
71 /**
72     \brief  This actually draws the grid.
73     \param  module   The effect that was called (unused)
74     \param  document What should be edited.
75 */
76 void
77 Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
78 {
79     Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;
81     Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
82     if (selection->isEmpty()) {
83         /* get page size */
84         SPDocument * doc = document->doc();
85         bounding_area = Geom::Rect(  Geom::Point(0,0),
86                                      Geom::Point(sp_document_width(doc), sp_document_height(doc))  );
87     } else {
88         boost::optional<NR::Rect> bounds = selection->bounds();
89         if (bounds) {
90             bounding_area = to_2geom(*bounds);
91         }
93         gdouble doc_height  =  sp_document_height(document->doc());
94         Geom::Rect temprec = Geom::Rect(Geom::Point(bounding_area.min()[Geom::X], doc_height - bounding_area.min()[Geom::Y]),
95                                     Geom::Point(bounding_area.max()[Geom::X], doc_height - bounding_area.max()[Geom::Y]));
97         bounding_area = temprec;
98     }
100     float spacings[2] = { module->get_param_float("xspacing"),
101                           module->get_param_float("yspacing") };
102     float line_width = module->get_param_float("lineWidth");
103     float offsets[2] = { module->get_param_float("xoffset"),
104                          module->get_param_float("yoffset") };
106     Glib::ustring path_data("");
107     for ( int axis = 0 ; axis < 2 ; ++axis ) {
108         path_data += build_lines(axis, bounding_area,
109                                  offsets[axis], spacings[axis]);
110     }
112     Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
113     Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->repr;
114     Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
116     path->setAttribute("d", path_data.c_str());
118     Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
119     style += ";stroke-width:";
120     gchar floatstring[64];
121     sprintf(floatstring, "%f", line_width);
122     style += floatstring;
123     style += "pt";
124     path->setAttribute("style", style.c_str());
126     current_layer->appendChild(path);
128     return;
131 /** \brief  A class to make an adjustment that uses Extension params */
132 class PrefAdjustment : public Gtk::Adjustment {
133     /** Extension that this relates to */
134     Inkscape::Extension::Extension * _ext;
135     /** The string which represents the parameter */
136     char * _pref;
137 public:
138     /** \brief  Make the adjustment using an extension and the string
139                 describing the parameter. */
140     PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
141             Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
142         this->set_value(_ext->get_param_float(_pref));
143         this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
144         return;
145     };
147     void val_changed (void);
148 }; /* class PrefAdjustment */
150 /** \brief  A function to respond to the value_changed signal from the
151             adjustment.
153     This function just grabs the value from the adjustment and writes
154     it to the parameter.  Very simple, but yet beautiful.
155 */
156 void
157 PrefAdjustment::val_changed (void)
159     // std::cout << "Value Changed to: " << this->get_value() << std::endl;
160     _ext->set_param_float(_pref, this->get_value());
161     return;
164 /** \brief  A function to get the prefences for the grid
165     \param  moudule  Module which holds the params
166     \param  view     Unused today - may get style information in the future.
168     Uses AutoGUI for creating the GUI.
169 */
170 Gtk::Widget *
171 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
173     SPDocument * current_document = view->doc();
175     using Inkscape::Util::GSListConstIterator;
176     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
177     Inkscape::XML::Node * first_select = NULL;
178     if (selected != NULL)
179         first_select = SP_OBJECT_REPR(*selected);
181     return module->autogui(current_document, first_select, changeSignal);
184 #include "clear-n_.h"
186 void
187 Grid::init (void)
189     Inkscape::Extension::build_from_mem(
190         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
191             "<name>" N_("Grid") "</name>\n"
192             "<id>org.inkscape.effect.grid</id>\n"
193             "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
194             "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
195             "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
196             "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
197             "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
198             "<effect>\n"
199                 "<object-type>all</object-type>\n"
200                 "<effects-menu>\n"
201                     "<submenu name=\"" N_("Render") "\" />\n"
202                 "</effects-menu>\n"
203                 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
204             "</effect>\n"
205         "</inkscape-extension>\n", new Grid());
206     return;
209 }; /* namespace Internal */
210 }; /* namespace Extension */
211 }; /* namespace Inkscape */
213 /*
214   Local Variables:
215   mode:c++
216   c-file-style:"stroustrup"
217   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218   indent-tabs-mode:nil
219   fill-column:99
220   End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :