Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Visualization/3.2/Options/Options.cs @ 4783

Last change on this file since 4783 was 1586, checked in by bspisic, 16 years ago

Changed OptionsDialog to an UserControl (#584)

File size: 8.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Windows.Forms;
5using HeuristicLab.Visualization.Legend;
6
7namespace HeuristicLab.Visualization.Options {
8  public partial class Options : UserControl {
9    private IChartDataRowsModel model;
10    private ViewSettings viewSettings;
11    private ViewSettings oldViewSettings;
12    private LineParams[] oldLineParams;
13    private Dictionary<CheckBox, bool> ShowYAxisBoxes;
14    private Dictionary<CheckBox, bool> yAxisClipChangeableBoxes;
15
16    internal class LineParams {
17      private string Label { get; set; }
18      private Color Color { get; set; }
19      private bool ShowMarkers { get; set; }
20      private int Thickness { get; set; }
21      private DrawingStyle Style { get; set; }
22      private readonly IDataRow row;
23
24      public LineParams(IDataRow row) {
25        Label = row.Label;
26        Color = row.Color;
27        Thickness = row.Thickness;
28        Style = row.Style;
29        this.row = row;
30        this.ShowMarkers = row.ShowMarkers;
31      }
32
33      public void applySettings() {
34        row.Label = Label;
35        row.Color = Color;
36        row.Thickness = Thickness;
37        row.Style = Style;
38        row.ShowMarkers = this.ShowMarkers;
39      }
40    }
41
42    public Options() {
43      InitializeComponent();
44
45      cbLegendPosition.Items.Add(LegendPosition.Top);
46      cbLegendPosition.Items.Add(LegendPosition.Bottom);
47      cbLegendPosition.Items.Add(LegendPosition.Left);
48      cbLegendPosition.Items.Add(LegendPosition.Right);
49
50      Model = new ChartDataRowsModel();
51    }
52
53    public IChartDataRowsModel Model {
54      get { return model; }
55      set {
56        model = value;
57        viewSettings = model.ViewSettings;
58        oldViewSettings = new ViewSettings(model.ViewSettings);
59        cbLegendPosition.SelectedItem = viewSettings.LegendPosition;
60      }
61    }
62
63    public void ResetSettings() {
64      foreach (var param in oldLineParams) {
65        param.applySettings();
66      }
67
68      foreach (var box in ShowYAxisBoxes) {
69        box.Key.Checked = box.Value;
70      }
71
72      foreach (KeyValuePair<CheckBox, bool> box in yAxisClipChangeableBoxes) {
73        box.Key.Checked = box.Value;
74      }
75
76      viewSettings.LegendColor = oldViewSettings.LegendColor;
77      viewSettings.LegendPosition = oldViewSettings.LegendPosition;
78      viewSettings.LegendFont = oldViewSettings.LegendFont;
79      viewSettings.TitleColor = oldViewSettings.TitleColor;
80      viewSettings.TitleFont = oldViewSettings.TitleFont;
81      viewSettings.XAxisColor = oldViewSettings.LegendColor;
82      viewSettings.XAxisFont = oldViewSettings.XAxisFont;
83      viewSettings.UpdateView();
84      cbLegendPosition.SelectedItem = viewSettings.LegendPosition;
85      this.LineSelectCB_SelectedIndexChanged(this, null);
86    }
87
88    private void OptionsDialogSelectColorBtn_Click(object sender, EventArgs e) {
89      ColorDialog dlg = new ColorDialog();
90      dlg.ShowDialog();
91      ColorPreviewTB.BackColor = dlg.Color;
92      ((IDataRow)LineSelectCB.SelectedValue).Color = dlg.Color;
93    }
94
95    private IList<int> GetThicknesses() {
96      return new List<int>(new[] {1, 2, 3, 4, 5, 6, 7, 8});
97    }
98
99    private IList<DrawingStyle> GetStyles() {
100      return new List<DrawingStyle>(new[] {DrawingStyle.Solid, DrawingStyle.Dashed});
101    }
102
103    private void btnChangeLegendFont_Click(object sender, EventArgs e) {
104      fdFont.Font = viewSettings.LegendFont;
105      fdFont.Color = viewSettings.LegendColor;
106
107      DialogResult dr = fdFont.ShowDialog();
108
109      if (dr == DialogResult.OK) {
110        viewSettings.LegendFont = fdFont.Font;
111        viewSettings.LegendColor = fdFont.Color;
112
113        viewSettings.UpdateView();
114      }
115    }
116
117    private void btnChangeTitleFont_Click(object sender, EventArgs e) {
118      fdFont.Font = viewSettings.TitleFont;
119      fdFont.Color = viewSettings.TitleColor;
120
121      DialogResult dr = fdFont.ShowDialog();
122
123      if (dr == DialogResult.OK) {
124        viewSettings.TitleFont = fdFont.Font;
125        viewSettings.TitleColor = fdFont.Color;
126
127        viewSettings.UpdateView();
128      }
129    }
130
131    private void btnChangeXAxisFont_Click(object sender, EventArgs e) {
132      fdFont.Font = viewSettings.XAxisFont;
133      fdFont.Color = viewSettings.XAxisColor;
134
135      DialogResult dr = fdFont.ShowDialog();
136
137      if (dr == DialogResult.OK) {
138        viewSettings.XAxisFont = fdFont.Font;
139        viewSettings.XAxisColor = fdFont.Color;
140
141        viewSettings.UpdateView();
142      }
143    }
144
145    private void Options_Load(object sender, EventArgs e) {
146      InitTabPageLines();
147      InitTabPageYAxes();
148    }
149
150    private void InitTabPageLines() {
151      if (model.Rows.Count != 0) {
152        int index = 0;
153        oldLineParams = new LineParams[model.Rows.Count];
154        foreach (var row in model.Rows) {
155          oldLineParams[index++] = new LineParams(row);
156        }
157        LineThicknessCB.DataSource = GetThicknesses();
158        LinestyleCB.DataSource = GetStyles();
159        LinestyleCB.SelectedItem = model.Rows[0].Style;
160        LineThicknessCB.SelectedItem = model.Rows[0].Thickness;
161        MarkercheckBox.Checked = model.Rows[0].ShowMarkers;
162
163        LineSelectCB.DataSource = model.Rows;
164        LineSelectCB.DisplayMember = "Label";
165
166
167        LineSelectCB.SelectedIndex = 0;
168        LineSelectCB_SelectedIndexChanged(this, null);
169      }
170    }
171
172    private void InitTabPageYAxes() {
173      ShowYAxisBoxes = new Dictionary<CheckBox, bool>();
174      yAxisClipChangeableBoxes = new Dictionary<CheckBox, bool>();
175
176      for (int i = 0; i < model.YAxes.Count; i++) {
177        YAxisDescriptor yAxisDescriptor = model.YAxes[i];
178
179        CheckBox cbxShowYAxis = new CheckBox();
180        cbxShowYAxis.Text = yAxisDescriptor.Label;
181        cbxShowYAxis.Checked = yAxisDescriptor.ShowYAxis;
182        ShowYAxisBoxes[cbxShowYAxis] = yAxisDescriptor.ShowYAxis;
183        cbxShowYAxis.CheckedChanged += delegate { yAxisDescriptor.ShowYAxis = cbxShowYAxis.Checked; };
184
185        flpShowYAxis.Controls.Add(cbxShowYAxis);
186
187        CheckBox cbxClipChangeable = new CheckBox();
188        cbxClipChangeable.Text = yAxisDescriptor.Label;
189        cbxClipChangeable.Checked = yAxisDescriptor.ClipChangeable;
190        yAxisClipChangeableBoxes[cbxClipChangeable] = yAxisDescriptor.ClipChangeable;
191        cbxClipChangeable.CheckedChanged += delegate { yAxisDescriptor.ClipChangeable = cbxClipChangeable.Checked; };
192
193        flpYAxisClipChangeable.Controls.Add(cbxClipChangeable);
194      }
195    }
196
197    private void LineSelectCB_SelectedIndexChanged(object sender, EventArgs e) {
198      if (LineSelectCB.SelectedValue != null) {
199        /*  int index =
200            LineThicknessCB.FindStringExact(((IDataRow)LineSelectCB.SelectedValue).Thickness.ToString());
201          LineThicknessCB.SelectedIndex = index;
202          index = LinestyleCB.FindStringExact(((IDataRow)LineSelectCB.SelectedValue).Style.ToString());
203          LinestyleCB.SelectedIndex = index;  */
204        LineThicknessCB.SelectedItem = ((IDataRow)LineSelectCB.SelectedValue).Thickness;
205        LinestyleCB.SelectedItem = ((IDataRow)LineSelectCB.SelectedValue).Style;
206        ColorPreviewTB.BackColor = ((IDataRow)LineSelectCB.SelectedValue).Color;
207        MarkercheckBox.Checked = ((IDataRow)LineSelectCB.SelectedValue).ShowMarkers;
208      }
209    }
210
211    private void cbLegendPosition_SelectedIndexChanged(object sender, EventArgs e) {
212      viewSettings.LegendPosition = (LegendPosition)cbLegendPosition.SelectedItem;
213      viewSettings.UpdateView();
214    }
215
216    private void LinestyleCB_SelectedIndexChanged(object sender, EventArgs e) {
217      if (LineSelectCB.SelectedValue != null) {
218        ((IDataRow)LineSelectCB.SelectedValue).Style = (DrawingStyle)LinestyleCB.SelectedItem;
219      }
220    }
221
222    private void LineThicknessCB_SelectedIndexChanged(object sender, EventArgs e) {
223      if (LineSelectCB.SelectedValue != null) {
224        ((IDataRow)LineSelectCB.SelectedValue).Thickness = (int)LineThicknessCB.SelectedItem;
225      }
226    }
227
228    private void MarkercheckBox_CheckedChanged(object sender, EventArgs e) {
229      if (LineSelectCB.SelectedValue != null) {
230        ((IDataRow)LineSelectCB.SelectedValue).ShowMarkers = MarkercheckBox.Checked;
231      }
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.