Skip to content

Commit

Permalink
Allow naiv code execution
Browse files Browse the repository at this point in the history
  • Loading branch information
dsp committed Mar 20, 2011
1 parent 542cd0d commit bfde959
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 41 deletions.
96 changes: 70 additions & 26 deletions ubcolorgui.glade
Expand Up @@ -17,17 +17,16 @@
<property name="visible">True</property>
<property name="left_padding">12</property>
<child>
<widget class="GtkColorSelection" id="colorChooser">
<widget class="GtkComboBox" id="lampChooser">
<property name="visible">True</property>
<property name="current_color">#000000000000</property>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">&lt;b&gt;Aktuelle Farbe&lt;/b&gt;</property>
<property name="label" translatable="yes">&lt;b&gt;Lampe&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
Expand All @@ -51,48 +50,93 @@
</packing>
</child>
<child>
<widget class="GtkFrame" id="frame1">
<widget class="GtkNotebook" id="pages">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<property name="can_focus">True</property>
<child>
<widget class="GtkAlignment" id="alignment1">
<widget class="GtkColorSelection" id="colorChooser">
<property name="visible">True</property>
<property name="current_color">#000000000000</property>
</widget>
</child>
<child>
<widget class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="label" translatable="yes">Farbe</property>
</widget>
<packing>
<property name="tab_fill">False</property>
<property name="type">tab</property>
</packing>
</child>
<child>
<widget class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="left_padding">12</property>
<child>
<widget class="GtkComboBox" id="lampChooser">
<widget class="GtkTextView" id="textCode">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="border_width">1</property>
<property name="text" translatable="yes">import colorsys, time
while True:
for H in range(0, 360, 13):
h = H/360.0
r, g, b = map(lambda x: x * 255, colorsys.hsv_to_rgb(h, 1.0, 0.7))
fadecolor(r,g,b,2)
time.sleep(3)</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkAspectFrame" id="aspectframe1">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<widget class="GtkButton" id="buttonRun">
<property name="label" translatable="yes">Run Code</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</widget>
</child>
</widget>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label2">
<widget class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="label" translatable="yes">&lt;b&gt;Lampen
&lt;/b&gt;</property>
<property name="use_markup">True</property>
<property name="label" translatable="yes">Code</property>
</widget>
<packing>
<property name="type">label_item</property>
<property name="position">1</property>
<property name="tab_fill">False</property>
<property name="type">tab</property>
</packing>
</child>
</widget>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
<child>
<widget class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
<packing>
<property name="type">tab</property>
</packing>
</child>
</widget>
<packing>
<property name="position">3</property>
<property name="position">2</property>
</packing>
</child>
<child>
Expand All @@ -102,7 +146,7 @@
</widget>
<packing>
<property name="expand">False</property>
<property name="position">4</property>
<property name="position">3</property>
</packing>
</child>
</widget>
Expand Down
49 changes: 34 additions & 15 deletions ubcolorgui.py
Expand Up @@ -2,7 +2,7 @@
import pygtk
pygtk.require("2.0")

import gtk, gtk.glade, gtk.gdk
import gtk, gtk.glade, gtk.gdk, pango
import sys
import dbus, gobject, avahi
from dbus.mainloop.glib import DBusGMainLoop
Expand All @@ -20,15 +20,20 @@ def __init__(self, loop):
# GLADE SETUP
self.wtree = gtk.glade.XML("ubcolorgui.glade", "MainWindow")

self.window = self.wtree.get_widget("MainWindow")
self.lampchooser = self.wtree.get_widget("lampChooser")
self.statusbar = self.wtree.get_widget("statusBar")
self.colorchooser = self.wtree.get_widget("colorChooser")
self.window = self.wtree.get_widget("MainWindow")
self.lampchooser = self.wtree.get_widget("lampChooser")
self.statusbar = self.wtree.get_widget("statusBar")
self.colorchooser = self.wtree.get_widget("colorChooser")
self.code = self.wtree.get_widget("textCode")
self.codeRunButton = self.wtree.get_widget("buttonRun")
self.colorchooser.connect("color_changed",self.new_color)
self.codeRunButton.connect("clicked", self.run_code)

self.status = {'connection': None}
self.code.modify_font(pango.FontDescription('monospace 10'))

self.status = {'connection': None, 'color': None}
self.status['connection'] = self.statusbar.get_context_id("connection status")
self.status['connection']
self.status['color'] = self.statusbar.get_context_id("color")

self.lampstore = gtk.ListStore(gobject.TYPE_STRING)
self.lampchooser.set_model(self.lampstore)
Expand Down Expand Up @@ -78,22 +83,36 @@ def print_error(self, *args):
print 'error_handler'
print args[0]

def new_color(self, color):
def run_code(self, widget):
buf = self.code.get_buffer()
code = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
c = compile(code, '<string>', 'exec')
eval(c, {'fadecolor': self.fade_color, 'setcolor': self.set_color})

def lamp_cb(self, cb):
model = self.lampchooser.get_model()
index = self.lampchooser.get_active()
if index:
lamp = model[index][0]
print "Active lamp: %s" % lamp
s = uberbus.moodlamp.Moodlamp(lamp, True)
c = color.get_current_color()
r = c.red/256;
g = c.green/256;
b = c.blue/256;
s.connect()
s.timedfade(r,g,b,t)
print "Setting %s to %s%s%s" % (lamp, hex(r)[2:], hex(g)[2:], hex(b)[2:])
cb(s)
s.disconnect()

def fade_color(self, r, g, b, t):
print "Fade color to 0x%x%x%x" % (r,g,b)
self.lamp_cb(
lambda s: s.timedfade(r,g,b,t))

def set_color(self, r, g, b):
print "Set color to 0x%x%x%x" % (r,g,b)
self.lamp_cb(
lambda s: s.setcolor(r,g,b))

def new_color(self, color):
c = color.get_current_color()
self.fade_color(c.red/256, c.green/256, c.blue/256, 0.5)

def main():
loop = DBusGMainLoop()
bcb = UBColorGui(loop)
Expand Down

0 comments on commit bfde959

Please sign in to comment.