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