1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Drawing;
|
---|
25 | using System.Drawing.Imaging;
|
---|
26 | using System.IO;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Windows.Forms;
|
---|
29 |
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
32 | public partial class SymbolicExpressionTreeChart : UserControl {
|
---|
33 | private Image image;
|
---|
34 | private readonly StringFormat stringFormat;
|
---|
35 | private Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>> visualTreeNodes;
|
---|
36 | private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection> visualLines;
|
---|
37 | private ILayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
|
---|
38 |
|
---|
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, 8);
|
---|
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
|
---|
65 | private int preferredNodeWidth = 70;
|
---|
66 | public int PreferredNodeWidth {
|
---|
67 | get { return preferredNodeWidth; }
|
---|
68 | set {
|
---|
69 | preferredNodeWidth = value;
|
---|
70 | Repaint();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private int preferredNodeHeight = 46;
|
---|
75 | public int PreferredNodeHeight {
|
---|
76 | get { return preferredNodeHeight; }
|
---|
77 | set {
|
---|
78 | preferredNodeHeight = value;
|
---|
79 | Repaint();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private int minHorizontalDistance = 30;
|
---|
84 | public int MinimumHorizontalDistance {
|
---|
85 | get { return minHorizontalDistance; }
|
---|
86 | set {
|
---|
87 | minHorizontalDistance = value;
|
---|
88 | Repaint();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private int minVerticalDistance = 30;
|
---|
93 | public int MinimumVerticalDistance {
|
---|
94 | get { return minVerticalDistance; }
|
---|
95 | set {
|
---|
96 | minVerticalDistance = value;
|
---|
97 | Repaint();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private int minHorizontalPadding = 20;
|
---|
102 | public int MinimumHorizontalPadding {
|
---|
103 | get { return minHorizontalPadding; }
|
---|
104 | set {
|
---|
105 | minHorizontalPadding = value;
|
---|
106 | Repaint();
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private int minVerticalPadding = 20;
|
---|
111 | public int MinimumVerticalPadding {
|
---|
112 | get { return minVerticalPadding; }
|
---|
113 | set {
|
---|
114 | minVerticalPadding = value;
|
---|
115 | Repaint();
|
---|
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 |
|
---|
146 | private ISymbolicExpressionTree tree;
|
---|
147 | public ISymbolicExpressionTree Tree {
|
---|
148 | get { return this.tree; }
|
---|
149 | set {
|
---|
150 | tree = value;
|
---|
151 | Repaint();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | private bool suspendRepaint;
|
---|
156 | public bool SuspendRepaint {
|
---|
157 | get { return suspendRepaint; }
|
---|
158 | set { suspendRepaint = value; }
|
---|
159 | }
|
---|
160 | #endregion
|
---|
161 |
|
---|
162 | protected override void OnPaint(PaintEventArgs e) {
|
---|
163 | e.Graphics.DrawImage(image, 0, 0);
|
---|
164 | base.OnPaint(e);
|
---|
165 | }
|
---|
166 | protected override void OnResize(EventArgs e) {
|
---|
167 | base.OnResize(e);
|
---|
168 | if (this.Width <= 1 || this.Height <= 1)
|
---|
169 | this.image = new Bitmap(1, 1);
|
---|
170 | else {
|
---|
171 | this.image = new Bitmap(Width, Height);
|
---|
172 | }
|
---|
173 | this.Repaint();
|
---|
174 | }
|
---|
175 |
|
---|
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 |
|
---|
184 | public void Repaint() {
|
---|
185 | if (!suspendRepaint) {
|
---|
186 | this.GenerateImage();
|
---|
187 | this.Refresh();
|
---|
188 | OnRepaint(this, EventArgs.Empty);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | public void RepaintNodes() {
|
---|
193 | if (!suspendRepaint) {
|
---|
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);
|
---|
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 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | this.Refresh();
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) {
|
---|
211 | if (!suspendRepaint) {
|
---|
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 | }
|
---|
217 | this.Refresh();
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void GenerateImage() {
|
---|
222 | using (Graphics graphics = Graphics.FromImage(image)) {
|
---|
223 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
224 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
225 | graphics.Clear(backgroundColor);
|
---|
226 | if (tree != null) {
|
---|
227 | DrawFunctionTree(graphics, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
|
---|
233 | if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
|
---|
234 | return visualTreeNodes[symbolicExpressionTreeNode];
|
---|
235 | return null;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
|
---|
239 | if (child.Parent != parent) throw new ArgumentException();
|
---|
240 | var key = Tuple.Create(parent, child);
|
---|
241 | VisualTreeNodeConnection connection = null;
|
---|
242 | visualLines.TryGetValue(key, out connection);
|
---|
243 | return connection;
|
---|
244 | }
|
---|
245 |
|
---|
246 | #region events
|
---|
247 | public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
|
---|
248 | protected virtual void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
249 | var clicked = SymbolicExpressionTreeNodeClicked;
|
---|
250 | if (clicked != null)
|
---|
251 | clicked(sender, e);
|
---|
252 | }
|
---|
253 |
|
---|
254 | protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
255 | var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
256 | if (visualTreeNode != null) {
|
---|
257 | OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
|
---|
262 | protected virtual void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
263 | var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
|
---|
264 | if (doubleClicked != null)
|
---|
265 | doubleClicked(sender, e);
|
---|
266 | }
|
---|
267 |
|
---|
268 | protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
269 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
270 | if (visualTreeNode != null)
|
---|
271 | OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
|
---|
272 | }
|
---|
273 |
|
---|
274 | public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
|
---|
275 | protected virtual void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
|
---|
276 | var dragged = SymbolicExpressionTreeNodeDrag;
|
---|
277 | if (dragged != null)
|
---|
278 | dragged(sender, e);
|
---|
279 | }
|
---|
280 |
|
---|
281 | private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree;
|
---|
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) {
|
---|
293 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
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 |
|
---|
308 | public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
|
---|
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 |
|
---|
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 |
|
---|
324 | var actualRoot = tree.Root;
|
---|
325 | if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
|
---|
326 | actualRoot = tree.Root.GetSubtree(0);
|
---|
327 | }
|
---|
328 | var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width - MinimumHorizontalPadding, Height - MinimumVerticalPadding).ToList();
|
---|
329 | // add the padding
|
---|
330 | foreach (var vn in visualNodes) {
|
---|
331 | vn.X += MinimumHorizontalPadding / 2;
|
---|
332 | vn.Y += MinimumVerticalPadding / 2;
|
---|
333 | }
|
---|
334 |
|
---|
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());
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | #region methods for painting the symbolic expression tree
|
---|
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);
|
---|
348 |
|
---|
349 | var visualNodes = visualTreeNodes.Values;
|
---|
350 | //draw nodes and connections
|
---|
351 | foreach (var visualNode in visualNodes) {
|
---|
352 | DrawTreeNode(graphics, visualNode);
|
---|
353 | var node = visualNode.Content;
|
---|
354 | foreach (var subtree in node.Subtrees) {
|
---|
355 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
|
---|
356 | var visualSubtree = visualTreeNodes[subtree];
|
---|
357 | var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
|
---|
358 | var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
|
---|
359 | graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
|
---|
360 | using (var linePen = new Pen(visualLine.LineColor)) {
|
---|
361 | linePen.DashStyle = visualLine.DashStyle;
|
---|
362 | graphics.DrawLine(linePen, origin, target);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | private Action<Brush, int, int, int, int> fill;
|
---|
369 | private Action<Pen, int, int, int, int> draw;
|
---|
370 | protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
|
---|
371 | graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1));
|
---|
372 | graphics.Clear(backgroundColor);
|
---|
373 | var node = visualTreeNode.Content;
|
---|
374 | using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
|
---|
375 | using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
|
---|
376 | using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
|
---|
377 | var x = visualTreeNode.X;
|
---|
378 | var y = visualTreeNode.Y;
|
---|
379 | var w = visualTreeNode.Width - 1; // allow 1px for the drawing of the line
|
---|
380 | var h = visualTreeNode.Height - 1; // allow 1px for the drawing of the line
|
---|
381 | // draw leaf nodes as rectangles and internal nodes as ellipses
|
---|
382 | if (node.SubtreeCount > 0) {
|
---|
383 | fill = graphics.FillEllipse; draw = graphics.DrawEllipse;
|
---|
384 | } else {
|
---|
385 | fill = graphics.FillRectangle; draw = graphics.DrawRectangle;
|
---|
386 | }
|
---|
387 | fill(nodeFillBrush, x, y, w, h);
|
---|
388 | draw(nodeLinePen, x, y, w, h);
|
---|
389 | //draw name of symbol
|
---|
390 | graphics.DrawString(node.ToString(), textFont, textBrush, new RectangleF(x, y, w, h), stringFormat);
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
|
---|
395 | var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
|
---|
396 | var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
|
---|
397 | graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
|
---|
398 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
|
---|
399 | using (var linePen = new Pen(visualLine.LineColor)) {
|
---|
400 | linePen.DashStyle = visualLine.DashStyle;
|
---|
401 | graphics.DrawLine(linePen, origin, target);
|
---|
402 | }
|
---|
403 | }
|
---|
404 | #endregion
|
---|
405 | #region save image
|
---|
406 | private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
407 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
408 | string filename = saveFileDialog.FileName.ToLower();
|
---|
409 | if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
|
---|
410 | else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
|
---|
411 | else SaveImageAsBitmap(filename);
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | public void SaveImageAsBitmap(string filename) {
|
---|
416 | if (tree == null) return;
|
---|
417 | Image bitmap = new Bitmap(Width, Height);
|
---|
418 | using (var g = Graphics.FromImage(bitmap)) {
|
---|
419 | DrawFunctionTree(g, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
|
---|
420 | }
|
---|
421 | bitmap.Save(filename);
|
---|
422 | }
|
---|
423 |
|
---|
424 | public void SaveImageAsEmf(string filename) {
|
---|
425 | if (tree == null) return;
|
---|
426 | using (Graphics g = CreateGraphics()) {
|
---|
427 | using (Metafile file = new Metafile(filename, g.GetHdc())) {
|
---|
428 | using (Graphics emfFile = Graphics.FromImage(file)) {
|
---|
429 | DrawFunctionTree(emfFile, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
|
---|
430 | }
|
---|
431 | }
|
---|
432 | g.ReleaseHdc();
|
---|
433 | }
|
---|
434 | }
|
---|
435 | #endregion
|
---|
436 | #region export pgf/tikz
|
---|
437 | private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
438 | var t = Tree;
|
---|
439 | if (t == null) return;
|
---|
440 | using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
|
---|
441 | if (dialog.ShowDialog() != DialogResult.OK) return;
|
---|
442 | string filename = dialog.FileName.ToLower();
|
---|
443 | var formatter = new SymbolicExpressionTreeLatexFormatter();
|
---|
444 | File.WriteAllText(filename, formatter.Format(t));
|
---|
445 | }
|
---|
446 | }
|
---|
447 | #endregion
|
---|
448 |
|
---|
449 | private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
450 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
|
---|
451 | NodeWidth = PreferredNodeWidth,
|
---|
452 | NodeHeight = PreferredNodeHeight,
|
---|
453 | HorizontalSpacing = MinimumHorizontalDistance,
|
---|
454 | VerticalSpacing = MinimumVerticalDistance
|
---|
455 | };
|
---|
456 | reingoldTilfordToolStripMenuItem.Checked = true;
|
---|
457 | boxesToolStripMenuItem.Checked = false;
|
---|
458 | Repaint();
|
---|
459 | }
|
---|
460 |
|
---|
461 | private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
462 | layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
|
---|
463 | NodeWidth = PreferredNodeWidth,
|
---|
464 | NodeHeight = PreferredNodeHeight,
|
---|
465 | HorizontalSpacing = MinimumHorizontalDistance,
|
---|
466 | VerticalSpacing = MinimumVerticalDistance
|
---|
467 | };
|
---|
468 | reingoldTilfordToolStripMenuItem.Checked = false;
|
---|
469 | boxesToolStripMenuItem.Checked = true;
|
---|
470 | Repaint();
|
---|
471 | }
|
---|
472 |
|
---|
473 | private static string ShortLabel(ISymbolicExpressionTreeNode node) {
|
---|
474 | var name = node.ToString();
|
---|
475 |
|
---|
476 | if (node.SubtreeCount > 0) {
|
---|
477 | switch (name) {
|
---|
478 | case "Addition":
|
---|
479 | return "Add";
|
---|
480 | case "Subtraction":
|
---|
481 | return "Sub";
|
---|
482 | case "Multiplication":
|
---|
483 | return "Mul";
|
---|
484 | case "Division":
|
---|
485 | return "Div";
|
---|
486 | case "Exponential":
|
---|
487 | return "Exp";
|
---|
488 | case "Logarithm":
|
---|
489 | return "Log";
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | return name;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | }
|
---|