Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/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
RevLine 
[17607]1#region License Information
2
3/* HeuristicLab
[17896]4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[17607]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;
[17891]26using System.Text;
[17893]27using System.Windows.Forms;
[17607]28using HeuristicLab.Collections;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
[17891]33  [View("ShapeConstraints View")]
[17887]34  [Content(typeof(ShapeConstraints), true)]
[17891]35  public partial class ShapeConstraintsView : AsynchronousContentView {
[17887]36    public new ShapeConstraints Content {
[17890]37      get => (ShapeConstraints)base.Content;
[17607]38      set => base.Content = value;
39    }
40
[17891]41    public bool suspendUpdates = false;
42
43    public ShapeConstraintsView() {
[17607]44      InitializeComponent();
45      errorOutput.Text = "";
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
[17896]50      this.shapeConstraintsView.Content = Content;
[17607]51      UpdateControl();
[17906]52      errorOutput.Text = "";
[17607]53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
[17891]57      Content.ItemsAdded += constraints_Added;
58      Content.ItemsRemoved += constraint_Removed;
[17890]59      Content.Changed += Content_Changed;
[17891]60      Content.CollectionReset += constraints_Reset;
61      Content.ItemsMoved += constraints_Moved;
62      Content.ItemsReplaced += Content_ItemsReplaced;
63      Content.CheckedItemsChanged += Content_CheckedItemsChanged;
[17607]64    }
65
[17891]66
[17607]67    protected override void DeregisterContentEvents() {
[17891]68      Content.ItemsAdded -= constraints_Added;
69      Content.ItemsRemoved -= constraint_Removed;
[17890]70      Content.Changed -= Content_Changed;
[17891]71      Content.CollectionReset -= constraints_Reset;
72      Content.ItemsMoved -= constraints_Moved;
73      Content.ItemsReplaced -= Content_ItemsReplaced;
74      Content.CheckedItemsChanged -= Content_CheckedItemsChanged;
[17607]75      base.DeregisterContentEvents();
76    }
77
78    protected override void SetEnabledStateOfControls() {
79      constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
[17906]80      parseBtn.Enabled = Content != null && !Locked && !ReadOnly;
[17607]81    }
82
83
84    private void parseBtn_Click(object sender, EventArgs e) {
85      if (constraintsInput.Text != null) {
[17891]86        suspendUpdates = true;
87        Content.Clear();
[17607]88        try {
[17890]89          var parsedConstraints = ShapeConstraintsParser.ParseConstraints(constraintsInput.Text);
[17910]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          }
[17891]97          errorOutput.Text = "Constraints successfully parsed.";
98          errorOutput.ForeColor = Color.DarkGreen;
[17890]99        } catch (ArgumentException ex) {
[17906]100          errorOutput.Text = ex.Message;
[17891]101          errorOutput.ForeColor = Color.DarkRed;
[17896]102        } finally {
103          suspendUpdates = false;
[17607]104        }
[17890]105      } else {
[17607]106        errorOutput.Text = "No constraints were found!";
107      }
108    }
109
110    private void UpdateControl() {
[17891]111      if (suspendUpdates) return;
[17607]112      if (Content == null) {
[17891]113        constraintsInput.Text = string.Empty;
[17890]114      } else {
[17896]115        var newText = ToString(Content);
116        if (newText != constraintsInput.Text)
117          constraintsInput.Text = newText;
[17891]118      }
119    }
[17607]120
[17891]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        }
[17607]129      }
[17891]130      return sb.ToString();
[17607]131    }
132
133    private void constraint_Changed(object sender, EventArgs e) {
[17891]134      UpdateControl();
[17607]135    }
136
[17891]137    private void constraints_Added(object sender,
[17887]138                                     CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
[17607]139      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
[17891]140      UpdateControl();
[17607]141    }
142
[17891]143    private void constraint_Removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
[17607]144      foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed;
[17891]145      UpdateControl();
[17607]146    }
147
[17891]148    private void constraints_Moved(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
149      UpdateControl();
[17607]150    }
151
[17891]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
[17607]168    private void constraintsInput_TextChanged(object sender, EventArgs e) {
[17891]169      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
170      errorOutput.ForeColor = Color.DarkOrange;
[17607]171    }
172
173    private void Content_Changed(object sender, EventArgs e) {
174      UpdateControl();
175    }
[17893]176
[17896]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      }
[17893]183    }
[17607]184  }
185}
Note: See TracBrowser for help on using the repository browser.