1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3 \file grid.cpp
4
5 A plug-in to add a grid creation effect into Inkscape.
6 */
7 /*
8 * Copyright (C) 2004-2005 Ted Gould <ted@gould.cx>
9 * Copyright (C) 2007 MenTaLguY <mental@rydia.net>
10 * Abhishek Sharma
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15 #include <gtkmm/box.h>
16 #include <gtkmm/adjustment.h>
17 #include <gtkmm/spinbutton.h>
18
19 #include "desktop.h"
20
21 #include "document.h"
22 #include "selection.h"
23 #include "2geom/geom.h"
24
25 #include "svg/path-string.h"
26
27 #include "extension/effect.h"
28 #include "extension/system.h"
29
30 #include "util/units.h"
31
32 #include "grid.h"
33
34 namespace Inkscape {
35 namespace Extension {
36 namespace Internal {
37
38 /**
39 \brief A function to allocated anything -- just an example here
40 \param module Unused
41 \return Whether the load was successful
42 */
43 bool
load(Inkscape::Extension::Extension *)44 Grid::load (Inkscape::Extension::Extension */*module*/)
45 {
46 // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
47 return TRUE;
48 }
49
50 namespace {
51
build_lines(Geom::Rect bounding_area,Geom::Point const & offset,Geom::Point const & spacing)52 Glib::ustring build_lines(Geom::Rect bounding_area,
53 Geom::Point const &offset, Geom::Point const &spacing)
54 {
55 Geom::Point point_offset(0.0, 0.0);
56
57 SVG::PathString path_data;
58
59 for ( int axis = Geom::X ; axis <= Geom::Y ; ++axis ) {
60 point_offset[axis] = offset[axis];
61
62 for (Geom::Point start_point = bounding_area.min();
63 start_point[axis] + offset[axis] <= (bounding_area.max())[axis];
64 start_point[axis] += spacing[axis]) {
65 Geom::Point end_point = start_point;
66 end_point[1-axis] = (bounding_area.max())[1-axis];
67
68 path_data.moveTo(start_point + point_offset)
69 .lineTo(end_point + point_offset);
70 }
71 }
72 // std::cout << "Path data:" << path_data.c_str() << std::endl;
73 return path_data;
74 }
75
76 } // namespace
77
78 /**
79 \brief This actually draws the grid.
80 \param module The effect that was called (unused)
81 \param document What should be edited.
82 */
83 void
effect(Inkscape::Extension::Effect * module,Inkscape::UI::View::View * document,Inkscape::Extension::Implementation::ImplementationDocumentCache *)84 Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
85 {
86 Inkscape::Selection * selection = ((SPDesktop *)document)->selection;
87
88 Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
89 if (selection->isEmpty()) {
90 /* get page size */
91 SPDocument * doc = document->doc();
92 bounding_area = Geom::Rect( Geom::Point(0,0),
93 Geom::Point(doc->getWidth().value("px"), doc->getHeight().value("px")) );
94 } else {
95 Geom::OptRect bounds = selection->visualBounds();
96 if (bounds) {
97 bounding_area = *bounds;
98 }
99
100 Geom::Rect temprec = bounding_area * static_cast<SPDesktop *>(document)->doc2dt();
101
102 bounding_area = temprec;
103 }
104
105 double scale = document->doc()->getDocumentScale().inverse()[Geom::X];
106
107 bounding_area *= Geom::Scale(scale);
108 Geom::Point spacings( scale * module->get_param_float("xspacing"),
109 scale * module->get_param_float("yspacing") );
110 gdouble line_width = scale * module->get_param_float("lineWidth");
111 Geom::Point offsets( scale * module->get_param_float("xoffset"),
112 scale * module->get_param_float("yoffset") );
113
114 Glib::ustring path_data("");
115
116 path_data = build_lines(bounding_area, offsets, spacings);
117 Inkscape::XML::Document * xml_doc = document->doc()->getReprDoc();
118
119 //XML Tree being used directly here while it shouldn't be.
120 Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->getRepr();
121 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
122
123 path->setAttribute("d", path_data);
124
125 std::ostringstream stringstream;
126 stringstream << "fill:none;stroke:#000000;stroke-width:" << line_width << "px";
127 path->setAttribute("style", stringstream.str());
128
129 current_layer->appendChild(path);
130 Inkscape::GC::release(path);
131 }
132
133 /** \brief A class to make an adjustment that uses Extension params */
134 class PrefAdjustment : public Gtk::Adjustment {
135 /** Extension that this relates to */
136 Inkscape::Extension::Extension * _ext;
137 /** The string which represents the parameter */
138 char * _pref;
139 public:
140 /** \brief Make the adjustment using an extension and the string
141 describing the parameter. */
PrefAdjustment(Inkscape::Extension::Extension * ext,char * pref)142 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
143 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
144 this->set_value(_ext->get_param_float(_pref));
145 this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
146 return;
147 };
148
149 void val_changed ();
150 }; /* class PrefAdjustment */
151
152 /** \brief A function to respond to the value_changed signal from the
153 adjustment.
154
155 This function just grabs the value from the adjustment and writes
156 it to the parameter. Very simple, but yet beautiful.
157 */
158 void
val_changed()159 PrefAdjustment::val_changed ()
160 {
161 // std::cout << "Value Changed to: " << this->get_value() << std::endl;
162 _ext->set_param_float(_pref, this->get_value());
163 return;
164 }
165
166 /** \brief A function to get the prefences for the grid
167 \param moudule Module which holds the params
168 \param view Unused today - may get style information in the future.
169
170 Uses AutoGUI for creating the GUI.
171 */
172 Gtk::Widget *
prefs_effect(Inkscape::Extension::Effect * module,Inkscape::UI::View::View * view,sigc::signal<void> * changeSignal,Inkscape::Extension::Implementation::ImplementationDocumentCache *)173 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
174 {
175 SPDocument * current_document = view->doc();
176
177 auto selected = ((SPDesktop *) view)->getSelection()->items();
178 Inkscape::XML::Node * first_select = nullptr;
179 if (!selected.empty()) {
180 first_select = selected.front()->getRepr();
181 }
182
183 return module->autogui(current_document, first_select, changeSignal);
184 }
185
186 #include "clear-n_.h"
187
188 void
init()189 Grid::init ()
190 {
191 // clang-format off
192 Inkscape::Extension::build_from_mem(
193 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
194 "<name>" N_("Grid") "</name>\n"
195 "<id>org.inkscape.effect.grid</id>\n"
196 "<param name=\"lineWidth\" gui-text=\"" N_("Line Width:") "\" type=\"float\">1.0</param>\n"
197 "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing:") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
198 "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing:") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
199 "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset:") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
200 "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset:") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
201 "<effect>\n"
202 "<object-type>all</object-type>\n"
203 "<effects-menu>\n"
204 "<submenu name=\"" N_("Render") "\">\n"
205 "<submenu name=\"" N_("Grids") "\" />\n"
206 "</submenu>\n"
207 "</effects-menu>\n"
208 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
209 "</effect>\n"
210 "</inkscape-extension>\n", new Grid());
211 // clang-format on
212 return;
213 }
214
215 }; /* namespace Internal */
216 }; /* namespace Extension */
217 }; /* namespace Inkscape */
218
219 /*
220 Local Variables:
221 mode:c++
222 c-file-style:"stroustrup"
223 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
224 indent-tabs-mode:nil
225 fill-column:99
226 End:
227 */
228 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
229