comparison Chronosv2/source/Presentation/Widgets/Clock/ClockWidgetViewModel.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2 using System.Windows;
3 using System.Windows.Media;
4 using System.Windows.Threading;
5 using Chronos.Presentation.ViewModel;
6
7 namespace Chronos.Presentation.Widgets
8 {
9 /// <summary>
10 /// Based on http://moonydesk.codeplex.com/
11 /// </summary>
12 public sealed class ClockWidgetViewModel
13 : WidgetViewModel
14 {
15 #region · Fields ·
16
17 private Point degreeStartPoint;
18 private Point degreeCurrentPoint;
19 private bool isLargeArc;
20 private int dayOfYear;
21 private int year;
22 private string seconds;
23 private string minutes;
24 private string hours;
25 private string pmAm;
26 private bool hours24;
27 private string date;
28 private string dayOfWeek;
29 private int angle;
30
31 #endregion
32
33 #region · Properties ·
34
35 public Point DegreeStartPoint
36 {
37 get { return this.degreeStartPoint; }
38 set
39 {
40 if (this.degreeStartPoint != value)
41 {
42 this.degreeStartPoint = value;
43
44 this.NotifyPropertyChanged(() => DegreeStartPoint);
45 }
46 }
47 }
48
49 public Point DegreeCurrentPoint
50 {
51 get { return this.degreeCurrentPoint; }
52 set
53 {
54 if (this.degreeCurrentPoint != value)
55 {
56 this.degreeCurrentPoint = value;
57
58 this.NotifyPropertyChanged(() => DegreeCurrentPoint);
59 }
60 }
61 }
62
63 public bool IsLargeArc
64 {
65 get { return this.isLargeArc; }
66 set
67 {
68 if (this.isLargeArc != value)
69 {
70 this.isLargeArc = value;
71
72 this.NotifyPropertyChanged(() => IsLargeArc);
73 }
74 }
75 }
76
77 public string Seconds
78 {
79 get { return this.seconds; }
80 set
81 {
82 if (this.seconds != value)
83 {
84 this.seconds = value;
85
86 this.NotifyPropertyChanged(() => Seconds);
87 }
88 }
89 }
90
91 public string Minutes
92 {
93 get { return this.minutes; }
94 set
95 {
96 if (this.minutes != value)
97 {
98 this.minutes = value;
99
100 this.NotifyPropertyChanged(() => Minutes);
101 }
102 }
103 }
104
105 public string Hours
106 {
107 get { return this.hours; }
108 set
109 {
110 if (this.hours != value)
111 {
112 this.hours = value;
113
114 this.NotifyPropertyChanged(() => Hours);
115 }
116 }
117 }
118
119 public string PmAm
120 {
121 get { return this.pmAm; }
122 set
123 {
124 if (this.pmAm != value)
125 {
126 this.pmAm = value;
127
128 this.NotifyPropertyChanged(() => PmAm);
129 }
130 }
131 }
132
133 public bool Hours24
134 {
135 get { return this.hours24; }
136 set
137 {
138 if (this.hours24 != value)
139 {
140 this.hours24 = value;
141
142 this.NotifyPropertyChanged(() => Hours24);
143 }
144 }
145 }
146
147 public string Date
148 {
149 get { return this.date; }
150 set
151 {
152 if (this.date != value)
153 {
154 this.date = value;
155
156 this.NotifyPropertyChanged(() => Date);
157 }
158 }
159 }
160
161 public string DayOfWeek
162 {
163 get { return this.dayOfWeek; }
164 set
165 {
166 if (this.dayOfWeek != value)
167 {
168 this.dayOfWeek = value;
169
170 this.NotifyPropertyChanged(() => DayOfWeek);
171 }
172 }
173 }
174
175 public int Angle
176 {
177 get { return this.angle; }
178 set
179 {
180 if (this.angle != value)
181 {
182 this.angle = value;
183
184 this.NotifyPropertyChanged(() => Angle);
185 }
186 }
187 }
188
189 public int DayOfYear
190 {
191 get { return this.dayOfYear; }
192 set
193 {
194 if (this.dayOfYear != value)
195 {
196 this.dayOfYear = value;
197
198 this.NotifyPropertyChanged(() => DayOfYear);
199 }
200 }
201 }
202
203 public int Year
204 {
205 get { return this.year; }
206 set
207 {
208 if (this.year != value)
209 {
210 this.year = value;
211
212 this.NotifyPropertyChanged(() => DayOfYear);
213 }
214 }
215 }
216
217 #endregion
218
219 #region · Constructors ·
220
221 public ClockWidgetViewModel()
222 : base()
223 {
224 this.DayOfYear = -1;
225 this.Year = -1;
226 this.Angle = -4;
227 this.Hours24 = Properties.Settings.Default.hours24;
228 this.DegreeStartPoint = new Point(110, 10);
229 this.DegreeCurrentPoint = new Point(110, 210);
230 }
231
232 #endregion
233
234 #region · Methods ·
235
236 public void Start()
237 {
238 DispatcherTimer timer = new DispatcherTimer();
239
240 timer.Interval = TimeSpan.FromSeconds(0.1);
241 timer.Tick += new EventHandler(OnTimerTick);
242 timer.Start();
243
244 this.SetCurTime();
245 }
246
247 #endregion
248
249 #region · Private Methods ·
250
251 /// <summary>
252 /// Set current time
253 /// </summary>
254 private void SetCurTime()
255 {
256 DateTime now = DateTime.Now;
257
258 this.SetDeg((now.Second + now.Millisecond / 1000.0) * 6);
259 this.Seconds = now.Second.ToString("00");
260 this.Minutes = now.Minute.ToString("00");
261
262 if (this.Hours24)
263 {
264 this.Hours = now.Hour.ToString("00");
265 this.PmAm = String.Empty;
266 }
267 else
268 {
269 this.Hours = now.ToString("hh");
270 this.PmAm = now.ToString("tt");
271 }
272
273 if (now.DayOfYear != this.DayOfYear || now.Year != this.Year)
274 {
275 this.Date = now.ToString("d MMMM yyyy");
276 this.DayOfWeek = now.ToString("dddd");
277 this.DayOfYear = now.DayOfYear;
278 this.Year = now.Year;
279 }
280 }
281
282 /// <summary>
283 /// Set seconds arc degree
284 /// </summary>
285 /// <param name="degree"></param>
286 private void SetDeg(double degree)
287 {
288 double offset = this.DegreeStartPoint.X;
289 double x = Math.Cos((degree - 90) * Math.PI / 180) * 100.0 + offset;
290 double y = Math.Sin((degree - 90) * Math.PI / 180) * 100.0 + offset;
291
292 this.DegreeCurrentPoint = new Point(x, y);
293 this.IsLargeArc = (degree > 180);
294 }
295
296 #endregion
297
298 #region · Event Handlers ·
299
300 private void OnTimerTick(object sender, EventArgs e)
301 {
302 this.SetCurTime();
303 }
304
305 #endregion
306 }
307 }