d0ba2d20ec781c509ebbc0098655a5f73749e9cf
1 /** \file
2 * Interface between Inkscape code (SPItem) and graphlayout functions.
3 */
4 /*
5 * Authors:
6 * Tim Dwyer <Tim.Dwyer@infotech.monash.edu.au>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
12 #include <iostream>
13 #include <config.h>
14 #include <map>
15 #include <vector>
16 #include <algorithm>
17 #include <cstring>
18 #include <cstdlib>
19 #include <float.h>
21 #include "desktop.h"
22 #include "inkscape.h"
23 #include "sp-namedview.h"
24 #include "util/glib-list-iterators.h"
25 #include "graphlayout/graphlayout.h"
26 #include "sp-path.h"
27 #include "sp-item.h"
28 #include "sp-item-transform.h"
29 #include "sp-conn-end-pair.h"
30 #include "style.h"
31 #include "conn-avoid-ref.h"
32 #include "libavoid/connector.h"
33 #include "libavoid/geomtypes.h"
34 #include "libcola/cola.h"
35 #include "libvpsc/generate-constraints.h"
36 #include "preferences.h"
38 using namespace std;
39 using namespace cola;
40 using namespace vpsc;
41 /**
42 * Returns true if item is a connector
43 */
44 bool isConnector(SPItem const *const i) {
45 SPPath *path = NULL;
46 if(SP_IS_PATH(i)) {
47 path = SP_PATH(i);
48 }
49 return path && path->connEndPair.isAutoRoutingConn();
50 }
52 struct CheckProgress : TestConvergence {
53 CheckProgress(double d,unsigned i,list<SPItem *>&
54 selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
55 TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
56 bool operator()(double new_stress, double* X, double* Y) {
57 /* This is where, if we wanted to animate the layout, we would need to update
58 * the positions of all objects and redraw the canvas and maybe sleep a bit
59 cout << "stress="<<new_stress<<endl;
60 cout << "x[0]="<<rs[0]->getMinX()<<endl;
61 for (list<SPItem *>::iterator it(selected.begin());
62 it != selected.end();
63 ++it)
64 {
65 SPItem *u=*it;
66 if(!isConnector(u)) {
67 Rectangle* r=rs[nodelookup[u->id]];
68 NR::Rect const item_box(sp_item_bbox_desktop(u));
69 NR::Point const curr(item_box.midpoint());
70 NR::Point const dest(r->getCentreX(),r->getCentreY());
71 sp_item_move_rel(u, Geom::Translate(dest - curr));
72 }
73 }
74 */
75 return TestConvergence::operator()(new_stress,X,Y);
76 }
77 list<SPItem *>& selected;
78 vector<Rectangle*>& rs;
79 map<string,unsigned>& nodelookup;
80 };
82 /**
83 * Scans the items list and places those items that are
84 * not connectors in filtered
85 */
86 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
87 for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
88 SPItem *item=SP_ITEM(i->data);
89 if(!isConnector(item)) {
90 filtered.push_back(item);
91 }
92 }
93 }
94 /**
95 * Takes a list of inkscape items, extracts the graph defined by
96 * connectors between them, and uses graph layout techniques to find
97 * a nice layout
98 */
99 void graphlayout(GSList const *const items) {
100 if(!items) {
101 return;
102 }
104 using Inkscape::Util::GSListConstIterator;
105 list<SPItem *> selected;
106 filterConnectors(items,selected);
107 if (selected.empty()) return;
109 const unsigned n=selected.size();
110 //Check 2 or more selected objects
111 if (n < 2) return;
113 // add the connector spacing to the size of node bounding boxes
114 // so that connectors can always be routed between shapes
115 SPDesktop* desktop = inkscape_active_desktop();
116 double spacing = 0;
117 if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
119 map<string,unsigned> nodelookup;
120 vector<Rectangle*> rs;
121 vector<Edge> es;
122 for (list<SPItem *>::iterator i(selected.begin());
123 i != selected.end();
124 ++i)
125 {
126 SPItem *u=*i;
127 boost::optional<Geom::Rect> const item_box(sp_item_bbox_desktop(u));
128 if(item_box) {
129 Geom::Point ll(item_box->min());
130 Geom::Point ur(item_box->max());
131 nodelookup[u->id]=rs.size();
132 rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
133 ll[1]-spacing,ur[1]+spacing));
134 } else {
135 // I'm not actually sure if it's possible for something with a
136 // NULL item-box to be attached to a connector in which case we
137 // should never get to here... but if such a null box can occur it's
138 // probably pretty safe to simply ignore
139 //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
140 }
141 }
143 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
144 SimpleConstraints scx,scy;
145 double ideal_connector_length = prefs->getDouble("tools.connector", "length", 100.0);
146 double directed_edge_height_modifier = 1.0;
148 bool directed = prefs->getBool("tools.connector", "directedlayout");
149 bool avoid_overlaps = prefs->getBool("tools.connector", "avoidoverlaplayout");
151 for (list<SPItem *>::iterator i(selected.begin());
152 i != selected.end();
153 ++i)
154 {
155 SPItem *iu=*i;
156 map<string,unsigned>::iterator i=nodelookup.find(iu->id);
157 if(i==nodelookup.end()) {
158 continue;
159 }
160 unsigned u=i->second;
161 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
162 list<SPItem *> connectors;
164 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
165 for (list<SPItem *>::iterator j(connectors.begin());
166 j != connectors.end();
167 ++j) {
168 SPItem *conn=*j;
169 SPItem *iv;
170 SPItem *items[2];
171 assert(isConnector(conn));
172 SP_PATH(conn)->connEndPair.getAttachedItems(items);
173 if(items[0]==iu) {
174 iv=items[1];
175 } else {
176 iv=items[0];
177 }
179 if (iv == NULL) {
180 // The connector is not attached to anything at the
181 // other end so we should just ignore it.
182 continue;
183 }
185 // If iv not in nodelookup we again treat the connector
186 // as disconnected and continue
187 map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
188 if(v_pair!=nodelookup.end()) {
189 unsigned v=v_pair->second;
190 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
191 es.push_back(make_pair(u,v));
192 if(conn->style->marker[SP_MARKER_LOC_END].set) {
193 if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
194 scy.push_back(new SimpleConstraint(v, u,
195 (ideal_connector_length * directed_edge_height_modifier)));
196 }
197 }
198 }
199 }
200 if(nlist) {
201 g_slist_free(nlist);
202 }
203 }
204 const unsigned E = es.size();
205 double eweights[E];
206 fill(eweights,eweights+E,1);
207 vector<Component*> cs;
208 connectedComponents(rs,es,scx,scy,cs);
209 for(unsigned i=0;i<cs.size();i++) {
210 Component* c=cs[i];
211 if(c->edges.size()<2) continue;
212 CheckProgress test(0.0001,100,selected,rs,nodelookup);
213 ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
214 alg.setupConstraints(NULL,NULL,avoid_overlaps,
215 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
216 alg.run();
217 }
218 separateComponents(cs);
220 for (list<SPItem *>::iterator it(selected.begin());
221 it != selected.end();
222 ++it)
223 {
224 SPItem *u=*it;
225 if(!isConnector(u)) {
226 map<string,unsigned>::iterator i=nodelookup.find(u->id);
227 if(i!=nodelookup.end()) {
228 Rectangle* r=rs[i->second];
229 boost::optional<Geom::Rect> item_box(sp_item_bbox_desktop(u));
230 if(item_box) {
231 Geom::Point const curr(item_box->midpoint());
232 Geom::Point const dest(r->getCentreX(),r->getCentreY());
233 sp_item_move_rel(u, Geom::Translate(dest - curr));
234 }
235 }
236 }
237 }
238 for(unsigned i=0;i<scx.size();i++) {
239 delete scx[i];
240 }
241 for(unsigned i=0;i<scy.size();i++) {
242 delete scy[i];
243 }
244 for(unsigned i=0;i<rs.size();i++) {
245 delete rs[i];
246 }
247 }
248 // vim: set cindent
249 // vim: ts=4 sw=4 et tw=0 wm=0
251 /*
252 Local Variables:
253 mode:c++
254 c-file-style:"stroustrup"
255 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256 indent-tabs-mode:nil
257 fill-column:99
258 End:
259 */
260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :