1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Drawing.Drawing2D;
|
---|
27 | using System.Drawing.Imaging;
|
---|
28 | using System.Linq;
|
---|
29 | using System.Windows.Forms;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Visualization {
|
---|
32 | public partial class ChartControl : UserControl {
|
---|
33 | protected Bitmap Picture;
|
---|
34 | protected bool RenderingRequired;
|
---|
35 | protected Dictionary<Type, ChartMode> Modes = new Dictionary<Type, ChartMode>();
|
---|
36 | protected Stack<ChartMode> ModeStack = new Stack<ChartMode>();
|
---|
37 | protected List<ChartMode.Shortcut> Shortcuts = new List<ChartMode.Shortcut>();
|
---|
38 | protected ChartMode.Shortcut ActiveShortcut;
|
---|
39 |
|
---|
40 | protected bool SuppressEvents { get; set; }
|
---|
41 | private bool SuppressRender { get; set; }
|
---|
42 |
|
---|
43 | public bool ShowToolBar {
|
---|
44 | get { return !splitContainer.Panel1Collapsed; }
|
---|
45 | set { splitContainer.Panel1Collapsed = !value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private IChart chart;
|
---|
49 | public IChart Chart {
|
---|
50 | get { return chart; }
|
---|
51 | protected set {
|
---|
52 | if (chart != null) DeregisterChartEvents(chart);
|
---|
53 | chart = value;
|
---|
54 | chart.Enabled = Enabled;
|
---|
55 | pictureBox.Enabled = Enabled;
|
---|
56 | if (chart != null) RegisterChartEvents(chart);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private ChartMode mode;
|
---|
61 | private ChartMode defaultMode;
|
---|
62 | public ChartMode Mode {
|
---|
63 | get { return mode; }
|
---|
64 | set {
|
---|
65 | if (mode == value) return;
|
---|
66 | mode = value;
|
---|
67 | if (defaultMode == null) defaultMode = mode;
|
---|
68 | if (mode == null) mode = defaultMode;
|
---|
69 | SetModeButtonCheckedState();
|
---|
70 | SetModeMenuItemCheckedState();
|
---|
71 | SetPictureBoxCursor(GetPictureBoxCursor());
|
---|
72 | OnModeChanged();
|
---|
73 | if (chart != null) {
|
---|
74 | GenerateImage();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public SmoothingMode SmoothingMode { get; set; }
|
---|
80 |
|
---|
81 | public bool ScaleOnResize { get; set; }
|
---|
82 |
|
---|
83 | public bool IsRenderingPaused { get { return SuppressRender; } }
|
---|
84 |
|
---|
85 | public ChartControl() : this(null) { }
|
---|
86 | public ChartControl(IChart defaultChart) {
|
---|
87 | InitializeComponent();
|
---|
88 | ScaleOnResize = true;
|
---|
89 | Chart = defaultChart ?? new Chart(0, 0, Width, Height);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void AddChartModes(params ChartMode[] chartModes) {
|
---|
93 | foreach (var chartMode in chartModes) {
|
---|
94 | Modes.Add(chartMode.GetType(), chartMode);
|
---|
95 | foreach (var shortcut in chartMode.Shortcuts)
|
---|
96 | Shortcuts.Add(shortcut);
|
---|
97 | }
|
---|
98 |
|
---|
99 | ReloadModeToolBar();
|
---|
100 | ReloadModeContextMenu();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void SetTempChartMode(ChartMode chartMode) {
|
---|
104 | if (Mode == chartMode) return;
|
---|
105 |
|
---|
106 | ModeStack.Push(mode);
|
---|
107 | Mode = chartMode;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void ResetTempChartMode() {
|
---|
111 | if (ModeStack.Any())
|
---|
112 | Mode = ModeStack.Pop();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void SuspendRendering() {
|
---|
116 | SuppressRender = true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void ResumeRendering() {
|
---|
120 | SuppressRender = false;
|
---|
121 | if (Chart != null) GenerateImage();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void RefreshPicture() {
|
---|
125 | pictureBox.Refresh();
|
---|
126 | }
|
---|
127 |
|
---|
128 | public void UpdatePicture() {
|
---|
129 | pictureBox.Update();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public Graphics CreatePictureGraphics() {
|
---|
133 | return pictureBox.CreateGraphics();
|
---|
134 | }
|
---|
135 |
|
---|
136 | public System.Drawing.Rectangle GetPictureBounds() {
|
---|
137 | return pictureBox.Bounds;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected virtual void ReloadModeToolBar() {
|
---|
141 | splitContainer.Panel1.Controls.Clear();
|
---|
142 |
|
---|
143 | var chartModes = Modes.Values.ToList();
|
---|
144 | for (int i = 0; i < chartModes.Count; i++) {
|
---|
145 | var chartMode = chartModes[i];
|
---|
146 | var rb = CreateChartModeRadioButton(chartMode);
|
---|
147 | rb.Location = new Point(3, 3 + i * (rb.Height + rb.Margin.Vertical));
|
---|
148 | rb.TabIndex = i;
|
---|
149 | toolTip.SetToolTip(rb, chartMode.ToolTipText);
|
---|
150 | splitContainer.Panel1.Controls.Add(rb);
|
---|
151 | }
|
---|
152 |
|
---|
153 | SetModeButtonCheckedState();
|
---|
154 | }
|
---|
155 |
|
---|
156 | protected virtual void ReloadModeContextMenu() {
|
---|
157 | modeToolStripMenuItem.DropDownItems.Clear();
|
---|
158 |
|
---|
159 | foreach (var chartMode in Modes.Values) {
|
---|
160 | var mi = CreateChartModeMenuItem(chartMode);
|
---|
161 | modeToolStripMenuItem.DropDownItems.Add(mi);
|
---|
162 | }
|
---|
163 |
|
---|
164 | SetModeMenuItemCheckedState();
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected virtual void SetModeButtonCheckedState() {
|
---|
168 | foreach (var rb in splitContainer.Panel1.Controls.OfType<RadioButton>())
|
---|
169 | rb.Checked = rb.Tag == Mode;
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected virtual void SetModeMenuItemCheckedState() {
|
---|
173 | foreach (var mi in modeToolStripMenuItem.DropDownItems.OfType<ToolStripMenuItem>())
|
---|
174 | mi.Checked = mi.Tag == Mode;
|
---|
175 | }
|
---|
176 |
|
---|
177 | protected virtual void SetPictureBoxCursor(Cursor cursor) {
|
---|
178 | if (pictureBox.Cursor != cursor) pictureBox.Cursor = cursor;
|
---|
179 | }
|
---|
180 |
|
---|
181 | protected virtual Cursor GetPictureBoxCursor() {
|
---|
182 | return mode != null ? mode.Cursor : null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | protected virtual void RegisterChartEvents(IChart chart) {
|
---|
186 | chart.RedrawRequired += ChartOnUpdateRequired;
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected virtual void DeregisterChartEvents(IChart chart) {
|
---|
190 | chart.RedrawRequired -= ChartOnUpdateRequired;
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected virtual void ChartOnUpdateRequired(object sender, EventArgs e) {
|
---|
194 | if (SuppressRender) return;
|
---|
195 | GenerateImage();
|
---|
196 | }
|
---|
197 |
|
---|
198 | #region PictureBox Events
|
---|
199 | protected virtual void PictureBoxOnSizeChanged(object sender, EventArgs e) {
|
---|
200 | if (Chart == null) return;
|
---|
201 | if (ScaleOnResize) {
|
---|
202 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0) && (Chart != null)) {
|
---|
203 | var point = Chart.TransformPixelToWorld(new Point(pictureBox.Width, Chart.SizeInPixels.Height - pictureBox.Height));
|
---|
204 | var diff = Chart.UpperRight - point;
|
---|
205 | Chart.SetPosition(new PointD(Chart.LowerLeft.X, Chart.LowerLeft.Y),
|
---|
206 | new PointD(Chart.UpperRight.X - diff.DX, Chart.UpperRight.Y - diff.DY));
|
---|
207 | }
|
---|
208 | }
|
---|
209 | GenerateImage();
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected virtual void PictureBoxOnVisibleChanged(object sender, EventArgs e) {
|
---|
213 | if (pictureBox.Visible && RenderingRequired) GenerateImage();
|
---|
214 | }
|
---|
215 |
|
---|
216 | protected virtual void PictureBoxOnMouseClick(object sender, MouseEventArgs e) {
|
---|
217 | if (mode != null)
|
---|
218 | mode.HandleOnMouseClick(sender, e);
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected virtual void PictureBoxOnMouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
222 | if (mode != null)
|
---|
223 | mode.HandleOnMouseDoubleClick(sender, e);
|
---|
224 | }
|
---|
225 |
|
---|
226 | protected virtual void OnKeyDown(object sender, KeyEventArgs e) {
|
---|
227 | if (mode != null) {
|
---|
228 | mode.HandleOnKeyDown(sender, e);
|
---|
229 |
|
---|
230 | if (e.Handled || ActiveShortcut != null) return;
|
---|
231 |
|
---|
232 | foreach (var shortcut in Shortcuts) {
|
---|
233 | if (e.KeyCode == shortcut.Key && e.Modifiers == shortcut.Modifiers) {
|
---|
234 | ActiveShortcut = shortcut;
|
---|
235 | var downAction = shortcut.DownAction;
|
---|
236 | if (downAction != null) downAction();
|
---|
237 | e.Handled = true;
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | protected virtual void OnKeyUp(object sender, KeyEventArgs e) {
|
---|
245 | if (mode != null) {
|
---|
246 | mode.HandleOnKeyUp(sender, e);
|
---|
247 |
|
---|
248 | if (e.Handled || ActiveShortcut == null) return;
|
---|
249 |
|
---|
250 | var modifiers = new List<Keys>(from key in Enum.GetValues(typeof(Keys)).Cast<Keys>()
|
---|
251 | where e.Modifiers.HasFlag(key)
|
---|
252 | select key);
|
---|
253 |
|
---|
254 | if (ActiveShortcut.Key == e.KeyCode || ActiveShortcut.GetModifiers().Except(modifiers).Any()) {
|
---|
255 | var upAction = ActiveShortcut.UpAction;
|
---|
256 | if (upAction != null) upAction();
|
---|
257 | ActiveShortcut = null;
|
---|
258 | e.Handled = true;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | protected virtual void PictureBoxOnMouseWheel(object sender, MouseEventArgs e) {
|
---|
264 | if (mode != null)
|
---|
265 | mode.HandleOnMouseWheel(sender, e);
|
---|
266 | }
|
---|
267 |
|
---|
268 | protected virtual void PictureBoxOnMouseDown(object sender, MouseEventArgs e) {
|
---|
269 | if (mode != null)
|
---|
270 | mode.HandleOnMouseDown(sender, e);
|
---|
271 | }
|
---|
272 |
|
---|
273 | protected virtual void PictureBoxOnMouseUp(object sender, MouseEventArgs e) {
|
---|
274 | if (mode != null)
|
---|
275 | mode.HandleOnMouseUp(sender, e);
|
---|
276 |
|
---|
277 | if (Chart == null) return;
|
---|
278 |
|
---|
279 | if (e.Button == MouseButtons.Right) {
|
---|
280 | propertiesToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
|
---|
281 | oneLayerUpToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
|
---|
282 | oneLayerDownToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
|
---|
283 | intoForegroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
|
---|
284 | intoBackgroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | protected virtual void PictureBoxOnMouseMove(object sender, MouseEventArgs e) {
|
---|
289 | if (InvokeRequired) {
|
---|
290 | Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseMove, sender, e);
|
---|
291 | } else {
|
---|
292 | var toolTipText = Chart.GetToolTipText(e.Location);
|
---|
293 | if (toolTip.GetToolTip(pictureBox) != toolTipText) toolTip.SetToolTip(pictureBox, toolTipText);
|
---|
294 |
|
---|
295 | if (mode != null)
|
---|
296 | mode.HandleOnMouseMove(sender, e);
|
---|
297 |
|
---|
298 | SetPictureBoxCursor(GetPictureBoxCursor());
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | protected virtual void PictureBoxOnMouseEnter(object sender, EventArgs e) {
|
---|
303 | if (InvokeRequired) {
|
---|
304 | Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseEnter, sender, e);
|
---|
305 | return;
|
---|
306 | }
|
---|
307 | if (!Focused) pictureBox.Focus();
|
---|
308 |
|
---|
309 | if (mode != null)
|
---|
310 | mode.HandleOnMouseEnter(sender, e);
|
---|
311 | }
|
---|
312 |
|
---|
313 | protected virtual void PictureBoxOnMouseLeave(object sender, EventArgs e) {
|
---|
314 | if (mode != null)
|
---|
315 | mode.HandleOnMouseLeave(sender, e);
|
---|
316 | }
|
---|
317 |
|
---|
318 | protected virtual void PictureBoxOnClick(object sender, EventArgs e) {
|
---|
319 | if (InvokeRequired) {
|
---|
320 | Invoke((Action<object, MouseEventArgs>)PictureBoxOnClick, sender, e);
|
---|
321 | return;
|
---|
322 | }
|
---|
323 | if (!Focused) pictureBox.Focus();
|
---|
324 |
|
---|
325 | if (mode != null)
|
---|
326 | mode.HandleOnClick(sender, e);
|
---|
327 | }
|
---|
328 | #endregion
|
---|
329 |
|
---|
330 | protected virtual void PictureBoxContextMenuStripOnOpening(object sender, CancelEventArgs e) { }
|
---|
331 |
|
---|
332 | protected virtual void oneLayerUpToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
333 | if (Chart == null) return;
|
---|
334 | if (Chart.Group.SelectedPrimitives.Count() == 1) {
|
---|
335 | Chart.OneLayerUp(Chart.Group.SelectedPrimitives.First());
|
---|
336 | }
|
---|
337 | }
|
---|
338 | protected virtual void oneLayerDownToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
339 | if (Chart == null) return;
|
---|
340 | if (Chart.Group.SelectedPrimitives.Count() == 1) {
|
---|
341 | Chart.OneLayerDown(Chart.Group.SelectedPrimitives.First());
|
---|
342 | }
|
---|
343 | }
|
---|
344 | protected virtual void intoForegroundToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
345 | if (Chart == null) return;
|
---|
346 | if (Chart.Group.SelectedPrimitives.Count() == 1) {
|
---|
347 | Chart.IntoForeground(Chart.Group.SelectedPrimitives.First());
|
---|
348 | }
|
---|
349 | }
|
---|
350 | protected virtual void intoBackgroundToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
351 | if (Chart == null) return;
|
---|
352 | if (Chart.Group.SelectedPrimitives.Count() == 1) {
|
---|
353 | Chart.IntoBackground(Chart.Group.SelectedPrimitives.First());
|
---|
354 | }
|
---|
355 | }
|
---|
356 | protected virtual void propertiesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
357 | if (Chart == null) return;
|
---|
358 | if (Chart.Group.SelectedPrimitives.Count() == 1) {
|
---|
359 | using (var dialog = new PropertiesDialog(Chart.Group.SelectedPrimitives.First())) {
|
---|
360 | dialog.ShowDialog(this);
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | protected virtual void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
365 | if (Picture == null) return;
|
---|
366 |
|
---|
367 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
368 | var format = ImageFormat.Bmp;
|
---|
369 | var filename = saveFileDialog.FileName.ToLower();
|
---|
370 | if (filename.EndsWith("emf")) {
|
---|
371 | using (var g1 = Graphics.FromImage(Picture)) {
|
---|
372 | var rectangle = new System.Drawing.Rectangle(0, 0, Picture.Width, Picture.Height);
|
---|
373 | using (var metafile = new Metafile(filename, g1.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual))
|
---|
374 | using (var g2 = Graphics.FromImage(metafile))
|
---|
375 | Chart.Render(g2, pictureBox.Width, pictureBox.Height);
|
---|
376 | }
|
---|
377 | } else {
|
---|
378 | using (var graphics = Graphics.FromImage(Picture))
|
---|
379 | Chart.Render(graphics, Picture.Width, Picture.Height);
|
---|
380 |
|
---|
381 | if (filename.EndsWith("jpg")) {
|
---|
382 | format = ImageFormat.Jpeg;
|
---|
383 | } else if (filename.EndsWith("gif")) {
|
---|
384 | format = ImageFormat.Gif;
|
---|
385 | } else if (filename.EndsWith("png")) {
|
---|
386 | format = ImageFormat.Png;
|
---|
387 | } else if (filename.EndsWith("tif")) {
|
---|
388 | format = ImageFormat.Tiff;
|
---|
389 | }
|
---|
390 |
|
---|
391 | Picture.Save(saveFileDialog.FileName, format);
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | protected virtual void GenerateImage() {
|
---|
397 | if (InvokeRequired) {
|
---|
398 | Invoke((Action)GenerateImage);
|
---|
399 | return;
|
---|
400 | }
|
---|
401 | if (!Visible) {
|
---|
402 | RenderingRequired = true;
|
---|
403 | } else {
|
---|
404 | if ((pictureBox.Width == 0) || (pictureBox.Height == 0)) {
|
---|
405 | pictureBox.Image = pictureBox.ErrorImage;
|
---|
406 | if (Picture != null) Picture.Dispose();
|
---|
407 | Picture = null;
|
---|
408 | } else {
|
---|
409 | if (Picture != null) Picture.Dispose();
|
---|
410 | Picture = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
411 | using (var graphics = Graphics.FromImage(Picture)) {
|
---|
412 | graphics.SmoothingMode = SmoothingMode;
|
---|
413 | graphics.SetClip(new System.Drawing.Rectangle(0, 0, pictureBox.Width, pictureBox.Height));
|
---|
414 | Chart.Render(graphics, pictureBox.Width, pictureBox.Height);
|
---|
415 | }
|
---|
416 | }
|
---|
417 | pictureBox.Image = Picture;
|
---|
418 | RenderingRequired = false;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | public event EventHandler ModeChanged;
|
---|
423 | protected void OnModeChanged() {
|
---|
424 | var handler = ModeChanged;
|
---|
425 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
426 | }
|
---|
427 |
|
---|
428 | private void ChartControl_Load(object sender, EventArgs e) {
|
---|
429 | if (!SuppressRender) GenerateImage();
|
---|
430 | }
|
---|
431 |
|
---|
432 | #region Helpers
|
---|
433 | private RadioButton CreateChartModeRadioButton(ChartMode chartMode) {
|
---|
434 | var rb = new RadioButton {
|
---|
435 | Appearance = Appearance.Button,
|
---|
436 | Image = chartMode.Image,
|
---|
437 | Size = new Size(24, 24),
|
---|
438 | TabStop = true,
|
---|
439 | Tag = chartMode,
|
---|
440 | UseVisualStyleBackColor = true
|
---|
441 | };
|
---|
442 |
|
---|
443 | rb.CheckedChanged += (sender, args) => { if (rb.Checked) chartMode.Select(); };
|
---|
444 |
|
---|
445 | return rb;
|
---|
446 | }
|
---|
447 |
|
---|
448 | private ToolStripMenuItem CreateChartModeMenuItem(ChartMode chartMode) {
|
---|
449 | var mi = new ToolStripMenuItem(chartMode.Text) {
|
---|
450 | CheckOnClick = true,
|
---|
451 | Image = chartMode.Image,
|
---|
452 | Tag = chartMode
|
---|
453 | };
|
---|
454 |
|
---|
455 | mi.CheckedChanged += (sender, args) => { if (mi.Checked) chartMode.Select(); };
|
---|
456 |
|
---|
457 | return mi;
|
---|
458 | }
|
---|
459 | #endregion
|
---|
460 | }
|
---|
461 | }
|
---|