2
* @brief Interface between Inkscape code (SPItem) and graphlayout functions.
6
* Tim Dwyer <Tim.Dwyer@infotech.monash.edu.au>
8
* Copyright (C) 2005 Authors
10
* Released under GNU GPL. Read the file 'COPYING' for more information.
24
#include "sp-namedview.h"
25
#include "util/glib-list-iterators.h"
26
#include "graphlayout/graphlayout.h"
29
#include "sp-item-transform.h"
30
#include "sp-conn-end-pair.h"
32
#include "conn-avoid-ref.h"
33
#include "libavoid/connector.h"
34
#include "libavoid/geomtypes.h"
35
#include "libcola/cola.h"
36
#include "libvpsc/generate-constraints.h"
37
#include "preferences.h"
43
* Returns true if item is a connector
45
bool isConnector(SPItem const *const i) {
50
return path && path->connEndPair.isAutoRoutingConn();
53
struct CheckProgress : TestConvergence {
54
CheckProgress(double d,unsigned i,list<SPItem *>&
55
selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
56
TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
57
bool operator()(double new_stress, double* X, double* Y) {
58
/* This is where, if we wanted to animate the layout, we would need to update
59
* the positions of all objects and redraw the canvas and maybe sleep a bit
60
cout << "stress="<<new_stress<<endl;
61
cout << "x[0]="<<rs[0]->getMinX()<<endl;
62
for (list<SPItem *>::iterator it(selected.begin());
68
Rectangle* r=rs[nodelookup[u->id]];
69
Geom::Rect const item_box(sp_item_bbox_desktop(u));
70
Geom::Point const curr(item_box.midpoint());
71
Geom::Point const dest(r->getCentreX(),r->getCentreY());
72
sp_item_move_rel(u, Geom::Translate(dest - curr));
76
return TestConvergence::operator()(new_stress,X,Y);
78
list<SPItem *>& selected;
79
vector<Rectangle*>& rs;
80
map<string,unsigned>& nodelookup;
84
* Scans the items list and places those items that are
85
* not connectors in filtered
87
void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
88
for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
89
SPItem *item=SP_ITEM(i->data);
90
if(!isConnector(item)) {
91
filtered.push_back(item);
96
* Takes a list of inkscape items, extracts the graph defined by
97
* connectors between them, and uses graph layout techniques to find
100
void graphlayout(GSList const *const items) {
105
using Inkscape::Util::GSListConstIterator;
106
list<SPItem *> selected;
107
filterConnectors(items,selected);
108
if (selected.empty()) return;
110
const unsigned n=selected.size();
111
//Check 2 or more selected objects
114
// add the connector spacing to the size of node bounding boxes
115
// so that connectors can always be routed between shapes
116
SPDesktop* desktop = inkscape_active_desktop();
118
if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
120
map<string,unsigned> nodelookup;
121
vector<Rectangle*> rs;
123
for (list<SPItem *>::iterator i(selected.begin());
128
Geom::OptRect const item_box(sp_item_bbox_desktop(u));
130
Geom::Point ll(item_box->min());
131
Geom::Point ur(item_box->max());
132
nodelookup[u->id]=rs.size();
133
rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
134
ll[1]-spacing,ur[1]+spacing));
136
// I'm not actually sure if it's possible for something with a
137
// NULL item-box to be attached to a connector in which case we
138
// should never get to here... but if such a null box can occur it's
139
// probably pretty safe to simply ignore
140
//fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
144
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
145
SimpleConstraints scx,scy;
146
double ideal_connector_length = prefs->getDouble("/tools/connector/length", 100.0);
147
double directed_edge_height_modifier = 1.0;
149
bool directed = prefs->getBool("/tools/connector/directedlayout");
150
bool avoid_overlaps = prefs->getBool("/tools/connector/avoidoverlaplayout");
152
for (list<SPItem *>::iterator i(selected.begin());
157
map<string,unsigned>::iterator i=nodelookup.find(iu->id);
158
if(i==nodelookup.end()) {
161
unsigned u=i->second;
162
GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
163
list<SPItem *> connectors;
165
connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
166
for (list<SPItem *>::iterator j(connectors.begin());
167
j != connectors.end();
172
assert(isConnector(conn));
173
SP_PATH(conn)->connEndPair.getAttachedItems(items);
181
// The connector is not attached to anything at the
182
// other end so we should just ignore it.
186
// If iv not in nodelookup we again treat the connector
187
// as disconnected and continue
188
map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
189
if(v_pair!=nodelookup.end()) {
190
unsigned v=v_pair->second;
191
//cout << "Edge: (" << u <<","<<v<<")"<<endl;
192
es.push_back(make_pair(u,v));
193
if(conn->style->marker[SP_MARKER_LOC_END].set) {
194
if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
195
scy.push_back(new SimpleConstraint(v, u,
196
(ideal_connector_length * directed_edge_height_modifier)));
205
const unsigned E = es.size();
207
fill(eweights,eweights+E,1);
208
vector<Component*> cs;
209
connectedComponents(rs,es,scx,scy,cs);
210
for(unsigned i=0;i<cs.size();i++) {
212
if(c->edges.size()<2) continue;
213
CheckProgress test(0.0001,100,selected,rs,nodelookup);
214
ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
215
alg.setupConstraints(NULL,NULL,avoid_overlaps,
216
NULL,NULL,&c->scx,&c->scy,NULL,NULL);
219
separateComponents(cs);
221
for (list<SPItem *>::iterator it(selected.begin());
222
it != selected.end();
226
if(!isConnector(u)) {
227
map<string,unsigned>::iterator i=nodelookup.find(u->id);
228
if(i!=nodelookup.end()) {
229
Rectangle* r=rs[i->second];
230
Geom::OptRect item_box(sp_item_bbox_desktop(u));
232
Geom::Point const curr(item_box->midpoint());
233
Geom::Point const dest(r->getCentreX(),r->getCentreY());
234
sp_item_move_rel(u, Geom::Translate(dest - curr));
239
for(unsigned i=0;i<scx.size();i++) {
242
for(unsigned i=0;i<scy.size();i++) {
245
for(unsigned i=0;i<rs.size();i++) {
250
// vim: ts=4 sw=4 et tw=0 wm=0
255
c-file-style:"stroustrup"
256
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
261
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :