1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Windows.Forms;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
29 | public sealed partial class SymbolicExpressionTreeChart : UserControl {
|
---|
30 | private Image image;
|
---|
31 | private StringFormat stringFormat;
|
---|
32 | private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
|
---|
33 | private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines;
|
---|
34 |
|
---|
35 | public SymbolicExpressionTreeChart() {
|
---|
36 | InitializeComponent();
|
---|
37 | this.image = new Bitmap(Width, Height);
|
---|
38 | this.stringFormat = new StringFormat();
|
---|
39 | this.stringFormat.Alignment = StringAlignment.Center;
|
---|
40 | this.stringFormat.LineAlignment = StringAlignment.Center;
|
---|
41 | this.spacing = 5;
|
---|
42 | this.lineColor = Color.Black;
|
---|
43 | this.backgroundColor = Color.White;
|
---|
44 | this.textFont = new Font("Times New Roman", 8);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
|
---|
48 | : this() {
|
---|
49 | this.Tree = tree;
|
---|
50 | }
|
---|
51 |
|
---|
52 | private int spacing;
|
---|
53 | public int Spacing {
|
---|
54 | get { return this.spacing; }
|
---|
55 | set {
|
---|
56 | this.spacing = value;
|
---|
57 | this.Repaint();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private Color lineColor;
|
---|
62 | public Color LineColor {
|
---|
63 | get { return this.lineColor; }
|
---|
64 | set {
|
---|
65 | this.lineColor = value;
|
---|
66 | this.Repaint();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private Color backgroundColor;
|
---|
71 | public Color BackgroundColor {
|
---|
72 | get { return this.backgroundColor; }
|
---|
73 | set {
|
---|
74 | this.backgroundColor = value;
|
---|
75 | this.Repaint();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private Font textFont;
|
---|
80 | public Font TextFont {
|
---|
81 | get { return this.textFont; }
|
---|
82 | set {
|
---|
83 | this.textFont = value;
|
---|
84 | this.Repaint();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private ISymbolicExpressionTree tree;
|
---|
89 | public ISymbolicExpressionTree Tree {
|
---|
90 | get { return this.tree; }
|
---|
91 | set {
|
---|
92 | tree = value;
|
---|
93 | visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode>();
|
---|
94 | visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection>();
|
---|
95 | if (tree != null) {
|
---|
96 | foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
|
---|
97 | visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node);
|
---|
98 | if (node.Parent != null) visualLines[Tuple.Create(node.Parent, node)] = new VisualSymbolicExpressionTreeNodeConnection();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | Repaint();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | private bool suspendRepaint;
|
---|
106 | public bool SuspendRepaint {
|
---|
107 | get { return suspendRepaint; }
|
---|
108 | set { suspendRepaint = value; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void OnPaint(PaintEventArgs e) {
|
---|
112 | e.Graphics.DrawImage(image, 0, 0);
|
---|
113 | base.OnPaint(e);
|
---|
114 | }
|
---|
115 | protected override void OnResize(EventArgs e) {
|
---|
116 | base.OnResize(e);
|
---|
117 | if (this.Width <= 1 || this.Height <= 1)
|
---|
118 | this.image = new Bitmap(1, 1);
|
---|
119 | else
|
---|
120 | this.image = new Bitmap(Width, Height);
|
---|
121 | this.Repaint();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void Repaint() {
|
---|
125 | if (!suspendRepaint) {
|
---|
126 | this.GenerateImage();
|
---|
127 | this.Refresh();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void GenerateImage() {
|
---|
132 | using (Graphics graphics = Graphics.FromImage(image)) {
|
---|
133 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
---|
134 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
---|
135 | graphics.Clear(backgroundColor);
|
---|
136 | if (tree != null) {
|
---|
137 | int height = this.Height / tree.Depth;
|
---|
138 | DrawFunctionTree(tree, graphics, 0, 0, this.Width, height);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public VisualSymbolicExpressionTreeNode GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
|
---|
144 | if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
|
---|
145 | return visualTreeNodes[symbolicExpressionTreeNode];
|
---|
146 | return null;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public VisualSymbolicExpressionTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
|
---|
150 | if (child.Parent != parent) throw new ArgumentException();
|
---|
151 | var key = Tuple.Create(parent, child);
|
---|
152 | VisualSymbolicExpressionTreeNodeConnection connection = null;
|
---|
153 | visualLines.TryGetValue(key, out connection);
|
---|
154 | return connection;
|
---|
155 | }
|
---|
156 |
|
---|
157 | #region events
|
---|
158 | public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
|
---|
159 | private void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
160 | var clicked = SymbolicExpressionTreeNodeClicked;
|
---|
161 | if (clicked != null)
|
---|
162 | clicked(sender, e);
|
---|
163 | }
|
---|
164 |
|
---|
165 | private void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
166 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
167 | if (visualTreeNode != null)
|
---|
168 | OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
|
---|
169 | }
|
---|
170 |
|
---|
171 | public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
|
---|
172 | private void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
173 | var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
|
---|
174 | if (doubleClicked != null)
|
---|
175 | doubleClicked(sender, e);
|
---|
176 | }
|
---|
177 |
|
---|
178 | private void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
179 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
180 | if (visualTreeNode != null)
|
---|
181 | OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
|
---|
185 | private void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
|
---|
186 | var dragged = SymbolicExpressionTreeNodeDrag;
|
---|
187 | if (dragged != null)
|
---|
188 | dragged(sender, e);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private VisualSymbolicExpressionTreeNode draggedSymbolicExpressionTree;
|
---|
192 | private MouseButtons dragButtons;
|
---|
193 | private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
|
---|
194 | this.dragButtons = e.Button;
|
---|
195 | this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
196 | }
|
---|
197 | private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
|
---|
198 | this.draggedSymbolicExpressionTree = null;
|
---|
199 | this.dragButtons = MouseButtons.None;
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
|
---|
203 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
204 | if (draggedSymbolicExpressionTree != null &&
|
---|
205 | draggedSymbolicExpressionTree != visualTreeNode) {
|
---|
206 | OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
|
---|
207 | draggedSymbolicExpressionTree = null;
|
---|
208 | } else if (draggedSymbolicExpressionTree == null &&
|
---|
209 | visualTreeNode != null) {
|
---|
210 | string tooltipText = visualTreeNode.ToolTip;
|
---|
211 | if (this.toolTip.GetToolTip(this) != tooltipText)
|
---|
212 | this.toolTip.SetToolTip(this, tooltipText);
|
---|
213 |
|
---|
214 | } else if (visualTreeNode == null)
|
---|
215 | this.toolTip.SetToolTip(this, "");
|
---|
216 | }
|
---|
217 |
|
---|
218 | public VisualSymbolicExpressionTreeNode FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
|
---|
219 | foreach (var visualTreeNode in visualTreeNodes.Values) {
|
---|
220 | if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
|
---|
221 | y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
|
---|
222 | return visualTreeNode;
|
---|
223 | }
|
---|
224 | return null;
|
---|
225 | }
|
---|
226 | #endregion
|
---|
227 |
|
---|
228 | #region methods for painting the symbolic expression tree
|
---|
229 | private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) {
|
---|
230 | DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /// <summary>
|
---|
234 | ///
|
---|
235 | /// </summary>
|
---|
236 | /// <param name="functionTree"> functiontree to draw</param>
|
---|
237 | /// <param name="graphics">graphics object to draw on</param>
|
---|
238 | /// <param name="x">x coordinate of drawing area</param>
|
---|
239 | /// <param name="y">y coordinate of drawing area</param>
|
---|
240 | /// <param name="width">width of drawing area</param>
|
---|
241 | /// <param name="height">height of drawing area</param>
|
---|
242 | private void DrawFunctionTree(ISymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) {
|
---|
243 | VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node];
|
---|
244 | float center_x = x + width / 2;
|
---|
245 | float center_y = y + height / 2;
|
---|
246 | int actualWidth = width - spacing;
|
---|
247 | int actualHeight = height - spacing;
|
---|
248 |
|
---|
249 | SolidBrush textBrush = new SolidBrush(visualTreeNode.TextColor);
|
---|
250 | Pen nodeLinePen = new Pen(visualTreeNode.LineColor);
|
---|
251 | SolidBrush nodeFillBrush = new SolidBrush(visualTreeNode.FillColor);
|
---|
252 |
|
---|
253 | //calculate size of node
|
---|
254 | if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
|
---|
255 | visualTreeNode.Width = visualTreeNode.PreferredWidth;
|
---|
256 | visualTreeNode.Height = visualTreeNode.PreferredHeight;
|
---|
257 | visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
|
---|
258 | visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
|
---|
259 | }
|
---|
260 | //width too small to draw in desired sized
|
---|
261 | else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
|
---|
262 | visualTreeNode.Width = actualWidth;
|
---|
263 | visualTreeNode.Height = visualTreeNode.PreferredHeight;
|
---|
264 | visualTreeNode.X = x;
|
---|
265 | visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
|
---|
266 | }
|
---|
267 | //height too small to draw in desired sized
|
---|
268 | else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) {
|
---|
269 | visualTreeNode.Width = visualTreeNode.PreferredWidth;
|
---|
270 | visualTreeNode.Height = actualHeight;
|
---|
271 | visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
|
---|
272 | visualTreeNode.Y = y;
|
---|
273 | }
|
---|
274 | //width and height too small to draw in desired size
|
---|
275 | else {
|
---|
276 | visualTreeNode.Width = actualWidth;
|
---|
277 | visualTreeNode.Height = actualHeight;
|
---|
278 | visualTreeNode.X = x;
|
---|
279 | visualTreeNode.Y = y;
|
---|
280 | }
|
---|
281 |
|
---|
282 | //draw terminal node
|
---|
283 | if (node.SubtreeCount == 0) {
|
---|
284 | graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
285 | graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
286 | } else {
|
---|
287 | graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
288 | graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
|
---|
289 | }
|
---|
290 |
|
---|
291 | //draw name of symbol
|
---|
292 | var text = node.ToString();
|
---|
293 | graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
|
---|
294 |
|
---|
295 | //draw connection line to parent node
|
---|
296 | if (!connectionPoint.IsEmpty && node.Parent != null) {
|
---|
297 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node.Parent, node);
|
---|
298 | using (Pen linePen = new Pen(visualLine.LineColor)) {
|
---|
299 | linePen.DashStyle = visualLine.DashStyle;
|
---|
300 | graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | //calculate areas for the subtrees according to their tree size and call drawFunctionTree
|
---|
305 | Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height);
|
---|
306 | int[] xBoundaries = new int[node.SubtreeCount + 1];
|
---|
307 | xBoundaries[0] = x;
|
---|
308 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
309 | xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.GetSubtree(i).GetLength()) / (node.GetLength() - 1));
|
---|
310 | DrawFunctionTree(node.GetSubtree(i), graphics, xBoundaries[i], y + height,
|
---|
311 | xBoundaries[i + 1] - xBoundaries[i], height, connectFrom);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | #endregion
|
---|
315 |
|
---|
316 | #region save image
|
---|
317 | private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
318 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
319 | string filename = saveFileDialog.FileName.ToLower();
|
---|
320 | if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
|
---|
321 | else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
|
---|
322 | else SaveImageAsBitmap(filename);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | private void SaveImageAsBitmap(string filename) {
|
---|
327 | if (tree == null) return;
|
---|
328 | Image image = new Bitmap(Width, Height);
|
---|
329 | using (Graphics g = Graphics.FromImage(image)) {
|
---|
330 | int height = this.Height / tree.Depth;
|
---|
331 | DrawFunctionTree(tree, g, 0, 0, Width, height);
|
---|
332 | }
|
---|
333 | image.Save(filename);
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void SaveImageAsEmf(string filename) {
|
---|
337 | if (tree == null) return;
|
---|
338 | using (Graphics g = CreateGraphics()) {
|
---|
339 | using (Metafile file = new Metafile(filename, g.GetHdc())) {
|
---|
340 | using (Graphics emfFile = Graphics.FromImage(file)) {
|
---|
341 | int height = this.Height / tree.Depth;
|
---|
342 | DrawFunctionTree(tree, emfFile, 0, 0, Width, height);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | g.ReleaseHdc();
|
---|
346 | }
|
---|
347 | }
|
---|
348 | #endregion
|
---|
349 | }
|
---|
350 | }
|
---|