#!/usr/bin/env python # -*- coding: utf-8 -*- # Widgets.py por: # Flavio Danesse # Ignacio Rodriguez # CeibalJAM! - Uruguay # 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 import gtk import gobject import os import Globals as G class Button(gtk.EventBox): __gsignals__ = {"clicked":(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT, ))} def __init__(self): gtk.EventBox.__init__(self) self.set_visible_window(True) self.set_border_width(1) # http://developer.gnome.org/pygtk/stable/gdk-constants.html#gdk-event-mask-constants self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.ENTER_NOTIFY_MASK | gtk.gdk.LEAVE_NOTIFY_MASK) self.connect("button_press_event", self.button_press) self.connect("button_release_event", self.button_release) self.connect("enter-notify-event", self.enter_notify_event) self.connect("leave-notify-event", self.leave_notify_event) self.tamanio = (0,0) self.normalcolor = G.AMARILLO self.selectlocor = G.BLANCO self.clickedcolor = G.NARANJA self.modify_bg(gtk.STATE_NORMAL, self.normalcolor) self.show_all() def button_release(self, widget, event): self.modify_bg(gtk.STATE_NORMAL, self.selectlocor) def leave_notify_event(self, widget, event): self.modify_bg(gtk.STATE_NORMAL, self.normalcolor) def enter_notify_event(self, widget, event): self.modify_bg(gtk.STATE_NORMAL, self.selectlocor) def button_press(self, widget, event): if event.button == 1: self.modify_bg(gtk.STATE_NORMAL, self.clickedcolor) self.emit("clicked", event) def set_tooltip(self, texto): tooltips = gtk.Tooltips() tooltips.set_tip(self, texto, tip_private=None) def set_tamanio(self, w, h): if self.tamanio != (w,h): self.tamanio = (w,h) self.set_size_request(w,h) class ButtonElemento(Button): def __init__(self, diccionario): Button.__init__(self) self.elementoquimico = diccionario self.add(gtk.Label(self.elementoquimico["simbolo"])) texto = "" for key in self.elementoquimico.keys(): texto += "%s = %s%s" % (key, self.elementoquimico[key], "\n") self.set_tooltip(texto) self.show_all() ''' class ElementoenJuego(ButtonElemento): def __init__(self, diccionario): ButtonElemento.__init__(self, diccionario) self.pregunta = None self.opciones = [] self.respuesta = None preguntas = G.PREGUNTAS[self.elementoquimico["Z"]] if preguntas: self.pregunta = preguntas["pregunta"] for opcion in preguntas["opciones"]: self.opciones.append( opcion ) self.respuesta = preguntas["opciones"][preguntas["respuesta"]] #if self.pregunta: # print self.pregunta # for opcion in self.opciones: # print opcion # print "Respuesta:", self.respuesta''' class TablaPeriodica(gtk.EventBox): def __init__(self): gtk.EventBox.__init__(self) self.set_visible_window(True) self.modify_bg(gtk.STATE_NORMAL, G.FONDO3) self.tabla = None self.filasdeelementos = None self.set_layout() self.show_all() def set_layout(self): self.tabla = Tabla() # Cabecera for x in range(1,19): button = Button() button.normalcolor = G.BLANCO button.selectlocor = G.BLANCO button.clickedcolor = G.BLANCO button.modify_bg(gtk.STATE_NORMAL, button.normalcolor) button.add(gtk.Label(x)) self.tabla.attach(button, x, x+1, 0, 1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=0, ypadding=0) for x in range(1,10): button = Button() button.normalcolor = G.BLANCO button.selectlocor = G.BLANCO button.clickedcolor = G.BLANCO button.modify_bg(gtk.STATE_NORMAL, button.normalcolor) button.add(gtk.Label(x)) self.tabla.attach(button, 0, 1, x, x+1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=0, ypadding=0) self.add(self.tabla) estructura = G.INDICEELEMENTOS elementos = G.ELEMENTOS # Elementos self.filasdeelementos = [] for linea in estructura: fila = [] for index in linea: if index: boton = ButtonElemento(elementos[index-1]) self.set_colores(boton) boton.show_all() fila.append(boton) else: fila.append(None) self.filasdeelementos.append(fila) for line in self.filasdeelementos: row = self.filasdeelementos.index(line)+1 for boton in line: col = line.index(boton)+1 if boton != None: self.tabla.attach(boton, col, col+1, row, row+1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=gtk.EXPAND|gtk.FILL, xpadding=0, ypadding=0) pregunta = Pregunta(G.PREGUNTAS[2]["pregunta"]) self.tabla.attach(pregunta, 3, 13, 1, 2) respuesta = Respuestas() self.tabla.attach(respuesta, 3, 13, 2, 3) for x in G.PREGUNTAS[2]["opciones"]: pregunta = Pregunta(x, original=G.PREGUNTAS[2]["opciones"][G.PREGUNTAS[2]["respuesta"]],respuesta=True) respuesta._add(pregunta) def set_colores(self, boton): for elemen in G.COLORS: color, indices = elemen if boton.elementoquimico["Z"] in indices: boton.normalcolor = color boton.modify_bg(gtk.STATE_NORMAL, boton.normalcolor) class Tabla(gtk.Table): def __init__(self): gtk.Table.__init__(self, rows = 10, columns = 19, homogeneous = True) self.set_row_spacing(7, 5) self.show_all() class Toolbar1(gtk.Toolbar): def __init__(self): gtk.Toolbar.__init__(self) self.modify_bg(gtk.STATE_NORMAL, G.NEGRO) ''' separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(5, -1) separator.set_expand(False) self.insert(separator, -1)''' boton = gtk.ToolButton() imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(G.ICONOS, "atomo.png"), 32, 32) imagen.set_from_pixbuf(pixbuf) boton.set_icon_widget(imagen) imagen.show() self.insert(boton, -1) boton.show() #tooltips = gtk.Tooltips() #tooltips.set_tip(boton, _("Detener Grabación."), tip_private=None) #boton.connect("clicked", self.ok_callback) ''' separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(5, -1) separator.set_expand(False) self.insert(separator, -1)''' boton = gtk.ToolButton() imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(G.ICONOS, "caminoalaplata-ico.png"), 32, 32) imagen.set_from_pixbuf(pixbuf) boton.set_icon_widget(imagen) imagen.show() self.insert(boton, -1) boton.show() #tooltips = gtk.Tooltips() #tooltips.set_tip(boton, _("Detener Grabación."), tip_private=None) #boton.connect("clicked", self.ok_callback) separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(0, -1) separator.set_expand(True) self.insert(separator, -1) imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(G.ICONOS, 'ceibaljam.png')) imagen.set_from_pixbuf(pixbuf) imagen.show() item = gtk.ToolItem() item.add(imagen) self.insert(item, -1) separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(5, -1) separator.set_expand(False) self.insert(separator, -1) imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(G.ICONOS, 'uruguay.png')) imagen.set_from_pixbuf(pixbuf) imagen.show() item = gtk.ToolItem() item.add(imagen) self.insert(item, -1) separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(5, -1) separator.set_expand(False) self.insert(separator, -1) imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(G.ICONOS, 'licencia.png')) imagen.set_from_pixbuf(pixbuf) imagen.show() item = gtk.ToolItem() item.add(imagen) self.insert(item, -1) separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(0, -1) separator.set_expand(True) self.insert(separator, -1) boton = gtk.ToolButton() imagen = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(G.ICONOS, "salir.png"), 32, 32) imagen.set_from_pixbuf(pixbuf) boton.set_icon_widget(imagen) boton.connect("clicked", lambda x: Salida()) imagen.show() self.insert(boton, -1) boton.show() #tooltips = gtk.Tooltips() #tooltips.set_tip(boton, _("Detener Grabación."), tip_private=None) #boton.connect("clicked", self.ok_callback) self.show_all() class Respuestas(gtk.ScrolledWindow): def __init__(self): super(Respuestas, self).__init__() self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) self._actual = 0 self.set_size_request(120, -1) self._box = gtk.HBox() self._botones = gtk.VBox() self.arriba = gtk.EventBox() self.arriba.add(gtk.Arrow(0, 0)) self.arriba.connect("button-press-event", self._arriba) self.abajo = gtk.EventBox() self.abajo.add(gtk.Arrow(1, 0)) self.abajo.connect("button-press-event", self._abajo) self._botones.pack_start(self.arriba) self._botones.pack_end(self.abajo) self._box.pack_end(self._botones) self.add_with_viewport(self._box) self._respuestas = [] self.show_all() def _actualizar(self): for x in self._box: self._box.remove(x) self._box.pack_start(self._respuestas[self._actual], True, True, 0) self._box.pack_start(self._botones, False, False, 0) self.show_all() def _arriba(self, widget, event): self._actualizar() self._actual += 1 if self._actual > len(self._respuestas) - 1: self._actual = 0 def _abajo(self, widget, event): self._actualizar() self._actual -= 1 if self._actual <= 0: self._actual = len(self._respuestas) - 1 def _add(self, respuesta): for x in self._box: self._box.remove(x) self._box.pack_start(respuesta, True, True, 0) self._box.pack_start(self._botones, False, False, 0) self._respuestas.append(respuesta) class Salida: def __init__(self): self._ventana = gtk.Window(gtk.WINDOW_POPUP) self._ventana.set_position(gtk.WIN_POS_CENTER) self._label = gtk.Label() self._label.set_markup("¡Gracias por Jugar!") self._salir = gtk.Button(gtk.STOCK_OK) self._salir.set_use_stock(True) self._salir.connect("clicked", lambda x: exit("Gracias!")) self._ventana.connect("destroy", lambda x: exit("Gracias!")) self._ventana.connect("delete-event", lambda x, i: exit("Gracias!")) self._box = gtk.VBox() self._box.pack_start(self._label, True, True, 0) self._box.pack_end(self._salir, False, False, 0) self._ventana.add(self._box) self._ventana.show_all() class Pregunta(gtk.EventBox): def __init__(self, pregunta, original=None, respuesta=False): super(Pregunta, self).__init__() self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.ENTER_NOTIFY_MASK | gtk.gdk.LEAVE_NOTIFY_MASK) if respuesta: self.connect("enter-notify-event", self._adentro, True) self.connect("leave-notify-event", self._adentro, False) self.connect("button-press-event", self._chequear) self.modify_bg(gtk.STATE_NORMAL, G.CELESTE) self._original = original self._pregunta = gtk.Label() self._pregunta.set_markup("" + pregunta + "") self.add(self._pregunta) self.show_all() def _chequear(self, widget, event): if self._original == self._pregunta.get_text(): def pro(): self.modify_bg(gtk.STATE_NORMAL, G.VERDECLARO) def pro1(): self.modify_bg(gtk.STATE_NORMAL, G.VERDEOSCURO) def pro2(): self.modify_bg(gtk.STATE_NORMAL, G.CELESTE) gobject.timeout_add(200, pro) gobject.timeout_add(400, pro1) gobject.timeout_add(600, pro) gobject.timeout_add(800, pro2) else: def pro(): self.modify_bg(gtk.STATE_NORMAL, G.ROJO) def pro1(): self.modify_bg(gtk.STATE_NORMAL, G.ROSADOCLARO) def pro2(): self.modify_bg(gtk.STATE_NORMAL, G.CELESTE) gobject.timeout_add(200, pro) gobject.timeout_add(400, pro1) gobject.timeout_add(600, pro) gobject.timeout_add(800, pro2) def _adentro(self, widget, event, estado): if estado: self.modify_bg(gtk.STATE_NORMAL, G.CELESTECLARO) else: self.modify_bg(gtk.STATE_NORMAL, G.CELESTE)