1 /*
2 * A class for handling shape interaction with libavoid.
3 *
4 * Authors:
5 * Michael Wybrow <mjwybrow@users.sourceforge.net>
6 *
7 * Copyright (C) 2005 Michael Wybrow
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
14 #include "sp-item.h"
15 #include "conn-avoid-ref.h"
16 #include "libnr/nr-rect-ops.h"
17 #include "libavoid/polyutil.h"
18 #include "libavoid/incremental.h"
19 #include "libavoid/connector.h"
20 #include "xml/simple-node.cpp"
21 #include "document.h"
22 #include "prefs-utils.h"
24 #include "desktop.h"
25 #include "desktop-handles.h"
26 #include "sp-namedview.h"
27 #include "inkscape.h"
30 static Avoid::Polygn avoid_item_poly(SPItem const *item);
33 SPAvoidRef::SPAvoidRef(SPItem *spitem)
34 : shapeRef(NULL)
35 , item(spitem)
36 , setting(false)
37 , new_setting(false)
38 , _transformed_connection()
39 {
40 }
43 SPAvoidRef::~SPAvoidRef()
44 {
45 _transformed_connection.disconnect();
46 if (shapeRef) {
47 // shapeRef is finalised by delShape,
48 // so no memory is lost here.
49 Avoid::delShape(shapeRef);
50 shapeRef = NULL;
51 }
52 }
55 void SPAvoidRef::setAvoid(char const *value)
56 {
57 if (SP_OBJECT_IS_CLONED(item)) {
58 // Don't keep avoidance information for cloned objects.
59 return;
60 }
61 new_setting = false;
62 if (value && (strcmp(value, "true") == 0)) {
63 new_setting = true;
64 }
65 }
68 void SPAvoidRef::handleSettingChange(void)
69 {
70 SPDesktop *desktop = inkscape_active_desktop();
71 if (desktop == NULL) {
72 return;
73 }
75 if (new_setting == setting) {
76 // Don't need to make any changes
77 return;
78 }
80 _transformed_connection.disconnect();
81 if (new_setting) {
82 _transformed_connection = item->connectTransformed(
83 sigc::ptr_fun(&avoid_item_move));
85 Avoid::Polygn poly = avoid_item_poly(item);
86 if (poly.pn > 0) {
87 const char *id = SP_OBJECT_REPR(item)->attribute("id");
88 g_assert(id != NULL);
90 // Get a unique ID for the item.
91 GQuark itemID = g_quark_from_string(id);
93 shapeRef = new Avoid::ShapeRef(itemID, poly);
94 Avoid::freePoly(poly);
96 Avoid::addShape(shapeRef);
97 }
98 }
99 else
100 {
101 g_assert(shapeRef);
103 // shapeRef is finalised by delShape,
104 // so no memory is lost here.
105 Avoid::delShape(shapeRef);
106 shapeRef = NULL;
107 }
108 setting = new_setting;
109 }
112 GSList *SPAvoidRef::getAttachedConnectors(const unsigned int type)
113 {
114 GSList *list = NULL;
116 Avoid::IntList conns;
117 GQuark shapeId = g_quark_from_string(item->id);
118 Avoid::attachedToShape(conns, shapeId, type);
120 Avoid::IntList::iterator finish = conns.end();
121 for (Avoid::IntList::iterator i = conns.begin(); i != finish; ++i) {
122 const gchar *connId = g_quark_to_string(*i);
123 SPItem *citem = SP_ITEM(item->document->getObjectById(connId));
124 g_assert(citem != NULL);
125 list = g_slist_prepend(list, citem);
126 }
127 return list;
128 }
131 static Avoid::Polygn avoid_item_poly(SPItem const *item)
132 {
133 SPDesktop *desktop = inkscape_active_desktop();
134 g_assert(desktop != NULL);
136 Avoid::Polygn poly;
138 // TODO: The right way to do this is to return the convex hull of
139 // the object, or an approximation in the case of a rounded
140 // object. Specific SPItems will need to have a new
141 // function that returns points for the convex hull.
142 // For some objects it is enough to feed the snappoints to
143 // some convex hull code, though not NR::ConvexHull as this
144 // only keeps the bounding box of the convex hull currently.
146 // TODO: SPItem::invokeBbox gives the wrong result for some objects
147 // that have internal representations that are updated later
148 // by the sp_*_update functions, e.g., text.
149 sp_document_ensure_up_to_date(item->document);
151 NR::Rect rHull = item->invokeBbox(sp_item_i2doc_affine(item));
154 double spacing = desktop->namedview->connector_spacing;
156 // Add a little buffer around the edge of each object.
157 NR::Rect rExpandedHull = NR::expand(rHull, -spacing);
158 poly = Avoid::newPoly(4);
160 for (unsigned n = 0; n < 4; ++n) {
161 // TODO: I think the winding order in libavoid or inkscape might
162 // be backwards, probably due to the inverse y co-ordinates
163 // used for the screen. The '3 - n' reverses the order.
164 /* On "correct" winding order: Winding order of NR::Rect::corner is in a positive
165 * direction, like libart. "Positive direction" means the same as in most of Inkscape and
166 * SVG: if you visualize y as increasing upwards, as is the convention in mathematics, then
167 * positive angle is visualized as anticlockwise, as in mathematics; so if you visualize y
168 * as increasing downwards, as is common outside of mathematics, then positive angle
169 * direction is visualized as clockwise, as is common outside of mathematics. This
170 * convention makes it easier mix pure mathematics code with graphics code: the important
171 * thing when mixing code is that the number values stored in variables (representing y
172 * coordinate, angle) match up; variables store numbers, not visualized positions, and the
173 * programmer is free to switch between visualizations when thinking about a given piece of
174 * code.
175 *
176 * MathWorld, libart and NR::Rect::corner all seem to take positive winding (i.e. winding
177 * that yields +1 winding number inside a simple closed shape) to mean winding in a
178 * positive angle. This, together with the observation that variables store numbers rather
179 * than positions, suggests that NR::Rect::corner uses the right direction.
180 */
181 NR::Point hullPoint = rExpandedHull.corner(3 - n);
182 poly.ps[n].x = hullPoint[NR::X];
183 poly.ps[n].y = hullPoint[NR::Y];
184 }
186 return poly;
187 }
190 GSList *get_avoided_items(GSList *list, SPObject *from, SPDesktop *desktop,
191 bool initialised)
192 {
193 for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ;
194 child != NULL; child = SP_OBJECT_NEXT(child) ) {
195 if (SP_IS_ITEM(child) &&
196 !desktop->isLayer(SP_ITEM(child)) &&
197 !SP_ITEM(child)->isLocked() &&
198 !desktop->itemIsHidden(SP_ITEM(child)) &&
199 (!initialised || SP_ITEM(child)->avoidRef->shapeRef)
200 )
201 {
202 list = g_slist_prepend (list, SP_ITEM(child));
203 }
205 if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) {
206 list = get_avoided_items(list, child, desktop, initialised);
207 }
208 }
210 return list;
211 }
214 void avoid_item_move(NR::Matrix const *mp, SPItem *moved_item)
215 {
216 Avoid::ShapeRef *shapeRef = moved_item->avoidRef->shapeRef;
217 g_assert(shapeRef);
219 Avoid::Polygn poly = avoid_item_poly(moved_item);
220 if (poly.pn > 0) {
221 // moveShape actually destroys the old shapeRef and returns a new one.
222 moved_item->avoidRef->shapeRef = Avoid::moveShape(shapeRef, &poly);
223 Avoid::freePoly(poly);
224 }
225 }
228 void init_avoided_shape_geometry(SPDesktop *desktop)
229 {
230 // Don't count this as changes to the document,
231 // it is basically just llate initialisation.
232 SPDocument *document = SP_DT_DOCUMENT(desktop);
233 gboolean saved = sp_document_get_undo_sensitive(document);
234 sp_document_set_undo_sensitive(document, FALSE);
236 bool initialised = false;
237 GSList *items = get_avoided_items(NULL, desktop->currentRoot(), desktop,
238 initialised);
240 for ( GSList const *iter = items ; iter != NULL ; iter = iter->next ) {
241 SPItem *item = reinterpret_cast<SPItem *>(iter->data);
242 item->avoidRef->handleSettingChange();
243 }
245 if (items) {
246 g_slist_free(items);
247 }
248 sp_document_set_undo_sensitive(document, saved);
249 }
252 /*
253 Local Variables:
254 mode:c++
255 c-file-style:"stroustrup"
256 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
257 indent-tabs-mode:nil
258 fill-column:99
259 End:
260 */
261 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :