ac2d5429f4ae1518e50d73706d01567b37f12a88
1 /** \file
2 * Interface between Inkscape code (SPItem) and graphlayout functions.
3 */
4 /*
5 * Authors:
6 * Tim Dwyer <tgdwyer@gmail.com>
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 <float.h>
19 #include "util/glib-list-iterators.h"
20 #include "graphlayout/graphlayout.h"
21 #include "sp-path.h"
22 #include "sp-item.h"
23 #include "sp-item-transform.h"
24 #include "sp-conn-end-pair.h"
25 #include "conn-avoid-ref.h"
26 #include "libavoid/connector.h"
27 #include "libavoid/geomtypes.h"
28 #include "libcola/cola.h"
29 #include "libvpsc/generate-constraints.h"
30 #include "prefs-utils.h"
32 using namespace std;
33 using namespace cola;
35 /**
36 * Returns true if item is a connector
37 */
38 bool isConnector(SPItem const *const i) {
39 SPPath *path = NULL;
40 if(SP_IS_PATH(i)) {
41 path = SP_PATH(i);
42 }
43 return path && path->connEndPair.isAutoRoutingConn();
44 }
46 /**
47 * Scans the items list and places those items that are
48 * not connectors in filtered
49 */
50 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
51 for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
52 SPItem *item=SP_ITEM(i->data);
53 if(!isConnector(item)) {
54 filtered.push_back(item);
55 }
56 }
57 }
58 /**
59 * Takes a list of inkscape items, extracts the graph defined by
60 * connectors between them, and uses graph layout techniques to find
61 * a nice layout
62 */
63 void graphlayout(GSList const *const items) {
64 if(!items) {
65 return;
66 }
68 using Inkscape::Util::GSListConstIterator;
69 list<SPItem *> selected;
70 filterConnectors(items,selected);
71 if (selected.empty()) return;
73 const unsigned n=selected.size();
74 //Check 2 or more selected objects
75 if (n < 2) return;
77 double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
79 map<string,unsigned> nodelookup;
80 vector<Rectangle*> rs;
81 vector<Edge> es;
82 for (list<SPItem *>::iterator i(selected.begin());
83 i != selected.end();
84 ++i)
85 {
86 SPItem *u=*i;
87 NR::Rect const item_box(sp_item_bbox_desktop(u));
88 NR::Point ll(item_box.min());
89 NR::Point ur(item_box.max());
90 minX=min(ll[0],minX); minY=min(ll[1],minY);
91 maxX=max(ur[0],maxX); maxY=max(ur[1],maxY);
92 nodelookup[u->id]=rs.size();
93 rs.push_back(new Rectangle(ll[0],ur[0],ll[1],ur[1]));
94 }
97 for (list<SPItem *>::iterator i(selected.begin());
98 i != selected.end();
99 ++i)
100 {
101 SPItem *iu=*i;
102 unsigned u=nodelookup[iu->id];
103 GSList *nlist=iu->avoidRef->getAttachedShapes(Avoid::runningFrom);
104 list<SPItem *> neighbours;
105 neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
106 for (list<SPItem *>::iterator j(neighbours.begin());
107 j != neighbours.end();
108 ++j) {
110 SPItem *iv=*j;
111 // What do we do if iv not in nodelookup?!?!
112 unsigned v=nodelookup[iv->id];
113 es.push_back(make_pair(u,v));
114 }
115 if(nlist) {
116 g_slist_free(nlist);
117 }
118 }
119 double width=maxX-minX;
120 double height=maxY-minY;
121 const unsigned E = es.size();
122 double eweights[E];
123 fill(eweights,eweights+E,1);
125 ConstrainedMajorizationLayout alg(rs,es,eweights,
126 prefs_get_double_attribute("tools.connector","length",100));
127 gchar const *directed = NULL, *overlaps = NULL;
128 directed = prefs_get_string_attribute("tools.connector",
129 "directedlayout");
130 overlaps = prefs_get_string_attribute("tools.connector",
131 "avoidoverlaplayout");
132 bool avoid_overlaps = false;
133 if (directed && !strcmp(directed, "true")) {
134 cout << "Directed layout requested, but not yet implemented\n";
135 cout << " because we haven't coded cyclic removal alg...\n";
136 }
137 if (overlaps && !strcmp(overlaps, "true")) {
138 cout << "Avoid overlaps requested.\n";
139 avoid_overlaps = true;
140 }
141 alg.setupConstraints(NULL,NULL,avoid_overlaps,
142 NULL,NULL,NULL,NULL,NULL,NULL);
143 alg.run();
145 for (list<SPItem *>::iterator it(selected.begin());
146 it != selected.end();
147 ++it)
148 {
149 SPItem *u=*it;
150 if(!isConnector(u)) {
151 Rectangle* r=rs[nodelookup[u->id]];
152 NR::Rect const item_box(sp_item_bbox_desktop(u));
153 NR::Point const curr(item_box.midpoint());
154 NR::Point const dest(r->getCentreX(),r->getCentreY());
155 sp_item_move_rel(u, NR::translate(dest - curr));
156 }
157 }
158 }