Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/Primitives/LabeledRectangle.cs @ 9963

Last change on this file since 9963 was 9963, checked in by bburlacu, 11 years ago

#1772: Merged changes from the trunk and other branches. Added new ExtendedSymbolicExpressionTreeCanvas control for the visual exploration of tree genealogies. Reorganized some files and folders.

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Visualization;
27using Rectangle = HeuristicLab.Visualization.Rectangle;
28
29namespace HeuristicLab.EvolutionaryTracking.Views {
30  public class LabeledRectangle : Rectangle, ILayoutNode<ISymbolicExpressionTreeNode> {
31    private string text;
32    public string Text { get { return text; } set { text = value; } }
33
34    private Font font;
35    public Font Font { get { return font; } set { font = value; } }
36
37    private Brush fontBrush;
38    public Brush FontBrush { get { return fontBrush; } set { fontBrush = value; } }
39
40    public LabeledRectangle(IChart chart, PointD lowerLeft, PointD upperRight)
41      : base(chart, lowerLeft, upperRight) {
42      font = new Font(FontFamily.GenericSansSerif, 10);
43    }
44    public LabeledRectangle(IChart chart, double x1, double y1, double x2, double y2)
45      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
46    }
47    public LabeledRectangle(IChart chart, PointD lowerLeft, PointD upperRight, Pen pen, Brush brush)
48      : base(chart, lowerLeft, upperRight, pen, brush) {
49      font = new Font(FontFamily.GenericSansSerif, 10);
50    }
51    public LabeledRectangle(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
52      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
53    }
54
55    public override void Draw(Graphics graphics) {
56      var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y));
57      var s = Chart.TransformWorldToPixel(Size);
58      var stringSize = graphics.MeasureString(Text, Font);
59      var fontSize = Font.Size * s.Width / stringSize.Width;
60      if (fontSize > 12) fontSize = 12; // limit maximum font size so that nodes look nicer
61      font = new Font(font.Name, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
62      stringSize = graphics.MeasureString(Text, Font);
63      graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
64      graphics.DrawString(Text, Font, FontBrush, p.X + (s.Width - stringSize.Width) / 2f, p.Y - (s.Height + stringSize.Height) / 2f);
65      base.Draw(graphics);
66    }
67
68    public ILayoutNode<ISymbolicExpressionTreeNode> Thread { get; set; }
69    public ILayoutNode<ISymbolicExpressionTreeNode> Ancestor { get; set; }
70    public ILayoutNode<ISymbolicExpressionTreeNode> Parent { get; set; }
71    public List<ILayoutNode<ISymbolicExpressionTreeNode>> Children { get; set; }
72    public float Mod { get; set; }
73    public float Prelim { get; set; }
74    public float Change { get; set; }
75    public float Shift { get; set; }
76    public int Number { get; set; }
77    public int Level { get; set; }
78    public float X { get; set; }
79    public float Y { get; set; }
80    public bool IsLeaf {
81      get { return Children == null || Children.Count == 0; }
82    }
83    public ILayoutNode<ISymbolicExpressionTreeNode> NextLeft {
84      get {
85        return Children == null ? Thread : Children.First();
86      }
87    }
88    public ILayoutNode<ISymbolicExpressionTreeNode> NextRight {
89      get {
90        return Children == null ? Thread : Children.Last();
91      }
92    }
93    public ILayoutNode<ISymbolicExpressionTreeNode> LeftSibling {
94      get {
95        if (Parent == null) return null;
96        return Number == 0 ? null : Parent.Children[Number - 1];
97      }
98    }
99    public ILayoutNode<ISymbolicExpressionTreeNode> LeftmostSibling {
100      get {
101        if (Parent == null) return null;
102        return Number == 0 ? null : Parent.Children[0];
103      }
104    }
105    public ISymbolicExpressionTreeNode Content { get; set; }
106  }
107}
Note: See TracBrowser for help on using the repository browser.