import pvl.web
from pvl.web import urls, html
import logging; log = logging.getLogger('qmsk.dmx.web')
def colorize (x, red, green, blue, alpha=1.0) :
return x(style='background-color: rgba({red}, {green}, {blue}, {alpha:0.2f})'.format(red=red, green=green, blue=blue, alpha=alpha))
def input (head, name, value) :
return html.input(
type = 'text',
name = '-'.join([head, name]),
id = '-'.join([head, name]),
class_ = 'form-control dmx-input dmx-input-{name}'.format(name=name),
placeholder = name,
value = '{v:d}'.format(v=value) if value else None,
)
def color_input (head, c, value) :
color = dict(red=0, green=0, blue=0, alpha=0)
color[c] = 255
if value :
color['alpha'] = value / 255.0
return colorize(input(head, c, value), **color)
def slider (head, name) :
return html.div(id='-'.join([head, name, 'slider']), class_='dmx-slider dmx-slider-{name}'.format(name=name))
def color_slider (head, c) :
return slider(head, c)
def head_color (head, value) :
return html.div(class_='dmx-color-background')(colorize(html.div(id='-'.join([head, 'color']), class_='dmx-color')(' '), **value))
class Handler (pvl.web.Handler) :
# Bootstrap
DOCTYPE = 'html'
HTML_XMLNS = None
HTML_LANG = 'en'
CSS = (
'//code.jquery.com/ui/1.10.4/themes/ui-darkness/jquery-ui.css',
'//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css',
'/static/dmx.css',
)
JS = (
'//code.jquery.com/jquery-2.1.0.js',
'//code.jquery.com/ui/1.10.4/jquery-ui.js',
'//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js',
'/static/color-slider.js',
'/static/dmx.js',
)
# test
TITLE = u"DMX Control"
def process (self) :
if self.request.method == 'POST' :
for name, value in self.request.form.iteritems() :
head_name, attr = name.split('-', 1)
head = self.app.heads[head_name]
if value :
value = int(value)
# update head
head[attr] = value
# update dmx
self.app.update()
def render_head (self, name, head) :
if head.alpha() is None :
head_input = head_slider = None
else :
head_input = input(name, 'alpha', head.alpha()['alpha'])
head_slider = slider(name, 'alpha')
rowspan = 1
if head.color() is None :
colors = { }
color = None
else :
colors = head.color()
color = head_color(name, colors)
rowspan += 3
yield html.tr(
html.th(rowspan=rowspan)(name),
html.td(head_input),
html.td(head_slider),
html.td(rowspan=rowspan)(color),
)
for c in colors :
yield html.tr(
html.td(
color_input(name, c, colors[c]),
),
html.td(
color_slider(name, c),
),
)
def render (self) :
return html.div(class_='container')(
html.form(action='.', method='POST')(
html.table(class_='dmx')(
html.thead(
html.th(class_='dmx-head')(u"Head"),
html.th(class_='dmx-value')(u"DMX"),
html.th(class_='dmx-control')(u"Control"),
html.th(class_='dmx-head-control')(u"Head Control"),
),
html.tbody(self.render_head(name, head) for name, head in sorted(self.app.heads.iteritems())),
),
html.button(type='submit', class_='btn btn-primary')("Go"),
)
)
class DMXWebApplication (pvl.web.Application) :
URLS = urls.Map((
urls.rule('/', Handler),
))
def __init__ (self, dmx, heads, **opts) :
super(DMXWebApplication, self).__init__(**opts)
self.dmx = dmx
self.heads = heads
def update (self) :
if not self.dmx :
return
for head in self.heads.itervalues() :
self.dmx[head.channel] = tuple(head)