comparison frontends/src/primitivus/progress.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents frontends/primitivus/progress.py@3198bfd66daa
children fd9b7834d98a
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Primitivus: a SAT frontend
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 import urwid
23 from urwid_satext import sat_widgets
24 from tools.jid import JID
25
26
27 class Progress(urwid.WidgetWrap):
28
29 def __init__(self, host):
30 self.host = host
31 self.progress_list = urwid.SimpleListWalker([])
32 self.progress_dict = {}
33 listbox = urwid.ListBox(self.progress_list)
34 buttons = []
35 buttons.append(sat_widgets.CustomButton(_('Clear progress list'), self.__onClear))
36 max_len = max([button.getSize() for button in buttons])
37 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
38 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
39 urwid.WidgetWrap.__init__(self, main_wid)
40
41 def addProgress(self, id, message):
42 mess_wid = urwid.Text(message)
43 progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete')
44 column = urwid.Columns([mess_wid, progr_wid])
45 self.progress_dict[id] = {'full':column,'progress':progr_wid,'state':'init'}
46 self.progress_list.append(column)
47 self.progressCB(self.host.loop, (id, message))
48
49 def progressCB(self, loop, data):
50 id, message = data
51 data = self.host.bridge.getProgress(id)
52 pbar = self.progress_dict[id]['progress']
53 if data:
54 if self.progress_dict[id]['state'] == 'init':
55 #first answer, we must construct the bar
56 self.progress_dict[id]['state'] = 'progress'
57 pbar.done = float(data['size'])
58
59 pbar.set_completion(float(data['position']))
60 self.updateNotBar()
61 else:
62 if self.progress_dict[id]['state'] == 'progress':
63 self.progress_dict[id]['state'] = 'done'
64 pbar.set_completion(pbar.done)
65 self.updateNotBar()
66 return
67
68 loop.set_alarm_in(1,self.progressCB, (id, message))
69
70 def __removeBar(self, id):
71 wid = self.progress_dict[id]['full']
72 self.progress_list.remove(wid)
73 del(self.progress_dict[id])
74
75 def __onClear(self, button):
76 to_remove = []
77 for id in self.progress_dict:
78 if self.progress_dict[id]['state'] == 'done':
79 to_remove.append(id)
80 for id in to_remove:
81 self.__removeBar(id)
82 self.updateNotBar()
83
84 def updateNotBar(self):
85 if not self.progress_dict:
86 self.host.setProgress(None)
87 return
88 progress = 0
89 nb_bars = 0
90 for id in self.progress_dict:
91 pbar = self.progress_dict[id]['progress']
92 progress += pbar.current/pbar.done*100
93 nb_bars+=1
94 av_progress = progress/float(nb_bars)
95 self.host.setProgress(av_progress)
96