/[cits3200i]/branches/metadata-branch-rvvs89/UWAPlugins/MetadataPlugin/Source/MetadataList.cs


UCC Code Repository

Contents of /branches/metadata-branch-rvvs89/UWAPlugins/MetadataPlugin/Source/MetadataList.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (show annotations) (download)
Wed Oct 13 00:11:17 2010 UTC (11 years, 1 month ago) by rvvs89
File size: 7628 byte(s)
EditFile control now supports picking files.
Selected file metadata is now serialized and hence recoverable from the logbook.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Runtime.Serialization;
6 using ZoneFiveSoftware.Common.Data.Fitness;
7 using ZoneFiveSoftware.Common.Visuals.Mapping;
8 using System.Drawing;
9 using System.Windows.Forms;
10 using ZoneFiveSoftware.Common.Data.GPS;
11 using ZoneFiveSoftware.Common.Data;
12 using ZoneFiveSoftware.Common.Visuals.Util;
13
14 namespace MetadataPlugin.Source
15 {
16 [Serializable]
17 class MetadataList : IDeserializationCallback
18 {
19 private IList<IMetadata> items = new List<IMetadata>();
20 [NonSerialized]
21 private IActivity activity;
22 [NonSerialized]
23 private MapIcon icon;
24
25 public MetadataList()
26 {
27 OnDeserialization(null);
28 }
29
30 private MapIcon MakeMapIcon(string name, Size size)
31 {
32 string testPath = Plugin.Instance.Application.Configuration.CommonPluginsInstalledFolder + "\\" + Plugin.Instance.Id.ToString("D") + "\\" + name;
33 if (File.Exists(testPath))
34 {
35 return new MapIcon(testPath, size, new Point(size.Width / 2, size.Height / 2));
36 }
37 return null;
38 }
39
40 public IActivity Activity
41 {
42 set { activity = value; }
43 }
44
45 public void Commit()
46 {
47 activity.SetExtensionData(Plugin.Instance.Id, MetadataListSerializer.Serialize(this));
48 }
49
50 public void OnDeserialization(object sender)
51 {
52 icon = MakeMapIcon("MetadataPlugin\\Data\\pointmarker.png", new Size(32, 32));
53 activity = CollectionUtils.GetSingleItemOfType<IActivity>(MetadataActivityReportPages.View.SelectionProvider.SelectedItems);
54 InvalidateAll();
55 }
56
57 public static MetadataList LoadFromActivity(IActivity activity)
58 {
59 if (activity == null)
60 return null;
61 MetadataList ret = MetadataListSerializer.Deserialize(activity.GetExtensionData(Plugin.Instance.Id));
62 if (ret == null)
63 ret = new MetadataList();
64 return ret;
65 }
66
67 public void Add(IMetadata item)
68 {
69 items.Add(item);
70 CreateMarker(item);
71 }
72
73 public void Remove(IMetadata item)
74 {
75 items.Remove(item);
76 RemoveMarker(item);
77 Commit();
78 }
79
80 public void Edit(IMetadata item)
81 {
82 EditMetadataDialog.Instance.Control.Editor = item.Editor;
83 EditMetadataDialog.Instance.ShowDialog();
84 }
85
86 public void SelectRegion(IMetadata item)
87 {
88 IRouteControlItem selected = MetadataRouteControlLayer.Instances[0].Control.Items[0];
89 DateTime start = selected.Item.GPSRoute.StartTime;
90 DateTime end = selected.Item.GPSRoute.StartTime.AddSeconds(selected.Item.GPSRoute.TotalElapsedSeconds);
91 switch(item.Location)
92 {
93 case MetadataLocation.POINT:
94 start = item.Point;
95 end = item.Point;
96 break;
97 case MetadataLocation.RANGE:
98 start = item.Point;
99 end = item.End;
100 break;
101 }
102 MetadataRouteControlSelection selection = new MetadataRouteControlSelection(selected, start, end);
103 MetadataRouteControlLayer.Instances[0].Control.SelectedItems = new List<IRouteControlSelection> { selection };
104 }
105
106 public void Invalidate(IMetadata item)
107 {
108 RemoveMarker(item);
109 CreateMarker(item);
110 }
111
112 public void InvalidateAll()
113 {
114 foreach (IMetadata item in items)
115 {
116 Invalidate(item);
117 }
118 }
119
120 private void RemoveMarker(IMetadata item)
121 {
122 if (item.FormData != null)
123 {
124 IMapOverlay marker = (MapMarker)item.FormData;
125 MetadataRouteControlLayer.Instances[0].Control.MapControl.RemoveOverlay(marker);
126 }
127 }
128
129 private void CreateMarker(IMetadata item)
130 {
131 MapMarker marker = new MapMarker(FindStartPoint(item), icon, true);
132 marker.MouseUp += new MouseEventHandler(marker_MouseUp);
133 marker.DoubleClick += new MouseEventHandler(marker_DoubleClick);
134 item.FormData = marker;
135 MetadataRouteControlLayer.Instances[0].Control.MapControl.AddOverlay(marker);
136 }
137
138 void marker_DoubleClick(object sender, MouseEventArgs e)
139 {
140 if (e.Button == MouseButtons.Left)
141 {
142 Edit(FindMetadataFromMarker((MapMarker)sender));
143 }
144 }
145
146 void marker_MouseUp(object sender, MouseEventArgs e)
147 {
148 IMetadata data = FindMetadataFromMarker((MapMarker)sender);
149 switch (e.Button)
150 {
151 case MouseButtons.Left:
152 SelectRegion(data);
153 break;
154 case MouseButtons.Right:
155 EditMetadataDialog.Instance.MapMarkerReference = data;
156 List<MetadataLocation> supported = new List<MetadataLocation>(data.Type.ApplicableLocations);
157 if (MetadataActivityReportPages.RANGE_ACTION.Enabled)
158 {
159 EditMetadataDialog.Instance.SupportsSelectedRegion = supported.Contains(MetadataLocation.RANGE);
160 }
161 else if (MetadataActivityReportPages.POINT_ACTION.Enabled)
162 {
163 EditMetadataDialog.Instance.SupportsSelectedRegion = supported.Contains(MetadataLocation.POINT);
164 }
165 else
166 {
167 EditMetadataDialog.Instance.SupportsSelectedRegion = false;
168 }
169 EditMetadataDialog.Instance.SupportsTrackSelection = supported.Contains(MetadataLocation.TRACK);
170 EditMetadataDialog.Instance.MapMarkerContextMenu.Show(MetadataRouteControlLayer.Instances[0].Control.MapControl.Control, e.Location);
171 break;
172 }
173 }
174
175 private IMetadata FindMetadataFromMarker(MapMarker marker)
176 {
177 IMetadata reference = null;
178 foreach (IMetadata item in items)
179 {
180 if (item.FormData == marker)
181 {
182 reference = item;
183 break;
184 }
185 }
186 if (reference == null)
187 {
188 MetadataRouteControlLayer.Instances[0].Control.MapControl.RemoveOverlay(marker);
189 }
190 return reference;
191 }
192
193 private IGPSPoint FindStartPoint(IMetadata item)
194 {
195 switch (item.Location)
196 {
197 case MetadataLocation.TRACK:
198 return activity.GPSRoute.GetInterpolatedValue(activity.StartTime).Value;
199 case MetadataLocation.RANGE:
200 return activity.GPSRoute.GetInterpolatedValue(item.Point).Value;
201 case MetadataLocation.POINT:
202 return activity.GPSRoute.GetInterpolatedValue(item.Point).Value;
203 }
204 return activity.GPSRoute.GetInterpolatedValue(activity.StartTime).Value;
205 }
206 }
207 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26