]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/analogclock.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.analogclock
3 # Purpose: A simple analog clock window
5 # Author: several folks on wxPython-users
7 # Created: 16-April-2003
9 # Copyright: (c) 2003 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
12 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 # o Updated for wx namespace
15 # o Tested with updated demo and with builtin test.
25 class AnalogClockWindow(wx
.Window
):
26 """A simple analog clock window"""
32 def __init__(self
, parent
, ID
=-1, pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
,
33 style
=0, name
="clock"):
35 # Initialize the wxWindow...
36 wx
.Window
.__init
__(self
, parent
, ID
, pos
, size
, style
, name
)
38 # Initialize the default clock settings...
41 self
.tickMarksBrushC
= self
.GetForegroundColour()
42 self
.tickMarksPenC
= self
.GetForegroundColour()
43 self
.tickMarkStyle
= self
.TICKS_SQUARE
45 # Make an initial bitmap for the face, it will be updated and
46 # painted at the first EVT_SIZE event.
48 self
.faceBitmap
= wx
.EmptyBitmap(max(W
,1), max(H
,1))
50 # Initialize the timer that drives the update of the clock
51 # face. Update every half second to ensure that there is at
52 # least one true update during each realtime second.
53 self
.timer
= wx
.Timer(self
)
56 # Set event handlers...
57 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
58 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, lambda x
: None)
59 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
60 self
.Bind(wx
.EVT_TIMER
, self
.OnTimerExpire
)
61 self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.OnQuit
)
64 def SetTickMarkStyle(self
, style
):
66 Set the style of the marks around the edge of the clock.
67 Options are TICKS_NONE, TICKS_SQUARE, and TICKS_CIRCLE
69 self
.tickMarkStyle
= style
72 def SetTickMarkColours(self
, brushC
, penC
="BLACK"):
74 Set the brush colour and optionally the pen colour of
75 the marks around the edge of the clock.
77 self
.tickMarksBrushC
= brushC
78 self
.tickMarksPenC
= penC
80 SetTickMarkColour
= SetTickMarkColours
83 def SetHandsColour(self
, c
):
84 """An alias for SetForegroundColour"""
85 self
.SetForegroundColour(c
) # the hands just use the foreground colour
88 # Using the current settings, render the points and line endings for the
89 # circle inside the specified device context. In this case, the DC is
90 # a memory based device context that will be blitted to the actual
91 # display DC inside the OnPaint() event handler.
92 def OnSize(self
, event
):
93 # The faceBitmap init is done here, to make sure the buffer is always
94 # the same size as the Window
95 size
= self
.GetClientSize()
96 self
.faceBitmap
= wx
.EmptyBitmap(size
.width
, size
.height
)
100 def OnPaint(self
, event
):
101 self
.DrawHands(wx
.PaintDC(self
))
104 def OnQuit(self
, event
):
109 def OnTimerExpire(self
, event
):
110 self
.DrawHands(wx
.ClientDC(self
))
113 def DrawHands(self
, drawDC
):
114 # Start by drawing the face bitmap
115 drawDC
.DrawBitmap(self
.faceBitmap
, (0,0))
117 currentTime
= time
.localtime(time
.time())
118 hour
, minutes
, seconds
= currentTime
[3:6]
120 W
,H
= self
.faceBitmap
.GetWidth(), self
.faceBitmap
.GetHeight()
124 radius
= min(centerX
, centerY
)
125 hour
+= minutes
/ 60.0 # added so the hour hand moves continuously
126 x
, y
= self
.point(hour
, 12, (radius
* .65))
127 hourX
, hourY
= (x
+ centerX
), (centerY
- y
)
128 x
, y
= self
.point(minutes
, 60, (radius
* .85))
129 minutesX
, minutesY
= (x
+ centerX
), (centerY
- y
)
130 x
, y
= self
.point(seconds
, 60, (radius
* .85))
131 secondsX
, secondsY
= (x
+ centerX
), (centerY
- y
)
133 # Draw the hour hand...
134 drawDC
.SetPen(wx
.Pen(self
.GetForegroundColour(), 5, wx
.SOLID
))
135 drawDC
.DrawLine((centerX
, centerY
), (hourX
, hourY
))
137 # Draw the minutes hand...
138 drawDC
.SetPen(wx
.Pen(self
.GetForegroundColour(), 3, wx
.SOLID
))
139 drawDC
.DrawLine((centerX
, centerY
), (minutesX
, minutesY
))
141 # Draw the seconds hand...
142 drawDC
.SetPen(wx
.Pen(self
.GetForegroundColour(), 1, wx
.SOLID
))
143 drawDC
.DrawLine((centerX
, centerY
), (secondsX
, secondsY
))
146 # Draw the specified set of line marks inside the clock face for the
147 # hours or minutes...
149 backgroundBrush
= wx
.Brush(self
.GetBackgroundColour(), wx
.SOLID
)
150 drawDC
= wx
.MemoryDC()
151 drawDC
.SelectObject(self
.faceBitmap
)
152 drawDC
.SetBackground(backgroundBrush
)
155 W
,H
= self
.faceBitmap
.GetWidth(), self
.faceBitmap
.GetHeight()
159 # Draw the marks for hours and minutes...
160 self
.DrawTimeMarks(drawDC
, self
.minuteMarks
, centerX
, centerY
, 4)
161 self
.DrawTimeMarks(drawDC
, self
.hourMarks
, centerX
, centerY
, 9)
164 def DrawTimeMarks(self
, drawDC
, markCount
, centerX
, centerY
, markSize
):
165 for i
in range(markCount
):
166 x
, y
= self
.point(i
+ 1, markCount
, min(centerX
,centerY
) - 16)
167 scaledX
= x
+ centerX
- markSize
/2
168 scaledY
= centerY
- y
- markSize
/2
170 drawDC
.SetBrush(wx
.Brush(self
.tickMarksBrushC
, wx
.SOLID
))
171 drawDC
.SetPen(wx
.Pen(self
.tickMarksPenC
, 1, wx
.SOLID
))
173 if self
.tickMarkStyle
!= self
.TICKS_NONE
:
174 if self
.tickMarkStyle
== self
.TICKS_CIRCLE
:
175 drawDC
.DrawEllipse((scaledX
- 2, scaledY
), (markSize
, markSize
))
177 drawDC
.DrawRectangle((scaledX
- 3, scaledY
), (markSize
, markSize
))
180 def point(self
, tick
, range, radius
):
181 angle
= tick
* (360.0 / range)
182 radiansPerDegree
= math
.pi
/ 180
183 pointX
= int(round(radius
* math
.sin(angle
* radiansPerDegree
)))
184 pointY
= int(round(radius
* math
.cos(angle
* radiansPerDegree
)))
185 return wx
.Point(pointX
, pointY
)
190 if __name__
== "__main__":
193 frame
= wx
.Frame(None, -1, "AnalogClockWindow Test", size
=(375,375))
195 clock
= AnalogClockWindow(frame
)
196 clock
.SetTickMarkColours("RED")
197 clock
.SetHandsColour("WHITE")
198 clock
.SetBackgroundColour("BLUE")
200 frame
.Centre(wx
.BOTH
)
202 self
.SetTopWindow(frame
)