[3244] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16140] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3244] | 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.Drawing;
|
---|
[4651] | 25 | using System.Drawing.Imaging;
|
---|
[10496] | 26 | using System.IO;
|
---|
| 27 | using System.Linq;
|
---|
[3244] | 28 | using System.Windows.Forms;
|
---|
| 29 |
|
---|
[10565] | 30 |
|
---|
[3244] | 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
[8942] | 32 | public partial class SymbolicExpressionTreeChart : UserControl {
|
---|
[3470] | 33 | private Image image;
|
---|
[10496] | 34 | private readonly StringFormat stringFormat;
|
---|
[10520] | 35 | private Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>> visualTreeNodes;
|
---|
| 36 | private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection> visualLines;
|
---|
| 37 | private ILayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
|
---|
[3244] | 38 |
|
---|
[15044] | 39 | public SymbolicExpressionTreeChart() {
|
---|
| 40 | InitializeComponent();
|
---|
| 41 | this.image = new Bitmap(Width, Height);
|
---|
| 42 | this.stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
|
---|
| 43 | this.lineColor = Color.Black;
|
---|
| 44 | this.backgroundColor = Color.White;
|
---|
| 45 | this.textFont = new Font(FontFamily.GenericSansSerif, 12);
|
---|
| 46 |
|
---|
| 47 | visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>>();
|
---|
| 48 | visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
|
---|
| 49 |
|
---|
| 50 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
|
---|
| 51 | NodeWidth = PreferredNodeWidth,
|
---|
| 52 | NodeHeight = PreferredNodeHeight,
|
---|
| 53 | HorizontalSpacing = MinimumHorizontalDistance,
|
---|
| 54 | VerticalSpacing = MinimumVerticalDistance
|
---|
| 55 | };
|
---|
| 56 | reingoldTilfordToolStripMenuItem.Checked = true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
|
---|
| 60 | : this() {
|
---|
| 61 | this.Tree = tree;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | #region Public properties
|
---|
[15040] | 65 | private int preferredNodeWidth = 70;
|
---|
| 66 | public int PreferredNodeWidth {
|
---|
| 67 | get { return preferredNodeWidth; }
|
---|
[15044] | 68 | set {
|
---|
| 69 | preferredNodeWidth = value;
|
---|
| 70 | Repaint();
|
---|
| 71 | }
|
---|
[15040] | 72 | }
|
---|
| 73 |
|
---|
| 74 | private int preferredNodeHeight = 46;
|
---|
| 75 | public int PreferredNodeHeight {
|
---|
| 76 | get { return preferredNodeHeight; }
|
---|
[15044] | 77 | set {
|
---|
| 78 | preferredNodeHeight = value;
|
---|
| 79 | Repaint();
|
---|
| 80 | }
|
---|
[15040] | 81 | }
|
---|
| 82 |
|
---|
[10799] | 83 | private int minHorizontalDistance = 30;
|
---|
[15040] | 84 | public int MinimumHorizontalDistance {
|
---|
| 85 | get { return minHorizontalDistance; }
|
---|
[15044] | 86 | set {
|
---|
| 87 | minHorizontalDistance = value;
|
---|
| 88 | Repaint();
|
---|
| 89 | }
|
---|
[15040] | 90 | }
|
---|
| 91 |
|
---|
[10799] | 92 | private int minVerticalDistance = 30;
|
---|
[15040] | 93 | public int MinimumVerticalDistance {
|
---|
| 94 | get { return minVerticalDistance; }
|
---|
[15044] | 95 | set {
|
---|
| 96 | minVerticalDistance = value;
|
---|
| 97 | Repaint();
|
---|
| 98 | }
|
---|
[15040] | 99 | }
|
---|
[10496] | 100 |
|
---|
[15040] | 101 | private int minHorizontalPadding = 20;
|
---|
| 102 | public int MinimumHorizontalPadding {
|
---|
| 103 | get { return minHorizontalPadding; }
|
---|
[15044] | 104 | set {
|
---|
| 105 | minHorizontalPadding = value;
|
---|
| 106 | Repaint();
|
---|
| 107 | }
|
---|
[15040] | 108 | }
|
---|
| 109 |
|
---|
| 110 | private int minVerticalPadding = 20;
|
---|
| 111 | public int MinimumVerticalPadding {
|
---|
| 112 | get { return minVerticalPadding; }
|
---|
[3244] | 113 | set {
|
---|
[15044] | 114 | minVerticalPadding = value;
|
---|
| 115 | Repaint();
|
---|
[3244] | 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | private Color lineColor;
|
---|
| 120 | public Color LineColor {
|
---|
| 121 | get { return this.lineColor; }
|
---|
| 122 | set {
|
---|
| 123 | this.lineColor = value;
|
---|
| 124 | this.Repaint();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private Color backgroundColor;
|
---|
| 129 | public Color BackgroundColor {
|
---|
| 130 | get { return this.backgroundColor; }
|
---|
| 131 | set {
|
---|
| 132 | this.backgroundColor = value;
|
---|
| 133 | this.Repaint();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | private Font textFont;
|
---|
| 138 | public Font TextFont {
|
---|
| 139 | get { return this.textFont; }
|
---|
| 140 | set {
|
---|
| 141 | this.textFont = value;
|
---|
| 142 | this.Repaint();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[5513] | 146 | private ISymbolicExpressionTree tree;
|
---|
| 147 | public ISymbolicExpressionTree Tree {
|
---|
[3244] | 148 | get { return this.tree; }
|
---|
| 149 | set {
|
---|
| 150 | tree = value;
|
---|
[10565] | 151 | Repaint();
|
---|
[3244] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[6803] | 155 | private bool suspendRepaint;
|
---|
| 156 | public bool SuspendRepaint {
|
---|
| 157 | get { return suspendRepaint; }
|
---|
| 158 | set { suspendRepaint = value; }
|
---|
| 159 | }
|
---|
[10496] | 160 | #endregion
|
---|
[6803] | 161 |
|
---|
[3470] | 162 | protected override void OnPaint(PaintEventArgs e) {
|
---|
[6803] | 163 | e.Graphics.DrawImage(image, 0, 0);
|
---|
[3470] | 164 | base.OnPaint(e);
|
---|
| 165 | }
|
---|
| 166 | protected override void OnResize(EventArgs e) {
|
---|
| 167 | base.OnResize(e);
|
---|
[5956] | 168 | if (this.Width <= 1 || this.Height <= 1)
|
---|
[3470] | 169 | this.image = new Bitmap(1, 1);
|
---|
[10561] | 170 | else {
|
---|
[3470] | 171 | this.image = new Bitmap(Width, Height);
|
---|
[10561] | 172 | }
|
---|
[3470] | 173 | this.Repaint();
|
---|
| 174 | }
|
---|
[3244] | 175 |
|
---|
[10561] | 176 | public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted
|
---|
| 177 | protected virtual void OnRepaint(object sender, EventArgs e) {
|
---|
| 178 | var repainted = Repainted;
|
---|
| 179 | if (repainted != null) {
|
---|
| 180 | repainted(sender, e);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[3244] | 184 | public void Repaint() {
|
---|
[6803] | 185 | if (!suspendRepaint) {
|
---|
| 186 | this.GenerateImage();
|
---|
| 187 | this.Refresh();
|
---|
[10561] | 188 | OnRepaint(this, EventArgs.Empty);
|
---|
[6803] | 189 | }
|
---|
[3470] | 190 | }
|
---|
| 191 |
|
---|
[8980] | 192 | public void RepaintNodes() {
|
---|
| 193 | if (!suspendRepaint) {
|
---|
[8986] | 194 | using (var graphics = Graphics.FromImage(image)) {
|
---|
| 195 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
| 196 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
| 197 | foreach (var visualNode in visualTreeNodes.Values) {
|
---|
| 198 | DrawTreeNode(graphics, visualNode);
|
---|
[10561] | 199 | if (visualNode.Content.SubtreeCount > 0) {
|
---|
| 200 | foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) {
|
---|
| 201 | DrawLine(graphics, visualNode, visualSubtree);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
[8986] | 204 | }
|
---|
[8980] | 205 | }
|
---|
| 206 | this.Refresh();
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[10520] | 210 | public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) {
|
---|
[8980] | 211 | if (!suspendRepaint) {
|
---|
[8986] | 212 | using (var graphics = Graphics.FromImage(image)) {
|
---|
| 213 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
| 214 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
| 215 | DrawTreeNode(graphics, visualNode);
|
---|
| 216 | }
|
---|
[8980] | 217 | this.Refresh();
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[3470] | 221 | private void GenerateImage() {
|
---|
| 222 | using (Graphics graphics = Graphics.FromImage(image)) {
|
---|
[3244] | 223 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
| 224 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
| 225 | graphics.Clear(backgroundColor);
|
---|
| 226 | if (tree != null) {
|
---|
[15044] | 227 | DrawFunctionTree(graphics, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance);
|
---|
[3244] | 228 | }
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[10520] | 232 | public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
|
---|
[3915] | 233 | if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
|
---|
| 234 | return visualTreeNodes[symbolicExpressionTreeNode];
|
---|
| 235 | return null;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[10520] | 238 | public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
|
---|
[6803] | 239 | if (child.Parent != parent) throw new ArgumentException();
|
---|
| 240 | var key = Tuple.Create(parent, child);
|
---|
[10520] | 241 | VisualTreeNodeConnection connection = null;
|
---|
[6803] | 242 | visualLines.TryGetValue(key, out connection);
|
---|
| 243 | return connection;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[3244] | 246 | #region events
|
---|
| 247 | public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
|
---|
[8942] | 248 | protected virtual void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
[3244] | 249 | var clicked = SymbolicExpressionTreeNodeClicked;
|
---|
| 250 | if (clicked != null)
|
---|
| 251 | clicked(sender, e);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[9043] | 254 | protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
[10520] | 255 | var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
[8942] | 256 | if (visualTreeNode != null) {
|
---|
[3244] | 257 | OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
|
---|
[8942] | 258 | }
|
---|
[3244] | 259 | }
|
---|
| 260 |
|
---|
| 261 | public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
|
---|
[8942] | 262 | protected virtual void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
[3244] | 263 | var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
|
---|
| 264 | if (doubleClicked != null)
|
---|
| 265 | doubleClicked(sender, e);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[9043] | 268 | protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
[10520] | 269 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
[3244] | 270 | if (visualTreeNode != null)
|
---|
| 271 | OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
|
---|
[8942] | 275 | protected virtual void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
|
---|
[3244] | 276 | var dragged = SymbolicExpressionTreeNodeDrag;
|
---|
| 277 | if (dragged != null)
|
---|
| 278 | dragged(sender, e);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[10520] | 281 | private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree;
|
---|
[3244] | 282 | private MouseButtons dragButtons;
|
---|
| 283 | private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 284 | this.dragButtons = e.Button;
|
---|
| 285 | this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
| 286 | }
|
---|
| 287 | private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 288 | this.draggedSymbolicExpressionTree = null;
|
---|
| 289 | this.dragButtons = MouseButtons.None;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
|
---|
[10520] | 293 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
[3244] | 294 | if (draggedSymbolicExpressionTree != null &&
|
---|
| 295 | draggedSymbolicExpressionTree != visualTreeNode) {
|
---|
| 296 | OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
|
---|
| 297 | draggedSymbolicExpressionTree = null;
|
---|
| 298 | } else if (draggedSymbolicExpressionTree == null &&
|
---|
| 299 | visualTreeNode != null) {
|
---|
| 300 | string tooltipText = visualTreeNode.ToolTip;
|
---|
| 301 | if (this.toolTip.GetToolTip(this) != tooltipText)
|
---|
| 302 | this.toolTip.SetToolTip(this, tooltipText);
|
---|
| 303 |
|
---|
| 304 | } else if (visualTreeNode == null)
|
---|
| 305 | this.toolTip.SetToolTip(this, "");
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[10520] | 308 | public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
|
---|
[3244] | 309 | foreach (var visualTreeNode in visualTreeNodes.Values) {
|
---|
| 310 | if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
|
---|
| 311 | y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
|
---|
| 312 | return visualTreeNode;
|
---|
| 313 | }
|
---|
| 314 | return null;
|
---|
| 315 | }
|
---|
| 316 | #endregion
|
---|
| 317 |
|
---|
[10565] | 318 | private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
|
---|
| 319 | layoutEngine.NodeWidth = preferredWidth;
|
---|
| 320 | layoutEngine.NodeHeight = preferredHeight;
|
---|
| 321 | layoutEngine.HorizontalSpacing = minHDistance;
|
---|
| 322 | layoutEngine.VerticalSpacing = minVDistance;
|
---|
| 323 |
|
---|
[10561] | 324 | var actualRoot = tree.Root;
|
---|
| 325 | if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
|
---|
| 326 | actualRoot = tree.Root.GetSubtree(0);
|
---|
| 327 | }
|
---|
[15044] | 328 | var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width - MinimumHorizontalPadding, Height - MinimumVerticalPadding).ToList();
|
---|
[15029] | 329 | // add the padding
|
---|
| 330 | foreach (var vn in visualNodes) {
|
---|
[15044] | 331 | vn.X += MinimumHorizontalPadding / 2;
|
---|
| 332 | vn.Y += MinimumVerticalPadding / 2;
|
---|
[15029] | 333 | }
|
---|
[10561] | 334 |
|
---|
[10520] | 335 | visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x);
|
---|
| 336 | visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
|
---|
| 337 | foreach (var node in visualNodes.Select(n => n.Content)) {
|
---|
| 338 | foreach (var subtree in node.Subtrees) {
|
---|
| 339 | visualLines.Add(new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree), new VisualTreeNodeConnection());
|
---|
[8986] | 340 | }
|
---|
[10496] | 341 | }
|
---|
[10531] | 342 | }
|
---|
[10565] | 343 |
|
---|
[10531] | 344 | #region methods for painting the symbolic expression tree
|
---|
[12237] | 345 | private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance, bool recalculateLayout = true) {
|
---|
| 346 | if (recalculateLayout)
|
---|
| 347 | CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance);
|
---|
[10531] | 348 | var visualNodes = visualTreeNodes.Values;
|
---|
| 349 | //draw nodes and connections
|
---|
[10520] | 350 | foreach (var visualNode in visualNodes) {
|
---|
[10885] | 351 | DrawTreeNode(graphics, visualNode);
|
---|
[10520] | 352 | var node = visualNode.Content;
|
---|
[10496] | 353 | foreach (var subtree in node.Subtrees) {
|
---|
| 354 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
|
---|
| 355 | var visualSubtree = visualTreeNodes[subtree];
|
---|
| 356 | var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
|
---|
| 357 | var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
|
---|
| 358 | graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
|
---|
| 359 | using (var linePen = new Pen(visualLine.LineColor)) {
|
---|
[8986] | 360 | linePen.DashStyle = visualLine.DashStyle;
|
---|
[10496] | 361 | graphics.DrawLine(linePen, origin, target);
|
---|
[8986] | 362 | }
|
---|
[6803] | 363 | }
|
---|
[3244] | 364 | }
|
---|
| 365 | }
|
---|
[8980] | 366 |
|
---|
[15029] | 367 | private Action<Brush, int, int, int, int> fill;
|
---|
| 368 | private Action<Pen, int, int, int, int> draw;
|
---|
[10520] | 369 | protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
|
---|
[8980] | 370 | graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1));
|
---|
| 371 | graphics.Clear(backgroundColor);
|
---|
[10520] | 372 | var node = visualTreeNode.Content;
|
---|
[8986] | 373 | using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
|
---|
| 374 | using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
|
---|
| 375 | using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
|
---|
[15029] | 376 | var x = visualTreeNode.X;
|
---|
| 377 | var y = visualTreeNode.Y;
|
---|
| 378 | var w = visualTreeNode.Width - 1; // allow 1px for the drawing of the line
|
---|
| 379 | var h = visualTreeNode.Height - 1; // allow 1px for the drawing of the line
|
---|
| 380 | // draw leaf nodes as rectangles and internal nodes as ellipses
|
---|
| 381 | if (node.SubtreeCount > 0) {
|
---|
| 382 | fill = graphics.FillEllipse; draw = graphics.DrawEllipse;
|
---|
[8986] | 383 | } else {
|
---|
[15029] | 384 | fill = graphics.FillRectangle; draw = graphics.DrawRectangle;
|
---|
[8986] | 385 | }
|
---|
[15029] | 386 | fill(nodeFillBrush, x, y, w, h);
|
---|
| 387 | draw(nodeLinePen, x, y, w, h);
|
---|
[8986] | 388 | //draw name of symbol
|
---|
[15029] | 389 | graphics.DrawString(node.ToString(), textFont, textBrush, new RectangleF(x, y, w, h), stringFormat);
|
---|
[8980] | 390 | }
|
---|
| 391 | }
|
---|
[10561] | 392 |
|
---|
| 393 | protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
|
---|
| 394 | var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
|
---|
| 395 | var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
|
---|
| 396 | graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
|
---|
| 397 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
|
---|
| 398 | using (var linePen = new Pen(visualLine.LineColor)) {
|
---|
| 399 | linePen.DashStyle = visualLine.DashStyle;
|
---|
| 400 | graphics.DrawLine(linePen, origin, target);
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
[3244] | 403 | #endregion
|
---|
[4651] | 404 | #region save image
|
---|
| 405 | private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 406 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 407 | string filename = saveFileDialog.FileName.ToLower();
|
---|
| 408 | if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
|
---|
| 409 | else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
|
---|
| 410 | else SaveImageAsBitmap(filename);
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[9587] | 414 | public void SaveImageAsBitmap(string filename) {
|
---|
[4651] | 415 | if (tree == null) return;
|
---|
| 416 | Image image = new Bitmap(Width, Height);
|
---|
| 417 | using (Graphics g = Graphics.FromImage(image)) {
|
---|
[15044] | 418 | DrawFunctionTree(g, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
|
---|
[4651] | 419 | }
|
---|
| 420 | image.Save(filename);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[9587] | 423 | public void SaveImageAsEmf(string filename) {
|
---|
[4651] | 424 | if (tree == null) return;
|
---|
| 425 | using (Graphics g = CreateGraphics()) {
|
---|
| 426 | using (Metafile file = new Metafile(filename, g.GetHdc())) {
|
---|
| 427 | using (Graphics emfFile = Graphics.FromImage(file)) {
|
---|
[15044] | 428 | DrawFunctionTree(emfFile, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
|
---|
[4651] | 429 | }
|
---|
| 430 | }
|
---|
| 431 | g.ReleaseHdc();
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 | #endregion
|
---|
[10496] | 435 | #region export pgf/tikz
|
---|
| 436 | private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[11065] | 437 | var t = Tree;
|
---|
| 438 | if (t == null) return;
|
---|
[10496] | 439 | using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
|
---|
| 440 | if (dialog.ShowDialog() != DialogResult.OK) return;
|
---|
| 441 | string filename = dialog.FileName.ToLower();
|
---|
| 442 | var formatter = new SymbolicExpressionTreeLatexFormatter();
|
---|
[11065] | 443 | File.WriteAllText(filename, formatter.Format(t));
|
---|
[10496] | 444 | }
|
---|
| 445 | }
|
---|
| 446 | #endregion
|
---|
[10520] | 447 |
|
---|
| 448 | private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[10565] | 449 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
|
---|
[15044] | 450 | NodeWidth = PreferredNodeWidth,
|
---|
| 451 | NodeHeight = PreferredNodeHeight,
|
---|
| 452 | HorizontalSpacing = MinimumHorizontalDistance,
|
---|
| 453 | VerticalSpacing = MinimumVerticalDistance
|
---|
[10520] | 454 | };
|
---|
[10565] | 455 | reingoldTilfordToolStripMenuItem.Checked = true;
|
---|
| 456 | boxesToolStripMenuItem.Checked = false;
|
---|
| 457 | Repaint();
|
---|
[10520] | 458 | }
|
---|
| 459 |
|
---|
| 460 | private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[10565] | 461 | layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
|
---|
[15044] | 462 | NodeWidth = PreferredNodeWidth,
|
---|
| 463 | NodeHeight = PreferredNodeHeight,
|
---|
| 464 | HorizontalSpacing = MinimumHorizontalDistance,
|
---|
| 465 | VerticalSpacing = MinimumVerticalDistance
|
---|
[10520] | 466 | };
|
---|
[10565] | 467 | reingoldTilfordToolStripMenuItem.Checked = false;
|
---|
| 468 | boxesToolStripMenuItem.Checked = true;
|
---|
| 469 | Repaint();
|
---|
[10520] | 470 | }
|
---|
[3244] | 471 | }
|
---|
| 472 | }
|
---|