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;
94 SnapManager &m = desktop->namedview->snap_manager;
95 m.setup(desktop, true, item);
96 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, s, Inkscape::SNAPSOURCE_HANDLE);
97 return s * i2d.inverse();
98 }
100 Geom::Point
101 KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::ConstraintLine const &constraint)
102 {
103 Geom::Matrix const i2d (sp_item_i2d_affine(item));
104 Geom::Point s = p * i2d;
105 Inkscape::Snapper::ConstraintLine transformed_constraint = Inkscape::Snapper::ConstraintLine(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
106 SnapManager &m = desktop->namedview->snap_manager;
107 m.setup(desktop, true, item);
108 m.constrainedSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, s, Inkscape::SNAPSOURCE_HANDLE, transformed_constraint);
109 return s * i2d.inverse();
110 }
113 /* Pattern manipulation */
115 /* 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. */
117 static gdouble sp_pattern_extract_theta(SPPattern *pat)
118 {
119 Geom::Matrix transf = pat->patternTransform;
120 return Geom::atan2(transf.xAxis());
121 }
123 static Geom::Point sp_pattern_extract_scale(SPPattern *pat)
124 {
125 Geom::Matrix transf = pat->patternTransform;
126 return Geom::Point( transf.expansionX(), transf.expansionY() );
127 }
129 static Geom::Point sp_pattern_extract_trans(SPPattern const *pat)
130 {
131 return Geom::Point(pat->patternTransform[4], pat->patternTransform[5]);
132 }
134 void
135 PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &origin, guint state)
136 {
137 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
139 // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
140 Geom::Point p_snapped = snap_knot_position(p);
142 if ( state & GDK_CONTROL_MASK ) {
143 if (fabs((p - origin)[Geom::X]) > fabs((p - origin)[Geom::Y])) {
144 p_snapped[Geom::Y] = origin[Geom::Y];
145 } else {
146 p_snapped[Geom::X] = origin[Geom::X];
147 }
148 }
150 if (state) {
151 Geom::Point const q = p_snapped - sp_pattern_extract_trans(pat);
152 sp_item_adjust_pattern(item, Geom::Matrix(Geom::Translate(q)));
153 }
155 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
156 }
158 Geom::Point
159 PatternKnotHolderEntityXY::knot_get()
160 {
161 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
162 return sp_pattern_extract_trans(pat);
163 }
165 Geom::Point
166 PatternKnotHolderEntityAngle::knot_get()
167 {
168 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
170 gdouble x = (pattern_width(pat));
171 gdouble y = 0;
172 Geom::Point delta = Geom::Point(x,y);
173 Geom::Point scale = sp_pattern_extract_scale(pat);
174 gdouble theta = sp_pattern_extract_theta(pat);
175 delta = delta * Geom::Matrix(Geom::Scale(scale))*Geom::Matrix(Geom::Rotate(theta));
176 delta = delta + sp_pattern_extract_trans(pat);
177 return delta;
178 }
180 void
181 PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
182 {
183 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
184 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
186 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
188 // get the angle from pattern 0,0 to the cursor pos
189 Geom::Point delta = p - sp_pattern_extract_trans(pat);
190 gdouble theta = atan2(delta);
192 if ( state & GDK_CONTROL_MASK ) {
193 theta = sp_round(theta, M_PI/snaps);
194 }
196 // get the scale from the current transform so we can keep it.
197 Geom::Point scl = sp_pattern_extract_scale(pat);
198 Geom::Matrix rot = Geom::Matrix(Geom::Scale(scl)) * Geom::Matrix(Geom::Rotate(theta));
199 Geom::Point const t = sp_pattern_extract_trans(pat);
200 rot[4] = t[Geom::X];
201 rot[5] = t[Geom::Y];
202 sp_item_adjust_pattern(item, rot, true);
203 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
204 }
206 void
207 PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
208 {
209 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
211 // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
212 Geom::Point p_snapped = snap_knot_position(p);
214 // get angle from current transform
215 gdouble theta = sp_pattern_extract_theta(pat);
217 // Get the new scale from the position of the knotholder
218 Geom::Point d = p_snapped - sp_pattern_extract_trans(pat);
219 gdouble pat_x = pattern_width(pat);
220 gdouble pat_y = pattern_height(pat);
221 Geom::Scale scl(1);
222 if ( state & GDK_CONTROL_MASK ) {
223 // if ctrl is pressed: use 1:1 scaling
224 gdouble pat_h = hypot(pat_x, pat_y);
225 scl = Geom::Scale(d.length() / pat_h);
226 } else {
227 d *= Geom::Rotate(-theta);
228 scl = Geom::Scale(d[Geom::X] / pat_x, d[Geom::Y] / pat_y);
229 }
231 Geom::Matrix rot = (Geom::Matrix)scl * Geom::Rotate(theta);
233 Geom::Point const t = sp_pattern_extract_trans(pat);
234 rot[4] = t[Geom::X];
235 rot[5] = t[Geom::Y];
236 sp_item_adjust_pattern(item, rot, true);
237 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
238 }
241 Geom::Point
242 PatternKnotHolderEntityScale::knot_get()
243 {
244 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
246 gdouble x = pattern_width(pat);
247 gdouble y = pattern_height(pat);
248 Geom::Point delta = Geom::Point(x,y);
249 Geom::Matrix a = pat->patternTransform;
250 a[4] = 0;
251 a[5] = 0;
252 delta = delta * a;
253 delta = delta + sp_pattern_extract_trans(pat);
254 return delta;
255 }
257 /*
258 Local Variables:
259 mode:c++
260 c-file-style:"stroustrup"
261 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
262 indent-tabs-mode:nil
263 fill-column:99
264 End:
265 */
266 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :