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