-
Notifications
You must be signed in to change notification settings - Fork 0
/
VistaReproductor.py
411 lines (318 loc) · 14.4 KB
/
VistaReproductor.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# VistaReproductor.py por:
# Flavio Danesse <fdanesse@gmail.com>
# Ceibal Jam - Uruguay - Plan Ceibal
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango
from Reproductor import Reproductor
from BarradeReproduccion import BarradeReproduccion
from Radio import Radio
import os
import shutil
from sugar3.activity import activity
from ManejodeBasedeDatos import ManejodeBasedeDatos
# Directorio para crear la base de datos
directorio_base = os.path.join(activity.get_activity_root(), 'data/')
mi_base = os.path.join(directorio_base + "Radios.db")
# Si el directorio no existe, crearlo
if not os.path.exists(directorio_base):
os.mkdir(directorio_base)
# Si la base de datos no existe, crearla
if not os.path.exists(mi_base):
BasedeDatos = ManejodeBasedeDatos(mi_base)
BasedeDatos.CrearBasededatos()
BasedeDatos.Llenar_Base()
os.chmod(os.path.join(directorio_base, 'Radios.db'), 0660)
class VistaReproductor(Gtk.Table):
def __init__(self):
Gtk.Table.__init__(self, 10, 30, False)
# para los archivos mp3 - wav - ogg
self.treeview = Gtk.TreeView()
self.liststore = Gtk.ListStore(str)
self.treeselection = self.treeview.get_selection()
self.treeselection.set_mode(Gtk.SelectionMode.SINGLE)
self.directorio_de_reproduccion = None # el directorio desde donde se cargaron los archivos
self.indice_archivo_en_reproduccion = 0 # para mantener una lista de reproduccion con los archivos
# La base de datos con las radios
self.BasedeDatos = ManejodeBasedeDatos(mi_base)
self.listaderadios = []
# Reproductor helix
self.reproductor = Reproductor(self)
self.is_radio = False
self.fuentededatosparareproducir = None
self.directoriodeiconos = os.getcwd() + "/Iconos/"
self.rosado = Gdk.Color(65000,13000,25000)
self.celeste1 = Gdk.Color(0,33000,33000)
# ******************** Interface Grafica *****************************
# Zona Central.
# Definicion de radios online
viewportderadios = Gtk.ScrolledWindow()
viewportderadios.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.caja_radios = Gtk.VBox()
self.cargar_radios()
viewportderadios.add_with_viewport(self.caja_radios)
self.attach(viewportderadios, 0, 8, 0, 29)
# Barra vertical derecha para la lista de reproduccion
self.viewportderecho = Gtk.ScrolledWindow()
self.viewportderecho.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.attach(self.viewportderecho, 8, 10, 0, 29)
# Informacion adicional de la actividad
self.viewportderecho.add_with_viewport(self.crear_barra_info())
# Barra con controles de reproduccion
# Zona inferior
self.barradereproduccion = BarradeReproduccion()
self.reproductor.asignacion_de_barra_de_reproduccion(self.barradereproduccion)
self.attach(self.barradereproduccion, 0, 10, 29, 30)
self.show_all()
# Conexion de eventos de los controles de la barra de reproduccion con las
# funciones de reproduccion del reproductor
# Abre un selector de archivos
self.barradereproduccion.botonabrir.connect("clicked", self.abrirdirectoriomp3)
self.barradereproduccion.botonstop.connect("clicked", self.reproductor.stop)
self.barradereproduccion.botonplay.connect("clicked", self.reproductor.pause)# moficar para un click en la lista
self.barradereproduccion.botonatras.connect("clicked", self.tema_anterior)
self.barradereproduccion.botonsiguiente.connect("clicked", self.siguiente_tema)
# ***************************************************************************
# ************************** FUNCIONES **************************************
def cargar_radios(self):
# se llama al iniciar la actividad y al agregar o eliminar una radio
for child in self.caja_radios:
self.caja_radios.remove(child)
radios = self.BasedeDatos.CargarRadios() # Devuelve una lista de tuplas
self.listaderadios = []
for radio in radios:
self.listaderadios.append(Radio(radio, self.directoriodeiconos))
for radio in self.listaderadios:
radio.connect("button_press_event", self.handler_click_Radio)
self.caja_radios.pack_start(radio, True, True, 5)
def handler_click_Radio(self, widget, event):
# reacciona a los clicks sobre las radios
boton = event.button
pos = (event.x, event.y)
tiempo = event.time
if boton == 1:
self.cargar_fuente_de_datos (widget, widget.direccionderadio, True)
self.pintar_radios_no_seleccionadas(widget)
print "Reproducir radio"
return
elif boton == 3:
print "Abrir menu - popup"
self.crear_menu_emergente_para_radios(widget, boton, pos, tiempo)
return
def crear_menu_emergente_para_radios(self, widget, boton, pos, tiempo):
# un menu para agregar o eliminar radios de la base de datos
menu = Gtk.Menu()
# Items del menu
agregar = Gtk.MenuItem("Agregar nueva Radio . . .")
eliminar = Gtk.MenuItem("Eliminar de la lista")
eliminar2 = Gtk.MenuItem("Eliminar definitivamente")
# Agregar los items al menu
menu.append(agregar)
menu.append(eliminar)
menu.append(eliminar2)
# Se conectan las funciones de retrollamada a la senal "activate"
agregar.connect_object("activate", self.construir_dialogo_agregar_radio, None)
eliminar.connect_object("activate", self.destruirwidget, widget, widget)
eliminar2.connect_object("activate", self.eliminar_radio, widget)
menu.show_all()
menu.popup(None, None, self.posicionar_menu, boton, tiempo, None)
def posicionar_menu(self, widget, pos):
# Establece la posicion del menu desplegable
print "Posicionando menu desplegable"
def construir_dialogo_agregar_radio(self, widget):
# Crea un cuadro de dialogo para ingresar los datos de una nueva radio
dialog = Gtk.Dialog("Agregar Radio", None, Gtk.DialogFlags.MODAL, None)
etiqueta0 = Gtk.Label("No utilices tildes ni ñ en los datos.")
dialog.vbox.pack_start(etiqueta0, True, True, 5)
renglo1 = Gtk.HBox()
etiqueta1 = Gtk.Label("Nombre de la Radio: ")
texto1 = Gtk.Entry()
renglo1.pack_start(etiqueta1, True, True, 5)
renglo1.pack_start(texto1, True, True, 5)
dialog.vbox.pack_start(renglo1, True, True, 5)
renglo2 = Gtk.HBox()
etiqueta2 = Gtk.Label("Direccion de Reproduccion: ")
texto2 = Gtk.Entry()
renglo2.pack_start(etiqueta2, True, True, 5)
renglo2.pack_start(texto2, True, True, 5)
dialog.vbox.pack_start(renglo2, True, True, 5)
renglo3 = Gtk.HBox()
etiqueta3 = Gtk.Label("Descripcion: ")
texto3 = Gtk.Entry()
renglo3.pack_start(etiqueta3, True, True, 5)
renglo3.pack_start(texto3, True, True, 5)
dialog.vbox.pack_start(renglo3, True, True, 5)
renglo4 = Gtk.HBox()
etiqueta4 = Gtk.Label("Pais de procedencia: ")
texto4 = Gtk.Entry()
renglo4.pack_start(etiqueta4, True, True, 5)
renglo4.pack_start(texto4, True, True, 5)
dialog.vbox.pack_start(renglo4, True, True, 5)
dialog.add_button("Guardar", 1)
dialog.add_button("Cancelar", 2)
dialog.show_all()
if dialog.run() == 1:
# verificar y guardar
if texto2.get_text() != "":
radio = self.listaderadios[-1]
id_de_radio = radio.id_en_base
indice = int (id_de_radio + 1)
nombre = texto1.get_text()
dir_rep = texto2.get_text()
detalles = texto3.get_text()
pais = texto4.get_text()
self.BasedeDatos.agregar_radio(indice, nombre, dir_rep, detalles, pais)
self.cargar_radios()
elif dialog.run() == 2:
# sale automaticamente
pass
dialog.destroy()
def eliminar_radio (self, widget):
indice = widget.id_en_base
self.BasedeDatos.eliminar_radio(indice)
self.cargar_radios()
def crear_barra_info(self):
# Informacion
self.barra_ceibal_radio_info = Gtk.VBox()
imagen1 = Gtk.Image()
imagen1.set_from_file(self.directoriodeiconos + "Flavio.png")
etiquetadeinformacion1 = Gtk.Label("Ceibal Radio" + "\n" + "fdanesse@hotmail.com" + "\n" + "http://sites.google.com/" + "\n" + "site/sugaractivities/home")
etiquetadeinformacion1.modify_font(Pango.FontDescription("purisa 4"))
etiquetadeinformacion1.set_justify(Gtk.Justification.CENTER)
etiquetadeinformacion2 = Gtk.Label("http://drupal.ceibaljam.org/" + "\n" + "webmaster@ceibaljam.org")
etiquetadeinformacion2.modify_font(Pango.FontDescription("purisa 4"))
etiquetadeinformacion2.set_justify(Gtk.Justification.CENTER)
imagen2 = Gtk.Image()
imagen2.set_from_file(self.directoriodeiconos + "ceibaljam.png")
self.barra_ceibal_radio_info.pack_start(imagen2, True, True, 5)
self.barra_ceibal_radio_info.pack_start(etiquetadeinformacion2, True, True, 5)
self.barra_ceibal_radio_info.pack_start(imagen1, True, True, 5)
self.barra_ceibal_radio_info.pack_start(etiquetadeinformacion1, True, True, 5)
self.barra_ceibal_radio_info.show_all()
return self.barra_ceibal_radio_info
def pintar_radios_no_seleccionadas (self, widget):
# Cambia los colores de los controles al hacerles click
for radio in self.listaderadios:
radio.pintame(self.celeste1)
widget.pintame(self.rosado)
def abrirdirectoriomp3(self,widget):
# Abre un Filechooser para cargar un directorio con archivos mp3 - wav - ogg
selectordedirectorio = Gtk.FileChooserDialog("Abrir directorio MP3", None, Gtk.FileChooserAction.OPEN, None)
selectordedirectorio.set_current_folder_uri("file:///media")
selectordedirectorio.set_select_multiple(True)
# extras
frame = Gtk.Frame()
hbox = Gtk.HBox()
frame.add(hbox)
botonabrirdirectorio = Gtk.Button("Abrir")
botonseleccionartodo = Gtk.Button("Seleccionar Todos")
botonsalir = Gtk.Button("Salir")
hbox.pack_end(botonsalir, False, False, 5)
hbox.pack_end(botonseleccionartodo, False, False, 5)
hbox.pack_end(botonabrirdirectorio, False, False, 5)
selectordedirectorio.set_extra_widget(frame)
botonsalir.connect("clicked",self.destruirwidget,selectordedirectorio)
botonabrirdirectorio.connect("clicked",self.abrirdirectorio,selectordedirectorio)
botonseleccionartodo.connect("clicked",self.seleccionartodo,selectordedirectorio)
# filtro Musica
filter = Gtk.FileFilter()
filter.set_name("Musica")
filter.add_mime_type("sound/mp3")
filter.add_mime_type("sound/ogg")
filter.add_mime_type("sound/wav")
filter.add_pattern("*.mp3")
filter.add_pattern("*.ogg")
filter.add_pattern("*.wav")
selectordedirectorio.add_filter(filter)
selectordedirectorio.add_shortcut_folder_uri("file:///media/")
selectordedirectorio.show_all()
def seleccionartodo(self,widget,selectordedirectorio):
selectordedirectorio.select_all()
def abrirdirectorio(self,widget,selectordedirectorio):
# Carga los mp3 del directorio seleccionado
listadereproduccion = selectordedirectorio.get_filenames()
self.cargardirectorio(listadereproduccion)
self.destruirwidget(None, selectordedirectorio)
#****************
#****************
def cargardirectorio(self, listadereproduccion):
self.liststore = Gtk.ListStore(str)
for archivo in listadereproduccion:
if os.path.isfile(archivo):
nombre_de_archivo = os.path.basename(archivo)
self.liststore.append([nombre_de_archivo])
if len(self.liststore) > 0:
for child in self.viewportderecho:
self.viewportderecho.remove(child)
# el directorio de reproduccion
self.directorio_de_reproduccion = os.path.dirname(listadereproduccion[0])
self.treeview = Gtk.TreeView()
self.treeview.connect("row-activated", self.open_file)
self.treeview.set_model(self.liststore)
self.treeview.append_column(Gtk.TreeViewColumn('Mi música', Gtk.CellRendererText(), text=0))
self.viewportderecho.add_with_viewport(self.treeview)
self.viewportderecho.show_all()
self.treeview.show_all()
self.indice_archivo_en_reproduccion = 0
self.open_file(self.treeview, self.indice_archivo_en_reproduccion, 0)
self.treeselection = self.treeview.get_selection()
self.treeselection.set_mode(Gtk.SelectionMode.SINGLE)
self.treeselection.select_path(self.indice_archivo_en_reproduccion)
def siguiente_tema(self, widget):
if int(self.indice_archivo_en_reproduccion) + 1 < len(self.liststore) and self.is_radio == False:
self.indice_archivo_en_reproduccion += 1
self.open_file(self.treeview, self.indice_archivo_en_reproduccion, 0)
self.treeselection = self.treeview.get_selection()
self.treeselection.set_mode(Gtk.SelectionMode.SINGLE)
self.treeselection.select_path(self.indice_archivo_en_reproduccion)
def tema_anterior(self, widget):
if int(self.indice_archivo_en_reproduccion) - 1 > -1 and self.is_radio == False:
self.indice_archivo_en_reproduccion -= 1
self.open_file(self.treeview, self.indice_archivo_en_reproduccion, 0)
self.treeselection = self.treeview.get_selection()
self.treeselection.set_mode(Gtk.SelectionMode.SINGLE)
self.treeselection.select_path(self.indice_archivo_en_reproduccion)
def open_file(self, treeview, path, column):
# def callback(treeview, iter, path, user_data)
# metodo de la interfase treeview
model = treeview.get_model()
iterador = model.get_iter(path)
archivo = self.directorio_de_reproduccion + "/" + model.get_value(iterador, 0)
self.cargar_fuente_de_datos(None, archivo, False)
# El path es de tipo (1,) hay que sacar la coma y los parentesis para usarlo como indice entero
indice1 = str(path)
indice = ""
for x in indice1:
if x != "," and x != "(" and x != ")":
indice += x
#self.indice_archivo_en_reproduccion = path
self.indice_archivo_en_reproduccion = int(indice)
print "self.indice_archivo_en_reproduccion: ", self.indice_archivo_en_reproduccion
def cargar_fuente_de_datos(self, widget, archivo, is_radio):
# Se carga la fuente de datos que se va a reproducir
self.is_radio = is_radio
if is_radio == False:
self.fuentededatosparareproducir = "file://" + archivo
else:
self.fuentededatosparareproducir = archivo
self.reproductor.abrirfuentededatos(None, self.fuentededatosparareproducir, is_radio)
def destruirwidget(self,widgetquedalasenal,widgetquesedestruira):
# Elimina un widget (filechooser y radio eliminada por el menu contextual)
widgetquesedestruira.destroy()
return False