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

Source Code for Module horizons.gui.widgets.playerssettlements

  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 fife.extensions.pychan import widgets 
 23   
 24  from horizons.component.namedcomponent import NamedComponent 
 25  from horizons.gui.widgets.statswidget import StatsWidget 
 26  from horizons.i18n import gettext as T 
 27  from horizons.util.python.callback import Callback 
 28   
 29   
30 -class PlayersSettlements(StatsWidget):
31 """Widget that shows a list of the player's settlements.""" 32 33 widget_file_name = 'players_settlements.xml' 34
35 - def __init__(self, session):
36 super().__init__(session)
37
38 - def refresh(self):
39 super().refresh() 40 self._gui.findChild(name='headline').text = T("Settlements of {player}").format(player=self.session.world.player.name) 41 42 sequence_number = 0 43 events = {} 44 for settlement in sorted(self.session.world.settlements, key=lambda settlement: (settlement.get_component(NamedComponent).name, settlement.worldid)): 45 if settlement.owner is self.session.world.player: 46 sequence_number += 1 47 name_label, rename_icon = self._add_line_to_gui(settlement, sequence_number) 48 events['{}/mouseClicked'.format(name_label.name)] = Callback(self._go_to_settlement, settlement) 49 cb = Callback(self.session.ingame_gui.show_change_name_dialog, settlement) 50 events['{}/mouseClicked'.format(rename_icon.name)] = cb 51 self._gui.mapEvents(events) 52 self._add_summary_line_to_gui() 53 self._content_vbox.adaptLayout()
54
55 - def _go_to_settlement(self, settlement):
58
59 - def _add_generic_line_to_gui(self, id, line_prefix, people, tax, costs):
60 inhabitants = widgets.Label(name='inhabitants_{:d}'.format(id)) 61 inhabitants.text = str(people) 62 inhabitants.min_size = inhabitants.max_size = (110, 20) 63 64 taxes = widgets.Label(name='taxes_{:d}'.format(id)) 65 taxes.text = str(tax) 66 taxes.min_size = taxes.max_size = (50, 20) 67 68 running_costs = widgets.Label(name='running_costs_{:d}'.format(id)) 69 running_costs.text = str(costs) 70 running_costs.min_size = running_costs.max_size = (100, 20) 71 72 balance = widgets.Label(name='balance_{:d}'.format(id)) 73 balance.text = str(tax - costs) 74 balance.min_size = balance.max_size = (60, 20) 75 76 hbox = widgets.HBox() 77 for widget in line_prefix: 78 hbox.addChild(widget) 79 hbox.addChild(inhabitants) 80 hbox.addChild(taxes) 81 hbox.addChild(running_costs) 82 hbox.addChild(balance) 83 self._content_vbox.addChild(hbox)
84
85 - def _add_line_to_gui(self, settlement, sequence_number):
86 sequence_number_label = widgets.Label(name='sequence_number_{:d}'.format(settlement.worldid)) 87 sequence_number_label.text = str(sequence_number) 88 sequence_number_label.min_size = sequence_number_label.max_size = (15, 20) 89 90 name = widgets.Label(name='name_{:d}'.format(settlement.worldid)) 91 name.text = settlement.get_component(NamedComponent).name 92 name.min_size = name.max_size = (175, 20) 93 94 from horizons.engine.pychan_util import RenameImageButton 95 rename_icon = RenameImageButton(name='rename_{:d}'.format(settlement.worldid)) 96 rename_icon.path = "images/background/rename_feather_20" 97 rename_icon.helptext = T("Click to change the name of your settlement") 98 rename_icon.max_size = (20, 20) # (width, height) 99 100 self._add_generic_line_to_gui(settlement.worldid, [sequence_number_label, name, rename_icon], 101 settlement.inhabitants, settlement.cumulative_taxes, settlement.cumulative_running_costs) 102 return name, rename_icon
103
104 - def _add_summary_line_to_gui(self):
105 people = 0 106 tax = 0 107 costs = 0 108 for settlement in self.session.world.settlements: 109 if settlement.owner is self.session.world.player: 110 people += settlement.inhabitants 111 tax += settlement.cumulative_taxes 112 costs += settlement.cumulative_running_costs 113 114 sequence_number_label = widgets.Label(name='sequence_number_total') 115 sequence_number_label.min_size = sequence_number_label.max_size = (15, 20) 116 117 name = widgets.Label(name='name_total') 118 name.text = T('Total') 119 name.min_size = name.max_size = (200, 20) 120 121 self._add_generic_line_to_gui(0, [sequence_number_label, name], people, tax, costs)
122