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#include "layer-manager.h"
21
22#include "document.h"
23#include "selection.h"
24#include <2geom/geom.h>
25
26#include "object/sp-object.h"
27
28#include "svg/path-string.h"
29
30#include "extension/effect.h"
31#include "extension/system.h"
32
33#include "util/units.h"
34
35#include "grid.h"
36
37namespace Inkscape {
38namespace Extension {
39namespace Internal {
40
46bool
48{
49 // std::cerr << "Hey, I'm Grid, I'm loading!" << std::endl;
50 return TRUE;
51}
52
53namespace {
54
55Glib::ustring build_lines(Geom::Rect bounding_area,
56 Geom::Point const &offset, Geom::Point const &spacing)
57{
58
59 std::cerr << "Building lines" << std::endl;
60
61 Geom::Point point_offset(0.0, 0.0);
62
63 SVG::PathString path_data;
64
65 for ( int axis = Geom::X ; axis <= Geom::Y ; ++axis ) {
66 point_offset[axis] = offset[axis];
67
68 for (Geom::Point start_point = bounding_area.min();
69 start_point[axis] + offset[axis] <= (bounding_area.max())[axis];
70 start_point[axis] += spacing[axis]) {
71 Geom::Point end_point = start_point;
72 end_point[1-axis] = (bounding_area.max())[1-axis];
73
74 path_data.moveTo(start_point + point_offset)
75 .lineTo(end_point + point_offset);
76 }
77 }
78 std::cerr << "Path data:" << path_data.c_str() << std::endl;
79 return path_data;
80}
81
82} // namespace
83
89void
91{
92
93 std::cerr << "Executing effect" << std::endl;
94
96
97 Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
98 if (selection->isEmpty()) {
99 /* get page size */
100 SPDocument * doc = desktop->doc();
101 bounding_area = *(doc->preferredBounds());
102 } else {
103 Geom::OptRect bounds = selection->visualBounds();
104 if (bounds) {
105 bounding_area = *bounds;
106 }
107
108 gdouble doc_height = (desktop->doc())->getHeight().value("px");
109 Geom::Rect temprec = Geom::Rect(Geom::Point(bounding_area.min()[Geom::X], doc_height - bounding_area.min()[Geom::Y]),
110 Geom::Point(bounding_area.max()[Geom::X], doc_height - bounding_area.max()[Geom::Y]));
111
112 bounding_area = temprec;
113 }
114
116
117 bounding_area *= Geom::Scale(scale);
118 Geom::Point spacings( scale * module->get_param_float("xspacing"),
119 scale * module->get_param_float("yspacing") );
120 gdouble line_width = scale * module->get_param_float("lineWidth");
121 Geom::Point offsets( scale * module->get_param_float("xoffset"),
122 scale * module->get_param_float("yoffset") );
123
124 Glib::ustring path_data("");
125
126 path_data = build_lines(bounding_area, offsets, spacings);
128
129 //XML Tree being used directly here while it shouldn't be.
131 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
132
133 path->setAttribute("d", path_data);
134
135 std::ostringstream stringstream;
136 stringstream << "fill:none;stroke:#000000;stroke-width:" << line_width << "px";
137 path->setAttribute("style", stringstream.str());
138
139 current_layer->appendChild(path);
141}
142
144class PrefAdjustment : public Gtk::Adjustment {
148 char * _pref;
149public:
152 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
153 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
154 this->set_value(_ext->get_param_float(_pref));
155 this->signal_value_changed().connect(sigc::mem_fun(*this, &PrefAdjustment::val_changed));
156 return;
157 };
158
159 void val_changed ();
160}; /* class PrefAdjustment */
161
168void
169PrefAdjustment::val_changed ()
170{
171 // std::cerr << "Value Changed to: " << this->get_value() << std::endl;
172 _ext->set_param_float(_pref, this->get_value());
173 return;
174}
175
182Gtk::Widget *
184{
185 auto current_document = desktop->doc();
186 auto selected = desktop->getSelection()->items();
187
188 Inkscape::XML::Node * first_select = nullptr;
189 if (!selected.empty()) {
190 first_select = selected.front()->getRepr();
191 }
192
193 return module->autogui(current_document, first_select, changeSignal);
194}
195
196
197
198
199}; /* namespace Internal */
200}; /* namespace Extension */
201}; /* namespace Inkscape */
202
203
204/*
205 Local Variables:
206 mode:c++
207 c-file-style:"stroustrup"
208 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
209 indent-tabs-mode:nil
210 fill-column:99
211 End:
212*/
213// 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.
Axis-aligned rectangle that can be empty.
Definition rect.h:203
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
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
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.
Editable view implementation.
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
double offset
Definition desktop.h:50
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