X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fgraphlayout%2Fgraphlayout.cpp;h=81ea590598397ba993e4163abb4b6e456dcad680;hb=42b53baed61b6b06f33ecfa440a747382eb350df;hp=4031456369fcb74c0f37f655a9ca79e037622d78;hpb=d1c7cc7eb10f1bf5756d299e4a1e4f3060bfed8f;p=inkscape.git diff --git a/src/graphlayout/graphlayout.cpp b/src/graphlayout/graphlayout.cpp index 403145636..81ea59059 100644 --- a/src/graphlayout/graphlayout.cpp +++ b/src/graphlayout/graphlayout.cpp @@ -1,21 +1,27 @@ -/** \file - * Interface between Inkscape code (SPItem) and graphlayout functions. +/** @file + * @brief Interface between Inkscape code (SPItem) and graphlayout functions. */ /* -* Authors: -* Tim Dwyer -* -* Copyright (C) 2005 Authors -* -* Released under GNU GPL. Read the file 'COPYING' for more information. -*/ + * Authors: + * Tim Dwyer + * + * Copyright (C) 2005 Authors + * + * Released under GNU GPL. Read the file 'COPYING' for more information. + */ + #include #include #include #include #include +#include +#include #include +#include "desktop.h" +#include "inkscape.h" +#include "sp-namedview.h" #include "util/glib-list-iterators.h" #include "graphlayout/graphlayout.h" #include "sp-path.h" @@ -25,15 +31,15 @@ #include "style.h" #include "conn-avoid-ref.h" #include "libavoid/connector.h" +#include "libavoid/router.h" #include "libavoid/geomtypes.h" #include "libcola/cola.h" #include "libvpsc/generate-constraints.h" -#include "prefs-utils.h" +#include "preferences.h" using namespace std; using namespace cola; using namespace vpsc; - /** * Returns true if item is a connector */ @@ -45,6 +51,36 @@ bool isConnector(SPItem const *const i) { return path && path->connEndPair.isAutoRoutingConn(); } +struct CheckProgress : TestConvergence { + CheckProgress(double d,unsigned i,list& + selected,vector& rs,map& nodelookup) : + TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {} + bool operator()(double new_stress, double* X, double* Y) { + /* This is where, if we wanted to animate the layout, we would need to update + * the positions of all objects and redraw the canvas and maybe sleep a bit + cout << "stress="<getDouble("/tools/connector/length", 100.0); double directed_edge_height_modifier = 1.0; - gchar const *directed_str = NULL, *overlaps_str = NULL; - directed_str = prefs_get_string_attribute("tools.connector", - "directedlayout"); - overlaps_str = prefs_get_string_attribute("tools.connector", - "avoidoverlaplayout"); - bool avoid_overlaps = false; - bool directed = false; - if (directed_str && !strcmp(directed_str, "true")) { - directed = true; - } - if (overlaps_str && !strcmp(overlaps_str, "true")) { - avoid_overlaps = true; - } + + bool directed = prefs->getBool("/tools/connector/directedlayout"); + bool avoid_overlaps = prefs->getBool("/tools/connector/avoidoverlaplayout"); for (list::iterator i(selected.begin()); i != selected.end(); ++i) { SPItem *iu=*i; - unsigned u=nodelookup[iu->id]; - GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom); - list connectors; - - connectors.insert >(connectors.end(),nlist,NULL); - for (list::iterator j(connectors.begin()); - j != connectors.end(); - ++j) { - SPItem *conn=*j; - SPItem *iv; - SPItem *items[2]; - assert(isConnector(conn)); - SP_PATH(conn)->connEndPair.getAttachedItems(items); - if(items[0]==iu) { - iv=items[1]; - } else { - iv=items[0]; - } - - // What do we do if iv not in nodelookup?!?! - map::iterator v_pair=nodelookup.find(iv->id); - if(v_pair!=nodelookup.end()) { - unsigned v=v_pair->second; - //cout << "Edge: (" << u <<","<style->marker[SP_MARKER_LOC_END].set) { - if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) { - scy.push_back(new SimpleConstraint(v, u, + map::iterator i=nodelookup.find(iu->id); + if(i==nodelookup.end()) { + continue; + } + unsigned u=i->second; + GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom); + list connectors; + + connectors.insert >(connectors.end(),nlist,NULL); + for (list::iterator j(connectors.begin()); + j != connectors.end(); + ++j) { + SPItem *conn=*j; + SPItem *iv; + SPItem *items[2]; + assert(isConnector(conn)); + SP_PATH(conn)->connEndPair.getAttachedItems(items); + if(items[0]==iu) { + iv=items[1]; + } else { + iv=items[0]; + } + + if (iv == NULL) { + // The connector is not attached to anything at the + // other end so we should just ignore it. + continue; + } + + // If iv not in nodelookup we again treat the connector + // as disconnected and continue + map::iterator v_pair=nodelookup.find(iv->id); + if(v_pair!=nodelookup.end()) { + unsigned v=v_pair->second; + //cout << "Edge: (" << u <<","<style->marker[SP_MARKER_LOC_END].set) { + if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) { + scy.push_back(new SimpleConstraint(v, u, (ideal_connector_length * directed_edge_height_modifier))); - } - } - } - } + } + } + } + } if(nlist) { g_slist_free(nlist); } } - //double width=maxX-minX; - //double height=maxY-minY; const unsigned E = es.size(); double eweights[E]; fill(eweights,eweights+E,1); - - ConstrainedMajorizationLayout alg(rs,es,eweights,ideal_connector_length); - alg.setupConstraints(NULL,NULL,avoid_overlaps, - NULL,NULL,NULL,&scy,NULL,NULL); - alg.run(); + vector cs; + connectedComponents(rs,es,scx,scy,cs); + for(unsigned i=0;iedges.size()<2) continue; + CheckProgress test(0.0001,100,selected,rs,nodelookup); + ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test); + alg.setupConstraints(NULL,NULL,avoid_overlaps, + NULL,NULL,&c->scx,&c->scy,NULL,NULL); + alg.run(); + } + separateComponents(cs); for (list::iterator it(selected.begin()); it != selected.end(); @@ -171,13 +225,38 @@ void graphlayout(GSList const *const items) { { SPItem *u=*it; if(!isConnector(u)) { - Rectangle* r=rs[nodelookup[u->id]]; - NR::Rect const item_box(sp_item_bbox_desktop(u)); - NR::Point const curr(item_box.midpoint()); - NR::Point const dest(r->getCentreX(),r->getCentreY()); - sp_item_move_rel(u, NR::translate(dest - curr)); + map::iterator i=nodelookup.find(u->id); + if(i!=nodelookup.end()) { + Rectangle* r=rs[i->second]; + Geom::OptRect item_box(sp_item_bbox_desktop(u)); + if(item_box) { + Geom::Point const curr(item_box->midpoint()); + Geom::Point const dest(r->getCentreX(),r->getCentreY()); + sp_item_move_rel(u, Geom::Translate(dest - curr)); + } + } } } + for(unsigned i=0;i