]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/analogclock.py
Fixed OOR typo
[wxWidgets.git] / wxPython / wx / lib / analogclock.py
1 # -*- coding: iso-8859-1 -*-
2 #----------------------------------------------------------------------
3 # Name: wx.lib.analogclock
4 # Purpose: A simple analog clock window
5 #
6 # Author: several folks on wxPython-users
7 #
8 # Created: 16-April-2003
9 # RCS-ID: $Id$
10 # Copyright: (c) 2003 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
13 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 #
15 # o Updated for wx namespace
16 # o Tested with updated demo and with builtin test.
17 #
18 # 15-February-2004 - E. A. Tacao
19 #
20 # o Many ehnacements
21 #
22
23
24 import math
25 import sys
26 import string
27 import time
28
29 import wx
30
31 from analogclockopts import ACCustomizationFrame
32
33
34 # self.clockStyle:
35 SHOW_QUARTERS_TICKS = 1
36 SHOW_HOURS_TICKS = 2
37 SHOW_MINUTES_TICKS = 4
38 ROTATE_TICKS = 8
39 SHOW_HOURS_HAND = 16
40 SHOW_MINUTES_HAND = 32
41 SHOW_SECONDS_HAND = 64
42 SHOW_SHADOWS = 128
43 OVERLAP_TICKS = 256
44
45 # self.tickMarkHoursStyle and self.tickMarkMinutesStyle:
46 TICKS_NONE = 1
47 TICKS_SQUARE = 2
48 TICKS_CIRCLE = 4
49 TICKS_POLY = 8
50 TICKS_DECIMAL = 16
51 TICKS_ROMAN = 32
52
53
54 class AnalogClockWindow(wx.Window):
55 """An analog clock window"""
56
57 def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, size=wx.DefaultSize,
58 style=0, name="clock"):
59
60 # Initialize the wxWindow...
61 wx.Window.__init__(self, parent, ID, pos, size, style, name)
62
63 # Initialize some variables and defaults...
64 self.clockStep = 1
65 self.prefs_open = False
66
67 self.tickShapeHours = self.tickShapeMinutes= [[0,0],
68 [1,-1],
69 [2,0],
70 [1,4]]
71 self.handHoursThickness = 5
72 self.handHoursColour = (0, 0, 0)
73
74 self.handMinutesThickness = 3
75 self.handMinutesColour = (0, 0, 0)
76
77 self.handSecondsThickness = 1
78 self.handSecondsColour = (0, 0, 0)
79
80 self.tickMarkHoursPen = wx.Pen((0, 0, 0), 1, wx.SOLID)
81 self.tickMarkHoursBrush = wx.Brush((0, 0, 0), wx.SOLID)
82 self.markSizeHour = 10
83 self.tickMarkHoursFont = wx.Font(0, wx.SWISS, wx.NORMAL, wx.BOLD)
84 self.tickMarkHoursFont.SetPointSize(self.markSizeHour)
85
86 self.tickMarkMinutesPen = wx.Pen((0, 0, 0), 1, wx.SOLID)
87 self.tickMarkMinutesBrush = wx.Brush((0, 0, 0), wx.SOLID)
88 self.markSizeMin = 6
89 self.tickMarkMinutesFont = wx.Font(self.markSizeMin, wx.SWISS, wx.NORMAL, wx.BOLD)
90
91 self.offM = 0
92
93 self.shadowPenColour = self.shadowBrushColour = (128,128,128)
94
95 self.watchPen = None
96 self.watchBrush = None
97
98 self.clockStyle = SHOW_HOURS_TICKS | SHOW_MINUTES_TICKS | SHOW_SHADOWS | ROTATE_TICKS
99 self.handsStyle = SHOW_SECONDS_HAND
100
101 self.tickMarkHoursStyle = TICKS_POLY
102 self.tickMarkMinutesStyle = TICKS_CIRCLE
103
104 self.currentTime=None
105
106 # Make an initial bitmap for the face, it will be updated and
107 # painted at the first EVT_SIZE event.
108 W, H = size
109 self.faceBitmap = wx.EmptyBitmap(max(W,1), max(H,1))
110
111 # Set event handlers...
112 self.Bind(wx.EVT_PAINT, self.OnPaint)
113 self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: None)
114 self.Bind(wx.EVT_SIZE, self.OnSize)
115 self.Bind(wx.EVT_TIMER, self.OnTimerExpire)
116 self.Bind(wx.EVT_WINDOW_DESTROY, self.OnQuit)
117 self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
118 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
119
120
121 # Initialize the timer that drives the update of the clock
122 # face. Update every half second to ensure that there is at
123 # least one true update during each realtime second.
124 self.timer = wx.Timer(self)
125 self.timer.Start(500)
126
127
128 def OnPaint(self, event):
129 self._doDrawHands(wx.BufferedPaintDC(self), True)
130
131
132 def OnTimerExpire(self, event):
133 size = self.GetClientSize()
134 dc = wx.BufferedDC(wx.ClientDC(self), size)
135 self._doDrawHands(dc, True)
136
137
138 def OnQuit(self, event):
139 self.timer.Stop()
140 del self.timer
141
142
143 def OnRightDown(self, event):
144 self.x = event.GetX()
145 self.y = event.GetY()
146 event.Skip()
147
148
149 def OnRightClick(self, event):
150 # only do this part the first time so the events are only bound once
151 if not hasattr(self, "popupID1"):
152 self.popupID1 = wx.NewId()
153 self.popupID2 = wx.NewId()
154 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
155 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
156
157 # make a menu
158 sm = wx.Menu()
159
160 sm.Append(self.popupID1, "Customize...")
161 sm.Append(self.popupID2, "About...")
162
163 # If there already a setup window open, we must not appear...
164 if not self.prefs_open:
165 # Popup the menu. If an item is selected then its handler
166 # will be called before PopupMenu returns.
167 self.PopupMenu(sm, (self.x,self.y))
168 sm.Destroy()
169
170
171 def OnPopupOne(self, event):
172 self.prefs_open=True
173 frame = ACCustomizationFrame(self, -1, "AnalogClock Preferences")
174 frame.Show(True)
175
176
177 def OnPopupTwo(self, event):
178 dlg = wx.MessageDialog(self, "AnalogClockWindow\n\nby Several folks on wxPython-users\nwith enhancements from E. A. Tacão",
179 'About', wx.OK | wx.ICON_INFORMATION)
180 dlg.ShowModal()
181 dlg.Destroy()
182
183
184 def OnSize(self, event):
185 # The faceBitmap init is done here, to make sure the buffer is always
186 # the same size as the Window
187 size = self.GetClientSize()
188 self.faceBitmap = wx.EmptyBitmap(size.width, size.height)
189
190 # Update drawing coordinates...
191 new_dim = size.Get()
192 if not hasattr(self,"dim"):
193 self.dim = new_dim
194
195 x,y=[0,1]
196 self.scale = min([float(new_dim[x]) / self.dim[x],
197 float(new_dim[y]) / self.dim[y]])
198
199 self.centerX = self.faceBitmap.GetWidth() / 2
200 self.centerY = self.faceBitmap.GetHeight() / 2
201
202 self.shadowDistance = 2 * self.scale
203
204 self.radius_watch = min(self.centerX, self.centerY)
205
206 self._doDrawFace()
207
208
209
210 def _doDrawHands(self, drawDC, force=0):
211 currentTime = list(time.localtime(time.time())[3:6])
212
213 if not (self.handsStyle & SHOW_SECONDS_HAND):
214 currentTime[2]=-1
215
216 if not (force or currentTime != self.currentTime):
217 return
218 self.currentTime = currentTime
219 hour, minutes, seconds = currentTime
220
221 # Start by drawing the face bitmap
222 drawDC.DrawBitmap(self.faceBitmap, (0,0))
223
224
225 # NOTE: All this hand drawing code below should be refactored into a helper function.
226
227
228 # Draw hours hand shadow
229 mStep = 6 * self.clockStep
230 angle = hour * 30
231 if angle > 360:
232 angle = angle - 360
233 angle = angle + round(minutes/(mStep*2)) * mStep
234
235 x,y,f = self._getCoords("hand_hours", angle)
236
237 if f and self.clockStyle & SHOW_SHADOWS:
238 drawDC.SetPen(wx.Pen(self.shadowPenColour,
239 int(self.handHoursThickness * self.scale),
240 wx.SOLID))
241 drawDC.DrawLineXY(int(self.centerX + self.shadowDistance),
242 int(self.centerY + self.shadowDistance),
243 int(x + self.shadowDistance),
244 int(y + self.shadowDistance))
245
246 # Draw minutes hand shadow
247 angle = minutes * 6
248 x,y,f = self._getCoords("hand_minutes", angle)
249
250 if f and self.clockStyle & SHOW_SHADOWS:
251 drawDC.SetPen(wx.Pen(self.shadowPenColour,
252 int(self.handMinutesThickness * self.scale),
253 wx.SOLID))
254 drawDC.DrawLineXY(int(self.centerX + self.shadowDistance),
255 int(self.centerY + self.shadowDistance),
256 int(x + self.shadowDistance),
257 int(y + self.shadowDistance))
258
259 # Draw seconds hand shadow if required
260 if seconds >= 0:
261 angle = seconds * 6
262 x,y,f = self._getCoords("hand_seconds", angle)
263
264 if f and self.clockStyle & SHOW_SHADOWS:
265 drawDC.SetPen(wx.Pen(self.shadowPenColour,
266 int(self.handSecondsThickness * self.scale),
267 wx.SOLID))
268 drawDC.DrawLineXY(int(self.centerX + self.shadowDistance),
269 int(self.centerY + self.shadowDistance),
270 int(x + self.shadowDistance),
271 int(y + self.shadowDistance))
272
273
274 # Draw hours hand
275 angle = hour * 30
276 if angle > 360:
277 angle = angle - 360
278 angle = angle + round(minutes/(mStep*2)) * mStep
279
280 x,y,f = self._getCoords("hand_hours", angle)
281
282 if f:
283 drawDC.SetPen(wx.Pen(self.handHoursColour,
284 int(self.handHoursThickness * self.scale),
285 wx.SOLID))
286 drawDC.DrawLineXY(int(self.centerX), int(self.centerY), int(x), int(y))
287
288 # Draw minutes hand
289 angle = minutes * 6
290 x,y,f = self._getCoords("hand_minutes", angle)
291
292 if f:
293 drawDC.SetPen(wx.Pen(self.handMinutesColour,
294 int(self.handMinutesThickness * self.scale),
295 wx.SOLID))
296 drawDC.DrawLineXY(int(self.centerX), int(self.centerY), int(x), int(y))
297
298 # Draw seconds hand if required
299 if seconds >= 0:
300 angle = seconds * 6
301 x,y,f = self._getCoords("hand_seconds", angle)
302 if f:
303 drawDC.SetPen(wx.Pen(self.handSecondsColour,
304 int(self.handSecondsThickness * self.scale),
305 wx.SOLID))
306 drawDC.DrawLineXY(int(self.centerX), int(self.centerY), int(x), int(y))
307
308
309
310 def _doDrawFace(self):
311 backgroundBrush = wx.Brush(self.GetBackgroundColour(), wx.SOLID)
312 drawDC = wx.MemoryDC()
313 drawDC.SelectObject(self.faceBitmap)
314 drawDC.SetBackground(backgroundBrush)
315 drawDC.Clear()
316
317 self.handHoursLength = 0.65 * (self.radius_watch - self._getMarkMaxSize("ticks_hours", drawDC))
318 self.handMinutesLength = 0.85 * (self.radius_watch - self._getMarkMaxSize("ticks_hours", drawDC))
319 self.handSecondsLength = 0.85 * (self.radius_watch - self._getMarkMaxSize("ticks_hours", drawDC))
320
321 self.radius_ticks_hours = self.radius_watch - self.shadowDistance - self._getMarkMaxSize("ticks_hours", drawDC)
322 self.radius_ticks_minutes = self.radius_ticks_hours
323
324 self._calcSteps()
325
326 # Draw the watch...
327 self._drawWatch(drawDC)
328
329 # Draw the marks for hours and minutes...
330 circle = 360
331 mStep = 6 * self.clockStep
332
333 if self.clockStyle & SHOW_SHADOWS:
334 for i in range(0, circle, mStep):
335 for t in self.coords.keys():
336 if t.find("ticks") > -1:
337 x,y,f = self._getCoords(t, i)
338 if f:
339 self._doDrawTickMark(i, drawDC, t,
340 x + self.shadowDistance,
341 y + self.shadowDistance,
342 True)
343
344 for i in range(0, circle, mStep):
345 for t in self.coords.keys():
346 if t.find("ticks") > -1:
347 x,y,f = self._getCoords(t, i)
348 if f:
349 self._doDrawTickMark(i, drawDC, t, x, y)
350
351
352
353 def _doDrawTickMark(self, angle, drawDC, tipo, x, y, is_a_shadow=None):
354 opts = {"ticks_hours": [self.tickMarkHoursPen, self.tickMarkHoursBrush, self.markSizeHour, self.tickMarkHoursStyle],
355 "ticks_quarters": [self.tickMarkHoursPen, self.tickMarkHoursBrush, self.markSizeHour, self.tickMarkHoursStyle],
356 "ticks_minutes": [self.tickMarkMinutesPen, self.tickMarkMinutesBrush, self.markSizeMin, self.tickMarkMinutesStyle]}
357
358 pen, brush, size, style = opts[tipo];
359 size = size * self.scale
360
361 if is_a_shadow:
362 drawDC.SetPen(wx.Pen(self.shadowPenColour, 1, wx.SOLID))
363 drawDC.SetBrush(wx.Brush(self.shadowBrushColour, wx.SOLID))
364 drawDC.SetTextForeground(self.shadowBrushColour)
365 else:
366 drawDC.SetPen(pen)
367 drawDC.SetBrush(brush)
368 drawDC.SetTextForeground(brush.GetColour())
369
370 if style & TICKS_CIRCLE:
371 x, y = self._center2corner(x, y, tipo)
372 drawDC.DrawEllipse((x, y), (int(size), int(size)))
373
374 elif style & TICKS_SQUARE:
375 x, y = self._center2corner(x, y, tipo)
376 drawDC.DrawRectangle((x, y), (int(size), int(size)))
377
378 elif (style & TICKS_DECIMAL) or (style & TICKS_ROMAN):
379 self._draw_rotate_text(drawDC, x, y, tipo, angle)
380
381 elif style & TICKS_POLY:
382 self._draw_rotate_polygon(drawDC, x, y, tipo, angle)
383
384
385 def _draw_rotate_text(self, drawDC, x, y, tipo, angle):
386 text = self._build_text(angle, tipo)
387 lX, lY = self._center2corner(x, y, tipo, drawDC)
388 lX = lX * len(text)
389 angle = 360 - angle
390
391 if self.clockStyle & ROTATE_TICKS:
392 radiansPerDegree = math.pi / 180
393 x = int(x -
394 ((math.cos((angle) * radiansPerDegree)*lX) +
395 (math.sin((angle) * radiansPerDegree)*lY)))
396 y = int(y -
397 ((math.cos((angle) * radiansPerDegree)*lY) -
398 (math.sin((angle) * radiansPerDegree)*lX)))
399 drawDC.DrawRotatedText(text, (x,y), angle)
400
401 else:
402 x = x - lX
403 y = y - lY
404 drawDC.DrawText(text, (x, y))
405
406
407 def _draw_rotate_polygon(self, drawDC, x, y, tipo, angle):
408 if tipo=="ticks_quarters":
409 tipo="ticks_hours"
410
411 # Add to empty list to prevent system-wide hard freezes under XP...
412 points = {"ticks_hours":self.tickShapeHours+[], "ticks_minutes":self.tickShapeMinutes+[]}[tipo]
413 size = self.scale * {"ticks_hours":self.markSizeHour, "ticks_minutes":self.markSizeMin}[tipo]
414
415 maxX = max(map(lambda x:x[0],points))
416 minX = min(map(lambda x:x[0],points))
417 maxY = max(map(lambda x:x[0],points))
418 minY = min(map(lambda x:x[0],points))
419
420 maxB = abs(max(maxX, maxY));
421 f = size / maxB
422
423 orgX = (maxX - minX) / 2.
424 orgY = (maxY - minY) / 2.
425
426 radiansPerDegree = math.pi / 180
427 scaledX = x
428 scaledY = y
429
430 for z in range(0, len(points)):
431 x,y = points[z]
432 x = x * f - orgX * f
433 y = y * f - orgY * f
434 if self.clockStyle & ROTATE_TICKS:
435 m,t = self._rect2pol(x,y)
436 t = t + angle
437 x,y = self._pol2rect(m,t)
438 x = x + scaledX
439 y = y + scaledY
440 points[z] = [int(x), int(y)]
441
442 drawDC.DrawPolygon(points)
443
444
445 def _pol2rect(self, r, w, deg=1): # radian if deg=0; degree if deg=1
446 if deg:
447 w = math.pi * w / 180.0
448 return r * math.cos(w), r * math.sin(w)
449
450
451 def _rect2pol(self, x, y, deg=1): # radian if deg=0; degree if deg=1
452 if deg:
453 return math.hypot(x, y), 180.0 * math.atan2(y, x) / math.pi
454 else:
455 return math.hypot(x, y), math.atan2(y, x)
456
457
458 def _center2corner(self, x, y, tipo, drawDC=None):
459 if tipo == "ticks_quarters":
460 tipo = "ticks_hours"
461
462 style = {"ticks_hours":self.tickMarkHoursStyle, "ticks_minutes":self.tickMarkMinutesStyle}[tipo]
463 size = self.scale * {"ticks_hours":self.markSizeHour, "ticks_minutes":self.markSizeMin}[tipo]
464
465 if style & TICKS_DECIMAL or style & TICKS_ROMAN:
466 font = {"ticks_hours":self.tickMarkHoursFont, "ticks_minutes":self.tickMarkMinutesFont}[tipo]
467 font.SetPointSize(int(size));
468 drawDC.SetFont(font)
469 lX = drawDC.GetCharWidth() / 2.
470 lY = drawDC.GetCharHeight() / 2.
471 x = lX
472 y = lY
473 else:
474 size = self.scale * {"ticks_hours":self.markSizeHour, "ticks_minutes":self.markSizeMin}[tipo]
475 x=x-size/2.;y=y-size/2.
476 return x, y
477
478
479 def _build_text(self, angle, tipo):
480 if tipo == "ticks_quarters":
481 tipo = "ticks_hours"
482 a = angle
483 if a <= 0:
484 a = a + 360
485 divider = {"ticks_hours":30,"ticks_minutes":6}[tipo]
486 a = int(a / divider)
487
488 style = {"ticks_hours":self.tickMarkHoursStyle," ticks_minutes":self.tickMarkMinutesStyle}[tipo]
489 if style & TICKS_ROMAN:
490 text=["I","II","III","IV","V","VI","VII","VIII","IX","X", \
491 "XI","XII","XIII","XIV","XV","XVI","XVII","XVIII","XIX","XX", \
492 "XXI","XXII","XXIII","XXIV","XXV","XXVI","XXVII","XXVIII","XXIX","XXX", \
493 "XXXI","XXXII","XXXIII","XXXIV","XXXV","XXXVI","XXXVII","XXXVIII","XXXIX","XL", \
494 "XLI","XLII","XLIII","XLIV","XLV","XLVI","XLVII","XLVIII","XLIX","L", \
495 "LI","LII","LIII","LIV","LV","LVI","LVII","LVIII","LIX","LX"][a-1]
496 else:
497 text = "%s" % a
498
499 return text
500
501
502 def _getMarkMaxSize(self, tipo, drawDC=None):
503 if tipo == "ticks_quarters":
504 tipo = "ticks_hours"
505
506 style = {"ticks_hours":self.tickMarkHoursStyle, "ticks_minutes":self.tickMarkMinutesStyle}[tipo]
507 size = self.scale * {"ticks_hours":self.markSizeHour, "ticks_minutes":self.markSizeMin}[tipo]
508
509 if style & TICKS_DECIMAL or style & TICKS_ROMAN:
510 lX = 2 * drawDC.GetCharWidth()
511 lY = drawDC.GetCharHeight()
512 size = math.sqrt(lX**2 + lY**2) * self.scale
513 else:
514 size=math.sqrt(2) * size
515
516 return size
517
518
519 def _drawWatch(self, drawDC):
520 # Draw the watch...
521 if self.watchPen or self.watchBrush:
522 if self.watchPen:
523 drawDC.SetPen(self.watchPen)
524 if self.watchBrush:
525 drawDC.SetBrush(self.watchBrush)
526 else:
527 drawDC.SetBrush(wx.Brush(self.GetBackgroundColour(), wx.SOLID))
528 drawDC.DrawCircle((self.centerX, self.centerY), self.radius_watch)
529
530
531 def _calcSteps(self):
532 # Calcule todos os pontos para:
533 # - marcas de horas
534 # - marcas de minutos
535 # - ponteiro de horas
536 # - ponteiro de minutos
537 # - ponteiro de segundos
538
539 circle = 360
540 mStep = 6 * self.clockStep # Step in degrees...
541
542 vq = 90 * (self.clockStyle & SHOW_QUARTERS_TICKS) / SHOW_QUARTERS_TICKS
543 vh = 30 * (self.clockStyle & SHOW_HOURS_TICKS) / SHOW_HOURS_TICKS
544 vm = 1 * (self.clockStyle & SHOW_MINUTES_TICKS) / SHOW_MINUTES_TICKS
545
546 coords = {"ticks_quarters": [self.radius_ticks_hours, 60,vq,{}],
547 "ticks_hours": [self.radius_ticks_hours, 60,vh,{}],
548 "ticks_minutes": [self.radius_ticks_minutes,60,vm,{}],
549 "hand_hours": [self.handHoursLength, 60,1, {}],
550 "hand_minutes": [self.handMinutesLength, 60,1, {}],
551 "hand_seconds": [self.handSecondsLength, 60,1, {}]}
552
553 radiansPerDegree = math.pi / 180
554
555 for t in coords.keys():
556 for i in range(0, circle+mStep, mStep):
557 radius = coords[t][0]
558 if t == "ticks_minutes":
559 radius = radius - self.offM
560 step_angle = 360. / coords[t][1]
561 pre = coords[t][2]
562 x = self.centerX + radius * math.sin(i * radiansPerDegree)
563 y = self.centerY + radius * math.cos(i * radiansPerDegree)
564 f = (pre and (i/step_angle == int(i/step_angle)) and (float(i)/pre == int(i/pre)))
565 coords[t][3][i] = [x,y,f]
566
567 if not self.clockStyle & OVERLAP_TICKS:
568 for i in range(0, circle + mStep, mStep):
569 f=coords["ticks_minutes"][3][i][2]
570 if f and \
571 (coords["ticks_hours"][3].get(i,[0,0,0])[2] or coords["ticks_quarters"][3].get(i,[0,0,0])[2]):
572 f=False
573 coords["ticks_minutes"][3][i][2]=f
574
575 self.coords=coords
576
577
578 def _getCoords(self, tipo, angle):
579 # Returns coords and 'use flag' based on current angle...
580 k = 360 - (angle + 180)
581 if k <= 0:
582 k = k + 360
583 return self.coords[tipo][3][k]
584
585
586 # -----------------------------------------------------
587 #
588 def SetTickShapes(self, tsh, tsm=None):
589 """
590 tsh, tsm: [[x0,y0], [x1,y1], ... [xn,yn]]
591
592 Sets lists of lists of points to be used as polygon shapes
593 when using the TICKS_POLY style. If tsm is ommitted,
594 we'll use tsh for both shapes.
595 """
596
597 if not tsm:
598 tsm=tsh
599
600 self.tickShapeHours = tsh
601 self.tickShapeMinutes = tsm
602
603
604 def SetHandWeights(self, h=None, m=None, s=None):
605 """
606 h, m, s: value
607
608 Sets thickness of hands.
609 """
610
611 if h:
612 self.handHoursThickness = h
613 if m:
614 self.handMinutesThickness = m
615 if s:
616 self.handSecondsThickness = s
617
618
619 def SetHandColours(self, h=None, m=None, s=None):
620 """
621 h, m, s: wx.Colour
622
623 Sets colours of hands. If m and s are ommitted,
624 we'll use h for all.
625 """
626
627 if h and not m and not s:
628 m=h
629 s=h
630
631 if h:
632 self.handHoursColour = h
633 if m:
634 self.handMinutesColour = m
635 if s:
636 self.handSecondsColour = s
637
638
639 def SetTickColours(self, h=None, m=None):
640 """
641 h, m: wx.Colour
642
643 Sets colours of ticks. If m is ommitted,
644 we'll use h for both.
645 """
646
647 if not m:
648 m=h
649
650 if h:
651 self.tickMarkHoursPen = wx.Pen(h, 1, wx.SOLID)
652 self.tickMarkHoursBrush = wx.Brush(h, wx.SOLID)
653
654 if m:
655 self.tickMarkMinutesPen = wx.Pen(m, 1, wx.SOLID)
656 self.tickMarkMinutesBrush = wx.Brush(m, wx.SOLID)
657
658
659 def SetTickSizes(self, h=None, m=None):
660 """
661 h, m: value
662
663 Sizes for tick marks.
664 """
665
666 if h:
667 self.markSizeHour = h
668 if m:
669 self.markSizeMin = m
670
671
672 def SetTickFonts(self, h=None, m=None):
673 """
674 h, m: wx.Font
675
676 Fonts for tick marks when using TICKS_DECIMAL or TICKS_ROMAN style.
677 If m is ommitted, we'll use h for both.
678 """
679
680 if not m:
681 m=h
682
683 if h:
684 self.tickMarkHoursFont = h
685 self.tickMarkHoursFont.SetPointSize(self.markSizeHour)
686 if m:
687 self.tickMarkMinutesFont = m
688 self.tickMarkMinutesFont.SetPointSize(self.markSizeMin)
689
690
691 def SetMinutesOffset(self, o):
692 """
693 s = value
694
695 Sets the distance between tick marks for hours and minutes.
696 """
697 self.offM = o
698
699
700 def SetShadowColour(self, s):
701 """
702 s = wx.Colour or (r,g,b) tuple.
703
704 Sets the colour to be used to draw shadows.
705 """
706
707 self.shadowPenColour = self.shadowBrushColour = s
708
709
710 def SetWatchPenBrush(self, p=None, b=None):
711 """
712 p = wx.Pen; b = wx.Brush
713
714 Set the pen and brush for the watch.
715 """
716
717 if p:
718 self.watchPen = p
719 if b:
720 self.watchBrush = b
721
722
723 def SetClockStyle(self, style):
724 """
725 Set the clock style, acording to the options:
726
727 SHOW_QUARTERS_TICKS - Show marks for hours 3, 6, 9, 12
728 SHOW_HOURS_TICKS - Show marks for all hours
729 SHOW_MINUTES_TICKS - Show marks for minutes
730
731 SHOW_HOURS_HAND - Show hours hand
732 SHOW_MINUTES_HAND - Show minutes hand
733 SHOW_SECONDS_HAND - Show seconds hand
734
735 SHOW_SHADOWS - Show hands and marks shadows
736
737 ROTATE_TICKS - Align tick marks to watch
738 OVERLAP_TICKS - Draw tick marks for minutes even
739 when they match the hours marks.
740 """
741
742 self.clockStyle = style
743
744
745 def SetTickStyles(self, h=None, m=None):
746 """
747 Set the ticks styles, acording to the options below.
748
749 TICKS_NONE = Don't show tick marks.
750 TICKS_SQUARE = Use squares as tick marks.
751 TICKS_CIRCLE = Use circles as tick marks.
752 TICKS_POLY = Use a polygon as tick marks. The polygon
753 must be passed using SetTickShapes,
754 otherwise the default polygon will be used.
755 TICKS_DECIMAL = Use decimal numbers.
756 TICKS_ROMAN = Use Roman numbers.
757 """
758
759 if h:
760 self.tickMarkHoursStyle = h
761 if m:
762 self.tickMarkMinutesStyle = m
763 #
764 # -----------------------------------------------------
765
766
767 if __name__ == "__main__":
768 print wx.VERSION_STRING
769 class App(wx.App):
770 def OnInit(self):
771 frame = wx.Frame(None, -1, "AnalogClockWindow", size=(375,375))
772
773 clock = AnalogClockWindow(frame)
774
775 # Settings below are used by default...
776 #clock.SetClockStyle(SHOW_HOURS_TICKS|SHOW_MINUTES_TICKS|SHOW_SHADOWS|ROTATE_TICKS)
777 #clock.SetTickStyles(TICKS_POLY, TICKS_CIRCLE)
778
779 frame.Centre(wx.BOTH)
780 frame.Show(True)
781 self.SetTopWindow(frame)
782 return True
783
784 theApp = App(0)
785 theApp.MainLoop()
786
787
788 #
789 ##
790 ### eof