Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/fracciones.activity/gtkcake.py
blob: f0ebeb04b9897f97391dabad866a7962aa49749d (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
#!/usr/bin/env python
# -*- encoding: utf8 -*-
"""
La torta es una imagen redonda en un archivo de 500x500 pixeles.
Tiene un borde de 30 pixeles.

Pasado a coordenadas mundiales el radio es: (500-2*30)/(2*500) = 0.44

"""
import os, sys
import math
import gtk
import cairo


WIDTH = 500
HEIGHT = 500
RADIUS = 220
WRADIUS = 0.44


class Cake(gtk.DrawingArea):
    """Widget que dibuja una torta y permite seleccionar trozos de ella"""

    def __init__(self, subdivisions, *args, **kwargs):
        # constructor de la clase base
        gtk.DrawingArea.__init__(self, *args, **kwargs)
        # señales
        self.connect("expose_event", self.expose)
        self.connect("button_press_event", self.button_press)
        # Los eventos del raton no estan activados para el DrawingArea
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
        # variables de estado de la torta
        self.subdivisions = subdivisions
        self.selected_list = subdivisions * [False]

        # Carga imagenes
        self.image_bg = cairo.ImageSurface.create_from_png(os.path.join("data", "bg.png"))
        self.image_fg = cairo.ImageSurface.create_from_png(os.path.join("data", "fg.png"))


    def expose(self, widget, event):
        """Manejador del evento expose_event"""
        context = widget.window.cairo_create()
        # Elegimos solo la region expuesta al evento
        context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
        context.clip()
        self._draw(context)


    def button_press(self, widget, event):
        """Manejador del evento button_press_event"""
        x, y = event.get_coords()
        if self._select(x, y):
            self._draw(widget.window.cairo_create())
        

    def _draw(self, context):
        """Dibuja el contenido del widget"""

        def draw_grid():
            """Dibuja la rejilla de la torta y sus subdivisiones"""
            context.set_source_rgb(0, 0, 0)
            context.arc(WIDTH/2, HEIGHT/2, RADIUS, 0, 2 * math.pi)
            context.stroke()
            for i in range(self.subdivisions):
                angle = 2 * math.pi * i / self.subdivisions
                context.move_to(WIDTH/2, HEIGHT/2)
                context.line_to(
                    WIDTH/2 + RADIUS*math.cos(angle),
                    HEIGHT/2 + RADIUS*math.sin(angle)
                    )
                context.stroke()


        def mask_image(image):
            """Enmascara la imagen de la torta y dibuja solo los trozos que no
            fueron seleccionados
            """
            image_ctx = cairo.Context(image)
            image_ctx.set_operator(cairo.OPERATOR_CLEAR)
            for index, selected in enumerate(self.selected_list):
                if selected:
                    angle_start = 2 * math.pi * index / self.subdivisions
                    angle_end = 2 * math.pi * (index + 1) / self.subdivisions
                    image_ctx.move_to(WIDTH/2, HEIGHT/2)
                    image_ctx.line_to(
                        WIDTH/2 + RADIUS*math.cos(angle_start),
                        HEIGHT/2 + RADIUS*math.sin(angle_start)
                        )
                    image_ctx.arc(WIDTH/2, HEIGHT/2, RADIUS, angle_start,
                        angle_end)
                    image_ctx.close_path()
                    image_ctx.fill()
            context.set_source_surface(image)
            context.paint()


        # Escala la imagen al tamaño de la superficie
        # WIDTH/HEIGHT corresponden al tamaño de los graficos
        rect = self.get_allocation()
        context.scale(
            float(rect.width) / WIDTH,
            float(rect.height) / HEIGHT
            )

        # Dibuja el fondo
        context.set_source_surface(self.image_bg)
        context.paint()

        # Dibuja el frente 
        mask_image(self.image_fg)

        # Dibuja la rejilla
        draw_grid()


    def _select(self, ux, uy):
        """Selecciona un trozo de la torta. Devuelve True si un objeto fue
        seleccionado, False en caso contrario."""
        # Transformamos las coordenadas del usuario a coordenadas reales (0-1)
        rect = self.get_allocation()
        wx = float(ux - rect.x) / float(rect.width)
        wy = float(uy - rect.y) / float(rect.height)
        # Centramos
        wx -= 0.5
        wy -= 0.5
        if math.pow(wx, 2) + math.pow(wy, 2) > math.pow(WRADIUS, 2):
            return False
        angle = math.atan2(wy, wx)
        if angle < 0:
            angle += 2 * math.pi
        sector = angle * self.subdivisions / (2 * math.pi)
        index = int(math.floor(sector))
        self.selected_list[index] = not self.selected_list[index]
        return True


if __name__ == "__main__":
    # Probamos el nuevo widget Cake
    window = gtk.Window()
    try:
        cake = Cake(int(sys.argv[1]))
    except IndexError:
        cake = Cake(6)
    window.add(cake)
    window.connect("destroy", gtk.main_quit)
    window.show_all()
    gtk.main()