#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Drawing; using System.Linq; using HeuristicLab.Visualization; using Rectangle = HeuristicLab.Visualization.Rectangle; namespace HeuristicLab.EvolutionaryTracking.Views.Primitives { public class LabeledRectangle : Rectangle, ILayoutNode { private string text; public string Text { get { return text; } set { text = value; } } private Font font; public Font Font { get { return font; } set { font = value; } } private Brush fontBrush; public Brush FontBrush { get { return fontBrush; } set { fontBrush = value; } } public LabeledRectangle(IChart chart, PointD lowerLeft, PointD upperRight) : base(chart, lowerLeft, upperRight) { font = new Font(FontFamily.GenericSansSerif, 10); } public LabeledRectangle(IChart chart, double x1, double y1, double x2, double y2) : this(chart, new PointD(x1, y1), new PointD(x2, y2)) { } public LabeledRectangle(IChart chart, PointD lowerLeft, PointD upperRight, Pen pen, Brush brush) : base(chart, lowerLeft, upperRight, pen, brush) { font = new Font(FontFamily.GenericSansSerif, 10); } public LabeledRectangle(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush) : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) { } public override void Draw(Graphics graphics) { var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y)); var s = Chart.TransformWorldToPixel(Size); var stringSize = graphics.MeasureString(Text, Font); var fontSize = Font.Size * s.Width / stringSize.Width; if (fontSize > 12) fontSize = 12; // limit maximum font size so that nodes look nicer font = new Font(font.Name, fontSize, FontStyle.Regular, GraphicsUnit.Pixel); stringSize = graphics.MeasureString(Text, Font); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; graphics.DrawString(Text, Font, FontBrush, p.X + (s.Width - stringSize.Width) / 2f, p.Y - (s.Height + stringSize.Height) / 2f); base.Draw(graphics); } public ILayoutNode Thread { get; set; } public ILayoutNode Ancestor { get; set; } public ILayoutNode Parent { get; set; } public List> Children { get; set; } public float Mod { get; set; } public float Prelim { get; set; } public float Change { get; set; } public float Shift { get; set; } public int Number { get; set; } public int Level { get; set; } public float X { get; set; } public float Y { get; set; } public bool IsLeaf { get { return Children == null || Children.Count == 0; } } public ILayoutNode NextLeft { get { return Children == null ? Thread : Children.First(); } } public ILayoutNode NextRight { get { return Children == null ? Thread : Children.Last(); } } public ILayoutNode LeftSibling { get { if (Parent == null) return null; return Number == 0 ? null : Parent.Children[Number - 1]; } } public ILayoutNode LeftmostSibling { get { if (Parent == null) return null; return Number == 0 ? null : Parent.Children[0]; } } public ISymbolicExpressionTreeNode Content { get; set; } } }