[3244] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
[3294] | 32 | public sealed partial class SymbolicExpressionTreeChart : UserControl {
|
---|
[3244] | 33 | private StringFormat stringFormat;
|
---|
| 34 | private Dictionary<SymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
|
---|
| 35 |
|
---|
| 36 | public SymbolicExpressionTreeChart() {
|
---|
| 37 | InitializeComponent();
|
---|
| 38 | this.stringFormat = new StringFormat();
|
---|
| 39 | stringFormat.Alignment = StringAlignment.Center;
|
---|
| 40 | stringFormat.LineAlignment = StringAlignment.Center;
|
---|
| 41 | pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
| 42 | this.spacing = 5;
|
---|
| 43 | this.lineColor = Color.Black;
|
---|
| 44 | this.backgroundColor = Color.White;
|
---|
| 45 | this.textFont = new Font("Times New Roman", 8);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public SymbolicExpressionTreeChart(SymbolicExpressionTree tree)
|
---|
| 49 | : this() {
|
---|
| 50 | this.Tree = tree;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private int spacing;
|
---|
| 54 | public int Spacing {
|
---|
| 55 | get { return this.spacing; }
|
---|
| 56 | set {
|
---|
| 57 | this.spacing = value;
|
---|
| 58 | this.Repaint();
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | private Color lineColor;
|
---|
| 63 | public Color LineColor {
|
---|
| 64 | get { return this.lineColor; }
|
---|
| 65 | set {
|
---|
| 66 | this.lineColor = value;
|
---|
| 67 | this.Repaint();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private Color backgroundColor;
|
---|
| 72 | public Color BackgroundColor {
|
---|
| 73 | get { return this.backgroundColor; }
|
---|
| 74 | set {
|
---|
| 75 | this.backgroundColor = value;
|
---|
| 76 | this.Repaint();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private Font textFont;
|
---|
| 81 | public Font TextFont {
|
---|
| 82 | get { return this.textFont; }
|
---|
| 83 | set {
|
---|
| 84 | this.textFont = value;
|
---|
| 85 | this.Repaint();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | private SymbolicExpressionTree tree;
|
---|
| 90 | public SymbolicExpressionTree Tree {
|
---|
| 91 | get { return this.tree; }
|
---|
| 92 | set {
|
---|
| 93 | tree = value;
|
---|
| 94 | visualTreeNodes = new Dictionary<SymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode>();
|
---|
| 95 | if (tree != null) {
|
---|
| 96 | foreach (SymbolicExpressionTreeNode node in tree.IterateNodesPrefix())
|
---|
| 97 | visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node);
|
---|
| 98 | }
|
---|
| 99 | Repaint();
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | public void Repaint() {
|
---|
| 105 | using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
|
---|
| 106 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
| 107 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
| 108 | graphics.Clear(backgroundColor);
|
---|
| 109 | if (tree != null) {
|
---|
| 110 | int height = this.Height / tree.Height;
|
---|
| 111 | DrawFunctionTree(tree, graphics, 0, 0, this.Width, height);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | pictureBox.Refresh();
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | #region events
|
---|
| 119 | public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
|
---|
| 120 | private void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
| 121 | var clicked = SymbolicExpressionTreeNodeClicked;
|
---|
| 122 | if (clicked != null)
|
---|
| 123 | clicked(sender, e);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
| 127 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
| 128 | if (visualTreeNode != null)
|
---|
| 129 | OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
|
---|
| 133 | private void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
| 134 | var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
|
---|
| 135 | if (doubleClicked != null)
|
---|
| 136 | doubleClicked(sender, e);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | private void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
| 140 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
| 141 | if (visualTreeNode != null)
|
---|
| 142 | OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
|
---|
| 146 | private void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
|
---|
| 147 | var dragged = SymbolicExpressionTreeNodeDrag;
|
---|
| 148 | if (dragged != null)
|
---|
| 149 | dragged(sender, e);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private VisualSymbolicExpressionTreeNode draggedSymbolicExpressionTree;
|
---|
| 153 | private MouseButtons dragButtons;
|
---|
| 154 | private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 155 | this.dragButtons = e.Button;
|
---|
| 156 | this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
| 157 | }
|
---|
| 158 | private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 159 | this.draggedSymbolicExpressionTree = null;
|
---|
| 160 | this.dragButtons = MouseButtons.None;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 164 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
| 165 | if (draggedSymbolicExpressionTree != null &&
|
---|
| 166 | draggedSymbolicExpressionTree != visualTreeNode) {
|
---|
| 167 | OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
|
---|
| 168 | draggedSymbolicExpressionTree = null;
|
---|
| 169 | } else if (draggedSymbolicExpressionTree == null &&
|
---|
| 170 | visualTreeNode != null) {
|
---|
| 171 | string tooltipText = visualTreeNode.ToolTip;
|
---|
| 172 | if (this.toolTip.GetToolTip(this) != tooltipText)
|
---|
| 173 | this.toolTip.SetToolTip(this, tooltipText);
|
---|
| 174 |
|
---|
| 175 | } else if (visualTreeNode == null)
|
---|
| 176 | this.toolTip.SetToolTip(this, "");
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | private VisualSymbolicExpressionTreeNode FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
|
---|
| 180 | foreach (var visualTreeNode in visualTreeNodes.Values) {
|
---|
| 181 | if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
|
---|
| 182 | y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
|
---|
| 183 | return visualTreeNode;
|
---|
| 184 | }
|
---|
| 185 | return null;
|
---|
| 186 | }
|
---|
| 187 | #endregion
|
---|
| 188 |
|
---|
| 189 | #region methods for painting the symbolic expression tree
|
---|
| 190 | protected override void OnResize(EventArgs e) {
|
---|
| 191 | base.OnResize(e);
|
---|
| 192 | pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
| 193 | Repaint();
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | private void DrawFunctionTree(SymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) {
|
---|
| 197 | DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /// <summary>
|
---|
| 201 | ///
|
---|
| 202 | /// </summary>
|
---|
| 203 | /// <param name="functionTree"> functiontree to draw</param>
|
---|
| 204 | /// <param name="graphics">graphics object to draw on</param>
|
---|
| 205 | /// <param name="x">x coordinate of drawing area</param>
|
---|
| 206 | /// <param name="y">y coordinate of drawing area</param>
|
---|
| 207 | /// <param name="width">width of drawing area</param>
|
---|
| 208 | /// <param name="height">height of drawing area</param>
|
---|
| 209 | private void DrawFunctionTree(SymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) {
|
---|
| 210 | VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node];
|
---|
| 211 | float center_x = x + width / 2;
|
---|
| 212 | float center_y = y + height / 2;
|
---|
| 213 | int actualWidth = width - spacing;
|
---|
| 214 | int actualHeight = height - spacing;
|
---|
| 215 |
|
---|
| 216 | SolidBrush textBrush = new SolidBrush(visualTreeNode.TextColor);
|
---|
| 217 | Pen linePen = new Pen(this.lineColor);
|
---|
| 218 | Pen nodeLinePen = new Pen(visualTreeNode.LineColor);
|
---|
| 219 | SolidBrush nodeFillBrush = new SolidBrush(visualTreeNode.FillColor);
|
---|
| 220 |
|
---|
| 221 | //calculate size of node
|
---|
| 222 | if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
|
---|
| 223 | visualTreeNode.Width = visualTreeNode.PreferredWidth;
|
---|
| 224 | visualTreeNode.Height = visualTreeNode.PreferredHeight;
|
---|
| 225 | visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
|
---|
| 226 | visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
|
---|
| 227 | }
|
---|
| 228 | //width too small to draw in desired sized
|
---|
| 229 | else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
|
---|
| 230 | visualTreeNode.Width = actualWidth;
|
---|
| 231 | visualTreeNode.Height = visualTreeNode.PreferredHeight;
|
---|
| 232 | visualTreeNode.X = x;
|
---|
| 233 | visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
|
---|
| 234 | }
|
---|
| 235 | //height too small to draw in desired sized
|
---|
| 236 | else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) {
|
---|
| 237 | visualTreeNode.Width = visualTreeNode.PreferredWidth;
|
---|
| 238 | visualTreeNode.Height = actualHeight;
|
---|
| 239 | visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
|
---|
| 240 | visualTreeNode.Y = y;
|
---|
| 241 | }
|
---|
| 242 | //width and height too small to draw in desired size
|
---|
| 243 | else {
|
---|
| 244 | visualTreeNode.Width = actualWidth;
|
---|
| 245 | visualTreeNode.Height = actualHeight;
|
---|
| 246 | visualTreeNode.X = x;
|
---|
| 247 | visualTreeNode.Y = y;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | //draw terminal node
|
---|
| 251 | if (node.SubTrees.Count == 0) {
|
---|
| 252 | graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
| 253 | graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
| 254 | } else {
|
---|
| 255 | graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
| 256 | graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | //draw name of symbol
|
---|
| 260 | graphics.DrawString(node.Symbol.Name, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
|
---|
| 261 |
|
---|
| 262 | //draw connection line to parent node
|
---|
| 263 | if (!connectionPoint.IsEmpty)
|
---|
| 264 | graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));
|
---|
| 265 |
|
---|
| 266 | //calculate areas for the subtrees according to their tree size and call drawFunctionTree
|
---|
| 267 | Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height);
|
---|
| 268 | int[] xBoundaries = new int[node.SubTrees.Count + 1];
|
---|
| 269 | xBoundaries[0] = x;
|
---|
| 270 | for (int i = 0; i < node.SubTrees.Count; i++) {
|
---|
| 271 | xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.SubTrees[i].GetSize()) / (node.GetSize() - 1));
|
---|
| 272 | DrawFunctionTree(node.SubTrees[i], graphics, xBoundaries[i], y + height,
|
---|
| 273 | xBoundaries[i + 1] - xBoundaries[i], height, connectFrom);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | #endregion
|
---|
| 277 |
|
---|
| 278 | }
|
---|
| 279 | }
|
---|