Package horizons :: Package gui :: Package widgets :: Module statswidget
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.widgets.statswidget

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  from horizons.constants import GAME_SPEED, PLAYER 
23  from horizons.extscheduler import ExtScheduler 
24  from horizons.gui.util import load_uh_widget 
25  from horizons.util.python.callback import Callback 
26   
27   
28 -class StatsWidget:
29 """A widget that creates a large table with statistics.""" 30 31 # name of the widget's XML file 32 widget_file_name = None # type: str 33
34 - def __init__(self, session, center_widget=False):
35 super().__init__() 36 self.session = session 37 self.center_widget = center_widget 38 self._initialized = False 39 self._hiding_widget = False # True if and only if the widget is currently in the process of being hidden
40
41 - def refresh(self):
42 self._clear_entries()
43
44 - def _refresh_tick(self):
45 if self._initialized and self.is_visible(): 46 self.refresh()
47
48 - def show(self):
49 run_in = PLAYER.STATS_UPDATE_FREQUENCY / GAME_SPEED.TICKS_PER_SECOND 50 ExtScheduler().add_new_object(Callback(self._refresh_tick), 51 self, run_in=run_in, loops=-1) 52 if not self._initialized: 53 self._initialized = True 54 self._init_gui() 55 self.refresh() 56 self._gui.show()
57
58 - def hide(self):
59 ExtScheduler().rem_all_classinst_calls(self) 60 if not self._initialized: 61 return # can happen if the logbook calls hide on all statswidgets 62 if not self._hiding_widget: 63 self._hiding_widget = True 64 self._gui.hide() 65 self._hiding_widget = False
66
67 - def is_visible(self):
68 if not self._initialized: 69 return False 70 return self._gui.isVisible()
71
72 - def toggle_visibility(self):
73 if self.is_visible(): 74 self.hide() 75 else: 76 self.show() 77 self.refresh()
78
79 - def _init_gui(self):
80 self._gui = load_uh_widget(self.widget_file_name, center_widget=self.center_widget) 81 if not self.center_widget: 82 self._gui.position_technique = 'center+20:center+25' 83 self._content_vbox = self._gui.findChild(name='content_vbox') 84 self.refresh()
85
86 - def _clear_entries(self):
87 self._content_vbox.removeAllChildren()
88