60cdab8146c6490e07e810a1ca915e8385a0a243
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 "prefs-utils.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, NR::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<NR::Rect> const item_box(sp_item_bbox_desktop(u));
128 if(item_box) {
129 NR::Point ll(item_box->min());
130 NR::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 SimpleConstraints scx,scy;
144 double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
145 double directed_edge_height_modifier = 1.0;
146 gchar const *directed_str = NULL, *overlaps_str = NULL;
147 directed_str = prefs_get_string_attribute("tools.connector",
148 "directedlayout");
149 overlaps_str = prefs_get_string_attribute("tools.connector",
150 "avoidoverlaplayout");
151 bool avoid_overlaps = false;
152 bool directed = false;
153 if (directed_str && !strcmp(directed_str, "true")) {
154 directed = true;
155 }
156 if (overlaps_str && !strcmp(overlaps_str, "true")) {
157 avoid_overlaps = true;
158 }
160 for (list<SPItem *>::iterator i(selected.begin());
161 i != selected.end();
162 ++i)
163 {
164 SPItem *iu=*i;
165 map<string,unsigned>::iterator i=nodelookup.find(iu->id);
166 if(i==nodelookup.end()) {
167 continue;
168 }
169 unsigned u=i->second;
170 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
171 list<SPItem *> connectors;
173 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
174 for (list<SPItem *>::iterator j(connectors.begin());
175 j != connectors.end();
176 ++j) {
177 SPItem *conn=*j;
178 SPItem *iv;
179 SPItem *items[2];
180 assert(isConnector(conn));
181 SP_PATH(conn)->connEndPair.getAttachedItems(items);
182 if(items[0]==iu) {
183 iv=items[1];
184 } else {
185 iv=items[0];
186 }
188 if (iv == NULL) {
189 // The connector is not attached to anything at the
190 // other end so we should just ignore it.
191 continue;
192 }
194 // If iv not in nodelookup we again treat the connector
195 // as disconnected and continue
196 map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
197 if(v_pair!=nodelookup.end()) {
198 unsigned v=v_pair->second;
199 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
200 es.push_back(make_pair(u,v));
201 if(conn->style->marker[SP_MARKER_LOC_END].set) {
202 if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
203 scy.push_back(new SimpleConstraint(v, u,
204 (ideal_connector_length * directed_edge_height_modifier)));
205 }
206 }
207 }
208 }
209 if(nlist) {
210 g_slist_free(nlist);
211 }
212 }
213 const unsigned E = es.size();
214 double eweights[E];
215 fill(eweights,eweights+E,1);
216 vector<Component*> cs;
217 connectedComponents(rs,es,scx,scy,cs);
218 for(unsigned i=0;i<cs.size();i++) {
219 Component* c=cs[i];
220 if(c->edges.size()<2) continue;
221 CheckProgress test(0.0001,100,selected,rs,nodelookup);
222 ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
223 alg.setupConstraints(NULL,NULL,avoid_overlaps,
224 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
225 alg.run();
226 }
227 separateComponents(cs);
229 for (list<SPItem *>::iterator it(selected.begin());
230 it != selected.end();
231 ++it)
232 {
233 SPItem *u=*it;
234 if(!isConnector(u)) {
235 map<string,unsigned>::iterator i=nodelookup.find(u->id);
236 if(i!=nodelookup.end()) {
237 Rectangle* r=rs[i->second];
238 boost::optional<NR::Rect> item_box(sp_item_bbox_desktop(u));
239 if(item_box) {
240 NR::Point const curr(item_box->midpoint());
241 NR::Point const dest(r->getCentreX(),r->getCentreY());
242 sp_item_move_rel(u, NR::translate(dest - curr));
243 }
244 }
245 }
246 }
247 for(unsigned i=0;i<scx.size();i++) {
248 delete scx[i];
249 }
250 for(unsigned i=0;i<scy.size();i++) {
251 delete scy[i];
252 }
253 for(unsigned i=0;i<rs.size();i++) {
254 delete rs[i];
255 }
256 }
257 // vim: set cindent
258 // vim: ts=4 sw=4 et tw=0 wm=0