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