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