Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
grid.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
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 "layer-manager.h"
23#include "selection.h"
24#include <2geom/geom.h>
25
26#include "svg/path-string.h"
27
28#include "extension/effect.h"
29#include "extension/system.h"
30
31#include "util/units.h"
32
33#include "grid.h"
34
35namespace Inkscape {
36namespace Extension {
37namespace Internal {
38
44bool
46{
47 // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
48 return TRUE;
49}
50
51namespace {
52
53Glib::ustring build_lines(Geom::Rect bounding_area,
54 Geom::Point const &offset, Geom::Point const &spacing)
55{
56 Geom::Point point_offset(0.0, 0.0);
57
58 SVG::PathString path_data;
59
60 for ( int axis = Geom::X ; axis <= Geom::Y ; ++axis ) {
61 point_offset[axis] = offset[axis];
62
63 for (Geom::Point start_point = bounding_area.min();
64 start_point[axis] + offset[axis] <= (bounding_area.max())[axis];
65 start_point[axis] += spacing[axis]) {
66 Geom::Point end_point = start_point;
67 end_point[1-axis] = (bounding_area.max())[1-axis];
68
69 path_data.moveTo(start_point + point_offset)
70 .lineTo(end_point + point_offset);
71 }
72 }
73 // std::cout << "Path data:" << path_data.c_str() << std::endl;
74 return path_data;
75}
76
77} // namespace
78
84void
87{
89 SPDocument *doc = desktop->doc();
90
91 Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
92 if (selection->isEmpty()) {
93 /* get page size */
94 if (auto bounds = doc->preferredBounds()) {
95 bounding_area = *bounds;
96 }
97 } else {
98 if (auto bounds = selection->visualBounds()) {
99 bounding_area = *bounds;
100 }
101 Geom::Rect temprec = bounding_area * desktop->doc2dt();
102 bounding_area = temprec;
103 }
104
105 double scale = 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 = doc->getReprDoc();
118
119 //XML Tree being used directly here while it shouldn't be.
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);
131}
132
134class PrefAdjustment : public Gtk::Adjustment {
138 char * _pref;
139public:
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
158void
159PrefAdjustment::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
172Gtk::Widget *
174{
175 SPDocument * current_document = desktop->doc();
176
177 auto selected = desktop->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
188void
190{
191 // clang-format off
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", std::make_unique<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 :
Various geometrical calculations.
double scale
Definition aa.cpp:228
Geom::IntRect bounds
Definition canvas.cpp:182
CPoint min() const
Get the corner of the rectangle with smallest coordinate values.
CPoint max() const
Get the corner of the rectangle with largest coordinate values.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Scaling from the origin.
Definition transforms.h:150
Scale inverse() const
Definition transforms.h:172
Effects are extensions that take a document and do something to it in place.
Definition effect.h:39
The object that is the basis for the Extension system.
Definition extension.h:133
double set_param_float(char const *name, double value)
Sets a parameter identified by name with the double in the parameter value.
double get_param_float(char const *name) const
Gets a float parameter identified by name with the double placed in value.
A cache for the document and this implementation.
void effect(Inkscape::Extension::Effect *module, ExecutionEnv *executionEnv, SPDesktop *desktop, Inkscape::Extension::Implementation::ImplementationDocumentCache *docCache) override
This actually draws the grid.
Definition grid.cpp:85
bool load(Inkscape::Extension::Extension *module) override
A function to allocated anything – just an example here.
Definition grid.cpp:45
Gtk::Widget * prefs_effect(Inkscape::Extension::Effect *module, SPDesktop *desktop, sigc::signal< void()> *changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache *docCache) override
A function to get the preferences for the grid.
Definition grid.cpp:173
SPGroup * currentLayer() const
Returns current top layer.
SPItemRange items()
Returns a range of selected SPItems.
Definition object-set.h:230
bool isEmpty()
Returns true if no items are selected.
Geom::OptRect visualBounds() const
Builder for SVG path strings.
Definition path-string.h:38
PathString & moveTo(Geom::Coord x, Geom::Coord y)
Definition path-string.h:67
PathString & lineTo(Geom::Coord x, Geom::Coord y)
Definition path-string.h:79
The set of selected SPObjects for a given document and layer model.
Definition selection.h:80
Interface for refcounted XML nodes.
Definition node.h:80
virtual void appendChild(Node *child)=0
Append a node as the last child of this node.
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
To do: update description of desktop.
Definition desktop.h:149
Inkscape::Selection * getSelection() const
Definition desktop.h:188
SPDocument * doc() const
Definition desktop.h:159
Inkscape::LayerManager & layerManager()
Definition desktop.h:287
Geom::Affine const & doc2dt() const
Definition desktop.cpp:1337
Typed SVG document implementation.
Definition document.h:101
Geom::OptRect preferredBounds() const
Definition document.cpp:969
Inkscape::XML::Document * getReprDoc()
Our Inkscape::XML::Document.
Definition document.h:211
Geom::Scale getDocumentScale(bool computed=true) const
Returns document scale as defined by width/height (in pixels) and viewBox (real world to user-units).
Definition document.cpp:764
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
A way to clear the N_ macro, which is defined as an inline function.
Editable view implementation.
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
double offset
Definition desktop.h:50
void build_from_mem(gchar const *buffer, std::unique_ptr< Implementation::Implementation > in_imp)
Create a module from a buffer holding an XML description.
Definition system.cpp:459
static R & release(R &r)
Decrements the reference count of a anchored object.
Helper class to stream background task notifications as a series of messages.
Inkscape::SVG::PathString - builder for SVG path strings.
Interface for XML documents.
Definition document.h:43
virtual Node * createElement(char const *name)=0
SPDesktop * desktop