/[cits3200i]/branches/gps-running-clean-r116/AccumulatedSummaryPlugin/Source/AccumulatedSummaryView.cs


UCC Code Repository

Contents of /branches/gps-running-clean-r116/AccumulatedSummaryPlugin/Source/AccumulatedSummaryView.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations) (download)
Sun Sep 5 15:48:34 2010 UTC (11 years, 3 months ago) by rvvs89
File size: 14395 byte(s)
Branching a local copy of the HEAD revision of the gps-running project on Google Code.
We will probably have to use this project as a template for our own plugins if we want to support SportTracks 2.1.
1 /*
2 Copyright (C) 2007, 2008 Kristian Bisgaard Lassen
3 Copyright (C) 2010 Kristian Helkjaer Lassen
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22 using System.Windows.Forms;
23 using ZoneFiveSoftware.Common.Data.Fitness;
24 using ZoneFiveSoftware.Common.Data.Measurement;
25
26 using GpsRunningPlugin;
27 using GpsRunningPlugin.Source;
28 using ZoneFiveSoftware.Common.Visuals;
29 using GpsRunningPlugin.Properties;
30 using GpsRunningPlugin.Util;
31
32 namespace GpsRunningPlugin.Source
33 {
34 class AccumulatedSummaryView : Form
35 {
36 private WebBrowser browser;
37 private IList<IActivity> activities;
38 private ITheme m_visualTheme;
39
40 public AccumulatedSummaryView(IList<IActivity> activities)
41 {
42 m_visualTheme =
43 #if ST_2_1
44 Plugin.GetApplication().VisualTheme;
45 #else
46 Plugin.GetApplication().SystemPreferences.VisualTheme;
47 #endif
48
49 InitializeComponent();
50 this.activities = activities;
51 StringBuilder builder = new StringBuilder(10000);
52 makeReport(builder);
53 browser.DocumentText = builder.ToString();
54 if (activities.Count == 1)
55 Text = Resources.AS1;
56 else
57 Text = String.Format(Resources.AS2,activities.Count);
58 Icon = System.Drawing.Icon.FromHandle(Properties.Resources.Image_32_AccumulatedSummary.GetHicon());
59 ShowDialog();
60 }
61 string getRGB(System.Drawing.Color col)
62 {
63 return String.Format("#{0,2:X}{1,2:X}{2,2:X}",col.R, col.G, col.B);
64 }
65 private void makeReport(StringBuilder builder)
66 {
67 String distMetric = UnitUtil.Distance.LabelAbbr;//m_visualTheme.
68 builder.Append("<html><body style=\"background=" + getRGB(m_visualTheme.Control)
69 + "\"><table border=\"1\" width=\"100%\">");
70 double totalDistance = getTotalDistance();
71 addEntry(builder, UnitUtil.Distance.LabelAxis, UnitUtil.Distance.ToString(totalDistance));
72 TimeSpan totalTime = getTotalTime();
73 showTotalTime(builder, totalTime);
74 double averageSpeed = totalDistance / totalTime.TotalSeconds;
75 double fastestSpeed = getFastestSpeed();
76 addEntry(builder, UnitUtil.Pace.LabelAxis, UnitUtil.Pace.ToString(averageSpeed));
77 addEntry(builder, CommonResources.Text.LabelFastestPace + UnitUtil.Pace.LabelAbbr2,
78 UnitUtil.Pace.ToString(fastestSpeed));
79 addEntry(builder, UnitUtil.Speed.LabelAxis,
80 UnitUtil.Speed.ToString(averageSpeed));
81 addEntry(builder, CommonResources.Text.LabelFastestSpeed + UnitUtil.Speed.LabelAbbr2,
82 UnitUtil.Speed.ToString(fastestSpeed));
83 addEntry(builder, StringResources.ClimbSummary + UnitUtil.Elevation.LabelAbbr2, getTotalUpsAndDowns());
84 addEntry(builder, UnitUtil.Energy.LabelAxis, getCalories());
85 addEntry(builder, CommonResources.Text.LabelAvgHR + UnitUtil.HeartRate.LabelAbbr2, getAverageHeartRate(totalTime));
86 getSpeedZones(builder,totalTime);
87 addEntry(builder, StringResources.ClimbZones, getClimbZones(totalTime));
88 String ratio = getWorkoutRatio();
89 TimeSpan realTotalTimeForWorkout = getRealTotalTimeForWorkout(totalTime);
90 string fmt = StringResources.HRZone + UnitUtil.HeartRate.LabelAbbr2 + " (" + "{0}" + ")";
91 addEntry(builder, String.Format(fmt, ratio),
92 getWorkout(realTotalTimeForWorkout));
93 builder.Append("</table></body></html>");
94 }
95
96 private TimeSpan getRealTotalTimeForWorkout(TimeSpan totalTime)
97 {
98 TimeSpan result = totalTime;
99 foreach (IActivity activity in activities)
100 {
101 if (activity.HeartRatePerMinuteTrack == null)
102 {
103 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
104 if (info.Time.TotalSeconds == 0)
105 result = result.Subtract(activity.TotalTimeEntered);
106 else
107 result = result.Subtract(info.Time);
108 }
109 }
110 return result;
111 }
112
113 private string getWorkoutRatio()
114 {
115 int good = 0;
116 foreach (IActivity activity in activities)
117 {
118 if (activity.HeartRatePerMinuteTrack != null)
119 good++;
120 }
121 return String.Format(StringResources.NoutofM, good, activities.Count);
122 }
123
124 private string getClimbZones(TimeSpan totalTime)
125 {
126 return printCategories(Plugin.GetApplication().DisplayOptions.SelectedClimbZone,
127 totalTime, false, true, false);
128 }
129
130 private void getSpeedZones(StringBuilder builder, TimeSpan totalTime)
131 {
132 foreach (IZoneCategory speedZone in Plugin.GetApplication().Logbook.SpeedZones)
133 {
134 addEntry(builder, StringResources.SpeedZone + ": " + speedZone.Name,
135 printCategories(speedZone, totalTime, true, false, false));
136 }
137 }
138
139 private string getAverageHeartRate(TimeSpan totalTime)
140 {
141 double result = 0;
142 foreach (IActivity activity in activities)
143 {
144 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
145 if (activity.HeartRatePerMinuteTrack != null)
146 {
147 result += info.AverageHeartRate
148 * (info.Time.TotalSeconds / totalTime.TotalSeconds);
149 }
150 else if (activity.AverageHeartRatePerMinuteEntered > 0)
151 {
152 double time = info.Time.TotalSeconds;
153 if (time == 0)
154 time = activity.TotalTimeEntered.TotalSeconds;
155 result += activity.AverageHeartRatePerMinuteEntered
156 * (info.Time.TotalSeconds / totalTime.TotalSeconds);
157 }
158 }
159 return UnitUtil.HeartRate.ToString(result);
160 }
161
162 private string printCategories(IZoneCategory zones, TimeSpan totalTime,
163 bool speed, bool climb, bool heart)
164 {
165 bool evenStart = even;
166 IDictionary<String, TimeSpan> dict = new Dictionary<String, TimeSpan>();
167 foreach (IActivity activity in activities)
168 {
169 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
170 ZoneCategoryInfo zinfos = null;
171 bool cont = true;
172 if (speed) zinfos = info.SpeedZoneInfo(zones);
173 else if (climb) zinfos = info.ClimbZoneInfo(zones);
174 else if (heart)
175 {
176 if (activity.HeartRatePerMinuteTrack == null)
177 cont = false;
178 else
179 zinfos = info.HeartRateZoneInfo(zones);
180 }
181 if (cont)
182 {
183 foreach (ZoneInfo zinfo in zinfos.Zones)
184 {
185 TimeSpan v = zinfo.TotalTime;
186 if (!dict.ContainsKey(zinfo.Name))
187 {
188 dict.Add(zinfo.Name, v);
189 }
190 else
191 {
192 dict[zinfo.Name] = dict[zinfo.Name].Add(v);
193 }
194 }
195 }
196 }
197 StringBuilder builder = new StringBuilder();
198 builder.Append("<table width=\"100%\">");
199 foreach (String name in dict.Keys)
200 {
201 String timeType = StringResources.SecondsFormat;
202 String time = String.Format("{0:00}", dict[name].Seconds);
203 if (totalTime.TotalMinutes > 1)
204 {
205 timeType = StringResources.MinutesFormat + ":" + timeType;
206 time = String.Format("{0:00}", dict[name].Minutes) + ":" + time;
207 }
208 if (totalTime.TotalHours > 1)
209 {
210 timeType = StringResources.HoursFormat + ":" + timeType;
211 time = String.Format("{0:00}", dict[name].Hours) + ":" + time;
212 }
213 if (totalTime.TotalDays > 1)
214 {
215 timeType = StringResources.DaysFormat + ":" + timeType;
216 time = String.Format("{0:00}", dict[name].Days) + ":" + time;
217 }
218 addEntry(builder, name + " (" + timeType + ")", time +
219 " (" + (dict[name].TotalSeconds / totalTime.TotalSeconds).ToString("P2") + ")");
220 }
221 builder.Append("</table>");
222 even = evenStart;
223 return builder.ToString();
224 }
225
226 private string getWorkout(TimeSpan totalTime)
227 {
228 return printCategories(Plugin.GetApplication().Logbook.HeartRateZones[0],
229 totalTime,false,false,true);
230 }
231
232 private string getCalories()
233 {
234 double result = 0;
235 foreach (IActivity activity in activities)
236 {
237 result += activity.TotalCalories;
238 }
239 return ((int)Math.Round(result)).ToString();
240 }
241
242 private double getFastestSpeed()
243 {
244 double result = double.MinValue;
245 foreach (IActivity activity in activities)
246 {
247 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
248 double tmp = info.FastestSpeedMetersPerSecond;
249 if (tmp > result)
250 result = tmp;
251 }
252 return result;
253 }
254
255 private string getTotalUpsAndDowns()
256 {
257 double ups = 0, downs = 0;
258 foreach (IActivity activity in activities)
259 {
260 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
261 IZoneCategory cat = Plugin.GetApplication().DisplayOptions.SelectedClimbZone;
262 ups += info.TotalAscendingMeters(cat);
263 downs += info.TotalDescendingMeters(cat);
264 }
265 return "+" + UnitUtil.Elevation.ToString(ups) + "/" + UnitUtil.Elevation.ToString(downs);
266 }
267
268 private void showTotalTime(StringBuilder builder, TimeSpan totalTime)
269 {
270 String timeType = StringResources.SecondsFormat;
271 String time = String.Format("{0:00}", totalTime.Seconds);
272 if (totalTime.TotalMinutes > 1)
273 {
274 timeType = StringResources.MinutesFormat + ":" + timeType;
275 time = String.Format("{0:00}", totalTime.Minutes) + ":" + time;
276 }
277 if (totalTime.TotalHours > 1)
278 {
279 timeType = StringResources.HoursFormat + ":" + timeType;
280 time = String.Format("{0:00}", totalTime.Hours) + ":" + time;
281 }
282 if (totalTime.TotalDays > 1)
283 {
284 timeType = StringResources.DaysFormat + ":" + timeType;
285 time = String.Format("{0:00}", totalTime.Days) + ":" + time;
286 }
287 addEntry(builder, CommonResources.Text.LabelTime+ " (" + timeType + ")", time);
288 }
289
290 private TimeSpan getTotalTime()
291 {
292 TimeSpan result = new TimeSpan();
293 foreach (IActivity activity in activities)
294 {
295 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
296 result = result.Add(info.Time);
297 }
298 return result;
299 }
300
301 private bool even = false;
302
303 private void addEntry(StringBuilder builder, string name, string value)
304 {
305 builder.Append("<tr style=\"background:" + (even ? getRGB(m_visualTheme.Window) : getRGB(m_visualTheme.Border)) +
306 "\"><td>" + name + "</td><td align=\"right\">"
307 + value + "</td></tr>");
308 even = !even;
309 }
310
311 private double getTotalDistance()
312 {
313 double result = 0;
314 foreach (IActivity activity in activities)
315 {
316 ActivityInfo info = ActivityInfoCache.Instance.GetInfo(activity);
317 result += info.DistanceMeters;
318 }
319 return result;
320 }
321
322 private void InitializeComponent()
323 {
324 this.browser = new System.Windows.Forms.WebBrowser();
325 this.SuspendLayout();
326 //
327 // browser
328 //
329 this.browser.Dock = System.Windows.Forms.DockStyle.Fill;
330 this.browser.Location = new System.Drawing.Point(0, 0);
331 this.browser.MinimumSize = new System.Drawing.Size(20, 20);
332 this.browser.Name = "browser";
333 this.browser.Size = new System.Drawing.Size(440, 349);
334 this.browser.TabIndex = 0;
335 //
336 // AccumulatedSummaryView
337 //
338 this.ClientSize = new System.Drawing.Size(440, 349);
339 this.Controls.Add(this.browser);
340 this.Name = "AccumulatedSummaryView";
341 this.ResumeLayout(false);
342
343 }
344 }
345 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26