mirror of
https://github.com/rzzzwilson/pymlac.git
synced 2025-06-10 09:32:41 +00:00
Make it more lightweight
This commit is contained in:
parent
ad6a102992
commit
f205f712b7
130
pymlac/led16.py
130
pymlac/led16.py
@ -8,36 +8,25 @@ Event attributes:
|
||||
'state' - the current displayed value (int)
|
||||
|
||||
Instance methods:
|
||||
Led16(parent, label, pos, led, *args, **kwargs)
|
||||
Led16(parent, label, pos, led_on, led_off, *args, **kwargs)
|
||||
parent owning widget
|
||||
label label above leftmost led
|
||||
pos position (x, y)
|
||||
led image of led 'on' state
|
||||
led_on image of led 'on' state
|
||||
led_off image of led 'off' state
|
||||
args ?
|
||||
kwargs ?
|
||||
SetState(state)
|
||||
sets widget state, 'state' is as in constructor.
|
||||
sets widget state, 'state' is a 16 bit value.
|
||||
|
||||
The widget will look like:
|
||||
|
||||
+-<title>----------------------------+
|
||||
| <TopSpacer> |
|
||||
LeftSpacer--+-> O O O O O O O O O O O O O O O O |\TickSpacer
|
||||
| +-+-----+-----+-----+-----+-----+ |/
|
||||
| <BotSpacer> |
|
||||
+------------------------------------+
|
||||
|
||||
|
||||
-
|
||||
|top spacer
|
||||
-
|
||||
|-------------|<label> --label spacer
|
||||
left spacer /
|
||||
O O O O O O O O O O O O O O O O --tick spacer
|
||||
+-+-----+-----+-----+-----+-----+ /
|
||||
- ^
|
||||
|bot spacer |
|
||||
- octal ticks
|
||||
+-----------------------------------+
|
||||
| <TopSpacer> |
|
||||
<LeftSpacer>--> O O O O O O O O O O O O O O O O <---<RightSpacer>
|
||||
| +-+-----+-----+-----+-----+-----+ |
|
||||
| <BotSpacer> |
|
||||
+-----------------------------------+
|
||||
"""
|
||||
|
||||
import os
|
||||
@ -45,19 +34,21 @@ import wx
|
||||
|
||||
|
||||
# layout 'constants'
|
||||
print('os.name=%s' % str(os.name))
|
||||
if os.name == 'nt':
|
||||
LeftSpacer = 7 # left border
|
||||
TopSpacer = 30
|
||||
TickSpacer = 30
|
||||
BotSpacer = 0 # space between ticks and box
|
||||
RightSpacer = 7 # right border
|
||||
TopSpacer = 30 # space at top
|
||||
TickSpacer = 30 # space down to ticks
|
||||
BotSpacer = 0 # space at the bottom
|
||||
elif os.name == 'posix':
|
||||
LeftSpacer = 3 # left border
|
||||
TopSpacer = 10 # space between top box and label
|
||||
LabelSpacer = 10
|
||||
TickSpacer = 30
|
||||
BotSpacer = 10 # space between ticks and box
|
||||
RightSpacer = 3 # right border
|
||||
TopSpacer = 3 # space at top
|
||||
TickSpacer = 10 # space down to ticks
|
||||
BotSpacer = 3 # space at the bottom
|
||||
else:
|
||||
# TODO: Make it work on Mac
|
||||
# TODO: Make it work on ???
|
||||
raise Exception('Unrecognized platform: %s' % os.name)
|
||||
|
||||
|
||||
@ -68,82 +59,79 @@ else:
|
||||
class Led16(wx.Panel):
|
||||
"""A custom widget for showing 16 leds."""
|
||||
|
||||
def __init__(self, parent, label, led_img, background=None, *args, **kwargs):
|
||||
def __init__(self, parent, led_on, led_off, background=None, **kwargs):
|
||||
"""Initialize an Led16 object.
|
||||
|
||||
parent owning widget
|
||||
label label above leftmost led
|
||||
led_img image of led 'on' state
|
||||
led_on image of led 'on' state
|
||||
led_off image of led 'off' state
|
||||
background widget background colour
|
||||
args ?
|
||||
kwargs ?
|
||||
kwargs extra args for the panel
|
||||
"""
|
||||
|
||||
wx.Panel.__init__(self, parent, *args, **kwargs)
|
||||
self.SetBackgroundColour(background)
|
||||
print('Led16: background=%s' % str(background))
|
||||
|
||||
wx.Panel.__init__(self, parent, **kwargs)
|
||||
if background is not None:
|
||||
self.SetBackgroundColour(background)
|
||||
|
||||
self.led_on = led_on
|
||||
self.led_off = led_off
|
||||
led_width = led_on.GetWidth() # assume ON and OFF are same size image
|
||||
led_height = led_on.GetHeight()
|
||||
|
||||
x = LeftSpacer
|
||||
y = TopSpacer
|
||||
led_width = led_img.GetWidth()
|
||||
led_height = led_img.GetHeight()
|
||||
|
||||
label = ' ' + label.strip() + ' '
|
||||
sbox = wx.StaticBox(self, label=label)
|
||||
vbox = wx.StaticBoxSizer(sbox, wx.VERTICAL)
|
||||
|
||||
vbox.AddSpacer(BotSpacer)
|
||||
y += LabelSpacer
|
||||
|
||||
#hbox = wx.BoxSizer(wx.HORIZONTAL)
|
||||
self.leds = []
|
||||
mark_count = 2
|
||||
self.ticks = [(x-17+led_width, y+led_height/2+5,
|
||||
x-17+led_width, y+led_height/2+10)]
|
||||
self.led_posn = []
|
||||
|
||||
# calculate positions of all 16 LEDs and construct ticks draw list
|
||||
for i in range(16):
|
||||
led = wx.StaticBitmap(self, -1, led_img, pos=(x-1+i*17, y))
|
||||
#hbox.Add(led)
|
||||
self.leds.append(led)
|
||||
self.led_posn.append((x-1+i*17, y))
|
||||
mark_count += 1
|
||||
if mark_count >= 3:
|
||||
mark_count = 0
|
||||
self.ticks.append((x+i*17 + led_width, y+led_height/2+5,
|
||||
x+i*17 + led_width, y+led_height/2+10))
|
||||
vbox.Add(hbox)
|
||||
|
||||
first = self.ticks[0]
|
||||
last = self.ticks[-1]
|
||||
(fx1, fy1, fx2, fy2) = first
|
||||
(lx1, ly1, lx2, ly2) = last
|
||||
self.ticks.append((fx1, ly1+5, lx1, ly1+5))
|
||||
|
||||
vbox.AddSpacer(BotSpacer)
|
||||
|
||||
self.set_value(0)
|
||||
|
||||
self.SetSizer(vbox)
|
||||
|
||||
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
||||
self.Bind(wx.EVT_SIZE, self.OnSize)
|
||||
|
||||
|
||||
def OnPaint(self, event):
|
||||
# allow transparent colours
|
||||
dc = wx.PaintDC(self)
|
||||
dc = wx.GCDC(dc)
|
||||
|
||||
# draw 16 on/off LEDs
|
||||
mask = 0x8000
|
||||
for i in range(16):
|
||||
posn = self.led_posn[i]
|
||||
if self.value & mask:
|
||||
wx.StaticBitmap(self, -1, self.led_on, pos=posn)
|
||||
else:
|
||||
wx.StaticBitmap(self, -1, self.led_off, pos=posn)
|
||||
mask = mask >> 1
|
||||
|
||||
# draw the tick marks underneath the LEDs
|
||||
dc.SetPen(wx.Pen('black', 1))
|
||||
dc.DrawLineList(self.ticks)
|
||||
|
||||
|
||||
def OnSize(self, event):
|
||||
self.Refresh()
|
||||
|
||||
|
||||
def set_value(self, value):
|
||||
mask = 0x8000
|
||||
for l in self.leds:
|
||||
if value & mask:
|
||||
l.Enable()
|
||||
else:
|
||||
l.Disable()
|
||||
mask = mask >> 1
|
||||
self.value = value
|
||||
self.Refresh()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -154,14 +142,18 @@ if __name__ == "__main__":
|
||||
frame = wx.Frame(None, -1, "Test Led16", size=(350, 75))
|
||||
vbox = wx.BoxSizer(wx.VERTICAL)
|
||||
|
||||
led = wx.Image('images/led_on.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
|
||||
led_on = wx.Image('images/led_on.png',
|
||||
wx.BITMAP_TYPE_PNG).ConvertToBitmap()
|
||||
|
||||
self.led16 = Led16(frame, 'Led16', led, size=(300,100))
|
||||
led_off = wx.Image('images/led_off.png',
|
||||
wx.BITMAP_TYPE_PNG).ConvertToBitmap()
|
||||
|
||||
self.led16 = Led16(frame, led_on, led_off, size=(300,100))
|
||||
vbox.Add(self.led16)
|
||||
vbox.Fit(frame)
|
||||
frame.Show(True)
|
||||
self.SetTopWindow(frame)
|
||||
self.led16.set_value(0xfffe)
|
||||
self.led16.set_value(012345)
|
||||
return True
|
||||
|
||||
app = MyApp(0)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user