source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartControl.cs @ 13076

Last change on this file since 13076 was 13076, checked in by jkarder, 7 years ago

#1265: implemented new chart mode concept

File size: 12.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing.Imaging;
27using System.Linq;
28using System.Windows.Forms;
29
30namespace HeuristicLab.Visualization {
31  public partial class ChartControl : UserControl {
32    protected Bitmap Picture;
33    protected bool RenderingRequired;
34    protected Dictionary<Type, ChartMode> Modes = new Dictionary<Type, ChartMode>();
35
36    protected bool SuppressEvents { get; set; }
37    private bool SuppressRender { get; set; }
38
39    public PictureBox PictureBox { get { return pictureBox; } }
40
41    private IChart chart;
42    public IChart Chart {
43      get { return chart; }
44      protected set {
45        if (chart != null) DeregisterChartEvents(chart);
46        chart = value;
47        chart.Enabled = Enabled;
48        pictureBox.Enabled = Enabled;
49        if (chart != null) RegisterChartEvents(chart);
50      }
51    }
52
53    private ChartMode mode;
54    public ChartMode Mode {
55      get { return mode; }
56      set {
57        if (mode == value) return;
58        mode = value;
59        SetButtonCheckedSate();
60        SetPictureBoxCursor(GetPictureBoxCursor());
61        OnModeChanged();
62        if (chart != null) {
63          GenerateImage();
64        }
65      }
66    }
67
68    public bool ScaleOnResize { get; set; }
69
70    public bool IsRenderingPaused { get { return SuppressRender; } }
71
72    public ChartControl() : this(null) { }
73    public ChartControl(IChart defaultChart) {
74      InitializeComponent();
75      ScaleOnResize = true;
76      Chart = defaultChart ?? new Chart(0, 0, Width, Height);
77    }
78
79    public void AddChartModes(params ChartMode[] chartModes) {
80      foreach (var chartMode in chartModes)
81        Modes.Add(chartMode.GetType(), chartMode);
82
83      RebuildButtonPanel();
84    }
85
86    protected virtual void RebuildButtonPanel() {
87      buttonPanel.Controls.Clear();
88
89      int locationY = 3;
90      foreach (var chartMode in Modes.Values) {
91        var rb = CreateChartModeRadioButton(chartMode);
92        rb.Location = new Point(3, locationY);
93        toolTip.SetToolTip(rb, chartMode.ToolTip);
94        buttonPanel.Controls.Add(rb);
95        locationY += rb.Height + rb.Margin.Vertical;
96      }
97    }
98
99    public void SuspendRendering() {
100      SuppressRender = true;
101    }
102
103    public void ResumeRendering() {
104      SuppressRender = false;
105      if (Chart != null) GenerateImage();
106    }
107
108    protected virtual void SetButtonCheckedSate() {
109      foreach (var rb in buttonPanel.Controls.OfType<RadioButton>())
110        rb.Checked = rb.Tag == Mode;
111    }
112
113    protected virtual void SetPictureBoxCursor(Cursor cursor) {
114      if (pictureBox.Cursor != cursor) pictureBox.Cursor = cursor;
115    }
116
117    protected virtual Cursor GetPictureBoxCursor() {
118      if (mode == null) return null;
119
120      return mode.Cursor;
121    }
122
123    protected virtual void RegisterChartEvents(IChart chart) {
124      chart.RedrawRequired += ChartOnUpdateRequired;
125    }
126
127    protected virtual void DeregisterChartEvents(IChart chart) {
128      chart.RedrawRequired -= ChartOnUpdateRequired;
129    }
130
131    protected virtual void ChartOnUpdateRequired(object sender, EventArgs e) {
132      if (SuppressRender) return;
133      GenerateImage();
134    }
135
136    #region PictureBox Events
137    protected virtual void PictureBoxOnSizeChanged(object sender, EventArgs e) {
138      if (Chart == null) return;
139      if (ScaleOnResize) {
140        if ((pictureBox.Width > 0) && (pictureBox.Height > 0) && (Chart != null)) {
141          var point = Chart.TransformPixelToWorld(new Point(pictureBox.Width, Chart.SizeInPixels.Height - pictureBox.Height));
142          var diff = Chart.UpperRight - point;
143          Chart.SetPosition(new PointD(Chart.LowerLeft.X + (diff.DX / 2.0), Chart.LowerLeft.Y + (diff.DY / 2.0)),
144            new PointD(Chart.UpperRight.X - (diff.DX / 2.0), Chart.UpperRight.Y - (diff.DY / 2.0)));
145        }
146      }
147      GenerateImage();
148    }
149
150    protected virtual void PictureBoxOnVisibleChanged(object sender, EventArgs e) {
151      if (pictureBox.Visible && RenderingRequired) GenerateImage();
152    }
153
154    protected virtual void PictureBoxOnMouseClick(object sender, MouseEventArgs e) {
155      if (mode == null) return;
156      mode.HandleOnMouseClick(sender, e);
157    }
158
159    protected virtual void PictureBoxOnMouseDoubleClick(object sender, MouseEventArgs e) {
160      if (mode == null) return;
161      mode.HandleOnMouseDoubleClick(sender, e);
162    }
163
164    protected virtual void OnKeyDown(object sender, KeyEventArgs e) {
165      if (mode == null) return;
166      mode.HandleOnKeyDown(sender, e);
167    }
168
169    protected virtual void OnKeyUp(object sender, KeyEventArgs e) {
170      if (mode == null) return;
171      mode.HandleOnKeyUp(sender, e);
172    }
173
174    protected virtual void PictureBoxOnMouseWheel(object sender, MouseEventArgs e) {
175      if (mode == null) return;
176      mode.HandleOnMouseWheel(sender, e);
177    }
178
179    protected virtual void PictureBoxOnMouseDown(object sender, MouseEventArgs e) {
180      if (mode == null) return;
181      mode.HandleOnMouseDown(sender, e);
182    }
183
184    protected virtual void PictureBoxOnMouseUp(object sender, MouseEventArgs e) {
185      if (mode == null) return;
186      mode.HandleOnMouseUp(sender, e);
187
188      if (Chart == null) return;
189
190      if (e.Button == MouseButtons.Right) {
191        propertiesToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
192        oneLayerUpToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
193        oneLayerDownToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
194        intoForegroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
195        intoBackgroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
196      }
197    }
198
199    protected virtual void PictureBoxOnMouseMove(object sender, MouseEventArgs e) {
200      if (InvokeRequired) {
201        Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseMove, sender, e);
202      } else {
203        var toolTipText = Chart.GetToolTipText(e.Location);
204        if (toolTip.GetToolTip(pictureBox) != toolTipText) toolTip.SetToolTip(pictureBox, toolTipText);
205
206        if (mode == null) return;
207
208        mode.HandleOnMouseMove(sender, e);
209
210        SetPictureBoxCursor(mode.Cursor);
211      }
212    }
213
214    protected virtual void PictureBoxOnMouseEnter(object sender, EventArgs e) {
215      if (InvokeRequired) {
216        Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseEnter, sender, e);
217        return;
218      }
219      if (!Focused) this.pictureBox.Focus();
220
221      if (mode == null) return;
222
223      mode.HandleOnMouseEnter(sender, e);
224    }
225
226    protected virtual void PictureBoxOnMouseLeave(object sender, EventArgs e) {
227      if (mode == null) return;
228      mode.HandleOnMouseLeave(sender, e);
229    }
230
231    protected virtual void PictureBoxOnClick(object sender, EventArgs e) {
232      if (InvokeRequired) {
233        Invoke((Action<Object, MouseEventArgs>)PictureBoxOnClick, sender, e);
234        return;
235      }
236      if (!Focused) this.pictureBox.Focus();
237
238      if (mode == null) return;
239
240      mode.HandleOnClick(sender, e);
241    }
242    #endregion
243
244    protected virtual void PictureBoxContextMenuStripOnOpening(object sender, CancelEventArgs e) { }
245
246    protected virtual void oneLayerUpToolStripMenuItem_Click(object sender, EventArgs e) {
247      if (Chart == null) return;
248      if (Chart.Group.SelectedPrimitives.Count() == 1) {
249        Chart.OneLayerUp(Chart.Group.SelectedPrimitives.First());
250      }
251    }
252    protected virtual void oneLayerDownToolStripMenuItem_Click(object sender, EventArgs e) {
253      if (Chart == null) return;
254      if (Chart.Group.SelectedPrimitives.Count() == 1) {
255        Chart.OneLayerDown(Chart.Group.SelectedPrimitives.First());
256      }
257    }
258    protected virtual void intoForegroundToolStripMenuItem_Click(object sender, EventArgs e) {
259      if (Chart == null) return;
260      if (Chart.Group.SelectedPrimitives.Count() == 1) {
261        Chart.IntoForeground(Chart.Group.SelectedPrimitives.First());
262      }
263    }
264    protected virtual void intoBackgroundToolStripMenuItem_Click(object sender, EventArgs e) {
265      if (Chart == null) return;
266      if (Chart.Group.SelectedPrimitives.Count() == 1) {
267        Chart.IntoBackground(Chart.Group.SelectedPrimitives.First());
268      }
269    }
270    protected virtual void propertiesToolStripMenuItem_Click(object sender, EventArgs e) {
271      if (Chart == null) return;
272      if (Chart.Group.SelectedPrimitives.Count() == 1) {
273        using (var dialog = new PropertiesDialog(Chart.Group.SelectedPrimitives.First())) {
274          dialog.ShowDialog(this);
275        }
276      }
277    }
278    protected virtual void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
279      if (Picture == null) return;
280
281      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
282        var format = ImageFormat.Bmp;
283        var filename = saveFileDialog.FileName.ToLower();
284        if (filename.EndsWith("emf")) {
285          using (var g1 = Graphics.FromImage(Picture)) {
286            var rectangle = new System.Drawing.Rectangle(0, 0, Picture.Width, Picture.Height);
287            using (var metafile = new Metafile(filename, g1.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual))
288            using (var g2 = Graphics.FromImage(metafile))
289              Chart.Render(g2, pictureBox.Width, pictureBox.Height);
290          }
291        } else {
292          using (var graphics = Graphics.FromImage(Picture))
293            Chart.Render(graphics, Picture.Width, Picture.Height);
294
295          if (filename.EndsWith("jpg")) {
296            format = ImageFormat.Jpeg;
297          } else if (filename.EndsWith("gif")) {
298            format = ImageFormat.Gif;
299          } else if (filename.EndsWith("png")) {
300            format = ImageFormat.Png;
301          } else if (filename.EndsWith("tif")) {
302            format = ImageFormat.Tiff;
303          }
304
305          Picture.Save(saveFileDialog.FileName, format);
306        }
307      }
308    }
309
310    protected virtual void GenerateImage() {
311      if (InvokeRequired) {
312        Invoke((Action)GenerateImage);
313        return;
314      }
315      if (!Visible) {
316        RenderingRequired = true;
317      } else {
318        if ((pictureBox.Width == 0) || (pictureBox.Height == 0)) {
319          pictureBox.Image = pictureBox.ErrorImage;
320          if (Picture != null) Picture.Dispose();
321          Picture = null;
322        } else {
323          if (Picture != null) Picture.Dispose();
324          Picture = new Bitmap(pictureBox.Width, pictureBox.Height);
325          using (var graphics = Graphics.FromImage(Picture)) {
326            graphics.SetClip(new System.Drawing.Rectangle(0, 0, pictureBox.Width, pictureBox.Height));
327            Chart.Render(graphics, pictureBox.Width, pictureBox.Height);
328          }
329        }
330        pictureBox.Image = Picture;
331        RenderingRequired = false;
332      }
333    }
334
335    public event EventHandler ModeChanged;
336    protected void OnModeChanged() {
337      var handler = ModeChanged;
338      if (handler != null) handler(this, EventArgs.Empty);
339    }
340
341    private void ChartControl_Load(object sender, EventArgs e) {
342      if (!SuppressRender) GenerateImage();
343    }
344
345    #region Helpers
346    private RadioButton CreateChartModeRadioButton(ChartMode chartMode) {
347      var rb = new RadioButton {
348        Appearance = Appearance.Button,
349        Image = chartMode.Image,
350        Size = new Size(24, 24),
351        TabStop = true,
352        Tag = chartMode,
353        UseVisualStyleBackColor = true
354      };
355
356      rb.CheckedChanged += (sender, args) => { if (rb.Checked) Mode = chartMode; };
357
358      return rb;
359    }
360    #endregion
361  }
362}
Note: See TracBrowser for help on using the repository browser.