Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Charting/ChartControl.cs @ 4062

Last change on this file since 4062 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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