Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18154 was 18154, checked in by dpiringe, 2 years ago

#3136

  • overwrote the method SetEnabledStateOfControls for StructureTemplateView
  • fixed the wrong usage of infoLabel in StructureTemplateView -> added a new label errorLabel for textual output
  • deleted the resource file for StructureTemplateView
File size: 4.5 KB
Line 
1 using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using System.Windows.Forms;
7using HeuristicLab.Collections;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
11using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
12using HeuristicLab.MainForm;
13using HeuristicLab.MainForm.WindowsForms;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
16
17  [View("StructureTemplate View")]
18  [Content(typeof(StructureTemplate), true)]
19  public partial class StructureTemplateView : AsynchronousContentView {
20    public new StructureTemplate Content {
21      get => (StructureTemplate)base.Content;
22      set => base.Content = value;
23    }
24
25    public StructureTemplateView() {
26      InitializeComponent();
27      errorLabel.Text = "";
28      this.Resize += StructureTemplateViewResize;
29      splitContainer.SplitterMoved += SplitContainerSplitterMoved;
30      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
31     
32    }
33
34    private void SplitContainerSplitterMoved(object sender, EventArgs e) {
35      PaintTree();
36    }
37
38    private void StructureTemplateViewResize(object sender, EventArgs e) {
39      PaintTree();
40    }
41
42    private void SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
43      var visualTreeNode = sender as VisualTreeNode<ISymbolicExpressionTreeNode>;
44      if(visualTreeNode != null) {
45        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
46        var selectedSubFunction = Content.SubFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
47        if(subFunctionTreeNode != null && selectedSubFunction != null)
48          viewHost.Content = selectedSubFunction;
49      }
50    }
51
52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      parseButton.Enabled = Content != null && !Locked && !ReadOnly;
55      linearScalingCheckBox.Enabled = Content != null && !Locked && !ReadOnly;
56      expressionInput.Enabled = Content != null && !Locked && !ReadOnly;
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) return;
62      expressionInput.Text = Content.Template;
63      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
64      PaintTree();
65      errorLabel.Text = "";
66    }
67
68    private void ParseButtonClick(object sender, EventArgs e) {
69      Parse();
70    }
71
72    private void ExpressionInputTextChanged(object sender, EventArgs e) {
73      errorLabel.Text = "Unparsed changes! Press parse button to save changes.";
74      errorLabel.ForeColor = Color.DarkOrange;
75    }
76
77    private void PaintTree() {
78      if(Content != null && Content.Tree != null) {
79        treeChart.Tree = Content.Tree;
80        foreach (var n in Content.Tree.IterateNodesPrefix()) {
81          if (n.Symbol is SubFunctionSymbol) {
82            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
83            visualNode.FillColor = Color.LightCyan;
84            visualNode.LineColor = Color.SlateGray;
85          }
86        }
87        treeChart.RepaintNodes();
88      }
89    }
90
91    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
92      if (e.KeyCode == Keys.Enter)
93        Parse();
94    }
95
96    private void Parse() {
97      viewHost.Content = null; // reset active detail view
98      if (!string.IsNullOrEmpty(expressionInput.Text)) {
99        try {
100          Content.Template = expressionInput.Text;
101          PaintTree();
102          errorLabel.Text = "Template structure successfully parsed.";
103          errorLabel.ForeColor = Color.DarkGreen;
104        } catch (AggregateException ex) {
105          errorLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
106          errorLabel.ForeColor = Color.DarkRed;
107        } catch (Exception ex) {
108          errorLabel.Text = ex.Message;
109          errorLabel.ForeColor = Color.DarkRed;
110        }
111      }
112    }
113
114    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
115      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
116      PaintTree();
117    }
118
119    private void HelpButtonDoubleClick(object sender, EventArgs e) {
120      using (InfoBox dialog = new InfoBox("Help for structure template",
121        "HeuristicLab.Problems.DataAnalysis.Symbolic.Views.Resources.structureTemplateHelp.rtf",
122        this)) {
123        dialog.ShowDialog(this);
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.