Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7617 was 7617, checked in by bburlacu, 13 years ago

#1265: ChartControl.cs: fixed memory leak on GenerateImage() method.

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
25using System.Drawing.Drawing2D;
26using System.Windows.Forms;
27
28namespace HeuristicLab.Visualization {
29  public partial class ChartControl : UserControl {
30    private Bitmap bitmap;
31    private bool renderingRequired;
32    private Point buttonDownPoint;
33    private Point mousePosition;
34    private int mouseClickCount;
35
36    private Chart myChart;
37    public Chart Chart {
38      get { return myChart; }
39      set {
40        if (myChart != null) myChart.Update -= new EventHandler(myChart_Update);
41        myChart = value;
42        if (myChart != null) {
43          myChart.Update += new EventHandler(myChart_Update);
44          SetMode(Chart.Mode);
45        }
46        GenerateImage();
47      }
48    }
49    private bool myScaleOnResize;
50    public bool ScaleOnResize {
51      get { return myScaleOnResize; }
52      set { myScaleOnResize = value; }
53    }
54
55    public ChartControl() {
56      InitializeComponent();
57      myScaleOnResize = true;
58      GenerateImage();
59    }
60
61    private void myChart_Update(object sender, EventArgs e) {
62      GenerateImage();
63    }
64    private void pictureBox_SizeChanged(object sender, EventArgs e) {
65      if (ScaleOnResize) {
66        if ((pictureBox.Width > 0) && (pictureBox.Height > 0) && (Chart != null)) {
67          PointD point = Chart.TransformPixelToWorld(new Point(pictureBox.Width, Chart.SizeInPixels.Height - pictureBox.Height));
68          Chart.SetPosition(Chart.LowerLeft, point);
69        }
70      }
71      GenerateImage();
72    }
73    private void pictureBox_VisibleChanged(object sender, EventArgs e) {
74      if (pictureBox.Visible && renderingRequired) {
75        GenerateImage();
76      }
77    }
78
79    private void pictureBox_MouseDown(object sender, MouseEventArgs e) {
80      buttonDownPoint = e.Location;
81      mouseClickCount = e.Clicks;
82    }
83    private void pictureBox_MouseUp(object sender, MouseEventArgs e) {
84      if (e.Button == MouseButtons.Left) {
85        if (Chart.Mode == ChartMode.Zoom) {
86          pictureBox.Refresh();
87          Point lowerLeft = new Point(Math.Min(e.X, buttonDownPoint.X),
88                                      Math.Max(e.Y, buttonDownPoint.Y));
89          Point upperRight = new Point(Math.Max(e.X, buttonDownPoint.X),
90                                       Math.Min(e.Y, buttonDownPoint.Y));
91          if ((lowerLeft.X != upperRight.X) && (lowerLeft.Y != upperRight.Y)) {
92            Chart.ZoomIn(lowerLeft, upperRight);
93          }
94        }
95      } else if (e.Button == MouseButtons.Right) {
96        propertiesToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count == 1;
97        oneLayerUpToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count == 1;
98        oneLayerDownToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count == 1;
99        intoForegroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count == 1;
100        intoBackgroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count == 1;
101      } else if (e.Button == MouseButtons.Middle) {
102        if (Chart.Mode == ChartMode.Zoom) {
103          if (mouseClickCount == 1) Chart.ZoomOut();
104          else if (mouseClickCount == 2) Chart.Unzoom();
105        }
106      }
107      if (buttonDownPoint == e.Location) {
108        if (mouseClickCount == 1)
109          Chart.MouseClick(e.Location, e.Button);
110        else if (mouseClickCount == 2)
111          Chart.MouseDoubleClick(e.Location, e.Button);
112      }
113    }
114    private void pictureBox_MouseMove(object sender, MouseEventArgs e) {
115      toolTip.SetToolTip(pictureBox, Chart.GetToolTipText(e.Location));
116      Cursor cursor = Chart.GetCursor(e.Location);
117      if (cursor != null) pictureBox.Cursor = cursor;
118      else pictureBox.Cursor = Cursors.Default;
119
120      if (e.Button != MouseButtons.None) {
121        if ((Chart.Mode == ChartMode.Zoom) && (e.Button == MouseButtons.Left)) {
122          pictureBox.Refresh();
123          Graphics graphics = pictureBox.CreateGraphics();
124          Pen pen = new Pen(Color.Gray);
125          pen.DashStyle = DashStyle.Dash;
126          graphics.DrawRectangle(pen,
127                                 Math.Min(e.X, buttonDownPoint.X),
128                                 Math.Min(e.Y, buttonDownPoint.Y),
129                                 Math.Abs(e.X - buttonDownPoint.X),
130                                 Math.Abs(e.Y - buttonDownPoint.Y));
131          pen.Dispose();
132          graphics.Dispose();
133        }
134        Chart.MouseDrag(mousePosition, e.Location, e.Button);
135      } else {
136        Chart.MouseMove(mousePosition, e.Location);
137      }
138      mousePosition = e.Location;
139    }
140
141    private void ChartControl_KeyDown(object sender, KeyEventArgs e) {
142      if (Chart.Mode == ChartMode.Select) {
143        if (e.KeyCode == Keys.Delete) {
144          IList<IPrimitive> selected = Chart.Group.SelectedPrimitives;
145          Chart.UpdateEnabled = false;
146          foreach (IPrimitive primitive in selected)
147            Chart.Group.Remove(primitive);
148          Chart.UpdateEnabled = true;
149          Chart.EnforceUpdate();
150        }
151      }
152    }
153
154    private void moveToolStripMenuItem_Click(object sender, EventArgs e) {
155      SetMode(ChartMode.Move);
156    }
157    private void zoomToolStripMenuItem_Click(object sender, EventArgs e) {
158      SetMode(ChartMode.Zoom);
159    }
160    private void selectToolStripMenuItem_Click(object sender, EventArgs e) {
161      SetMode(ChartMode.Select);
162    }
163    private void oneLayerUpToolStripMenuItem_Click(object sender, EventArgs e) {
164      if (Chart.Group.SelectedPrimitives.Count == 1) {
165        Chart.Group.SelectedPrimitives[0].OneLayerUp();
166      }
167    }
168    private void oneLayerDownToolStripMenuItem_Click(object sender, EventArgs e) {
169      if (Chart.Group.SelectedPrimitives.Count == 1) {
170        Chart.Group.SelectedPrimitives[0].OneLayerDown();
171      }
172    }
173    private void intoForegroundToolStripMenuItem_Click(object sender, EventArgs e) {
174      if (Chart.Group.SelectedPrimitives.Count == 1) {
175        Chart.Group.SelectedPrimitives[0].IntoForeground();
176      }
177    }
178    private void intoBackgroundToolStripMenuItem_Click(object sender, EventArgs e) {
179      if (Chart.Group.SelectedPrimitives.Count == 1) {
180        Chart.Group.SelectedPrimitives[0].IntoBackground();
181      }
182    }
183    private void propertiesToolStripMenuItem_Click(object sender, EventArgs e) {
184      if (Chart.Group.SelectedPrimitives.Count == 1) {
185        PropertiesDialog dialog = new PropertiesDialog(Chart.Group.SelectedPrimitives[0]);
186        dialog.ShowDialog(this);
187        dialog.Dispose();
188      }
189    }
190
191    private void GenerateImage() {
192      if (!Visible) {
193        renderingRequired = true;
194      } else {
195        if ((pictureBox.Width == 0) || (pictureBox.Height == 0)) {
196          bitmap = null;
197        } else {
198          bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
199          if (Chart != null) {
200            Graphics graphics = Graphics.FromImage(bitmap);
201            graphics.SetClip(new System.Drawing.Rectangle(0, 0, pictureBox.Width, pictureBox.Height));
202            Chart.Render(graphics, pictureBox.Width, pictureBox.Height);
203            graphics.Dispose();
204          }
205        }
206        if (pictureBox.Image != null) pictureBox.Image.Dispose();
207        pictureBox.Image = bitmap;
208        renderingRequired = false;
209      }
210    }
211
212    private void SetMode(ChartMode mode) {
213      moveToolStripMenuItem.Checked = false;
214      zoomToolStripMenuItem.Checked = false;
215      selectToolStripMenuItem.Checked = false;
216      if (mode == ChartMode.Move) moveToolStripMenuItem.Checked = true;
217      else if (mode == ChartMode.Zoom) zoomToolStripMenuItem.Checked = true;
218      else if (mode == ChartMode.Select) selectToolStripMenuItem.Checked = true;
219      Chart.Mode = mode;
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.