Merge pull request #102 from wernerfb/2015-april-throbber

throbber doc update and a simple unittest
This commit is contained in:
Robin Dunn
2015-04-11 16:58:19 -07:00
4 changed files with 1438 additions and 27 deletions

View File

@@ -45,7 +45,11 @@ class TestPanel(wx.Panel):
self.throbbers['autoreverse']['throbber'] = \
throb.Throbber(self, -1, images, frameDelay = 0.1, reverse = True)
self.throbbers['autoreverse']['throbber'].sequence.append(0)
seq = self.throbbers['autoreverse']['throbber'].sequence
if isinstance(seq, range):
seq = list(seq).append(0)
else:
seq.append(0)
self.throbbers['label']['throbber'] = \
throb.Throbber(self, -1, images, frameDelay = 0.1, label = 'Label')

View File

@@ -0,0 +1,27 @@
import imp_unittest, unittest
import wtc
import wx
import wx.lib.throbber as th
import throbImages
images = [throbImages.catalog[i].GetBitmap()
for i in throbImages.index
if i not in ['eclouds', 'logo']]
#---------------------------------------------------------------------------
class lib_throbber_Tests(wtc.WidgetTestCase):
def test_lib_throbber(self):
pnl = wx.Panel(self.frame)
w = th.Throbber(pnl, -1, images, size=(36, 36))
def test_lib_throbber_Events(self):
th.EVT_UPDATE_THROBBER
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()

1277
unittests/throbImages.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@ can continue unencumbered.
#
# o 2.5 compatability update.
#
# Tags: phoenix-port, unittest, py3-port, documented
import os
@@ -48,23 +49,48 @@ class Throbber(wx.Panel):
animation. A label may also be specified to show on top of the animation.
"""
def __init__(self, parent, id,
bitmap, # single (composite) bitmap or list of bitmaps
bitmap,
pos = wx.DefaultPosition,
size = wx.DefaultSize,
frameDelay = 0.1,# time between frames
frames = 0, # number of frames (only necessary for composite image)
frameWidth = 0, # width of each frame (only necessary for composite image)
label = None, # optional text to be displayed
overlay = None, # optional image to overlay on animation
reverse = 0, # reverse direction at end of animation
style = 0, # window style
frameDelay = 0.1,
frames = 0,
frameWidth = 0,
label = None,
overlay = None,
reverse = 0,
style = 0,
name = "throbber",
rest = 0,
current = 0,
direction = 1,
sequence = None
):
wx.Panel.__init__(self, parent, id, pos, size, style, name)
"""
Default class constructor.
:param `parent`: parent window, must not be ``None``
:param integer `id`: window identifier. A value of -1 indicates a default value
:param `bitmap`: a :class:`Bitmap` to be used
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform
:param `frameDelay`: time delay between frames
:param `frames`: number of frames (only necessary for composite image)
:param `frameWidth`: width of each frame (only necessary for composite image)
:param string `label`: optional text to be displayed
:param `overlay`: optional :class:`Bitmap` to overlay on animation
:param boolean `reverse`: reverse direction at end of animation
:param integer `style`: the underlying :class:`Control` style
:param string `name`: the widget name.
:param `rest`: the rest frame
:param `current`: the current frame
:param `direction`: 1 advances = -1 reverses
:param `sequence`: sequence of frames, defaults to range(self.frames)
"""
super(Throbber, self).__init__(parent, id, pos, size, style, name)
self.name = name
self.label = label
self.running = (1 != 1)
@@ -135,19 +161,43 @@ class Throbber(wx.Panel):
def DoGetBestSize(self):
"""
Get the best size of the widget.
:returns: the width and height
"""
return (self.width, self.height)
def OnTimer(self, event):
"""
Handles the ``wx.EVT_TIMER`` event for :class:`Throbber`.
:param `event`: a :class:`TimerEvent` event to be processed.
"""
wx.PostEvent(self, UpdateThrobberEvent())
def OnDestroyWindow(self, event):
"""
Handles the ``wx.EVT_WINDOW_DESTROY`` event for :class:`Throbber`.
:param `event`: a :class:`WindowDestroyEvent` event to be processed.
"""
self.Stop()
event.Skip()
def Draw(self, dc):
"""
Draw the widget.
:param `dc`: the :class:`DC` to draw on
"""
dc.DrawBitmap(self.submaps[self.sequence[self.current]], 0, 0, True)
if self.overlay and self.showOverlay:
dc.DrawBitmap(self.overlay, self.overlayX, self.overlayY, True)
@@ -158,15 +208,28 @@ class Throbber(wx.Panel):
def OnPaint(self, event):
"""
Handles the ``wx.EVT_PAINT`` event for :class:`Throbber`.
:param `event`: a :class:`PaintEvent` event to be processed.
"""
self.Draw(wx.PaintDC(self))
event.Skip()
def Update(self, event):
"""
Handles the ``EVT_UPDATE_THROBBER`` event for :class:`ResizeWidget`.
:param `event`: a :class:`UpdateThrobberEvent` event to be processed.
"""
self.Next()
def Wrap(self):
"""Wrap the throbber around."""
if self.current >= len(self.sequence):
if self.autoReverse:
self.Reverse()
@@ -184,45 +247,55 @@ class Throbber(wx.Panel):
# --------- public methods ---------
def SetFont(self, font):
"""Set the font for the label"""
"""
Set the font for the label.
:param `font`: the :class:`Font` to use
"""
wx.Panel.SetFont(self, font)
self.SetLabel(self.label)
self.Draw(wx.ClientDC(self))
def Rest(self):
"""Stop the animation and return to frame 0"""
"""Stop the animation and return to frame 0."""
self.Stop()
self.current = self.rest
self.Draw(wx.ClientDC(self))
def Reverse(self):
"""Change the direction of the animation"""
"""Change the direction of the animation."""
self.direction = -self.direction
def Running(self):
"""Returns True if the animation is running"""
"""Returns True if the animation is running."""
return self.running
def Start(self):
"""Start the animation"""
"""Start the animation."""
if not self.running:
self.running = not self.running
self.timer.Start(int(self.frameDelay * 1000))
def Stop(self):
"""Stop the animation"""
"""Stop the animation."""
if self.running:
self.timer.Stop()
self.running = not self.running
def SetCurrent(self, current):
"""Set current image"""
"""
Set current image.
:param int `current`: the index to the current image
"""
running = self.Running()
if not running:
#FIXME: need to make sure value is within range!!!
@@ -231,12 +304,22 @@ class Throbber(wx.Panel):
def SetRest(self, rest):
"""Set rest image"""
"""
Set rest image.
:param int `rest`: the index for the rest frame.
"""
self.rest = rest
def SetSequence(self, sequence = None):
"""Order to display images"""
"""
Order to display images in.
:param `sequence`: a sequence containing the order to display images in.
"""
# self.sequence can be changed, but it's not recommended doing it
# while the throbber is running. self.sequence[0] should always
@@ -257,31 +340,36 @@ class Throbber(wx.Panel):
def Increment(self):
"""Display next image in sequence"""
"""Display next image in sequence."""
self.current += 1
self.Wrap()
def Decrement(self):
"""Display previous image in sequence"""
"""Display previous image in sequence."""
self.current -= 1
self.Wrap()
def Next(self):
"""Display next image in sequence according to direction"""
"""Display next image in sequence according to direction."""
self.current += self.direction
self.Wrap()
def Previous(self):
"""Display previous image in sequence according to direction"""
"""Display previous image in sequence according to direction."""
self.current -= self.direction
self.Wrap()
def SetFrameDelay(self, frameDelay = 0.05):
"""Delay between each frame"""
"""
Delay between each frame.
:param float `frameDelay`: the delay between frames.
"""
self.frameDelay = frameDelay
if self.running:
self.Stop()
@@ -289,7 +377,12 @@ class Throbber(wx.Panel):
def ToggleOverlay(self, state = None):
"""Toggle the overlay image"""
"""
Toggle the overlay image.
:param boolean `state`: set the overlay state or if None toggle state.
"""
if state is None:
self.showOverlay = not self.showOverlay
else:
@@ -298,7 +391,12 @@ class Throbber(wx.Panel):
def ToggleLabel(self, state = None):
"""Toggle the label"""
"""
Toggle the label.
:param boolean `state`: set the label state or if None toggle state.
"""
if state is None:
self.showLabel = not self.showLabel
else:
@@ -307,7 +405,12 @@ class Throbber(wx.Panel):
def SetLabel(self, label):
"""Change the text of the label"""
"""
Change the text of the label.
:param string `label`: the label text.
"""
self.label = label
if label:
extentX, extentY = self.GetTextExtent(label)