Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs @ 18068

Last change on this file since 18068 was 18068, checked in by dpiringe, 3 years ago

#3136

  • modified the StructureTemplateView to enable colorful tree nodes of type SubFunctionTreeNode
  • refactored SubFunctionTreeNode, SubFunction and StructureTemplate
File size: 2.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Collections;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
11using HeuristicLab.MainForm;
12using HeuristicLab.MainForm.WindowsForms;
13
14namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
15
16  [View("StructureTemplate View")]
17  [Content(typeof(StructureTemplate), true)]
18  public partial class StructureTemplateView : AsynchronousContentView {
19    public new StructureTemplate Content {
20      get => (StructureTemplate)base.Content;
21      set => base.Content = value;
22    }
23
24    public StructureTemplateView() {
25      InitializeComponent();
26      infoLabel.Text = "";
27      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
28     
29    }
30
31    private void SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
32      var visualTreeNode = sender as VisualTreeNode<ISymbolicExpressionTreeNode>;
33      if(visualTreeNode != null) {
34        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
35        if(Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
36          viewHost.Content = subFunction;
37      }
38    }
39
40    protected override void OnContentChanged() {
41      base.OnContentChanged();
42      if (Content == null) return;
43      expressionInput.Text = Content.Template;
44      PaintTree();
45      infoLabel.Text = "";
46    }
47
48    private void ParseButtonClick(object sender, EventArgs e) {
49      if(!string.IsNullOrEmpty(expressionInput.Text)) {
50        try {
51          Content.Template = expressionInput.Text;
52          PaintTree();
53          infoLabel.Text = "Template structure successfully parsed.";
54          infoLabel.ForeColor = Color.DarkGreen;
55        } catch (Exception ex) {
56          infoLabel.Text = ex.Message;
57          infoLabel.ForeColor = Color.DarkRed;
58        }
59      }
60    }
61
62    private void ExpressionInputTextChanged(object sender, EventArgs e) {
63      infoLabel.Text = "Unparsed changes! Press parse button to save changes.";
64      infoLabel.ForeColor = Color.DarkOrange;
65    }
66
67
68    private void PaintTree() {
69      if(Content != null && Content.Tree != null) {
70        treeChart.Tree = Content.Tree;
71        foreach (var n in Content.Tree.IterateNodesPrefix()) {
72          if (n.Symbol is SubFunctionSymbol) {
73            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
74            visualNode.FillColor = Color.LightCyan;
75            visualNode.LineColor = Color.SlateGray;
76          }
77        }
78        treeChart.RepaintNodes();
79      }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.