/[cits3200i]/trunk/UWAPlugins/MetadataPlugin/Source/MetadataRouteControlLayer.cs


UCC Code Repository

Contents of /trunk/UWAPlugins/MetadataPlugin/Source/MetadataRouteControlLayer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (show annotations) (download)
Wed Oct 13 01:51:32 2010 UTC (11 years, 1 month ago) by rvvs89
File size: 5933 byte(s)
Merging metadata-branch-rvvs89.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using ZoneFiveSoftware.Common.Visuals.Mapping;
5 using ZoneFiveSoftware.Common.Data.Fitness;
6 using ZoneFiveSoftware.Common.Visuals.Util;
7 using ZoneFiveSoftware.Common.Data.GPS;
8 using ZoneFiveSoftware.Common.Data;
9
10 namespace MetadataPlugin.Source
11 {
12 class MetadataRouteControlLayer : IRouteControlLayer
13 {
14 private static IList<MetadataRouteControlLayer> instances = new List<MetadataRouteControlLayer>(3);
15 private IRouteControl control;
16 private MetadataRouteControlLayerProvider provider;
17 private IActivity currentActivity;
18 private MetadataList currentList;
19
20 public MetadataRouteControlLayer(MetadataRouteControlLayerProvider provider, IRouteControl control)
21 {
22 this.provider = provider;
23 this.control = control;
24 instances.Add(this);
25 control.ItemsChanged += new EventHandler(control_ItemsChanged);
26 control.MapControlChanged += new EventHandler(control_MapControlChanged);
27 }
28
29 private void control_MapControlChanged(object sender, EventArgs e)
30 {
31 }
32
33 private void control_ItemsChanged(object sender, EventArgs e)
34 {
35 if (instances[0] == this)
36 {
37 IActivity selected = CollectionUtils.GetSingleItemOfType<IActivity>(MetadataActivityReportPages.View.SelectionProvider.SelectedItems);
38 if (selected != null && selected != currentActivity)
39 {
40 currentActivity = selected;
41 currentList = MetadataList.LoadFromActivity(currentActivity);
42 }
43 }
44 }
45
46 private static DateTime DateTimeMin(DateTime a, DateTime b)
47 {
48 return a < b ? a : b;
49 }
50
51 private static DateTime DateTimeMax(DateTime a, DateTime b)
52 {
53 return a > b ? a : b;
54 }
55
56 private static int RouteIndexFromDistance(IGPSRoute route, double distance, out double remaining)
57 {
58 IEnumerator<ITimeValueEntry<IGPSPoint>> i = route.GetEnumerator();
59 int index = 0;
60 double count = 0.0;
61 i.MoveNext();
62 IGPSPoint prev = i.Current.Value;
63 remaining = 0;
64 while (i.MoveNext())
65 {
66 IGPSPoint cur = i.Current.Value;
67 remaining = distance - count;
68 count += prev.DistanceMetersToPoint(cur);
69 if (count < distance)
70 {
71 index++;
72 prev = cur;
73 }
74 else
75 {
76 return index;
77 }
78 }
79 return index;
80 }
81
82 private static DateTime RouteTimeFromDistance(IGPSRoute route, double distance)
83 {
84 double remaining;
85 int index = RouteIndexFromDistance(route, distance, out remaining);
86 if (remaining > 0 && route.Count > index + 1)
87 {
88 remaining /= route[index].Value.DistanceMetersToPoint(route[index + 1].Value);
89 remaining *= route[index + 1].ElapsedSeconds - route[index].ElapsedSeconds;
90 return route.StartTime.AddSeconds(route[index].ElapsedSeconds + remaining);
91 }
92 else
93 {
94 return route.StartTime.AddSeconds(route[index].ElapsedSeconds);
95 }
96 }
97
98 public void CalculateSelectedRegion(out DateTime start, out DateTime end)
99 {
100 start = DateTime.MaxValue;
101 end = DateTime.MinValue;
102 IRouteControl control = MetadataRouteControlLayer.Instances[0].Control;
103 IActivity activity = CollectionUtils.GetSingleItemOfType<IActivity>(MetadataActivityReportPages.View.SelectionProvider.SelectedItems);
104 if (activity == null)
105 {
106 return;
107 }
108 IGPSRoute route = activity.GPSRoute;
109 for (int k = 0; k < control.SelectedItems.Count; k++)
110 {
111 IRouteControlSelection selection = control.SelectedItems[k];
112 if (selection == null || !selection.Item.DisplayRoute || selection.Item.Item == null)
113 {
114 return;
115 }
116 if (selection.DistanceMetersRanges != null && selection.DistanceMetersRanges.Count >= 1)
117 {
118 start = DateTimeMin(start, RouteTimeFromDistance(route, selection.DistanceMetersRanges[0].Lower));
119 end = DateTimeMax(end, RouteTimeFromDistance(route, selection.DistanceMetersRanges[0].Upper));
120 }
121 else if (selection.TimeRanges != null && selection.TimeRanges.Count >= 1)
122 {
123 start = DateTimeMin(start, selection.TimeRanges[0].Lower);
124 end = DateTimeMax(start, selection.TimeRanges[0].Upper);
125 }
126 }
127 if (start == DateTime.MaxValue)
128 {
129 start = DateTime.MinValue;
130 }
131 if (end == DateTime.MinValue)
132 {
133 end = DateTime.MaxValue;
134 }
135 }
136
137 public static IList<MetadataRouteControlLayer> Instances
138 {
139 get { return instances; }
140 }
141
142 public MetadataList CurrentList
143 {
144 get { return currentList; }
145 }
146
147 public IRouteControl Control
148 {
149 get { return control; }
150 }
151
152 public IRouteControlLayerProvider Provider
153 {
154 get { return provider; }
155 }
156
157 public int ZOrder
158 {
159 get { return 0; }
160 }
161 }
162 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26