Browse files

working on dialogs

  • Loading branch information...
1 parent 6cb1319 commit ab3baf0ec55d4220dfadf286b1a37123ec14a3bb FabioDive committed Apr 11, 2012
Showing with 142 additions and 65 deletions.
  1. +79 −23 datastore.py
  2. +59 −38 dialogs.py
  3. +4 −4 models.py
View
102 datastore.py
@@ -27,38 +27,94 @@
"""
-from models import Texts, Options
+from models import Texts, Options, Effects
from models import DBSession
import transaction
+from utils import rgb_to_hex
-def savetexts(new_texts):
- if len(new_texts) > 0:
+
+"""
+structure of the items dictionary:
+
+self.program = {
+ 0: {
+ 'text': 'aaa',
+ 'color': '#ffffff',
+ 'effect': 'flash',
+ 'effect_time': 20,
+ },
+
+ 1: {
+ 'text': 'bbb',
+ 'color': '#ffffff',
+ 'effect': 'flash',
+ 'effect_time': 30,
+ },
+ ...
+ }
+"""
+
+#
+# Our abstraction layer
+#
+
+class DataStore():
+ def __init__(self):
+ self.options = Options.get_options()
+ self.effects = Effects.list_effects()
+ i = 0
+ self.items = {}
+ for item in Texts.get_items():
+ d = {
+ 'text': item.text,
+ 'color': item.color,
+ 'effect': item.effect,
+ 'effect_time': item.effect_time
+ }
+ self.items[i] = d
+ i+=1
+
+ def reset_texts(self):
+ self.items = {}
+
+ #@classmethod
+ def append_values(self, text, color, effect, effect_time):
+ d = {
+ 'text': text,
+ 'color': rgb_to_hex(color),
+ 'effect': effect,
+ 'effect_time': effect_time
+ }
+ self.items[len(self.items)] = d
+
+ def save_values_to_db(self):
+ if len(new_texts) > 0:
+ try:
+ transaction.begin()
+ Texts.reset()
+ for sub_dict in self.items.iteritems():
+ for k,v in sub_dict:
+ dbsession = DBSession()
+ texts = Texts(v['text'], v['color'], v['effect'], v['effect_time'])
+ dbsession.add(text)
+
+ dbsession.commit()
+ transaction.commit()
+ return True
+ except:
+ return False
+
+ def save_options_to_db(self, timeon, timeoff, speed):
try:
transaction.begin()
- Texts.reset()
- for k,v in new_texts.iteritems():
- dbsession = DBSession()
- texts = Texts(v['text'], v['color'], v['effect'], v['effect_time'])
- dbsession.add(text)
-
+ Options.reset()
+ dbsession = DBSession()
+ options = Options(timeon, timeoff, speed)
+ dbsession.add(options)
dbsession.commit()
transaction.commit()
return True
except:
return False
-
-def saveoptions(timeon, timeoff, speed):
- try:
- transaction.begin()
- Options.reset()
- dbsession = DBSession()
- options = Options(timeon, timeoff, speed)
- dbsession.add(options)
- dbsession.commit()
- transaction.commit()
- return True
- except:
- return False
-
View
97 dialogs.py
@@ -27,9 +27,7 @@
"""
from pgu import gui
-from datastore import savetexts
-from models import Texts, Effects
-from utils import rgb_to_hex
+from datastore import DataStore
"""
structure of the resulting program dictionary:
@@ -55,15 +53,17 @@
class EditProgram(gui.Dialog):
def __init__(self, **params):
- self.program = {}
+ self.datastore = DataStore()
title = gui.Label("Impostazione scritte")
main = gui.Container()
c = gui.Table(vpadding=3)
self.textlist = gui.List(width=700, height=200)
- for item in Texts.list_texts():
- self.textlist.add(str(item.text), value=str(item.text))
+ i = 0
+ for item in self.datastore.items:
+ self.textlist.add(str(item.text), value=str(i) + ' ' +str(item.text))
+ i += 1
self.textlist.resize()
self.textlist.repaint()
@@ -72,7 +72,7 @@ def __init__(self, **params):
c.td(gui.Label("Impostazione scritte", cls="h1"), align=-1, colspan=5)
c.tr()
- c.td(gui.Label("Inserire in lista le scritte in ordine di apparizione."),
+ c.td(gui.Label("Inserire in lista le scritte in ordine di apparizione. Premere SALVA al termine."),
align=-1, colspan=5)
default_color = "#ffffff"
@@ -98,17 +98,17 @@ def update_color():
c.td(btnadd, rowspan=2)
self.effselector = gui.Select()
- for item in Effects.list_effects():
+ for item in self.datastore.effects:
self.effselector.add(str(item.effect), int(item.id))
if not self.effselector.value:
self.effselector.value = item.id
default_speed = 50
default_speed_label = 'Velocita: 50'
self.speed = gui.HSlider(value=default_speed,min=0,max=100,size=32,width=160,height=25)
- speed_indicator = gui.Input(value=default_speed_label,size=12)
+ speed_indicator = gui.Label(value=default_speed_label)
def change_speed():
- speed_indicator.value = 'Velocita: %s' % self.speed.value
+ speed_indicator.set_text('Velocita: %s' % self.speed.value)
self.speed.connect(gui.CHANGE,change_speed)
c.tr()
@@ -119,6 +119,17 @@ def change_speed():
c.tr()
c.td(self.textlist, align=-1, colspan=5)
+ def load_cfg_items():
+ idx = int(self.textlist.value.split(None, 1)[0])
+ #print idx
+ d = self.datastore.items[idx]
+ self.inputbox.value = d['text']
+ self.color.value = d['color']
+ self.effselector.value = d['effect']
+ self.speed.value = d['effect_time']
+
+ self.textlist.connect(gui.CHANGE,load_cfg_items)
+
btn_el = gui.Button("Elimina Riga")
btn_el.connect(gui.CLICK, self.remove_list_item)
btn_su = gui.Button("Muovi Riga SU")
@@ -127,7 +138,7 @@ def change_speed():
btn_elt.connect(gui.CLICK, self.clear_list)
btn_giu = gui.Button("Muovi Riga GIU")
btn_giu.connect(gui.CLICK, self.updown_list, 1)
- btn_sav = gui.Button("Chiudi")
+ btn_sav = gui.Button("SALVA")
btn_sav.connect(gui.CLICK, self.close)
c.tr()
@@ -143,44 +154,49 @@ def change_speed():
def add_list(self):
- if len(self.inputbox.value) > 0:
- self.textlist.add(self.inputbox.value, value=self.inputbox.value)
+ length = str(len(self.datastore.items))
+ v = self.inputbox.value[:100] #truncate to 100 chars maximum
+ if len(v) > 0:
+ self.textlist.add(v, value=length + ' ' + v)
self.textlist.resize()
self.textlist.repaint()
- self.add_to_program(self.inputbox.value,
- rgb_to_hex(self.color.value),
- self.effselector.value,
- self.speed.value)
self.inputbox.value=''
+ self.datastore.append_values(
+ text=v,
+ color=self.color.value,
+ effect=self.effselector.value,
+ effect_time=self.speed.value,
+ )
def clear_list(self):
self.textlist.clear()
self.textlist.resize()
self.textlist.repaint()
- self.program = {}
def updown_list(self, pos):
- l = self.textlist.items
- for item in l:
- if item.value == self.textlist.value: obj = item
-
- idx = l.index(obj)
+ idx = int(self.textlist.value.split(None, 1)[0])
#print 'pos: %s' % idx
newpos = idx + pos
#print 'newpos: %s' % newpos
#print 'len: %s' % len(l)
+ l = self.textlist.items
+ #print newpos
+ #print range(len(l))
if newpos in range(len(l)):
#print 'newpos: %s' % newpos
l.insert(newpos, l.pop(idx))
tmp_list = []
for item in l:
- tmp_list.append(item.value)
+ #print item.value
+ tmp_list.append(item.value.split(None, 1)[1])
#print tmp_list
self.textlist.clear()
- for item in tmp_list:
- self.textlist.add(item, value=item)
+ i = 0
+ for v in tmp_list:
+ self.textlist.add(v, value=str(i) + ' ' + v)
+ i += 1
self.textlist.repaint()
@@ -191,18 +207,23 @@ def remove_list_item(self):
self.textlist.remove(item)
self.textlist.resize()
self.textlist.repaint()
- self.program = {}
-
-
- def add_to_program(self, text, color, effect, effect_time):
- d = {
- 'text': text,
- 'color': color,
- 'effect': effect,
- 'effect_time': effect_time
- }
- self.program[len(self.program)] = d
- print self.program
+
+
+ #def add_to_program(self, text, color, effect, effect_time):
+ #d = {
+ # 'text': text,
+ # 'color': color,
+ # 'effect': effect,
+ # 'effect_time': effect_time
+ # }
+ #self.program[len(self.program)] = d
+
+ #def save_list(self):
+ #self.add_to_program(v,
+ # rgb_to_hex(self.color.value),
+ # self.effselector.value,
+ # self.speed.value)
+
class ColorDialog(gui.Dialog):
View
8 models.py
@@ -64,8 +64,8 @@ def __repr__(self):
return "<texts('%s %d %d %d')>" % (self.text, self.color, self.effect, self.effect_time)
@classmethod
- def list_texts(cls):
- return DBSession.query(cls).all()
+ def get_items(cls):
+ return DBSession.query(cls).order_by('id').all()
@classmethod
def reset(cls):
@@ -92,7 +92,7 @@ def __repr__(self):
@classmethod
def list_effects(cls):
- return DBSession.query(cls).all()
+ return DBSession.query(cls).order_by('id').all()
@classmethod
def reset(cls):
@@ -122,7 +122,7 @@ def __repr__(self):
return "<Options('%d, %d, %d')>" % (self.timeon, self.timeoff, self.speed)
@classmethod
- def list_options(cls):
+ def get_options(cls):
return DBSession.query(cls).first()
@classmethod

0 comments on commit ab3baf0

Please sign in to comment.