1 | # -*- coding: utf-8 -*-
|
---|
2 | """
|
---|
3 | pieces_test
|
---|
4 | ~~~~~~~
|
---|
5 |
|
---|
6 |
|
---|
7 | :copyright: © 2011 UfSoft.org - :email:`Pedro Algarvio (pedro@algarvio.me)`
|
---|
8 | :license: BSD, see LICENSE for more details.
|
---|
9 | """
|
---|
10 | import pygtk
|
---|
11 | pygtk.require('2.0')
|
---|
12 | import gtk
|
---|
13 |
|
---|
14 | class PiecesBar(gtk.DrawingArea):
|
---|
15 | # Draw in response to an expose-event
|
---|
16 | __gsignals__ = { "expose-event": "override" }
|
---|
17 |
|
---|
18 | def __init__(self):
|
---|
19 | gtk.DrawingArea.__init__(self)
|
---|
20 | self.width = 0
|
---|
21 | self.height = 0
|
---|
22 | self.pieces = []
|
---|
23 | self.colors = {}
|
---|
24 |
|
---|
25 | self.connect('size-allocate', self.on_size_allocate)
|
---|
26 | self.connect('realize', self.on_realize)
|
---|
27 |
|
---|
28 | self.show()
|
---|
29 |
|
---|
30 | def on_realize(self, widget):
|
---|
31 | map = widget.get_colormap()
|
---|
32 | done_color = map.alloc_color("#4883b5", True, True)
|
---|
33 | down_color = map.alloc_color("#fff600", True, True)
|
---|
34 | wait_color = map.alloc_color("#3dff58", True, True)
|
---|
35 | miss_color = map.alloc_color("#ff0000", True, True)
|
---|
36 | outline_color = map.alloc_color("#888888")
|
---|
37 |
|
---|
38 | self.colors = {
|
---|
39 | 0: (miss_color.red/65535.0, miss_color.green/65535.0, miss_color.blue/65535.0),
|
---|
40 | 1: (wait_color.red/65535.0, wait_color.green/65535.0, wait_color.blue/65535.0),
|
---|
41 | 2: (down_color.red/65535.0, down_color.green/65535.0, down_color.blue/65535.0),
|
---|
42 | 3: (done_color.red/65535.0, done_color.green/65535.0, done_color.blue/65535.0),
|
---|
43 | }
|
---|
44 | self.gc_outline = widget.window.new_gc(foreground=outline_color)
|
---|
45 |
|
---|
46 | def on_size_allocate(self, widget, size):
|
---|
47 | self.width = size.width
|
---|
48 | self.height = size.height
|
---|
49 |
|
---|
50 | # Handle the expose-event by drawing
|
---|
51 | def do_expose_event(self, event=None):
|
---|
52 |
|
---|
53 | num_pieces = len(self.pieces)
|
---|
54 |
|
---|
55 | if num_pieces < 1:
|
---|
56 | self.clear()
|
---|
57 | return None
|
---|
58 |
|
---|
59 | # Create the cairo context
|
---|
60 | cr = self.window.cairo_create()
|
---|
61 | # Restrict Cairo to the exposed area; avoid extra work
|
---|
62 | if event:
|
---|
63 | cr.rectangle(event.area.x, event.area.y,
|
---|
64 | event.area.width, event.area.height)
|
---|
65 | cr.clip()
|
---|
66 |
|
---|
67 | # Fill the background
|
---|
68 | cr.set_source_rgb(136, 136, 136)
|
---|
69 | cr.rectangle(0, 0, self.width-1, self.height-1)
|
---|
70 | cr.fill()
|
---|
71 |
|
---|
72 | width = self.width - 2
|
---|
73 | pieces = self.collapse_pieces()
|
---|
74 |
|
---|
75 | start_pos = 1
|
---|
76 |
|
---|
77 | for state, wpercent in pieces:
|
---|
78 | pwidth = width*wpercent
|
---|
79 | cr.set_source_rgb(*self.colors[state])
|
---|
80 | cr.rectangle(start_pos, 1, pwidth, self.height-2)
|
---|
81 | cr.fill()
|
---|
82 | start_pos += pwidth
|
---|
83 |
|
---|
84 | def collapse_pieces(self):
|
---|
85 | num_pieces = len(self.pieces)*1.0
|
---|
86 | opieces = self.pieces[:]
|
---|
87 | npieces = []
|
---|
88 | v = None
|
---|
89 | n = None
|
---|
90 | while True:
|
---|
91 | if not opieces:
|
---|
92 | break
|
---|
93 | if v is None:
|
---|
94 | v = opieces.pop(0)
|
---|
95 | n = 1
|
---|
96 | else:
|
---|
97 | vv = opieces.pop(0)
|
---|
98 | if v == vv:
|
---|
99 | n += 1
|
---|
100 | else:
|
---|
101 | npieces.append((v, n/num_pieces))
|
---|
102 | v = vv
|
---|
103 | n = 1
|
---|
104 | return npieces
|
---|
105 |
|
---|
106 | def draw_piece(self, piece, start_x, start_y, width, height):
|
---|
107 | self.window.draw_rectangle(
|
---|
108 | self.colors[piece], True, start_x, start_y, width, height
|
---|
109 | )
|
---|
110 |
|
---|
111 | def clear(self):
|
---|
112 | self.pieces = [0]
|
---|
113 | self.update()
|
---|
114 |
|
---|
115 | def get_text(self):
|
---|
116 | return ""
|
---|
117 |
|
---|
118 | def set_text(self, text):
|
---|
119 | pass
|
---|
120 |
|
---|
121 | pieces = PiecesBar()
|
---|
122 | pieces.pieces = [3, 2, 1, 1, 1, 0, 0, 2, 0, 1, 0, 2, 2, 2, 1, 2, 2, 0, 0, 0, 2,
|
---|
123 | 1, 1, 1, 2, 0, 0, 2, 2, 1, 0, 2, 1, 0, 1, 1, 0, 3, 3, 3, 3, 3,
|
---|
124 | 3, 3, 0, 3, 3, 0, 0, 2, 0, 2, 2, 2, 0, 2, 2, 1]*100
|
---|
125 |
|
---|
126 | window = gtk.Window(gtk.WINDOW_TOPLEVEL)
|
---|
127 | window.set_default_size(500, 200)
|
---|
128 | def destroy(widget, data=None):
|
---|
129 | gtk.main_quit()
|
---|
130 | window.connect("destroy", destroy)
|
---|
131 | vbox = gtk.VBox()
|
---|
132 | vbox.add(pieces)
|
---|
133 |
|
---|
134 | hbox1 = gtk.HBox()
|
---|
135 | hbox1.pack_start(gtk.Label("Missing:"), False, False)
|
---|
136 | missing_color = gtk.ColorButton(window.get_colormap().alloc_color("#ff0000", True, True))
|
---|
137 | def on_missing_collor_changed(button):
|
---|
138 | color = button.get_color()
|
---|
139 | r, g, b = color.red, color.green, color.blue
|
---|
140 | pieces.colors[0] = r/65535.0, g/65535.0, b/65535.0
|
---|
141 | print 'Miss color RGB', pieces.colors[0]
|
---|
142 | pieces.do_expose_event()
|
---|
143 | missing_color.connect("color-set", on_missing_collor_changed)
|
---|
144 | hbox1.pack_start(missing_color, False, False)
|
---|
145 | vbox.pack_start(hbox1, False, False)
|
---|
146 |
|
---|
147 | hbox2 = gtk.HBox()
|
---|
148 | hbox2.pack_start(gtk.Label("Waiting:"), False, False)
|
---|
149 | waiting_color = gtk.ColorButton(window.get_colormap().alloc_color("#3dff58", True, True))
|
---|
150 | def on_waiting_collor_changed(button):
|
---|
151 | color = button.get_color()
|
---|
152 | r, g, b = color.red, color.green, color.blue
|
---|
153 | pieces.colors[1] = r/65535.0, g/65535.0, b/65535.0
|
---|
154 | print 'Wait color RGB', pieces.colors[1]
|
---|
155 | pieces.do_expose_event()
|
---|
156 | waiting_color.connect("color-set", on_waiting_collor_changed)
|
---|
157 | hbox2.pack_start(waiting_color, False, False)
|
---|
158 | vbox.pack_start(hbox2, False, False)
|
---|
159 |
|
---|
160 | hbox3 = gtk.HBox()
|
---|
161 | hbox3.pack_start(gtk.Label("Downloading:"), False, False)
|
---|
162 | down_color = gtk.ColorButton(window.get_colormap().alloc_color("#fff600", True, True))
|
---|
163 | def on_down_collor_changed(button):
|
---|
164 | color = button.get_color()
|
---|
165 | r, g, b = color.red, color.green, color.blue
|
---|
166 | pieces.colors[2] = r/65535.0, g/65535.0, b/65535.0
|
---|
167 | print 'Down color RGB', pieces.colors[2]
|
---|
168 | pieces.do_expose_event()
|
---|
169 | down_color.connect("color-set", on_down_collor_changed)
|
---|
170 | hbox3.pack_start(down_color, False, False)
|
---|
171 | vbox.pack_start(hbox3, False, False)
|
---|
172 |
|
---|
173 | hbox4 = gtk.HBox()
|
---|
174 | hbox4.pack_start(gtk.Label("Completed:"), False, False)
|
---|
175 | done_color = gtk.ColorButton(window.get_colormap().alloc_color("#4883b5", True, True))
|
---|
176 | def on_done_collor_changed(button):
|
---|
177 | color = button.get_color()
|
---|
178 | r, g, b = color.red, color.green, color.blue
|
---|
179 | pieces.colors[3] = r/65535.0, g/65535.0, b/65535.0
|
---|
180 | print 'Done color RGB', pieces.colors[3]
|
---|
181 | pieces.do_expose_event()
|
---|
182 | done_color.connect("color-set", on_done_collor_changed)
|
---|
183 | hbox4.pack_start(done_color, False, False)
|
---|
184 | vbox.pack_start(hbox4, False, False)
|
---|
185 |
|
---|
186 | window.add(vbox)
|
---|
187 | window.show_all()
|
---|
188 | gtk.main()
|
---|
189 |
|
---|