Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/DensityTrackbar.cs

Last change on this file was 17208, checked in by gkronber, 5 years ago

#2971: merged r17180:17184 from trunk to branch

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Globalization;
27using System.Windows.Forms;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis.Views {
31  public partial class DensityTrackbar : UserControl {
32
33    public decimal Value {
34      get { return TickToValue(trackBar.Value); }
35      set { trackBar.Value = ValueToTick(value); }
36    }
37    public event EventHandler ValueChanged;
38
39    public bool Checked {
40      get { return radioButton.Checked; }
41      set { radioButton.Checked = value; }
42    }
43    public event EventHandler CheckedChanged;
44
45    public DoubleLimit Limits {
46      get { return doubleLimitView.Content; }
47    }
48    public event EventHandler LimitsChanged;
49
50    private IList<double> trainingValues;
51
52    public DensityTrackbar(string name, DoubleLimit limit, IList<double> trainingValues) {
53      InitializeComponent();
54
55      this.trainingValues = trainingValues;
56
57      doubleLimitView.Content = limit;
58      doubleLimitView.Content.ValueChanged += doubleLimit_ValueChanged;
59
60      textBox.Text = Value.ToString(CultureInfo.InvariantCulture);
61      radioButton.Text = name;
62
63      UpdateDensityChart();
64    }
65
66    public void UpdateTrainingValues(IList<double> trainingValues) {
67      this.trainingValues = trainingValues;
68      UpdateDensityChart();
69    }
70
71    private void UpdateDensityChart() {
72      chart.UpdateChart(trainingValues, Limits.Lower, Limits.Upper,
73        numBuckets: trackBar.Maximum - trackBar.Minimum, minimumHeight: 0.1);
74    }
75
76    #region Event Handlers
77    private void trackBar_ValueChanged(object sender, EventArgs e) {
78      textBox.Text = Value.ToString(CultureInfo.CurrentUICulture);
79      textBox.Update();
80      OnValueChanged();
81    }
82    private void doubleLimit_ValueChanged(object sender, EventArgs e) {
83      UpdateDensityChart();
84      OnLimitsChanged();
85      OnValueChanged();
86    }
87    private void radioButton_CheckedChanged(object sender, EventArgs e) {
88      Checked = radioButton.Checked;
89      trackBar.Enabled = !Checked;
90      textBox.Enabled = !Checked;
91      textBox.Text = Checked ? "Plotted" : Value.ToString(CultureInfo.CurrentUICulture);
92      radioButton.BackColor = Checked ? SystemColors.ActiveCaption : SystemColors.Control;
93      OnCheckedChanged();
94    }
95    protected override void OnTextChanged(EventArgs e) {
96      base.OnTextChanged(e);
97      radioButton.Text = Text;
98    }
99
100    private void textBox_Validating(object sender, CancelEventArgs e) {
101      if (Checked) return;
102      decimal number;
103      if (!decimal.TryParse(textBox.Text, NumberStyles.Any, CultureInfo.CurrentUICulture.NumberFormat, out number)) {
104        e.Cancel = true;
105        errorProvider.SetIconAlignment(textBox, ErrorIconAlignment.MiddleLeft);
106        errorProvider.SetIconPadding(textBox, 2);
107        errorProvider.SetError(textBox, "Illegal number format");
108      }
109    }
110
111    private void textBox_Validated(object sender, EventArgs e) {
112      if (Checked) return;
113      errorProvider.SetError(textBox, string.Empty);
114      Value = decimal.Parse(textBox.Text, NumberStyles.Any, CultureInfo.CurrentUICulture.NumberFormat);
115    }
116    #endregion
117
118    protected virtual void OnValueChanged() {
119      if (ValueChanged != null)
120        ValueChanged(this, EventArgs.Empty);
121    }
122    protected virtual void OnCheckedChanged() {
123      if (CheckedChanged != null)
124        CheckedChanged(this, EventArgs.Empty);
125    }
126    protected virtual void OnLimitsChanged() {
127      if (LimitsChanged != null)
128        LimitsChanged(this, EventArgs.Empty);
129    }
130
131    #region Helpers
132    private decimal TickToValue(int tick) {
133      return TickToValue(tick, trackBar.Maximum - trackBar.Minimum, (decimal)Limits.Lower, (decimal)Limits.Upper);
134    }
135    private int ValueToTick(decimal value) {
136      return ValueToTick(value, trackBar.Maximum - trackBar.Minimum, (decimal)Limits.Lower, (decimal)Limits.Upper);
137    }
138    private static decimal TickToValue(int tick, int numberOfTicks, decimal lower, decimal upper) {
139      return lower + (upper - lower) * tick / numberOfTicks;
140    }
141    private static int ValueToTick(decimal value, int numberOfTicks, decimal lower, decimal upper) {
142      return (int)((value - lower) / ((upper - lower) / numberOfTicks));
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.