Code

Formerly static function used for snapping is now a private member of KnotHolderEntity
[inkscape.git] / src / knot-holder-entity.cpp
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 "prefs-utils.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>
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     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
81     NR::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 NR::Point
89 KnotHolderEntity::snap_knot_position(NR::Point const &p)
90 {
91     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine(item)));
92     NR::Point s = p * i2d;
93     SnapManager &m = desktop->namedview->snap_manager;
94     m.setup(desktop, item);
95     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, s);
96     return s * i2d.inverse();
97 }
100 /* Pattern manipulation */
102 static gdouble sp_pattern_extract_theta(SPPattern *pat, NR::Point scale)
104     gdouble theta = asin(pat->patternTransform[1] / scale[NR::X]);
105     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
106     return theta;
109 static NR::Point sp_pattern_extract_scale(SPPattern *pat)
111     Geom::Matrix transf = to_2geom(pat->patternTransform);
112     return NR::Point( transf.expansionX(), transf.expansionY() );
115 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
117     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
120 void
121 PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
123     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
125     NR::Point p_snapped = snap_knot_position(p);
127     if ( state & GDK_CONTROL_MASK ) {
128         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
129             p_snapped[NR::Y] = origin[NR::Y];
130         } else {
131             p_snapped[NR::X] = origin[NR::X];
132         }
133     }
135     if (state)  {
136         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
137         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
138     }
140     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
143 NR::Point
144 PatternKnotHolderEntityXY::knot_get()
146     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
147     return sp_pattern_extract_trans(pat);
150 NR::Point
151 PatternKnotHolderEntityAngle::knot_get()
153     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
155     gdouble x = (pattern_width(pat));
156     gdouble y = 0;
157     NR::Point delta = NR::Point(x,y);
158     NR::Point scale = sp_pattern_extract_scale(pat);
159     gdouble theta = sp_pattern_extract_theta(pat, scale);
160     delta = delta * NR::Matrix(NR::scale(scale))*NR::Matrix(NR::rotate(theta));
161     delta = delta + sp_pattern_extract_trans(pat);
162     return delta;
165 void
166 PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
168     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
170     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
172     // get the angle from pattern 0,0 to the cursor pos
173     NR::Point delta = p - sp_pattern_extract_trans(pat);
174     gdouble theta = atan2(delta);
176     if ( state & GDK_CONTROL_MASK ) {
177         theta = sp_round(theta, M_PI/snaps);
178     }
180     // get the scale from the current transform so we can keep it.
181     NR::Point scl = sp_pattern_extract_scale(pat);
182     NR::Matrix rot = NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
183     NR::Point const t = sp_pattern_extract_trans(pat);
184     rot[4] = t[NR::X];
185     rot[5] = t[NR::Y];
186     sp_item_adjust_pattern(item, rot, true);
187     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
190 void
191 PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
193     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
195     NR::Point p_snapped = snap_knot_position(p);
197     // get angle from current transform, (need get current scale first to calculate angle)
198     NR::Point oldscale = sp_pattern_extract_scale(pat);
199     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
201     // Get the new scale from the position of the knotholder
202     NR::Point d = p_snapped - sp_pattern_extract_trans(pat);
203     d *= NR::Matrix(NR::rotate(-theta));
204     gdouble pat_x = pattern_width(pat);
205     gdouble pat_y = pattern_height(pat);
206     NR::Point scl (d[NR::X] / pat_x, d[NR::Y] / pat_y);
208     NR::Matrix rot =  NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
210     NR::Point const t = sp_pattern_extract_trans(pat);
211     rot[4] = t[NR::X];
212     rot[5] = t[NR::Y];
213     sp_item_adjust_pattern(item, rot, true);
214     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
218 NR::Point
219 PatternKnotHolderEntityScale::knot_get()
221     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
223     gdouble x = pattern_width(pat);
224     gdouble y = pattern_height(pat);
225     NR::Point delta = NR::Point(x,y);
226     NR::Matrix a = pat->patternTransform;
227     a[4] = 0;
228     a[5] = 0;
229     delta = delta * a;
230     delta = delta + sp_pattern_extract_trans(pat);
231     return delta;
234 /*
235   Local Variables:
236   mode:c++
237   c-file-style:"stroustrup"
238   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239   indent-tabs-mode:nil
240   fill-column:99
241   End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :