6fcb262475a674e30b1536ed21752070a9cdf184
1 #define __KNOT_HOLDER_ENTITY_C__
3 /** \file
4 * KnotHolderEntity definition.
5 *
6 * Authors:
7 * Mitsuru Oka <oka326@parkcity.ne.jp>
8 * Maximilian Albert <maximilian.albert@gmail.com>
9 *
10 * Copyright (C) 1999-2001 Lauris Kaplinski
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 * Copyright (C) 2001 Mitsuru Oka
13 * Copyright (C) 2004 Monash University
14 * Copyright (C) 2008 Maximilian Albert
15 *
16 * Released under GNU GPL
17 */
19 #include "knotholder.h"
20 #include "sp-item.h"
21 #include "style.h"
22 #include "preferences.h"
23 #include "macros.h"
24 #include <libnr/nr-matrix-ops.h>
25 #include "sp-pattern.h"
26 #include "snap.h"
27 #include "desktop.h"
28 #include "sp-namedview.h"
29 #include <2geom/matrix.h>
30 #include <2geom/transforms.h>
32 int KnotHolderEntity::counter = 0;
34 void
35 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
36 SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
37 {
38 knot = sp_knot_new(desktop, tip);
40 this->parent_holder = parent;
41 this->item = item; // TODO: remove the item either from here or from knotholder.cpp
42 this->desktop = desktop;
44 my_counter = KnotHolderEntity::counter++;
46 g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
47 g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
49 knot->fill [SP_KNOT_STATE_NORMAL] = color;
50 g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
52 update_knot();
53 sp_knot_show(knot);
55 _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
56 _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
57 _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
58 }
61 KnotHolderEntity::~KnotHolderEntity()
62 {
63 _moved_connection.disconnect();
64 _click_connection.disconnect();
65 _ungrabbed_connection.disconnect();
67 /* unref should call destroy */
68 if (knot) {
69 g_object_unref(knot);
70 } else {
71 // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
72 // If so, it will likely be fixed with upcoming refactoring efforts.
73 g_return_if_fail(knot);
74 }
75 }
77 void
78 KnotHolderEntity::update_knot()
79 {
80 Geom::Matrix const i2d(sp_item_i2d_affine(item));
82 Geom::Point dp(knot_get() * i2d);
84 _moved_connection.block();
85 sp_knot_set_position(knot, dp, SP_KNOT_STATE_NORMAL);
86 _moved_connection.unblock();
87 }
89 Geom::Point
90 KnotHolderEntity::snap_knot_position(Geom::Point const &p)
91 {
92 Geom::Matrix const i2d (sp_item_i2d_affine(item));
93 Geom::Point s = p * i2d;
95 SnapManager &m = desktop->namedview->snap_manager;
96 m.setup(desktop, true, item);
98 m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
100 return s * i2d.inverse();
101 }
103 Geom::Point
104 KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::ConstraintLine const &constraint)
105 {
106 Geom::Matrix const i2d (sp_item_i2d_affine(item));
107 Geom::Point s = p * i2d;
109 SnapManager &m = desktop->namedview->snap_manager;
110 m.setup(desktop, true, item);
112 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
113 if ((prefs->getBool("/options/snapmousepointer/value", false))) { // legacy behavior (pre v0.47)
114 // Snapping the mouse pointer instead of the constrained position of the knot allows to snap to
115 // things which don't intersect with the constraint line. This should be handled by the
116 // smart dynamic guides which are yet to be implemented, making this behavior more clean and
117 // transparent. With the current implementation it leads to unexpected results, and it doesn't
118 // allow accurately controlling what is being snapped to.
120 // freeSnap() will try snapping point p. This will not take into account the constraint, which
121 // is therefore to be enforced after snap_knot_position_constrained() has finished
122 m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
123 } else {
124 // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line.
125 // This way the constraint is already enforced, no need to worry about that later on
126 Inkscape::Snapper::ConstraintLine transformed_constraint = Inkscape::Snapper::ConstraintLine(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
127 m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint);
128 }
130 return s * i2d.inverse();
131 }
134 /* Pattern manipulation */
136 /* TODO: this pattern manipulation is not able to handle general transformation matrices. Only matrices that are the result of a pure scale times a pure rotation. */
138 Geom::Point
139 PatternKnotHolderEntityXY::knot_get()
140 {
141 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
143 gdouble x = 0;
144 gdouble y = -pattern_height(pat);
146 Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
147 return delta;
148 }
150 void
151 PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &origin, guint state)
152 {
153 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
155 // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
156 Geom::Point p_snapped = snap_knot_position(p);
158 if ( state & GDK_CONTROL_MASK ) {
159 if (fabs((p - origin)[Geom::X]) > fabs((p - origin)[Geom::Y])) {
160 p_snapped[Geom::Y] = origin[Geom::Y];
161 } else {
162 p_snapped[Geom::X] = origin[Geom::X];
163 }
164 }
166 if (state) {
167 Geom::Point knot_relpos(0, -pattern_height(pat));
168 Geom::Point const q = p_snapped - (knot_relpos * pat->patternTransform);
169 sp_item_adjust_pattern(item, Geom::Matrix(Geom::Translate(q)));
170 }
172 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
173 }
175 Geom::Point
176 PatternKnotHolderEntityAngle::knot_get()
177 {
178 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
180 gdouble x = pattern_width(pat);
181 gdouble y = -pattern_height(pat);
183 Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
184 return delta;
185 }
187 void
188 PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
189 {
190 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
191 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
193 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
195 // rotate pattern around XY knot position
196 Geom::Point knot_relpos(pattern_width(pat), -pattern_height(pat));
197 Geom::Point xy_knot_relpos(0, -pattern_height(pat));
198 Geom::Point transform_origin = xy_knot_relpos * pat->patternTransform;
200 Geom::Point oldp = (knot_relpos * pat->patternTransform) - transform_origin;
201 Geom::Point newp = p - transform_origin;
203 gdouble theta = Geom::angle_between(oldp, newp);
205 if ( state & GDK_CONTROL_MASK ) {
206 theta = sp_round(theta, M_PI/snaps);
207 }
209 Geom::Matrix rot = Geom::Matrix(Geom::Translate(-transform_origin))
210 * Geom::Rotate(theta)
211 * Geom::Translate(transform_origin);
212 sp_item_adjust_pattern(item, rot);
213 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
214 }
216 Geom::Point
217 PatternKnotHolderEntityScale::knot_get()
218 {
219 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
221 gdouble x = pattern_width(pat);
222 gdouble y = 0;
223 Geom::Point delta = Geom::Point(x,y) * pat->patternTransform;
224 return delta;
225 }
227 void
228 PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
229 {
230 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
232 // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
233 Geom::Point p_snapped = snap_knot_position(p);
235 Geom::Point knot_relpos(pattern_width(pat), 0);
236 Geom::Point xy_knot_relpos(0, -pattern_height(pat));
237 Geom::Point transform_origin = xy_knot_relpos * pat->patternTransform;
239 // do the scaling in pattern coordinate space
240 Geom::Point oldp = knot_relpos - xy_knot_relpos;
241 Geom::Point newp = p_snapped * pat->patternTransform.inverse() - xy_knot_relpos;
243 if (Geom::are_near(newp.length(), 0)) return;
245 Geom::Scale s(1);
246 if (state & GDK_CONTROL_MASK) {
247 // uniform scaling
248 s = Geom::Scale(oldp * (newp.length() * oldp.length()));
249 } else {
250 s = Geom::Scale(newp[Geom::X] / oldp[Geom::X], newp[Geom::Y] / oldp[Geom::Y]);
251 }
253 Geom::Matrix scl = Geom::Matrix(Geom::Translate(-xy_knot_relpos))
254 * s
255 * Geom::Translate(xy_knot_relpos)
256 * pat->patternTransform;
257 sp_item_adjust_pattern(item, scl, true);
258 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
259 }
261 /*
262 Local Variables:
263 mode:c++
264 c-file-style:"stroustrup"
265 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
266 indent-tabs-mode:nil
267 fill-column:99
268 End:
269 */
270 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :