Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Widgets.py
blob: 2eca4b0e81b2557475592e4f12303d59f2a92491 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   Widgets.py por:
#   Flavio Danesse <fdanesse@gmail.com>
#   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 time
import sys
import gobject
import os
import Globals as G

class ButtonElemento(gtk.EventBox):
	__gsignals__ = {"clicked":(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT, ))}
	def __init__(self, diccionario):
		gtk.EventBox.__init__(self)
		self.set_visible_window(True)
		#self.modify_bg(gtk.STATE_NORMAL, G.BLANCO)
		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.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()

	# --------------------------- EVENTOS --------------------------
	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)
	# --------------------------- EVENTOS --------------------------

	# --------------------------- SETEOS ---------------------------
	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)
	# --------------------------- SETEOS ---------------------------

'''
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.FONDO)

		self.fixed = None
		self.filasdeelementos = None
		self.tamanios = (0,0)

		self.set_layout()
		self.show_all()

		self.connect("expose_event", self.repaint)

	def set_layout(self):
		self.fixed = gtk.Fixed()
		estructura = G.INDICEELEMENTOS
		elementos = G.ELEMENTOS
		self.filasdeelementos = []
		for linea in estructura:
			fila = []
			for index in linea:
				if index:
					boton = ButtonElemento(elementos[index-1])#ElementoenJuego(elementos[index-1])
					self.set_colores(boton)
					self.fixed.put(boton,0,0)
					boton.show_all()
					fila.append(boton)
				else:
					fila.append(None)
			self.filasdeelementos.append(fila)
		self.add(self.fixed)

	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)
	'''			
	def get_elementos(self):
		elementos = []
		for fila in self.filasdeelementos:
			for elemento in fila:
				elementos.append(elemento)
		return elementos'''

	def repaint(self, widget= None, event= None):
		x,y,w,h= self.get_allocation()
		if self.tamanios == (w/18,h/10): return True
		self.tamanios = (w/18,h/10)
		x, y = (0,0)
		for line in self.filasdeelementos:
			for boton in line:
				if boton != None:
					boton.set_tamanio(self.tamanios[0], self.tamanios[1])
					self.fixed.move(boton,x,y)
				x += self.tamanios[0]
			x = 0
			y += self.tamanios[1]
		return True