private TreeMapNode FindNodeAt(TreeMapNode root, int x, int y, int level) { if (root.Bounds.Contains(x, y)) { if (depthLevel == 0 || level < depthLevel) { foreach (TreeMapNode node in root.Nodes) { TreeMapNode n = FindNodeAt(node, x, y, level + 1); if (n != null) { return(n); } } return(root); } else { return(root); } } else { return(null); } }
private long populateTree(TreeMapNode root) { DirectoryInfo d = new DirectoryInfo(root.getLabel()); try { long size = 0L; foreach (DirectoryInfo dir in d.GetDirectories()) { TreeMapNode node = new TreeMapNode(dir.FullName); long weight = populateTree(node); node.setWeight(weight); node.setValue(new DefaultValue(weight)); root.add(node); size += weight; } foreach (FileInfo file in d.GetFiles()) { size += file.Length; TreeMapNode node = new TreeMapNode(file.FullName, file.Length, new DefaultValue(file.Length)); root.add(node); } return(size); } catch { return(0); } }
/** * calculate the positions for all the elements of the root. * * @param root the root to calculate */ public void calculatePositions(TreeMapNode root) { if (root == null) { return; } ArrayList v = root.Nodes; if (v != null) { calculatePositionsRec(root.getX(), root.getY(), root.getWidth(), root.getHeight(), this.sumWeight(v), v); } }
public int CompareTo(object obj) { TreeMapNode n = (TreeMapNode)obj; if (weight < n.weight) { return(1); } else if (weight > n.weight) { return(-1); } else { return(0); } }
/** * add a new child to the node * * @param newChild new child */ public void add(TreeMapNode newChild) { Nodes.Add(newChild); newChild.parent = this; this.setWeight(this.weight + newChild.getWeight()); }
protected virtual void calculatePositionsRec(double x0, double y0, double w0, double h0, double weight0, ArrayList v) { if (w0 <= this.border || h0 <= this.border) { return; } //if the Vector contains only one element if (v.Count == 1) { TreeMapNode f = (TreeMapNode)v[0]; if (f.IsLeaf) { //if this is a leaf, we display with the border f.setDimension(x0 + this.border, y0 + this.border, w0 - this.border, h0 - this.border); } else { f.setDimension(x0, y0, w0, h0); //if this is not a leaf, calculation for the children if (this.border > 1) { this.border -= 2; calculatePositionsRec(x0 + 2, y0 + 2, w0 - 2, h0 - 2, weight0, f.Nodes); this.border += 2; } else if (this.border == 1) { this.border = 0; calculatePositionsRec(x0 + 1, y0 + 1, w0 - 1, h0 - 1, weight0, f.Nodes); this.border = 1; } else { calculatePositionsRec(x0, y0, w0, h0, weight0, f.Nodes); } } } else { //if there is more than one element //we split the Vector according to the selected strategy ArrayList v1 = new ArrayList(); ArrayList v2 = new ArrayList(); double weight1, weight2; //poids des 2 vecteurs this.splitElements(v, v1, v2); weight1 = this.sumWeight(v1); weight2 = this.sumWeight(v2); double w1, w2, h1, h2; double x2, y2; //if width is greater than height, we split the width if (w0 > h0) { w1 = (int)(w0 * weight1 / weight0); w2 = w0 - w1; h1 = h0; h2 = h0; x2 = x0 + w1; y2 = y0; } else { //else we split the height w1 = w0; w2 = w0; h1 = (int)(h0 * weight1 / weight0); h2 = h0 - h1; x2 = x0; y2 = y0 + h1; } //calculation for the new two Vectors calculatePositionsRec(x0, y0, w1, h1, weight1, v1); calculatePositionsRec(x2, y2, w2, h2, weight2, v2); } }
private void paint(TreeMapNode n, Graphics g, int level) { if (n.IsLeaf) { RectangleF rect = new RectangleF((float)n.getX(), (float)n.getY(), (float)n.getWidth(), (float)n.getHeight()); if (rect.Width > 0 && rect.Height > 0) { gp.Reset(); gp.AddEllipse(rect.Left - rect.Width / 4, rect.Top - rect.Height / 4, rect.Width + 2 * rect.Width / 4, rect.Height + 2 * rect.Height / 4); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.White; string ext = Path.GetExtension(n.getLabel()).TrimStart('.'); if (extensions.ContainsKey(ext.ToLower())) { pgb.SurroundColors = new Color[] { (Color)extensions[ext.ToLower()] }; } else { Color c = Color.FromKnownColor((KnownColor)((int)KnownColor.ForestGreen + extensions.Count)); extensions[ext.ToLower()] = c; pgb.SurroundColors = new Color[] { c }; } pgb.CenterPoint = new PointF(rect.Left + rect.Width / 4, rect.Top + rect.Height / 2); g.FillRectangle(pgb, rect); pgb.Dispose(); string label = Path.GetFileName(n.getLabel()); SizeF labelSize = g.MeasureString(label, f, (int)rect.Width); if (rect.Width > 1 && rect.Height > 1 && File.Exists(n.getLabel())) { if (n.Icon == null) { n.Icon = Icon.ExtractAssociatedIcon(n.getLabel()); } if (rect.Width >= n.Icon.Width && rect.Height >= n.Icon.Height + labelSize.Height) { Rectangle iconRect = new Rectangle((int)rect.Left + (int)rect.Width / 2 - n.Icon.Width / 2, (int)rect.Top + (int)rect.Height / 2 - n.Icon.Height / 2 - (int)labelSize.Height / 2, n.Icon.Width, n.Icon.Height); if (clipBoard == n && cutClipboard) { ImageAttributes ia = new ImageAttributes(); ia.SetGamma(0.3F); g.DrawImage(n.Icon.ToBitmap(), iconRect, 0, 0, n.Icon.Width, n.Icon.Height, GraphicsUnit.Pixel, ia); } else { g.DrawIconUnstretched(n.Icon, iconRect); } Region clip = g.Clip; g.Clip = new Region(rect); g.DrawString(label, f, Brushes.Black, new RectangleF(rect.Left + rect.Width / 2 - labelSize.Width / 2, rect.Top + rect.Height / 2 - labelSize.Height / 2 + iconRect.Height / 2, labelSize.Width, labelSize.Height)); g.Clip = clip; } else { double ratio = Math.Min(rect.Width / n.Icon.Width, rect.Height / n.Icon.Height); if (clipBoard == n && cutClipboard) { ImageAttributes ia = new ImageAttributes(); ia.SetGamma(0.3F); g.DrawImage(n.Icon.ToBitmap(), new Rectangle((int)Math.Round(rect.Left + rect.Width / 2 - n.Icon.Width * ratio / 2), (int)Math.Round(rect.Top + rect.Height / 2 - n.Icon.Height * ratio / 2), (int)Math.Round(n.Icon.Width * ratio), (int)Math.Round(n.Icon.Height * ratio)), 0, 0, n.Icon.Width, n.Icon.Height, GraphicsUnit.Pixel, ia); } else { g.DrawIcon(n.Icon, new Rectangle((int)Math.Round(rect.Left + rect.Width / 2 - n.Icon.Width * ratio / 2), (int)Math.Round(rect.Top + rect.Height / 2 - n.Icon.Height * ratio / 2), (int)Math.Round(n.Icon.Width * ratio), (int)Math.Round(n.Icon.Height * ratio))); } } } } } else if (level > 1) { RectangleF rect = new RectangleF((float)n.getX(), (float)n.getY(), (float)n.getWidth(), (float)n.getHeight()); if (rect.Width > 0 && rect.Height > 0) { gp.Reset(); gp.AddEllipse(rect.Left - rect.Width / 4, rect.Top - rect.Height / 4, rect.Width + 2 * rect.Width / 4, rect.Height + 2 * rect.Height / 4); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.White; pgb.SurroundColors = new Color[] { Color.YellowGreen }; pgb.CenterPoint = new PointF(rect.Left + rect.Width / 4, rect.Top + rect.Height / 2); g.FillRectangle(pgb, rect); pgb.Dispose(); string label = Path.GetFileName(n.getLabel()); SizeF labelSize = g.MeasureString(label, f); Region clip = g.Clip; g.Clip = new Region(rect); g.DrawString(label, f, Brushes.Black, new RectangleF(rect.Left + rect.Width / 2 - labelSize.Width / 2, rect.Top + rect.Height / 2 - labelSize.Height / 2, labelSize.Width, labelSize.Height)); g.Clip = clip; } } if (depthLevel == 0 || level < depthLevel) { foreach (TreeMapNode child in n.Nodes) { paint(child, g, level + 1); } } }