Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / knot-holder-entity.cpp
1 /** \file
2  * KnotHolderEntity definition.
3  *
4  * Authors:
5  *   Mitsuru Oka <oka326@parkcity.ne.jp>
6  *   Maximilian Albert <maximilian.albert@gmail.com>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 1999-2001 Lauris Kaplinski
10  * Copyright (C) 2000-2001 Ximian, Inc.
11  * Copyright (C) 2001 Mitsuru Oka
12  * Copyright (C) 2004 Monash University
13  * Copyright (C) 2008 Maximilian Albert
14  *
15  * Released under GNU GPL
16  */
18 #include "knotholder.h"
19 #include "sp-item.h"
20 #include "style.h"
21 #include "preferences.h"
22 #include "macros.h"
23 #include <libnr/nr-matrix-ops.h>
24 #include "sp-pattern.h"
25 #include "snap.h"
26 #include "desktop.h"
27 #include "sp-namedview.h"
28 #include <2geom/matrix.h>
29 #include <2geom/transforms.h>
31 int KnotHolderEntity::counter = 0;
33 void
34 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
35                          SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
36 {
37     knot = sp_knot_new(desktop, tip);
39     this->parent_holder = parent;
40     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
41     this->desktop = desktop;
43     my_counter = KnotHolderEntity::counter++;
45     g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
46     g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
48     knot->fill [SP_KNOT_STATE_NORMAL] = color;
49     g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
51     update_knot();
52     sp_knot_show(knot);
54     _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
55     _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
56     _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
57 }
60 KnotHolderEntity::~KnotHolderEntity()
61 {
62     _moved_connection.disconnect();
63     _click_connection.disconnect();
64     _ungrabbed_connection.disconnect();
66     /* unref should call destroy */
67     if (knot) {
68         g_object_unref(knot);
69     } else {
70         // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
71         //        If so, it will likely be fixed with upcoming refactoring efforts.
72         g_return_if_fail(knot);
73     }
74 }
76 void
77 KnotHolderEntity::update_knot()
78 {
79     Geom::Matrix const i2d(item->i2d_affine());
81     Geom::Point dp(knot_get() * i2d);
83     _moved_connection.block();
84     sp_knot_set_position(knot, dp, SP_KNOT_STATE_NORMAL);
85     _moved_connection.unblock();
86 }
88 Geom::Point
89 KnotHolderEntity::snap_knot_position(Geom::Point const &p)
90 {
91     Geom::Matrix const i2d (item->i2d_affine());
92     Geom::Point s = p * i2d;
94     SnapManager &m = desktop->namedview->snap_manager;
95     m.setup(desktop, true, item);
96     m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
97     m.unSetup();
99     return s * i2d.inverse();
102 Geom::Point
103 KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::SnapConstraint const &constraint)
105     Geom::Matrix const i2d (item->i2d_affine());
106     Geom::Point s = p * i2d;
108     SnapManager &m = desktop->namedview->snap_manager;
109     m.setup(desktop, true, item);
111     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
112     // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line.
113     // This way the constraint is already enforced, no need to worry about that later on
114     Inkscape::Snapper::SnapConstraint transformed_constraint = Inkscape::Snapper::SnapConstraint(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
115     m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint);
116     m.unSetup();
118     return s * i2d.inverse();
122 /* Pattern manipulation */
124 /*  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. */
126 static gdouble sp_pattern_extract_theta(SPPattern *pat)
128     Geom::Matrix transf = pat->patternTransform;
129     return Geom::atan2(transf.xAxis());
132 static Geom::Point sp_pattern_extract_scale(SPPattern *pat)
134     Geom::Matrix transf = pat->patternTransform;
135     return Geom::Point( transf.expansionX(), transf.expansionY() );
138 static Geom::Point sp_pattern_extract_trans(SPPattern const *pat)
140     return Geom::Point(pat->patternTransform[4], pat->patternTransform[5]);
143 void
144 PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &origin, guint state)
146     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
148     // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
149     Geom::Point p_snapped = snap_knot_position(p);
151     if ( state & GDK_CONTROL_MASK ) {
152         if (fabs((p - origin)[Geom::X]) > fabs((p - origin)[Geom::Y])) {
153             p_snapped[Geom::Y] = origin[Geom::Y];
154         } else {
155             p_snapped[Geom::X] = origin[Geom::X];
156         }
157     }
159     if (state)  {
160         Geom::Point const q = p_snapped - sp_pattern_extract_trans(pat);
161         item->adjust_pattern(Geom::Matrix(Geom::Translate(q)));
162     }
164     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
167 Geom::Point
168 PatternKnotHolderEntityXY::knot_get()
170     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
171     return sp_pattern_extract_trans(pat);
174 Geom::Point
175 PatternKnotHolderEntityAngle::knot_get()
177     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
179     gdouble x = (pattern_width(pat));
180     gdouble y = 0;
181     Geom::Point delta = Geom::Point(x,y);
182     Geom::Point scale = sp_pattern_extract_scale(pat);
183     gdouble theta = sp_pattern_extract_theta(pat);
184     delta = delta * Geom::Matrix(Geom::Scale(scale))*Geom::Matrix(Geom::Rotate(theta));
185     delta = delta + sp_pattern_extract_trans(pat);
186     return delta;
189 void
190 PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
192     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
193     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
195     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
197     // get the angle from pattern 0,0 to the cursor pos
198     Geom::Point delta = p - sp_pattern_extract_trans(pat);
199     gdouble theta = atan2(delta);
201     if ( state & GDK_CONTROL_MASK ) {
202         theta = sp_round(theta, M_PI/snaps);
203     }
205     // get the scale from the current transform so we can keep it.
206     Geom::Point scl = sp_pattern_extract_scale(pat);
207     Geom::Matrix rot = Geom::Matrix(Geom::Scale(scl)) * Geom::Matrix(Geom::Rotate(theta));
208     Geom::Point const t = sp_pattern_extract_trans(pat);
209     rot[4] = t[Geom::X];
210     rot[5] = t[Geom::Y];
211     item->adjust_pattern(rot, true);
212     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
215 void
216 PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
218     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
220     // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
221     Geom::Point p_snapped = snap_knot_position(p);
223     // get angle from current transform
224     gdouble theta = sp_pattern_extract_theta(pat);
226     // Get the new scale from the position of the knotholder
227     Geom::Point d = p_snapped - sp_pattern_extract_trans(pat);
228     gdouble pat_x = pattern_width(pat);
229     gdouble pat_y = pattern_height(pat);
230     Geom::Scale scl(1);
231     if ( state & GDK_CONTROL_MASK ) {
232         // if ctrl is pressed: use 1:1 scaling
233         gdouble pat_h = hypot(pat_x, pat_y);
234         scl = Geom::Scale(d.length() / pat_h);
235     } else {
236         d *= Geom::Rotate(-theta);
237         scl = Geom::Scale(d[Geom::X] / pat_x, d[Geom::Y] / pat_y);
238     }
240     Geom::Matrix rot = (Geom::Matrix)scl * Geom::Rotate(theta);
242     Geom::Point const t = sp_pattern_extract_trans(pat);
243     rot[4] = t[Geom::X];
244     rot[5] = t[Geom::Y];
245     item->adjust_pattern(rot, true);
246     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
250 Geom::Point
251 PatternKnotHolderEntityScale::knot_get()
253     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
255     gdouble x = pattern_width(pat);
256     gdouble y = pattern_height(pat);
257     Geom::Point delta = Geom::Point(x,y);
258     Geom::Matrix a = pat->patternTransform;
259     a[4] = 0;
260     a[5] = 0;
261     delta = delta * a;
262     delta = delta + sp_pattern_extract_trans(pat);
263     return delta;
266 /*
267   Local Variables:
268   mode:c++
269   c-file-style:"stroustrup"
270   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
271   indent-tabs-mode:nil
272   fill-column:99
273   End:
274 */
275 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :