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

# CeibalJAM! - Uruguay

import gtk
import os
import cairo
import gobject
import Globales as G

class CanvasRobot(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        self.motores_sensores = Motores_Sensores()
        self.bateria = Bateria()
        
        self.pack_start(self.motores_sensores, True, True, 0)
        self.pack_start(self.bateria, False, False, 0)
        
        self.show_all()
        
class Motores_Sensores(gtk.HBox):
    def __init__(self):
        gtk.HBox.__init__(self)
        self.motores = Motores()
        self.sensores = Sensores()
        self.pack_start(self.motores, True, True, 0)
        self.pack_start(self.sensores, True, True, 0)
        self.show_all()
        
class Motores(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        self.widgetMotorA = BotonMotor()
        self.widgetMotorA.set_tooltip("Motor A")
        self.widgetMotorB = BotonMotor()
        self.widgetMotorB.set_tooltip("Motor B")
        self.widgetMotorC = BotonMotor()
        self.widgetMotorC.set_tooltip("Motor C")
        motores = [self.widgetMotorA, self.widgetMotorB,
            self.widgetMotorC]
        for motor in motores:
            motor.set_imagen(os.path.join(G.ICONOS, 'motor.png'))
            self.pack_start(motor, True, True, 0)
        self.show_all()
        
class Sensores(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        
        self.widget_presion = ControlSensor()
        self.widget_presion.set_imagen(os.path.join(G.ICONOS, 'presion.png'))
        self.widget_presion.set_tooltip("Sensor de Presión")
        self.widget_sonido = ControlSensor()
        self.widget_sonido.set_imagen(os.path.join(G.ICONOS, 'sonido.png'))
        self.widget_sonido.set_tooltip("Sensor de Sonido")
        self.widget_ultrasonido = ControlSensor()
        self.widget_ultrasonido.set_imagen(os.path.join(G.ICONOS, 'ultrasonido.png'))
        self.widget_ultrasonido.set_tooltip("Sensor de Ultrasonido")
        self.widget_luz = ControlSensor()
        self.widget_luz.set_imagen(os.path.join(G.ICONOS, 'luz.png'))
        self.widget_luz.set_tooltip("Sensor de Luz")
        
        hbox1 = gtk.HBox()
        hbox1.pack_start(self.widget_presion, True, True, 0)
        hbox1.pack_start(self.widget_sonido, True, True, 0)
        hbox2 = gtk.HBox()
        hbox2.pack_start(self.widget_ultrasonido, True, True, 0)
        hbox2.pack_start(self.widget_luz, True, True, 0)
        
        self.pack_start(hbox1, True, True, 0)
        self.pack_start(hbox2, True, True, 0)
        self.show_all()

class ControlSensor(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        self.botonsensor = BotonSensor()
        self.barradeprogreso = Barra_de_Progreso()
        self.pack_start(self.botonsensor, True, True, 0)
        self.pack_start(self.barradeprogreso, False, False, 0)
        self.show_all()

    def set_tooltip(self, texto):
        self.botonsensor.set_tooltip(texto)

    def set_imagen(self, archivo):
        self.botonsensor.set_imagen(archivo)
        
class BotonSensor(gtk.EventBox):
    def __init__(self):
        gtk.EventBox.__init__(self)
        self.set_visible_window(True)
        self.modify_bg(gtk.STATE_NORMAL, G.BLANCO)
        self.set_border_width(1)
        self.imagen = None
        self.show_all()
        self.connect('expose-event', self.repintar)
        
    def set_tooltip(self, texto):
        tooltips = gtk.Tooltips()
        tooltips.set_tip(self, texto, tip_private=None)

    def set_imagen(self, archivo):
        self.imagen = cairo.ImageSurface.create_from_png(archivo)
    
    def set_tamanio(self, w, h):
        self.set_size_request(w,h)
        
    def repintar(self, widget, senial):
        imgpat = cairo.SurfacePattern(self.imagen)
        scaler = cairo.Matrix()
        
        x,y,w,h = self.get_allocation()
        width = float(self.imagen.get_width())
        height = float(self.imagen.get_height())
        ws = float(width/float(w))
        hs = float(height/float(h))
        
        scaler.scale(ws, hs)
        imgpat.set_matrix(scaler)
        imgpat.set_filter(cairo.FILTER_BEST)
        
        cr = self.window.cairo_create()
        cr.set_source(imgpat)
        cr.paint()
        
        return True
    
class BotonMotor(BotonSensor):
    __gsignals__ = {"clicked":(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT, ))}
    def __init__(self):
        BotonSensor.__init__(self)
        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.on = False
        self.normal = G.BLANCO
        self.clicked = G.NARANJA
        self.enter = G.AMARILLO
        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)
        
    def button_release(self, widget, event):
        self.modify_bg(gtk.STATE_NORMAL, self.enter)
    def leave_notify_event(self, widget, event):
        self.modify_bg(gtk.STATE_NORMAL, self.normal)
    def enter_notify_event(self, widget, event):
        self.modify_bg(gtk.STATE_NORMAL, self.enter)
    def button_press(self, widget, event):
        if event.button == 1:
            if not self.on:
                self.modify_bg(gtk.STATE_NORMAL, self.clicked)
                self.on = True
                self.normal = G.NARANJA
                self.enter = G.NARANJA
            else:
                self.modify_bg(gtk.STATE_NORMAL, G.AMARILLO)
                self.on = False
                self.normal = G.BLANCO
                self.enter = G.AMARILLO
            self.emit("clicked", event)
    
class Bateria(gtk.HBox):
    def __init__(self):
        gtk.HBox.__init__(self)
        etiqueta = gtk.Label("Batería: ")
        self.barradeprogreso = Barra_de_Progreso()
        self.pack_start(etiqueta, False, False, 0)
        self.pack_start(self.barradeprogreso, True, True, 0)
        
        self.show_all()

class Barra_de_Progreso(gtk.EventBox):
    def __init__(self):
        gtk.EventBox.__init__(self)
        self.scale = ProgressBar(gtk.Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0))
        self.valor = 0
        self.add(self.scale)
        self.show_all()
        
    def set_progress(self, valor = 0):
        self.scale.set_value(valor)

class ProgressBar(gtk.HScale):
    def __init__(self, ajuste):
        gtk.HScale.__init__(self, ajuste)
        self.ajuste = ajuste
        self.set_digits(0)
        self.set_draw_value(False)
        self.x, self.y, self.w, self.h = (0,0,200,40)
        self.borde, self.ancho = (15, 10)
        self.connect("expose_event", self.expose)
        self.connect("size-allocate", self.size_allocate)

    def expose( self, widget, event ):
        x, y, w, h = (self.x, self.y, self.w, self.h)
        ancho, borde = (self.ancho, self.borde)
        gc = gtk.gdk.Drawable.new_gc(self.window)
        gc.set_rgb_fg_color(G.BLANCO)
        self.window.draw_rectangle( gc, True, x, y, w, h )
        gc.set_rgb_fg_color(G.AMARILLO)
        ww = w- borde*2
        xx = x+ w/2 - ww/2
        hh = ancho
        yy = y+ h/2 - ancho/2
        self.window.draw_rectangle( gc, True, xx, yy, ww, hh )
        anchoimagen, altoimagen = (25,25)
        ximagen = int((xx- anchoimagen/2) + self.get_value() * (ww / (self.ajuste.upper - self.ajuste.lower)))
        yimagen = yy + hh/2 - altoimagen/2
        gc.set_rgb_fg_color(G.NARANJA)
        self.window.draw_rectangle( gc, True, xx, yy, ximagen, hh)
        gc.set_rgb_fg_color(G.BLANCO)
        self.window.draw_rectangle( gc, False, xx, yy, ww, hh )
        return True
    
    def size_allocate( self, widget, allocation ):
        self.x, self.y, self.w, self.h= allocation
        return False