annotate toolboxes/graph_visualisation/lib/lefty/tree.lefty @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 load ('def.lefty');
Daniel@0 2 definit ();
Daniel@0 3 #
Daniel@0 4 # initialize window data
Daniel@0 5 #
Daniel@0 6 canvas = defcanvas;
Daniel@0 7 wrect = [0 = ['x' = -5; 'y' = 0;]; 1 = ['x' = 410; 'y' = 500;];];
Daniel@0 8 setwidgetattr (canvas, ['window' = wrect;]);
Daniel@0 9 #
Daniel@0 10 # data structures
Daniel@0 11 #
Daniel@0 12 nodearray = [];
Daniel@0 13 nodenum = 0;
Daniel@0 14 dist = ['x' = 40; 'y' = 40;];
Daniel@0 15 defsize = ['x' = 10; 'y' = 10;];
Daniel@0 16 fontname = 'fixed';
Daniel@0 17 fontsize = 18;
Daniel@0 18 tree = null;
Daniel@0 19
Daniel@0 20 # drawing functions
Daniel@0 21 #
Daniel@0 22 boxnode = function (node) {
Daniel@0 23 local center;
Daniel@0 24 box (canvas, node, node.rect, ['color' = 0; 'fill' = 'on';]);
Daniel@0 25 box (canvas, node, node.rect);
Daniel@0 26 center = [
Daniel@0 27 'x' = (node.rect[0].x + node.rect[1].x) / 2;
Daniel@0 28 'y' = (node.rect[0].y + node.rect[1].y) / 2;
Daniel@0 29 ];
Daniel@0 30 if (node.name)
Daniel@0 31 text (canvas, node, center, node.name, fontname, fontsize, 'cc');
Daniel@0 32 };
Daniel@0 33 circlenode = function (node) {
Daniel@0 34 local center, radius;
Daniel@0 35 center = [
Daniel@0 36 'x' = (node.rect[0].x + node.rect[1].x) / 2;
Daniel@0 37 'y' = (node.rect[0].y + node.rect[1].y) / 2;
Daniel@0 38 ];
Daniel@0 39 radius = [
Daniel@0 40 'x' = center.x - node.rect[0].x;
Daniel@0 41 'y' = center.y - node.rect[0].y;
Daniel@0 42 ];
Daniel@0 43 arc (canvas, node, center, radius, ['color' = 0; 'fill' = 'on';]);
Daniel@0 44 arc (canvas, node, center, radius);
Daniel@0 45 if (node.name)
Daniel@0 46 text (canvas, node, center, node.name, fontname, fontsize, 'cc');
Daniel@0 47 };
Daniel@0 48 drawnode = boxnode;
Daniel@0 49 drawedge = function (node1, node2) {
Daniel@0 50 line (canvas, null,
Daniel@0 51 [
Daniel@0 52 'x' = (node1.rect[1].x + node1.rect[0].x) / 2;
Daniel@0 53 'y' = node1.rect[0].y;
Daniel@0 54 ], [
Daniel@0 55 'x' = (node2.rect[1].x + node2.rect[0].x) / 2;
Daniel@0 56 'y' = node2.rect[1].y;
Daniel@0 57 ]);
Daniel@0 58 };
Daniel@0 59 drawtree = function (node) {
Daniel@0 60 local i;
Daniel@0 61 for (i in nodearray)
Daniel@0 62 drawnode (nodearray[i]);
Daniel@0 63 drawtreerec (node);
Daniel@0 64 };
Daniel@0 65 drawtreerec = function (node) {
Daniel@0 66 local i, n;
Daniel@0 67 if ((n = tablesize (node.ch)) > 0) {
Daniel@0 68 for (i = 0; i < n; i = i + 1) {
Daniel@0 69 drawedge (node, node.ch[i]);
Daniel@0 70 drawtreerec (node.ch[i]);
Daniel@0 71 }
Daniel@0 72 }
Daniel@0 73 };
Daniel@0 74 redraw = function (c) {
Daniel@0 75 if (tree)
Daniel@0 76 drawtree (tree);
Daniel@0 77 };
Daniel@0 78
Daniel@0 79 # layout functions
Daniel@0 80 #
Daniel@0 81 complayout = function () {
Daniel@0 82 leafx = 0;
Daniel@0 83 leafrank = 0;
Daniel@0 84 dolayout (tree, wrect[1].y - 10);
Daniel@0 85 remove ('leafx');
Daniel@0 86 remove ('leafrank');
Daniel@0 87 };
Daniel@0 88 dolayout = function (node, pary) {
Daniel@0 89 local r, n, i, size, lchp, rchp;
Daniel@0 90 size = nodesize (node);
Daniel@0 91 if (node.chn > 0) {
Daniel@0 92 for (i = 0; i < node.chn; i = i + 1)
Daniel@0 93 dolayout (node.ch[i], pary - size.y - dist.y);
Daniel@0 94 node.rank = (node.ch[0].rank + node.ch[node.chn - 1].rank) / 2;
Daniel@0 95 lchp = node.ch[0].rect;
Daniel@0 96 rchp = node.ch[node.chn - 1].rect;
Daniel@0 97 r[0].x = lchp[0].x + ((rchp[1].x - lchp[0].x) - size.x) / 2;
Daniel@0 98 r[0].y = pary - size.y;
Daniel@0 99 r[1].x = r[0].x + size.x;
Daniel@0 100 r[1].y = pary;
Daniel@0 101 node.rect = r;
Daniel@0 102 } else {
Daniel@0 103 node.rank = leafrank;
Daniel@0 104 r[0].x = leafx;
Daniel@0 105 r[0].y = pary - size.y;
Daniel@0 106 r[1].x = r[0].x + size.x;
Daniel@0 107 r[1].y = pary;
Daniel@0 108 leafrank = leafrank + 1;
Daniel@0 109 leafx = r[1].x + dist.x;
Daniel@0 110 node.rect = r;
Daniel@0 111 }
Daniel@0 112 };
Daniel@0 113
Daniel@0 114 # editing functions
Daniel@0 115 #
Daniel@0 116 inode = function (point, name) {
Daniel@0 117 local i, nnum, size;
Daniel@0 118 nnum = nodenum;
Daniel@0 119 if (~name)
Daniel@0 120 name = ask ('give name of node:');
Daniel@0 121 nodearray[nnum].ch = [];
Daniel@0 122 nodearray[nnum].chn = 0;
Daniel@0 123 nodearray[nnum].name = name;
Daniel@0 124 size = nodesize (nodearray[nnum]);
Daniel@0 125 nodearray[nnum].rect[0] = point;
Daniel@0 126 nodearray[nnum].rect[1] = ['x' = point.x + size.x; 'y' = point.y + size.y;];
Daniel@0 127 nodenum = nodenum + 1;
Daniel@0 128 if (~tree) {
Daniel@0 129 tree = nodearray[nnum];
Daniel@0 130 tree.depth = 0;
Daniel@0 131 complayout ();
Daniel@0 132 drawtree (tree);
Daniel@0 133 } else
Daniel@0 134 drawtree (nodearray[nnum]);
Daniel@0 135 return nodearray[nnum];
Daniel@0 136 };
Daniel@0 137 iedge = function (node1, node2) {
Daniel@0 138 node1.ch[node1.chn] = node2;
Daniel@0 139 node1.chn = node1.chn + 1;
Daniel@0 140 node2.depth = node1.depth + 1;
Daniel@0 141 complayout ();
Daniel@0 142 clear (canvas);
Daniel@0 143 drawtree (tree);
Daniel@0 144 };
Daniel@0 145 fix = function (node, op, np) {
Daniel@0 146 if (node.depth ~= 0)
Daniel@0 147 dist.y = dist.y + (op.y - np.y) / node.depth;
Daniel@0 148 if (node.rank ~= 0)
Daniel@0 149 dist.x = dist.x + (np.x - op.x) / node.rank;
Daniel@0 150 complayout ();
Daniel@0 151 clear (canvas);
Daniel@0 152 drawtree (tree);
Daniel@0 153 };
Daniel@0 154 nodesize = function (node) {
Daniel@0 155 local siz;
Daniel@0 156 if (~(siz = textsize (canvas, node.name, fontname, fontsize)))
Daniel@0 157 siz = defsize;
Daniel@0 158 else {
Daniel@0 159 siz.x = siz.x + 8;
Daniel@0 160 siz.y = siz.y + 8;
Daniel@0 161 }
Daniel@0 162 return siz;
Daniel@0 163 };
Daniel@0 164 changenode = function (nodestyle) {
Daniel@0 165 drawnode = nodestyle;
Daniel@0 166 clear (canvas);
Daniel@0 167 drawtree (tree);
Daniel@0 168 };
Daniel@0 169
Daniel@0 170 # user interface functions
Daniel@0 171 #
Daniel@0 172 leftdown = function (data) {
Daniel@0 173 if (~data.obj)
Daniel@0 174 inode (data.pos, null);
Daniel@0 175 };
Daniel@0 176 leftup = function (data) {
Daniel@0 177 if (data.pobj)
Daniel@0 178 fix (data.pobj, data.ppos, data.pos);
Daniel@0 179 };
Daniel@0 180 middleup = function (data) {
Daniel@0 181 if (data.pobj & data.obj)
Daniel@0 182 iedge (data.pobj, data.obj);
Daniel@0 183 };
Daniel@0 184 dops = function () {
Daniel@0 185 local s;
Daniel@0 186
Daniel@0 187 s = ['x' = 8 * 300; 'y' = 10.5 * 300;];
Daniel@0 188 fontname = 'Times-Roman';
Daniel@0 189 canvas = createwidget (-1, ['type' = 'ps'; 'size' = s;]);
Daniel@0 190 setwidgetattr (canvas, ['window' = wrect;]);
Daniel@0 191 drawtree (tree);
Daniel@0 192 destroywidget (canvas);
Daniel@0 193 canvas=defcanvas;
Daniel@0 194 fontname = 'fixed';
Daniel@0 195 };