File: LineGraphControl.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (178 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Windows.Forms;
using QuickRoute.BusinessEntities;
using System.Drawing;

namespace QuickRoute.Controls
{
  public partial class LineGraphControl : UserControl
  {
    private LineGraph graph = new LineGraph();
    private Bitmap graphBitmap;
    private Bitmap hoverBitmap;
    private double? hoverXValue;
    private readonly Pen haircrossPen = new Pen(Color.FromArgb(128, Color.Red), 1);
    private readonly DiscAndCircleDrawer haircrossMarker =
      new DiscAndCircleDrawer(Color.FromArgb(192, Color.Red), 4,
                              Color.FromArgb(192, Color.Black), 6, 2);

    public event EventHandler<Canvas.RouteMouseHoverEventArgs> GraphMouseHover;
    public event EventHandler<Canvas.RouteMouseHoverEventArgs> GraphMouseDown;
    public event EventHandler<Canvas.RouteMouseHoverEventArgs> GraphMouseUp;

    public LineGraphControl()
    {
      InitializeComponent();
    }

    ~LineGraphControl()
    {
      haircrossPen.Dispose();
      if (graphBitmap != null) graphBitmap.Dispose();
      if (hoverBitmap != null) hoverBitmap.Dispose();
    }

    public LineGraph Graph
    {
      get { return graph; }
      set { graph = value; }
    }

    public double? HoverXValue
    {
      get { return hoverXValue; }
      set
      {
        hoverXValue = value;
        Draw();
      }
    }

    private void LineGraphControl_Paint(object sender, PaintEventArgs e)
    {
      Draw(e.Graphics);
    }

    private void LineGraphControl_Resize(object sender, System.EventArgs e)
    {
      Create();
      Draw();
    }

    public void Create()
    {
      if (graphBitmap != null) graphBitmap.Dispose();
      if (hoverBitmap != null) hoverBitmap.Dispose();
      if (Width > 0 && Height > 0)
      {
        graphBitmap = new Bitmap(Width, Height);
        hoverBitmap = new Bitmap(Width, Height);
        Graphics g = Graphics.FromImage(graphBitmap);
        g.Clear(BackColor);
        graph.Draw(g, new RectangleF(0, 0, Width, Height));
        g.Dispose();
      }
      else
      {
        graphBitmap = null;
        hoverBitmap = null;
      }
    }

    public void Draw(Graphics g)
    {
      if (graphBitmap == null) return;
      if (hoverXValue.HasValue && hoverXValue.Value >= graph.XAxisMinValue && hoverXValue.Value <= graph.XAxisMaxValue)
      {
        Graphics hoverGraphics = Graphics.FromImage(hoverBitmap);
        hoverGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        hoverGraphics.Clear(BackColor);
        hoverGraphics.DrawImage(graphBitmap, 0, 0);
        PointF centerF = new PointF(graph.ValueToX(hoverXValue.Value),
                                    graph.ValueToY(graph.GetYValueFromXValue(hoverXValue.Value)));
        Point center = new Point((int)centerF.X, (int)centerF.Y);
        center.Y = Math.Max(graph.GraphDrawingRectangle.Top, Math.Min(graph.GraphDrawingRectangle.Bottom, center.Y));
        SizeF size = new SizeF(10, 10);
        RectangleF rect = new RectangleF(center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height);
        hoverGraphics.DrawLine(haircrossPen, center, new Point(center.X, graph.GraphDrawingRectangle.Bottom));
        hoverGraphics.DrawLine(haircrossPen, new Point(graph.GraphDrawingRectangle.Left, center.Y), new Point(graph.GraphDrawingRectangle.Right, center.Y));
        haircrossMarker.Draw(hoverGraphics, new PointD(center.X, center.Y), 1);
        g.DrawImage(hoverBitmap, 0, 0);
        hoverGraphics.Dispose();
      }
      else
      {
        g.DrawImage(graphBitmap, new Point(0, 0));
      }
    }

    public void Draw()
    {
      Draw(CreateGraphics());
    }

    private ParameterizedLocation GetParameterizedLocationFromMouseLocation(int x)
    {
      double? xValue = graph.XToValue(x);
      if (xValue < graph.XAxisMinValue || xValue > graph.XAxisMaxValue) xValue = null;

      ParameterizedLocation pl = null;
      if (xValue != null)
      {
        DateTime time;
        switch (graph.XAxisAttribute)
        {
          case DomainAttribute.TimeOfDay:
            time = new DateTime((long)(xValue.Value * TimeSpan.TicksPerSecond)).ToUniversalTime();
            pl = graph.Session.Route.GetParameterizedLocationFromTime(time, true);
            break;
          case DomainAttribute.ElapsedTime:
            time = graph.Session.Route.FirstWaypoint.Time.AddTicks((long)xValue.Value * TimeSpan.TicksPerSecond);
            pl = graph.Session.Route.GetParameterizedLocationFromTime(time);
            break;
          case DomainAttribute.Distance:
            pl = graph.Session.Route.GetParameterizedLocationFromDistance(xValue.Value);
            break;
        }
      }
      return pl;
    }

    private void LineGraphControl_MouseMove(object sender, MouseEventArgs e)
    {
      if (graph.Session == null) return;
      hoverXValue = graph.XToValue(e.X);
      var pl = GetParameterizedLocationFromMouseLocation(e.X);
      if (pl == null || (hoverXValue.HasValue && (hoverXValue.Value < graph.XAxisMinValue || hoverXValue.Value > graph.XAxisMaxValue))) hoverXValue = null;
      Draw();
      if (GraphMouseHover != null) GraphMouseHover(this, new Canvas.RouteMouseHoverEventArgs(pl, hoverXValue != null));
    }

    private void LineGraphControl_MouseDown(object sender, MouseEventArgs e)
    {
      if (graph.Session == null) return;
      hoverXValue = graph.XToValue(e.X);
      if (hoverXValue.HasValue && hoverXValue.Value >= graph.XAxisMinValue && hoverXValue.Value <= graph.XAxisMaxValue)
      {
        var pl = GetParameterizedLocationFromMouseLocation(e.X);
        if (pl == null) hoverXValue = null; // prevent marker hovering
        Draw();
        if (GraphMouseDown != null) GraphMouseDown(this, new Canvas.RouteMouseHoverEventArgs(pl, hoverXValue != null));
      }
    }

    private void LineGraphControl_MouseUp(object sender, MouseEventArgs e)
    {
      if (graph.Session == null) return;
      hoverXValue = graph.XToValue(e.X);
      if (hoverXValue.HasValue && hoverXValue.Value >= graph.XAxisMinValue && hoverXValue.Value <= graph.XAxisMaxValue)
      {
        var pl = GetParameterizedLocationFromMouseLocation(e.X);
        if (pl == null) hoverXValue = null; // prevent marker hovering
        Draw();
        if (GraphMouseUp != null) GraphMouseUp(this, new Canvas.RouteMouseHoverEventArgs(pl, hoverXValue != null));
      }
    }
  }

}