Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3128_Prediction_Intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/ShapeConstraintsView.cs @ 17991

Last change on this file since 17991 was 17910, checked in by chaider, 4 years ago

#3073 Worked on todos: resource file update, added constraints with checked state into list, fixed constructor

File size: 6.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Drawing;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("ShapeConstraints View")]
34  [Content(typeof(ShapeConstraints), true)]
35  public partial class ShapeConstraintsView : AsynchronousContentView {
36    public new ShapeConstraints Content {
37      get => (ShapeConstraints)base.Content;
38      set => base.Content = value;
39    }
40
41    public bool suspendUpdates = false;
42
43    public ShapeConstraintsView() {
44      InitializeComponent();
45      errorOutput.Text = "";
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      this.shapeConstraintsView.Content = Content;
51      UpdateControl();
52      errorOutput.Text = "";
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.ItemsAdded += constraints_Added;
58      Content.ItemsRemoved += constraint_Removed;
59      Content.Changed += Content_Changed;
60      Content.CollectionReset += constraints_Reset;
61      Content.ItemsMoved += constraints_Moved;
62      Content.ItemsReplaced += Content_ItemsReplaced;
63      Content.CheckedItemsChanged += Content_CheckedItemsChanged;
64    }
65
66
67    protected override void DeregisterContentEvents() {
68      Content.ItemsAdded -= constraints_Added;
69      Content.ItemsRemoved -= constraint_Removed;
70      Content.Changed -= Content_Changed;
71      Content.CollectionReset -= constraints_Reset;
72      Content.ItemsMoved -= constraints_Moved;
73      Content.ItemsReplaced -= Content_ItemsReplaced;
74      Content.CheckedItemsChanged -= Content_CheckedItemsChanged;
75      base.DeregisterContentEvents();
76    }
77
78    protected override void SetEnabledStateOfControls() {
79      constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
80      parseBtn.Enabled = Content != null && !Locked && !ReadOnly;
81    }
82
83
84    private void parseBtn_Click(object sender, EventArgs e) {
85      if (constraintsInput.Text != null) {
86        suspendUpdates = true;
87        Content.Clear();
88        try {
89          var parsedConstraints = ShapeConstraintsParser.ParseConstraints(constraintsInput.Text);
90          //Content.AddRange(parsedConstraints);
91          foreach(var constraint in parsedConstraints) {
92            if (parsedConstraints.ItemChecked(constraint))
93              Content.Add(constraint, true);
94            else
95              Content.Add(constraint, false);
96          }
97          errorOutput.Text = "Constraints successfully parsed.";
98          errorOutput.ForeColor = Color.DarkGreen;
99        } catch (ArgumentException ex) {
100          errorOutput.Text = ex.Message;
101          errorOutput.ForeColor = Color.DarkRed;
102        } finally {
103          suspendUpdates = false;
104        }
105      } else {
106        errorOutput.Text = "No constraints were found!";
107      }
108    }
109
110    private void UpdateControl() {
111      if (suspendUpdates) return;
112      if (Content == null) {
113        constraintsInput.Text = string.Empty;
114      } else {
115        var newText = ToString(Content);
116        if (newText != constraintsInput.Text)
117          constraintsInput.Text = newText;
118      }
119    }
120
121    private string ToString(ShapeConstraints constraints) {
122      var sb = new StringBuilder();
123      foreach (var constraint in constraints) {
124        if (!constraints.ItemChecked(constraint)) {
125          sb.Append("# ").AppendLine(constraint.ToString());
126        } else {
127          sb.AppendLine(constraint.ToString());
128        }
129      }
130      return sb.ToString();
131    }
132
133    private void constraint_Changed(object sender, EventArgs e) {
134      UpdateControl();
135    }
136
137    private void constraints_Added(object sender,
138                                     CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
139      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
140      UpdateControl();
141    }
142
143    private void constraint_Removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
144      foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed;
145      UpdateControl();
146    }
147
148    private void constraints_Moved(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
149      UpdateControl();
150    }
151
152    private void constraints_Reset(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
153      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
154      foreach (var removedItem in e.OldItems) removedItem.Value.Changed -= constraint_Changed;
155      UpdateControl();
156    }
157
158    private void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
159      UpdateControl();
160    }
161
162    private void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
163      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
164      foreach (var removedItem in e.OldItems) removedItem.Value.Changed -= constraint_Changed;
165      UpdateControl();
166    }
167
168    private void constraintsInput_TextChanged(object sender, EventArgs e) {
169      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
170      errorOutput.ForeColor = Color.DarkOrange;
171    }
172
173    private void Content_Changed(object sender, EventArgs e) {
174      UpdateControl();
175    }
176
177    private void helpButton_DoubleClick(object sender, EventArgs e) {
178      using (InfoBox dialog = new InfoBox("Help for shape constraints",
179        "HeuristicLab.Problems.DataAnalysis.Views.Resources.shapeConstraintsHelp.rtf",
180        this)) {
181        dialog.ShowDialog(this);
182      }
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.