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.Drawing;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Common.Resources;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Visualization.ChartModes {
|
---|
28 | public class RulerChartMode : ChartMode {
|
---|
29 | protected Point? startPoint;
|
---|
30 |
|
---|
31 | public override Image Image { get { return VSImageLibrary.TrackBar; } }
|
---|
32 | public override string Text { get { return "&Ruler"; } }
|
---|
33 | public override string ToolTipText { get { return "Ruler"; } }
|
---|
34 | public override Cursor Cursor { get { return Cursors.Cross; } }
|
---|
35 |
|
---|
36 | public RulerChartMode(ChartControl control) : base(control) { }
|
---|
37 |
|
---|
38 | public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
|
---|
39 | try {
|
---|
40 | switch (e.Button) {
|
---|
41 | case MouseButtons.Left:
|
---|
42 | if (startPoint.HasValue) {
|
---|
43 | if (startPoint.Value == e.Location) {
|
---|
44 | startPoint = null;
|
---|
45 | chartControl.RefreshPicture();
|
---|
46 | }
|
---|
47 | } else
|
---|
48 | startPoint = e.Location;
|
---|
49 | break;
|
---|
50 | }
|
---|
51 | } finally {
|
---|
52 | base.HandleOnMouseDown(sender, e);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
|
---|
57 | try {
|
---|
58 | if (startPoint.HasValue) {
|
---|
59 | chartControl.RefreshPicture();
|
---|
60 | using (var graphics = chartControl.CreatePictureGraphics()) {
|
---|
61 | DrawCross(graphics, startPoint.Value);
|
---|
62 | DrawCross(graphics, e.Location);
|
---|
63 | DrawLineBetweenLocations(graphics, startPoint.Value, e.Location);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | } finally {
|
---|
67 | base.HandleOnMouseMove(sender, e);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
|
---|
72 | try {
|
---|
73 | switch (e.Button) {
|
---|
74 | case MouseButtons.Left:
|
---|
75 | if (startPoint.HasValue && startPoint.Value != e.Location) {
|
---|
76 | using (var graphics = chartControl.CreatePictureGraphics())
|
---|
77 | DrawInfoBox(graphics, e.Location);
|
---|
78 | startPoint = null;
|
---|
79 | }
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | } finally {
|
---|
83 | base.HandleOnMouseUp(sender, e);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | #region Helpers
|
---|
88 | protected virtual void DrawCross(Graphics graphics, Point point) {
|
---|
89 | using (var pen = new Pen(Color.Red)) {
|
---|
90 | graphics.DrawLine(pen, point.X - 3, point.Y - 3, point.X + 3, point.Y + 3);
|
---|
91 | graphics.DrawLine(pen, point.X - 3, point.Y + 3, point.X + 3, point.Y - 3);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected virtual void DrawLineBetweenLocations(Graphics graphics, Point startLocation, Point endLocation) {
|
---|
96 | using (var pen = new Pen(Color.Red))
|
---|
97 | graphics.DrawLine(pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected virtual void DrawInfoBox(Graphics graphics, Point location) {
|
---|
101 | var offsetP = new Size(location.X - startPoint.Value.X, location.Y - startPoint.Value.Y);
|
---|
102 | var offsetW = chartControl.Chart.TransformPixelToWorld(offsetP);
|
---|
103 |
|
---|
104 | double lengthP = Math.Sqrt(Math.Pow(offsetP.Width, 2) + Math.Pow(offsetP.Height, 2));
|
---|
105 | double lengthW = Math.Sqrt(Math.Pow(offsetW.Width, 2) + Math.Pow(offsetW.Height, 2));
|
---|
106 | string text = string.Format("Pixel: {0} " + Environment.NewLine + "World: {1:##.###}", Math.Round(lengthP), Math.Round(lengthW, 3));
|
---|
107 |
|
---|
108 | var textSize = graphics.MeasureString(text, SystemFonts.DefaultFont);
|
---|
109 | int width = (int)Math.Ceiling(textSize.Width);
|
---|
110 | int height = (int)Math.Ceiling(textSize.Height);
|
---|
111 |
|
---|
112 | graphics.FillRectangle(Brushes.White, location.X, location.Y, width, height);
|
---|
113 | graphics.DrawRectangle(Pens.Red, location.X, location.Y, width, height);
|
---|
114 | graphics.DrawString(text, SystemFonts.DefaultFont, Brushes.Black, location.X, location.Y + 2);
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 | }
|
---|
118 | }
|
---|