1 module treemapwidget; 2 3 import tm = treemap; 4 import std.file; 5 import std.variant; 6 import std.path; 7 import std.stdio; 8 import std.algorithm; 9 import std.range; 10 import dlangui; 11 import std.datetime; 12 13 Rect toUiRect(tm.Rect r) { 14 return Rect(cast(int)r.left(), 15 cast(int)r.top(), 16 cast(int)r.right(), 17 cast(int)r.bottom()); 18 } 19 20 string humanize(ulong v) { 21 auto units = ["", "k", "m", "g"]; 22 int idx = 0; 23 while (v / 1024 > 0) { 24 idx++; 25 v /= 1024; 26 } 27 return v.to!string ~ units[idx]; 28 } 29 30 class TreeMapWidget(Node) : Widget { 31 interface OnTreeMapHandler { 32 void onTreeMap(Node node); 33 } 34 35 tm.TreeMap!Node treeMap; 36 37 this(string id, Node rootNode) { 38 super(id); 39 this.treeMap = new tm.TreeMap!Node(rootNode); 40 } 41 42 public Signal!OnTreeMapHandler onTreeMapFocused; 43 public auto addTreeMapFocusedListener(void delegate (Node) listener) { 44 onTreeMapFocused.connect(listener); 45 return this; 46 } 47 48 override bool onMouseEvent(MouseEvent me) { 49 auto r = treeMap.findFor(me.pos.x, me.pos.y); 50 r.tryVisit!( 51 (Node node) { onTreeMapFocused(node); }, 52 () {}, 53 )(); 54 return true; 55 } 56 57 override void layout(Rect r) { 58 StopWatch sw; 59 sw.start(); 60 treeMap.layout(tm.Rect(0, 0, r.width, r.height)); 61 sw.stop(); 62 super.layout(r); 63 } 64 65 override void onDraw(DrawBuf buf) { 66 super.onDraw(buf); 67 if (visibility != Visibility.Visible) { 68 return; 69 } 70 71 auto rc = _pos; 72 auto saver = ClipRectSaver(buf, rc); 73 74 auto font = FontManager.instance.getFont(25, FontWeight.Normal, false, FontFamily.SansSerif, "Arial"); 75 76 foreach(child; treeMap.rootNode.childs) { 77 buf.drawFrame(treeMap.get(child).toUiRect(), 0xff00ff, Rect(1, 1, 1, 1), 0xa0a0a0); 78 } 79 } 80 }