X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fconn-avoid-ref.cpp;h=fbceb4eb5467096df8a220045e8c4af010aa4ef3;hb=ef9d155a8149d4793471047b3836db719b9e0292;hp=0dbd8c730156e51648bd7843e484ed5caaea7ac0;hpb=6b15695578f07a3f72c4c9475c1a261a3021472a;p=inkscape.git diff --git a/src/conn-avoid-ref.cpp b/src/conn-avoid-ref.cpp index 0dbd8c730..fbceb4eb5 100644 --- a/src/conn-avoid-ref.cpp +++ b/src/conn-avoid-ref.cpp @@ -15,13 +15,21 @@ #include "conn-avoid-ref.h" #include "libnr/nr-rect-ops.h" #include "libavoid/polyutil.h" -#include "libavoid/incremental.h" +#include "libavoid/router.h" +#include "libavoid/connector.h" #include "xml/simple-node.cpp" #include "document.h" +#include "prefs-utils.h" +#include "desktop.h" +#include "desktop-handles.h" +#include "sp-namedview.h" +#include "inkscape.h" + + +using Avoid::Router; static Avoid::Polygn avoid_item_poly(SPItem const *item); -static void avoid_item_move(NR::Matrix const *mp, SPItem *moved_item); SPAvoidRef::SPAvoidRef(SPItem *spitem) @@ -38,9 +46,10 @@ SPAvoidRef::~SPAvoidRef() { _transformed_connection.disconnect(); if (shapeRef) { + Router *router = shapeRef->router(); // shapeRef is finalised by delShape, // so no memory is lost here. - Avoid::delShape(shapeRef); + router->delShape(shapeRef); shapeRef = NULL; } } @@ -61,11 +70,28 @@ void SPAvoidRef::setAvoid(char const *value) void SPAvoidRef::handleSettingChange(void) { + SPDesktop *desktop = inkscape_active_desktop(); + if (desktop == NULL) { + return; + } + if (sp_desktop_document(desktop) != item->document) { + // We don't want to go any further if the active desktop's document + // isn't the same as the document that this item is part of. This + // case can happen if a new document is loaded from the file chooser + // or via the recent file menu. In this case, we can end up here + // as a rersult of a sp_document_ensure_up_to_date performed on a + // document not yet attached to the active desktop. + return; + } + if (new_setting == setting) { // Don't need to make any changes return; } + setting = new_setting; + Router *router = item->document->router; + _transformed_connection.disconnect(); if (new_setting) { _transformed_connection = item->connectTransformed( @@ -79,10 +105,10 @@ void SPAvoidRef::handleSettingChange(void) // Get a unique ID for the item. GQuark itemID = g_quark_from_string(id); - shapeRef = new Avoid::ShapeRef(itemID, poly); + shapeRef = new Avoid::ShapeRef(router, itemID, poly); Avoid::freePoly(poly); - Avoid::addShape(shapeRef); + router->addShape(shapeRef); } } else @@ -91,15 +117,65 @@ void SPAvoidRef::handleSettingChange(void) // shapeRef is finalised by delShape, // so no memory is lost here. - Avoid::delShape(shapeRef); + router->delShape(shapeRef); shapeRef = NULL; } - setting = new_setting; +} + + +GSList *SPAvoidRef::getAttachedShapes(const unsigned int type) +{ + GSList *list = NULL; + + Avoid::IntList shapes; + GQuark shapeId = g_quark_from_string(item->id); + item->document->router->attachedShapes(shapes, shapeId, type); + + Avoid::IntList::iterator finish = shapes.end(); + for (Avoid::IntList::iterator i = shapes.begin(); i != finish; ++i) { + const gchar *connId = g_quark_to_string(*i); + SPObject *obj = item->document->getObjectById(connId); + if (obj == NULL) { + g_warning("getAttachedShapes: Object with id=\"%s\" is not " + "found. Skipping.", connId); + continue; + } + SPItem *shapeItem = SP_ITEM(obj); + list = g_slist_prepend(list, shapeItem); + } + return list; +} + + +GSList *SPAvoidRef::getAttachedConnectors(const unsigned int type) +{ + GSList *list = NULL; + + Avoid::IntList conns; + GQuark shapeId = g_quark_from_string(item->id); + item->document->router->attachedConns(conns, shapeId, type); + + Avoid::IntList::iterator finish = conns.end(); + for (Avoid::IntList::iterator i = conns.begin(); i != finish; ++i) { + const gchar *connId = g_quark_to_string(*i); + SPObject *obj = item->document->getObjectById(connId); + if (obj == NULL) { + g_warning("getAttachedConnectors: Object with id=\"%s\" is not " + "found. Skipping.", connId); + continue; + } + SPItem *connItem = SP_ITEM(obj); + list = g_slist_prepend(list, connItem); + } + return list; } static Avoid::Polygn avoid_item_poly(SPItem const *item) { + SPDesktop *desktop = inkscape_active_desktop(); + g_assert(desktop != NULL); + Avoid::Polygn poly; // TODO: The right way to do this is to return the convex hull of @@ -116,9 +192,12 @@ static Avoid::Polygn avoid_item_poly(SPItem const *item) sp_document_ensure_up_to_date(item->document); NR::Rect rHull = item->invokeBbox(sp_item_i2doc_affine(item)); - + + + double spacing = desktop->namedview->connector_spacing; + // Add a little buffer around the edge of each object. - NR::Rect rExpandedHull = NR::expand(rHull, -10.0); + NR::Rect rExpandedHull = NR::expand(rHull, -spacing); poly = Avoid::newPoly(4); for (unsigned n = 0; n < 4; ++n) { @@ -151,19 +230,68 @@ static Avoid::Polygn avoid_item_poly(SPItem const *item) } -static void avoid_item_move(NR::Matrix const *mp, SPItem *moved_item) +GSList *get_avoided_items(GSList *list, SPObject *from, SPDesktop *desktop, + bool initialised) +{ + for (SPObject *child = sp_object_first_child(SP_OBJECT(from)) ; + child != NULL; child = SP_OBJECT_NEXT(child) ) { + if (SP_IS_ITEM(child) && + !desktop->isLayer(SP_ITEM(child)) && + !SP_ITEM(child)->isLocked() && + !desktop->itemIsHidden(SP_ITEM(child)) && + (!initialised || SP_ITEM(child)->avoidRef->shapeRef) + ) + { + list = g_slist_prepend (list, SP_ITEM(child)); + } + + if (SP_IS_ITEM(child) && desktop->isLayer(SP_ITEM(child))) { + list = get_avoided_items(list, child, desktop, initialised); + } + } + + return list; +} + + +void avoid_item_move(NR::Matrix const *mp, SPItem *moved_item) { Avoid::ShapeRef *shapeRef = moved_item->avoidRef->shapeRef; g_assert(shapeRef); + Router *router = moved_item->document->router; Avoid::Polygn poly = avoid_item_poly(moved_item); if (poly.pn > 0) { - // moveShape actually destroys the old shapeRef and returns a new one. - moved_item->avoidRef->shapeRef = Avoid::moveShape(shapeRef, &poly); + router->moveShape(shapeRef, &poly); Avoid::freePoly(poly); } } - + + +void init_avoided_shape_geometry(SPDesktop *desktop) +{ + // Don't count this as changes to the document, + // it is basically just llate initialisation. + SPDocument *document = sp_desktop_document(desktop); + bool saved = sp_document_get_undo_sensitive(document); + sp_document_set_undo_sensitive(document, false); + + bool initialised = false; + GSList *items = get_avoided_items(NULL, desktop->currentRoot(), desktop, + initialised); + + for ( GSList const *iter = items ; iter != NULL ; iter = iter->next ) { + SPItem *item = reinterpret_cast(iter->data); + item->avoidRef->handleSettingChange(); + } + + if (items) { + g_slist_free(items); + } + sp_document_set_undo_sensitive(document, saved); +} + + /* Local Variables: mode:c++