2fe9e5645c6d2dbae0c6f63270aa30491a86e363
[mevemon] / package / src / ui / diablo / gui.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
19 import sys, time
20
21 import gtk
22 import hildon
23 import gobject
24
25 import glib
26
27 from ui import models
28 import validation
29
30 class BaseUI():
31     menu_items = ("Settings", "About", "Refresh")
32
33     def create_menu(self, window):
34         menu = gtk.Menu()
35
36         for command in self.menu_items:
37             # Create menu entries
38             button = gtk.MenuItem(command)
39
40             if command == "About":
41                 button.connect("activate", self.about_clicked)
42             elif command == "Settings":
43                 button.connect("activate", self.settings_clicked, window)
44             elif command == "Refresh":
45                 button.connect("activate", self.refresh_clicked, window)
46             else:
47                 assert False, command
48
49             # Add entry to the view menu
50             menu.append(button)
51
52         menu.show_all()
53
54         return menu
55
56     def settings_clicked(self, button, window):
57
58         RESPONSE_NEW, RESPONSE_EDIT, RESPONSE_DELETE = range(3)
59
60         dialog = gtk.Dialog()
61         dialog.set_transient_for(window)
62         dialog.set_title("Settings")
63
64
65
66         vbox = dialog.vbox
67
68         acctsLabel = gtk.Label("Accounts:")
69         acctsLabel.set_justify(gtk.JUSTIFY_LEFT)
70
71         vbox.pack_start(acctsLabel, False, False, 1)
72
73         self.accounts_model = models.AccountsModel(self.controller)
74
75         accounts_treeview = gtk.TreeView(model = self.accounts_model)
76         self.add_columns_to_accounts(accounts_treeview)
77         vbox.pack_start(accounts_treeview, False, False, 1)
78
79         # all stock responses are negative, so we can use any positive value
80         new_button = dialog.add_button("New", RESPONSE_NEW)
81         #TODO: get edit button working
82         #edit_button = dialog.add_button("Edit", RESPONSE_EDIT)
83         delete_button = dialog.add_button("Delete", RESPONSE_DELETE)
84         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
85         cancel_button = dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
86
87         #TODO: for some reason the scrollbar shows up in the middle of the
88         # dialog. Why?
89         #scrollbar = gtk.VScrollbar()
90         
91         dialog.show_all()
92
93         result = dialog.run()
94
95         while(result != gtk.RESPONSE_CANCEL):
96             if result == RESPONSE_NEW:
97                 self.new_account_clicked(window)
98             #elif result == RESPONSE_EDIT:
99             #    # get the selected treeview item and pop up the account_box
100             #    self.edit_account(accounts_treeview)
101             elif result == RESPONSE_DELETE:
102                 # get the selected treeview item, and delete the gconf keys
103                 self.delete_account(accounts_treeview)
104             elif result == gtk.RESPONSE_OK:
105                 self.char_model.get_characters()
106                 break
107         
108             result = dialog.run()
109
110         dialog.destroy()
111
112
113
114     def get_selected_item(self, treeview, column):
115         selection = treeview.get_selection()
116         model, miter = selection.get_selected()
117
118         value = model.get_value(miter, column)
119
120         return value
121
122     def edit_account(self, treeview):
123         uid = self.get_selected_item(treeview, 0)
124         # pop up the account dialog
125
126         self.accounts_model.get_accounts()
127
128     def delete_account(self, treeview):
129         uid = self.get_selected_item(treeview, 0)
130         self.controller.remove_account(uid)
131         # refresh model
132         self.accounts_model.get_accounts()
133
134
135     def add_columns_to_accounts(self, treeview):
136         #Column 0 for the treeview
137         renderer = gtk.CellRendererText()
138         column = gtk.TreeViewColumn('User ID', renderer, 
139                 text=models.AccountsModel.C_UID)
140         column.set_property("expand", True)
141         treeview.append_column(column)
142
143         #Column 2 (characters) for the treeview
144         column = gtk.TreeViewColumn('Characters', renderer, 
145                 markup=models.AccountsModel.C_CHARS)
146         column.set_property("expand", True)
147         treeview.append_column(column)
148
149
150     def new_account_clicked(self, window):
151         dialog = gtk.Dialog()
152     
153         #get the vbox to pack all the settings into
154         vbox = dialog.vbox
155     
156         dialog.set_transient_for(window)
157         dialog.set_title("New Account")
158
159         uidLabel = gtk.Label("User ID:")
160         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
161         vbox.add(uidLabel)
162         
163         uidEntry = gtk.Entry()
164         uidEntry.set_property('is_focus', False)
165         
166         vbox.add(uidEntry)
167
168         apiLabel = gtk.Label("API key:")
169         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
170         vbox.add(apiLabel)
171         
172         apiEntry = gtk.Entry()
173         apiEntry.set_property('is_focus', False)
174
175         vbox.add(apiEntry)
176        
177         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
178
179         dialog.show_all()
180         result = dialog.run()
181         
182         valid_credentials = False
183
184         while not valid_credentials:
185             if result == gtk.RESPONSE_OK:
186                 uid = uidEntry.get_text()
187                 api_key = apiEntry.get_text()
188             
189                 try:
190                     validation.uid(uid)
191                     validation.api_key(api_key)
192                 except validation.ValidationError, e:
193                     self.report_error(e.message)
194                     result = dialog.run()
195                 else:
196                     valid_credentials = True
197                     self.controller.add_account(uid, api_key)
198                     self.accounts_model.get_accounts()
199             else:
200                 break
201
202         dialog.destroy()
203
204
205     def report_error(self, error):
206         hildon.hildon_banner_show_information(self.win, '', error)
207     
208     def about_clicked(self, button):
209         dialog = gtk.AboutDialog()
210         dialog.set_website(self.controller.about_website)
211         dialog.set_website_label(self.controller.about_website)
212         dialog.set_name(self.controller.about_name)
213         dialog.set_authors(self.controller.about_authors)
214         dialog.set_comments(self.controller.about_text)
215         dialog.set_version(self.controller.app_version)
216         dialog.run()
217         dialog.destroy()
218
219     def add_label(self, text, box, markup=True, align="left", padding=1):
220         label = gtk.Label(text)
221         if markup:
222             label.set_use_markup(True)
223         if align == "left":
224             label.set_alignment(0, 0.5)
225
226         box.pack_start(label, False, False, padding)
227
228         return label
229
230 class mEveMonUI(BaseUI):
231
232     def __init__(self, controller):
233         self.controller = controller
234         gtk.set_application_name("mEveMon")
235
236         # create the main window
237         self.win = hildon.Window()
238         self.win.connect("destroy", self.controller.quit)
239         self.win.show_all()
240         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
241         progress_bar.set_fraction(0.4)
242
243         # Create menu
244         menu = self.create_menu(self.win)
245
246         # Attach menu to the window
247         self.win.set_menu(menu)
248
249         character_win = CharacterSheetUI(self.controller)
250
251         # create the treeview --danny
252         self.char_model = models.CharacterListModel(self.controller)
253         treeview = gtk.TreeView(model = self.char_model)
254         treeview.connect('row-activated', character_win.build_window)
255         treeview.set_model(self.char_model)
256         self.add_columns_to_treeview(treeview)
257
258         # add the treeview with scrollbar --danny
259         self.win.add_with_scrollbar(treeview)
260         self.win.show_all()
261
262         progress_bar.set_fraction(1)
263         progress_bar.destroy()
264
265     def add_columns_to_treeview(self, treeview):
266         #Column 0 for the treeview
267         renderer = gtk.CellRendererPixbuf()
268         column = gtk.TreeViewColumn()
269         column.pack_start(renderer, True)
270         column.add_attribute(renderer, "pixbuf", 
271                 models.CharacterListModel.C_PORTRAIT)
272         treeview.append_column(column)
273
274         #Column 1 for the treeview
275         renderer = gtk.CellRendererText()
276         column = gtk.TreeViewColumn('Character Name', renderer, 
277                 text=models.CharacterListModel.C_NAME)
278         column.set_property("expand", True)
279         treeview.append_column(column)
280  
281     def refresh_clicked(self, button):
282         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading characters...")
283         progress_bar.set_fraction(1)
284         self.char_model.get_characters()
285         progress_bar.destroy()
286
287 class CharacterSheetUI(BaseUI):
288     UPDATE_INTERVAL = 1
289
290     def __init__(self, controller):
291         self.controller = controller
292         self.sheet = None
293         self.char_id = None
294         self.skills_model = None
295
296
297     def build_window(self, treeview, path, view_column):
298         # TODO: this is a really long and ugly function, split it up somehow
299
300         self.win = hildon.Window()
301
302         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading character sheet...")
303
304         self.win.show_all() 
305         progress_bar.set_fraction(0.4)
306
307         # Create menu
308         # NOTE: we probably want a window-specific menu for this page, but the
309         # main appmenu works for now
310         menu = self.create_menu(self.win)
311         # Attach menu to the window
312         self.win.set_menu(menu)
313
314         model = treeview.get_model()
315         miter = model.get_iter(path)
316         
317         # column 0 is the portrait, column 1 is name
318         char_name = model.get_value(miter, 1)
319         self.uid = model.get_value(miter, 2)
320         self.char_id = self.controller.char_name2id(char_name)
321
322         self.sheet = self.controller.get_char_sheet(self.uid, self.char_id)
323
324         self.win.set_title(char_name)
325
326
327         hbox = gtk.HBox(False, 0)
328         info_vbox = gtk.VBox(False, 0)
329
330         portrait = gtk.Image()
331         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
332         portrait.show()
333
334         hbox.pack_start(portrait, False, False, 10)
335         hbox.pack_start(info_vbox, False, False, 5)
336
337         vbox = gtk.VBox(False, 0)
338
339         vbox.pack_start(hbox, False, False, 0)
340
341         self.fill_info(info_vbox)
342         self.fill_stats(info_vbox)
343
344         separator = gtk.HSeparator()
345         vbox.pack_start(separator, False, False, 5)
346         separator.show()
347
348         
349         self.add_label("<big>Skill in Training:</big>", vbox, align="normal")
350
351         self.display_skill_in_training(vbox)
352
353         separator = gtk.HSeparator()
354         vbox.pack_start(separator, False, False, 0)
355         separator.show()
356         
357         self.add_label("<big>Skills:</big>", vbox, align="normal")
358
359
360         self.skills_model = models.CharacterSkillsModel(self.controller, self.char_id)
361         skills_treeview = gtk.TreeView(model = skills_model)
362         skills_treeview.set_model(self.skills_model)
363         self.add_columns_to_skills_view(skills_treeview)
364
365         vbox.pack_start(skills_treeview, False, False, 0)
366
367         self.win.add_with_scrollbar(vbox)
368         self.win.show_all()
369
370         progress_bar.set_fraction(1)
371         progress_bar.destroy()
372
373     def display_skill_in_training(self, vbox):
374         skill = self.controller.get_skill_in_training(self.uid, self.char_id)
375         
376         if skill.skillInTraining:
377
378             skilltree = self.controller.get_skill_tree()
379             
380             # I'm assuming that we will always find a skill with the skill ID
381             for group in skilltree.skillGroups:
382                 found_skill = group.skills.Get(skill.trainingTypeID, False)
383                 if found_skill:
384                     skill_name = found_skill.typeName
385                     break
386                 
387             self.add_label("%s <small>(Level %d)</small>" % (skill_name, skill.trainingToLevel),
388                     vbox, align="normal")
389             self.add_label("<small>start time: %s\t\tend time: %s</small>" 
390                     %(time.ctime(skill.trainingStartTime),
391                 time.ctime(skill.trainingEndTime)), vbox, align="normal")
392
393             progressbar = gtk.ProgressBar()
394             fraction_completed = (time.time() - skill.trainingStartTime) / \
395                     (skill.trainingEndTime - skill.trainingStartTime)
396
397             progressbar.set_fraction(fraction_completed)
398             align = gtk.Alignment(0.5, 0.5, 0.5, 0)
399             vbox.pack_start(align, False, False, 5)
400             align.show()
401             align.add(progressbar)
402             progressbar.show()
403         else:
404             self.add_label("<small>No skills are currently being trained</small>",
405                     vbox, align="normal")
406
407
408
409     def fill_info(self, box):
410         self.add_label("<big><big>%s</big></big>" % self.sheet.name, box)
411         self.add_label("<small>%s %s %s</small>" % (self.sheet.gender, 
412             self.sheet.race, self.sheet.bloodLine), box)
413         self.add_label("", box, markup=False)
414         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
415         self.add_label("<small><b>Balance:</b> %s ISK</small>" % self.sheet.balance, box)
416
417         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
418         self.live_sp = self.add_label("<small><b>Total SP:</b> %d</small>" %
419                 self.live_sp_val, box)
420         
421         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
422
423         glib.timeout_add_seconds(self.UPDATE_INTERVAL, self.update_live_sp)
424
425     def fill_stats(self, box):
426
427         atr = self.sheet.attributes
428
429         self.add_label("<small><b>I: </b>%d  <b>M: </b>%d  <b>C: </b>%d  " \
430                 "<b>P: </b>%d  <b>W: </b>%d</small>" % (atr.intelligence,
431                     atr.memory, atr.charisma, atr.perception, atr.willpower), box)
432
433
434     def add_columns_to_skills_view(self, treeview):
435         #Column 0 for the treeview
436         renderer = gtk.CellRendererText()
437         column = gtk.TreeViewColumn('Skill Name', renderer, 
438                 markup=models.CharacterSkillsModel.C_NAME)
439         column.set_property("expand", True)
440         treeview.append_column(column)
441         
442         #Column 1 for the treeview
443         column = gtk.TreeViewColumn('Rank', renderer, 
444                 markup=models.CharacterSkillsModel.C_RANK)
445         column.set_property("expand", True)
446         treeview.append_column(column)
447
448         #Column 2
449         column = gtk.TreeViewColumn('Points', renderer,
450                 markup=models.CharacterSkillsModel.C_SKILLPOINTS)
451         column.set_property("expand", True)
452         treeview.append_column(column)
453
454         #Column 3
455         column = gtk.TreeViewColumn('Level', renderer, 
456                 markup=models.CharacterSkillsModel.C_LEVEL)
457         column.set_property("expand", True)
458         treeview.append_column(column)
459
460
461     def refresh_clicked(self, button):
462         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
463         progress_bar.set_fraction(1)
464         self.skills_model.get_skills()
465         progress_bar.destroy()
466
467
468     def update_live_sp(self):
469         # we don't want to keep the timer running in the background
470         # when this callback returns False, the timer destorys itself
471        
472         # TODO: figure out why this doesn't work on the real device
473         #
474         #if not self.win.get_is_topmost():
475         #    return False
476         
477         self.live_sp_val = self.live_sp_val + self.spps * self.UPDATE_INTERVAL
478         self.live_sp.set_label("<small><b>Total SP:</b> %d</small>" %
479                                 self.live_sp_val)
480
481         return True
482
483
484 if __name__ == "__main__":
485     main()
486