Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/PrestametuVoz.activity/grid.py
blob: 46c1c70fa901cf908afcbd1a18487af6bb08ab84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
''' Ventana Principal de Prestame tu voz '''

#Copyright (C) 2009  Federico Moreira - <federico@piensalibre.info>
#                    Alejandro Esperón - <ratman26@gmail.com>
#                    Esteban Arias - <earias@plan.ceibal.edu.uy>
#
#    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 <http://www.gnu.org/licenses/>

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()