1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Visualization;
|
---|
26 | using HeuristicLab.Visualization.Primitives;
|
---|
27 | using Rectangle = HeuristicLab.Visualization.Rectangle;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
30 | public class SymbolicExpressionTreeTile : Group {
|
---|
31 | public double PreferredNodeWidth { get; set; }
|
---|
32 | public double PreferredNodeHeight { get; set; }
|
---|
33 | private ISymbolicExpressionTree symbolicExpressionTree;
|
---|
34 | public ISymbolicExpressionTree SymbolicExpressionTree {
|
---|
35 | get { return symbolicExpressionTree; }
|
---|
36 | set {
|
---|
37 | symbolicExpressionTree = value;
|
---|
38 | GeneratePrimitives(PreferredNodeWidth, PreferredNodeHeight);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> LayoutEngine { get; set; }
|
---|
43 | public SymbolicExpressionTreeLayoutAdapter LayoutAdapter { get; set; }
|
---|
44 | public SymbolicExpressionTreeTile(IChart chart) : base(chart) { }
|
---|
45 | public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree)
|
---|
46 | : base(chart) {
|
---|
47 | SymbolicExpressionTree = tree;
|
---|
48 | PreferredNodeWidth = 80;
|
---|
49 | PreferredNodeHeight = 40;
|
---|
50 | }
|
---|
51 | private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) {
|
---|
52 | Clear();
|
---|
53 | var layoutNodes = LayoutAdapter.Convert(SymbolicExpressionTree).ToList();
|
---|
54 | layoutNodes.RemoveAt(0);
|
---|
55 | LayoutEngine.Root = layoutNodes[0];
|
---|
56 | LayoutEngine.CalculateLayout();
|
---|
57 |
|
---|
58 | var primitivesMap = new Dictionary<LayoutNode<ISymbolicExpressionTreeNode>, LabeledPrimitive>(); // both Ellipse and Rectangle are derived from the RectangularPrimitiveBase
|
---|
59 | var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel);
|
---|
60 |
|
---|
61 | foreach (var node in layoutNodes) {
|
---|
62 | LabeledPrimitive labeledPrimitive;
|
---|
63 | if (node.Children == null || node.Children.Count == 0) {
|
---|
64 | var rectangle = new Rectangle(Chart, node.X, node.Y, node.X + preferredNodeWidth, node.Y + preferredNodeHeight);
|
---|
65 | var textPrimitive = new TextPrimitive(Chart, rectangle.LowerLeft, rectangle.UpperRight) {
|
---|
66 | Font = font,
|
---|
67 | Text = node.Content.ToString()
|
---|
68 | };
|
---|
69 | labeledPrimitive = new LabeledPrimitive(Chart, rectangle, textPrimitive);
|
---|
70 | } else {
|
---|
71 | var ellipse = new Ellipse(Chart, node.X, node.Y, node.X + preferredNodeWidth, node.Y + preferredNodeHeight);
|
---|
72 | var textPrimitive = new TextPrimitive(Chart, ellipse.LowerLeft, ellipse.UpperRight) {
|
---|
73 | Font = font,
|
---|
74 | Text = node.Content.ToString()
|
---|
75 | };
|
---|
76 | labeledPrimitive = new LabeledPrimitive(Chart, ellipse, textPrimitive);
|
---|
77 | }
|
---|
78 | this.Add(labeledPrimitive);
|
---|
79 | primitivesMap.Add(node, labeledPrimitive);
|
---|
80 | }
|
---|
81 |
|
---|
82 | foreach (var node in layoutNodes.Where(n => n.Children != null)) {
|
---|
83 | foreach (var child in node.Children) {
|
---|
84 | var start = new PointD(node.X + preferredNodeWidth / 2, node.Y + preferredNodeHeight);
|
---|
85 | var end = new PointD(child.X + preferredNodeWidth / 2, child.Y);
|
---|
86 | var line = new Line(this.Chart, start, end);
|
---|
87 | this.Add(line);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|