#!/usr/bin/python # -*- coding: iso-8859-15 -*- ''' Ventana Principal de Prestame tu voz ''' #Copyright (C) 2009 Federico Moreira - # Alejandro Esperón - # Esteban Arias - # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see import gtk import os, gobject import subprocess import threading import settings from manejar_datos import cargar_datos, cargar_planillas from sonido import reproducir from funciones import pintar_filas, pintar_columnas class VentanaPrincipal(gtk.Table): ''' Clase principal que dibuja la ventana, maneja controles y carga los archivos de imagenes ''' TITLE_FONT_DESC = "sans bold 10" def __init__(self): # Creamos los botones y sus labels correspondientes self.lista_botones = [gtk.Button() for dummy in xrange(1, (settings.TABLA_ANCHO * settings.TABLA_ALTO) + 1)] self.lista_titulos = [gtk.Label() for dummy in xrange(1, (settings.TABLA_ANCHO * settings.TABLA_ALTO) + 1)] # Cargo las planillas disponibles #self.planillas = cargar_planillas(os.path.join('planillas', # 'planillas.txt')) self.num_fila = 0 self.num_columna = 0 #Tiempo de barrido self.tiempo_repeticion = 2000 self.seleccione_fila = -1 self.handle_barrido_horizontal = 0 self.handle_barrido_vertical = 0 self.handle_check_process = 0 self.process_id = None self.hilo = None # Leo las tarjetas creadas #self.lista_tarjetas = cargar_datos(self.planillas[0].replace("\n", "")) #carga demo por defecto self.lista_tarjetas = cargar_datos('fede2') contador = 0 import pango # Creo la tabla con un tamaño específico gtk.Table.__init__(self, settings.TABLA_ANCHO, settings.TABLA_ALTO) self.flag = True # Por cada item cargo la fila correspondiente for item in range(settings.TABLA_ALTO): for pos in range(settings.TABLA_ANCHO): print self.lista_tarjetas[contador].audio # Agrego la label y la muestro #self.lista_titulos[contador].set_label(self.lista_tarjetas[contador].titulo) self.lista_titulos[contador].set_text(self.lista_tarjetas[contador].titulo) self.lista_titulos[contador].modify_font(pango.FontDescription(self.TITLE_FONT_DESC)) self.lista_titulos[contador].show() #Tamaño self.lista_botones[contador].set_size_request(settings.BUTTON_SIZE, settings.BUTTON_SIZE) # Agrego el botón y lo muestro self.lista_botones[contador].show() # Creo una caja para almacenar los titulos y la muestro caja = gtk.VBox() caja.show() caja.pack_start(self.lista_titulos[contador]) # Cargo las imágenes en los botones imagen = gtk.Image() imagen.set_from_file(self.lista_tarjetas[contador].imagen) self.lista_botones[contador].set_image(imagen) # Conecto el evento de click, y asigno el método a reproducir audio self.lista_botones[contador].connect("clicked", self.pulso_boton, self.lista_tarjetas[contador].audio, self.lista_tarjetas[contador]) caja.pack_start(self.lista_botones[contador]) self.attach(caja, pos, pos + 1, item, item + 1, gtk.EXPAND, gtk.EXPAND) contador += 1 # Comienzo con el barrido horizontal self.barrido_horizontal() def barrido_horizontal(self): '''Barrido horizontal de la ventana ''' self.num_columna = self.num_fila self.num_fila = pintar_filas(self.lista_botones, self.num_fila) self.handle_barrido_horizontal = gobject.timeout_add(self.tiempo_repeticion, self.barrido_horizontal) def pulso_boton(self, dummy_widget, info,btn): chat = get_chat_box() ''' Método general que es usado al presionar algun boton ''' if self.seleccione_fila == -1 : gobject.source_remove(self.handle_barrido_horizontal) self.seleccione_fila = self.num_fila - settings.TABLA_ANCHO self.barrido_vertical() else: if self.flag: if self.lista_tarjetas[self.num_columna].titulo != 'REPRODUCIR': gobject.source_remove(self.handle_barrido_vertical) self.hilo = threading.Thread(target=self.ejecutar_sonido, args=(self.lista_tarjetas[self.num_columna].audio,)) chat.add_card(self.lista_tarjetas[self.num_columna]) self.hilo.start() self.flag = False self.check_process_id() else: self.hilo = threading.Thread(target=self.ejecutar_chatbox) def ejecutar_chatbox(self): chat = get_chat_box() chat.play_sound() def ejecutar_sonido(self, info): print 'fila:' + str(self.seleccione_fila) + ' columna:' + str(self.num_columna) reproducir(info) #self.process_id = subprocess.Popen(['/usr/bin/aplay', info]) #self.process_id.wait() def check_process_id(self): "Verifica si el sonido termino" if not self.hilo.isAlive() : self.barrido_horizontal() self.seleccione_fila = -1 self.flag = True gobject.source_remove(self.handle_check_process) else: self.handle_check_process = gobject.timeout_add(1000, self.check_process_id) def barrido_vertical(self): '''Barrido vertical de la ventana''' self.num_columna = pintar_columnas(self.lista_botones, self.seleccione_fila, self.num_columna) self.handle_barrido_vertical = gobject.timeout_add(self.tiempo_repeticion, self.barrido_vertical) #self.flag=False def modifico_tiempo_barrido(self, tiempo): ''' Altero el tiempo de barrido ''' self.tiempo_repeticion = tiempo class ControlBarrido(gtk.Table): ''' Clase que controla la estabilidad del barrido ''' def __init__(self, grilla): gtk.Table.__init__(self, 1, 3) self.grilla = grilla self.valor = 2 self.tiempo = gtk.Label('Espera (segundos) : '.join(str(self.valor))) self.tiempo.show() boton_mas = gtk.Button('+') boton_mas.show() boton_menos = gtk.Button('-') boton_menos.show() boton_mas.connect("clicked", self.pulso_boton_mas, 1) boton_menos.connect("clicked", self.pulso_boton_menos, 1) self.attach(self.tiempo, 0, 1, 0, 1) self.attach(boton_mas, 1, 2, 0, 1) self.attach(boton_menos, 2, 3, 0, 1) def pulso_boton_mas(self, dummy_widget, dummy_info): '''Incrementa tiempo barrido''' self.valor += 1 self.tiempo.set_label('Espera : ' + str(self.valor) + ' seg') self.grilla.modifico_tiempo_barrido(self.valor * 1000) def pulso_boton_menos(self, dummy_widget, dummy_info): '''Decrementa tiempo de barrido''' if self.valor > 1: self.valor -= 1 self.tiempo.set_label('Espera : ' + str(self.valor) + ' seg') self.grilla.modifico_tiempo_barrido(self.valor * 1000) _chatbox = None class ChatBox(gtk.HBox): def __init__(self): super(ChatBox, self).__init__() self.set_size_request(settings.BUTTON_SIZE, settings.BUTTON_SIZE) def add_card(self, tarjeta): tarjeta.refresh_image() tarjeta.show() self.pack_start(tarjeta) def clear_box(self): for child in this.get_children(): child.remove() def play_sound(self): for child in this.get_children(): child.remove() def get_chat_box(): global _chatbox if _chatbox == None: _chatbox = ChatBox() return _chatbox #if __name__ == '__main__': # principal = gtk.Window(gtk.WINDOW_TOPLEVEL) # principal.set_title('Prestame tu voz') # tablebox = VentanaPrincipal() # control = ControlBarrido(tablebox) # chatbox = get_chat_box() # sw = gtk.ScrolledWindow() # # sw.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_NEVER) # sw.add(chatbox) # sw.show() # # caja_principal = gtk.VBox() # caja_principal.pack_start(control) # caja_principal.pack_start(tablebox) # caja_principal.pack_start(sw) # # principal.add(caja_principal) # tablebox.show_all() # control.show_all() # chatbox.show_all() # caja_principal.show_all() # # # principal.show() # principal.connect("delete_event", gtk.main_quit) # gtk.main()