Refactor Fremantule UI code and implement logging
[mevemon] / package / src / ui / fremantle / characterSheet.py
1 #
2 # mEveMon - A character monitor for EVE Online
3 # Copyright (c) 2010  Ryan and Danny Campbell, and the mEveMon Team
4 #
5 # mEveMon is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # mEveMon is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 import time
19
20 import hildon
21 import gtk
22 import gobject
23
24 import util
25 import ui.models as models
26 from menu import Menu
27
28 class CharacterSheetUI:
29     UPDATE_INTERVAL = 1
30
31     def __init__(self, controller, char_name, uid):
32         self.controller = controller
33         self.char_name = char_name
34         self.uid = uid
35         self.sheet = None
36         self.char_id = None
37         self.skills_model = None
38         
39         self.build_window()
40
41
42     def build_window(self):
43         # TODO: this is a really long and ugly function, split it up somehow
44
45         self.win = hildon.StackableWindow()
46         hildon.hildon_gtk_window_set_progress_indicator(self.win, 1)
47         self.win.show_all() 
48
49         # Create menu
50         # NOTE: we probably want a window-specific menu for this page, but the
51         # main appmenu works for now
52         menu = Menu(self.win, self.controller)
53         # Attach menu to the window
54         self.win.set_app_menu(menu)
55
56         pannable_area = hildon.PannableArea()
57
58         self.char_id = self.controller.char_name2id(self.char_name)
59
60         self.sheet = self.controller.get_char_sheet(self.uid, self.char_id)
61
62         self.win.set_title(self.char_name)
63
64
65         hbox = gtk.HBox(False, 0)
66         info_vbox = gtk.VBox(False, 0)
67
68         portrait = gtk.Image()
69         portrait.set_from_file(self.controller.get_portrait(self.char_name, 256))
70         portrait.show()
71
72         hbox.pack_start(portrait, False, False, 10)
73         hbox.pack_start(info_vbox, False, False, 5)
74
75         vbox = gtk.VBox(False, 0)
76         pannable_area.add_with_viewport(vbox)
77
78         vbox.pack_start(hbox, False, False, 0)
79
80         self.fill_info(info_vbox)
81         self.fill_stats(info_vbox)
82
83         separator = gtk.HSeparator()
84         vbox.pack_start(separator, False, False, 5)
85         separator.show()
86
87         
88         self.add_label("<big>Skill in Training:</big>", vbox, align='normal')
89
90         self.display_skill_in_training(vbox)
91
92         separator = gtk.HSeparator()
93         vbox.pack_start(separator, False, False, 0)
94         separator.show()
95         
96         self.add_label("<big>Skills:</big>", vbox, align='normal')
97
98         skills_treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
99         self.skills_model = models.CharacterSkillsModel(self.controller, self.char_id)
100         skills_treeview.set_model(self.skills_model)
101         self.add_columns_to_skills_view(skills_treeview)
102         vbox.pack_start(skills_treeview, False, False, 1)
103
104         self.win.add(pannable_area)
105         self.win.show_all()
106
107         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
108         
109         # if we start the timer too early, get_is_topmost() returns False
110         self.timer = gobject.timeout_add_seconds(self.UPDATE_INTERVAL, self.update_live_sp)
111
112         self.win.connect("destroy", self.back)
113
114     def back(self, widget):
115         gobject.source_remove(self.timer)
116         gtk.Window.destroy(self.win)
117
118
119     def display_skill_in_training(self, vbox):
120         skill = self.controller.get_skill_in_training(self.uid, self.char_id)
121         
122         if skill.skillInTraining:
123
124             skilltree = self.controller.get_skill_tree()
125             
126             # I'm assuming that we will always find a skill with the skill ID
127             for group in skilltree.skillGroups:
128                 found_skill = group.skills.Get(skill.trainingTypeID, False)
129                 if found_skill:
130                     skill_name = found_skill.typeName
131                     break
132                 
133             self.add_label("%s <small>(Level %d)</small>" % (skill_name, skill.trainingToLevel),
134                     vbox, align='normal')
135             self.add_label("<small>start time: %s\t\tend time: %s</small>" 
136                     %(time.ctime(skill.trainingStartTime),
137                 time.ctime(skill.trainingEndTime)), vbox, align='normal')
138
139             progressbar = gtk.ProgressBar()
140             fraction_completed = (time.time() - skill.trainingStartTime) / \
141                     (skill.trainingEndTime - skill.trainingStartTime)
142
143             progressbar.set_fraction(fraction_completed)
144             align = gtk.Alignment(0.5, 0.5, 0.5, 0)
145             vbox.pack_start(align, False, False, 5)
146             align.show()
147             align.add(progressbar)
148             progressbar.show()
149         else:
150             self.add_label("<small>No skills are currently being trained</small>",
151                     vbox, align='normal')
152
153
154
155     def fill_info(self, box):
156         self.add_label("<big><big>%s</big></big>" % self.sheet.name, box)
157         self.add_label("<small>%s %s %s</small>" % (self.sheet.gender, 
158             self.sheet.race, self.sheet.bloodLine), box)
159         self.add_label("", box, markup=False)
160         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
161         self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
162                 util.comma(self.sheet.balance), box)
163
164         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
165         self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
166                 util.comma(int(self.live_sp_val)), box)
167         
168         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
169
170
171     def fill_stats(self, box):
172
173         atr = self.sheet.attributes
174
175         self.add_label("<small><b>I: </b>%d  <b>M: </b>%d  <b>C: </b>%d  " \
176                 "<b>P: </b>%d  <b>W: </b>%d</small>" % (atr.intelligence,
177                     atr.memory, atr.charisma, atr.perception, atr.willpower), box)
178
179
180     def add_columns_to_skills_view(self, treeview):
181         #Column 0 for the treeview
182         renderer = gtk.CellRendererText()
183         column = gtk.TreeViewColumn('Skill Name', renderer, 
184                 markup=models.CharacterSkillsModel.C_NAME)
185         column.set_property("expand", True)
186         treeview.append_column(column)
187         
188         #Column 1 for the treeview
189         column = gtk.TreeViewColumn('Rank', renderer, 
190                 markup=models.CharacterSkillsModel.C_RANK)
191         column.set_property("expand", True)
192         treeview.append_column(column)
193
194         #Column 2
195         column = gtk.TreeViewColumn('Points', renderer,
196                 markup=models.CharacterSkillsModel.C_SKILLPOINTS)
197         column.set_property("expand", True)
198         treeview.append_column(column)
199
200         #Column 3
201         column = gtk.TreeViewColumn('Level', renderer, 
202                 markup=models.CharacterSkillsModel.C_LEVEL)
203         column.set_property("expand", True)
204         treeview.append_column(column)
205
206
207     def refresh_clicked(self, button):
208         hildon.hildon_gtk_window_set_progress_indicator(self.win, 1)
209         self.skills_model.get_skills()
210         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
211
212
213     def update_live_sp(self):
214         self.live_sp_val = self.live_sp_val + self.spps * self.UPDATE_INTERVAL
215         self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
216                                 util.comma(int(self.live_sp_val)))
217
218         return True
219     
220     def add_label(self, text, box, markup=True, align='left'):
221         label = gtk.Label(text)
222         if markup:
223             label.set_use_markup(True)
224         if align == 'left':
225             label.set_alignment(0, 0.5)
226            
227         box.pack_start(label, False, False)
228
229         return label