comparison sat_frontends/primitivus/progress.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/primitivus/progress.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # Primitivus: a SAT frontend
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 import urwid
22 from urwid_satext import sat_widgets
23 from sat_frontends.quick_frontend import quick_widgets
24
25
26 class Progress(urwid.WidgetWrap, quick_widgets.QuickWidget):
27 PROFILES_ALLOW_NONE = True
28
29 def __init__(self, host, target, profiles):
30 assert target is None and profiles is None
31 quick_widgets.QuickWidget.__init__(self, host, target)
32 self.host = host
33 self.progress_list = urwid.SimpleListWalker([])
34 self.progress_dict = {}
35 listbox = urwid.ListBox(self.progress_list)
36 buttons = []
37 buttons.append(sat_widgets.CustomButton(_('Clear progress list'), self._onClear))
38 max_len = max([button.getSize() for button in buttons])
39 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
40 main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
41 urwid.WidgetWrap.__init__(self, main_wid)
42
43 def add(self, progress_id, message, profile):
44 mess_wid = urwid.Text(message)
45 progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete')
46 column = urwid.Columns([mess_wid, progr_wid])
47 self.progress_dict[(progress_id, profile)] = {'full':column,'progress':progr_wid,'state':'init'}
48 self.progress_list.append(column)
49 self.progressCB(self.host.loop, (progress_id, message, profile))
50
51 def progressCB(self, loop, data):
52 progress_id, message, profile = data
53 data = self.host.bridge.progressGet(progress_id, profile)
54 pbar = self.progress_dict[(progress_id, profile)]['progress']
55 if data:
56 if self.progress_dict[(progress_id, profile)]['state'] == 'init':
57 #first answer, we must construct the bar
58 self.progress_dict[(progress_id, profile)]['state'] = 'progress'
59 pbar.done = float(data['size'])
60
61 pbar.set_completion(float(data['position']))
62 self.updateNotBar()
63 else:
64 if self.progress_dict[(progress_id, profile)]['state'] == 'progress':
65 self.progress_dict[(progress_id, profile)]['state'] = 'done'
66 pbar.set_completion(pbar.done)
67 self.updateNotBar()
68 return
69
70 loop.set_alarm_in(0.2,self.progressCB, (progress_id, message, profile))
71
72 def _removeBar(self, progress_id, profile):
73 wid = self.progress_dict[(progress_id, profile)]['full']
74 self.progress_list.remove(wid)
75 del(self.progress_dict[(progress_id, profile)])
76
77 def _onClear(self, button):
78 to_remove = []
79 for progress_id, profile in self.progress_dict:
80 if self.progress_dict[(progress_id, profile)]['state'] == 'done':
81 to_remove.append((progress_id, profile))
82 for progress_id, profile in to_remove:
83 self._removeBar(progress_id, profile)
84 self.updateNotBar()
85
86 def updateNotBar(self):
87 if not self.progress_dict:
88 self.host.setProgress(None)
89 return
90 progress = 0
91 nb_bars = 0
92 for progress_id, profile in self.progress_dict:
93 pbar = self.progress_dict[(progress_id, profile)]['progress']
94 progress += pbar.current/pbar.done*100
95 nb_bars+=1
96 av_progress = progress/float(nb_bars)
97 self.host.setProgress(av_progress)
98