Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit b5d1a3cb3a4a39e9852c0ed2d7a7da7ef653d7d4

Add first version of a object to display the state of the game

Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
stateview.py
(93 / 0)
  
1#!/usr/bin/env python
2# Copyright (C) 2011, One Laptop Per Child
3# Author, Gonzalo Odiard
4# License: LGPLv2
5#
6# The class StateView draw how many questions there are in the game,
7# how many have been read, and how many have been replied.
8
9import gtk
10import cairo
11import rsvg
12import model
13import math
14
15
16class StateView():
17
18 def __init__(self, model, x, y, width, height):
19 self.model = model
20 self._x = x
21 self._y = y
22 self._width = width
23 self._height = height
24 svg = rsvg.Handle(file='./icons/question.svg')
25 self._tmp_image = cairo.ImageSurface(cairo.FORMAT_ARGB32,
26 svg.props.width, svg.props.height)
27 self._tmp_ctx = cairo.Context(self._tmp_image)
28 svg.render_cairo(self._tmp_ctx)
29 self._svg_width = svg.props.width
30
31 def draw(self, ctx):
32 # calculate cell size
33 cant_questions = len(self.model.data['questions'])
34 if cant_questions == 0:
35 return
36 icon_size = int(self._width / cant_questions) # margin
37 print "icon_size", icon_size
38
39 state = self.model.data['state']
40 displayed_questions = state['displayed_questions']
41 replied_questions = state['replied_questions']
42
43 scale = float(self._svg_width) / float(icon_size)
44 print "scale", scale
45 ctx.translate(self._x, self._y)
46 for n in range(cant_questions):
47 if n < replied_questions:
48 radio = icon_size / 2.0
49 ctx.arc(radio, radio, radio, 0., 2 * math.pi)
50 ctx.set_source_rgb(0.913, 0.733, 0.0) # eebb00
51 ctx.fill()
52
53 ctx.scale(1.0 / scale, 1.0 / scale)
54 ctx.set_source_surface(self._tmp_image)
55 if n < displayed_questions:
56 ctx.paint()
57 else:
58 ctx.paint_with_alpha(0.25)
59 ctx.scale(scale, scale)
60 ctx.translate(icon_size, 0)
61
62
63def main():
64 window = gtk.Window()
65 _model = model.GameModel()
66 state_view = StateView(_model, 10, 10, 150, 200)
67 area = gtk.DrawingArea()
68
69 # add fake questions to test
70 _model.data['questions'].append('')
71 _model.data['questions'].append('')
72 _model.data['questions'].append('')
73 _model.data['questions'].append('')
74 _model.data['questions'].append('')
75
76 # ...and a fake state
77 state = {'displayed_questions': 3, 'replied_questions': 2}
78 _model.data['state'] = state
79
80 def expose(widget, event, state_view):
81 ctx = widget.window.cairo_create()
82 state_view.draw(ctx)
83 return False
84
85 area.connect('expose_event', expose, state_view)
86
87 window.add(area)
88 window.connect("destroy", gtk.main_quit)
89 window.show_all()
90 gtk.main()
91
92if __name__ == "__main__":
93 main()