cbc2b4c0fc666234743b82c9e4c82ff9b7a096af
[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                 api_key = apiEntry.get_text()
188             
189                 try:
190                     validation.validate_uid(uid)
191                     validation.validate_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     def run(self):
237         # create the main window
238         self.win = hildon.Window()
239         self.win.connect("destroy", self.controller.quit)
240         self.win.show_all()
241         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
242         progress_bar.set_fraction(0.4)
243
244         # Create menu
245         menu = self.create_menu(self.win)
246
247         # Attach menu to the window
248         self.win.set_menu(menu)
249
250
251         # create the treeview --danny
252         self.char_model = models.CharacterListModel(self.controller)
253         treeview = gtk.TreeView(model = self.char_model)
254         treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL)
255         treeview.connect('row-activated', self.do_charactersheet)
256         treeview.set_model(self.char_model)
257         self.add_columns_to_treeview(treeview)
258
259         # add the treeview with scrollbar --danny
260         self.win.add_with_scrollbar(treeview)
261         self.win.show_all()
262
263         progress_bar.set_fraction(1)
264         progress_bar.destroy()
265
266     def add_columns_to_treeview(self, treeview):
267         #Column 0 for the treeview
268         renderer = gtk.CellRendererPixbuf()
269         column = gtk.TreeViewColumn()
270         column.pack_start(renderer, True)
271         column.add_attribute(renderer, "pixbuf", 
272                 models.CharacterListModel.C_PORTRAIT)
273         treeview.append_column(column)
274
275         #Column 1 for the treeview
276         renderer = gtk.CellRendererText()
277         column = gtk.TreeViewColumn('Character Name', renderer, 
278                 text=models.CharacterListModel.C_NAME)
279         column.set_property("expand", True)
280         treeview.append_column(column)
281  
282     def refresh_clicked(self, button):
283         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading characters...")
284         progress_bar.set_fraction(1)
285         self.char_model.get_characters()
286         progress_bar.destroy()
287
288
289     def do_charactersheet(self, treeview, path, view_column):
290
291         model = treeview.get_model()
292         miter = model.get_iter(path)
293         
294         # column 0 is the portrait, column 1 is name
295         char_name = model.get_value(miter, 1)
296         uid = model.get_value(miter, 2)
297         
298         if uid:
299             CharacterSheetUI(self.controller, char_name, uid)
300         else:
301             pass
302
303 class CharacterSheetUI(BaseUI):
304     #time between live sp updates (in milliseconds)
305     UPDATE_INTERVAL = 1000
306
307     def __init__(self, controller, char_name, uid):
308         self.controller = controller
309         self.char_name = char_name
310         self.uid = uid
311         self.sheet = None
312         self.char_id = None
313         self.skills_model = None
314
315         self.build_window()
316
317
318     def build_window(self):
319         # TODO: this is a really long and ugly function, split it up somehow
320
321         self.win = hildon.Window()
322
323         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading character sheet...")
324
325         self.win.show_all() 
326         progress_bar.set_fraction(0.4)
327
328         # Create menu
329         # NOTE: we probably want a window-specific menu for this page, but the
330         # main appmenu works for now
331         menu = self.create_menu(self.win)
332         # Attach menu to the window
333         self.win.set_menu(menu)
334
335         self.char_id = self.controller.char_name2id(self.char_name)
336
337         self.sheet = self.controller.get_char_sheet(self.uid, self.char_id)
338
339         self.win.set_title(self.char_name)
340
341
342         hbox = gtk.HBox(False, 0)
343         info_vbox = gtk.VBox(False, 0)
344
345         portrait = gtk.Image()
346         portrait.set_from_file(self.controller.get_portrait(self.char_name, 256))
347         portrait.show()
348
349         hbox.pack_start(portrait, False, False, 10)
350         hbox.pack_start(info_vbox, False, False, 5)
351
352         vbox = gtk.VBox(False, 0)
353
354         vbox.pack_start(hbox, False, False, 0)
355
356         self.fill_info(info_vbox)
357         self.fill_stats(info_vbox)
358
359         separator = gtk.HSeparator()
360         vbox.pack_start(separator, False, False, 5)
361         separator.show()
362
363         
364         self.add_label("<big>Skill in Training:</big>", vbox, align="normal")
365
366         self.display_skill_in_training(vbox)
367
368         separator = gtk.HSeparator()
369         vbox.pack_start(separator, False, False, 0)
370         separator.show()
371         
372         self.add_label("<big>Skills:</big>", vbox, align="normal")
373
374
375         self.skills_model = models.CharacterSkillsModel(self.controller, self.char_id)
376         skills_treeview = gtk.TreeView(model=self.skills_model)
377         self.add_columns_to_skills_view(skills_treeview)
378
379         vbox.pack_start(skills_treeview, False, False, 0)
380
381         self.win.add_with_scrollbar(vbox)
382         self.win.show_all()
383
384         progress_bar.set_fraction(1)
385         progress_bar.destroy()
386         
387         # diablo doesnt have a glib module, but gobject module seems to have
388         # the same functions...
389         self.timer = gobject.timeout_add(self.UPDATE_INTERVAL, self.update_live_sp)
390         self.win.connect("destroy", self.back)
391
392     def back(self, widget):
393         gobject.source_remove(self.timer)
394         gtk.Window.destroy(self.win)
395
396     def display_skill_in_training(self, vbox):
397         skill = self.controller.get_skill_in_training(self.uid, self.char_id)
398         
399         if skill.skillInTraining:
400
401             skilltree = self.controller.get_skill_tree()
402             
403             # I'm assuming that we will always find a skill with the skill ID
404             for group in skilltree.skillGroups:
405                 found_skill = group.skills.Get(skill.trainingTypeID, False)
406                 if found_skill:
407                     skill_name = found_skill.typeName
408                     break
409                 
410             self.add_label("%s <small>(Level %d)</small>" % (skill_name, skill.trainingToLevel),
411                     vbox, align="normal")
412             self.add_label("<small>start time: %s\t\tend time: %s</small>" 
413                     %(time.ctime(skill.trainingStartTime),
414                 time.ctime(skill.trainingEndTime)), vbox, align="normal")
415
416             progressbar = gtk.ProgressBar()
417             fraction_completed = (time.time() - skill.trainingStartTime) / \
418                     (skill.trainingEndTime - skill.trainingStartTime)
419
420             progressbar.set_fraction(fraction_completed)
421             align = gtk.Alignment(0.5, 0.5, 0.5, 0)
422             vbox.pack_start(align, False, False, 5)
423             align.show()
424             align.add(progressbar)
425             progressbar.show()
426         else:
427             self.add_label("<small>No skills are currently being trained</small>",
428                     vbox, align="normal")
429
430
431
432     def fill_info(self, box):
433         self.add_label("<big><big>%s</big></big>" % self.sheet.name, box)
434         self.add_label("<small>%s %s %s</small>" % (self.sheet.gender, 
435             self.sheet.race, self.sheet.bloodLine), box)
436         self.add_label("", box, markup=False)
437         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
438         self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
439                 util.comma(self.sheet.balance), box)
440
441         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
442         self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
443                 util.comma(int(self.live_sp_val)), box)
444         
445         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
446
447
448     def fill_stats(self, box):
449
450         atr = self.sheet.attributes
451
452         self.add_label("<small><b>I: </b>%d  <b>M: </b>%d  <b>C: </b>%d  " \
453                 "<b>P: </b>%d  <b>W: </b>%d</small>" % (atr.intelligence,
454                     atr.memory, atr.charisma, atr.perception, atr.willpower), box)
455
456
457     def add_columns_to_skills_view(self, treeview):
458         #Column 0 for the treeview
459         renderer = gtk.CellRendererText()
460         column = gtk.TreeViewColumn('Skill Name', renderer, 
461                 markup=models.CharacterSkillsModel.C_NAME)
462         column.set_property("expand", True)
463         treeview.append_column(column)
464         
465         #Column 1 for the treeview
466         column = gtk.TreeViewColumn('Rank', renderer, 
467                 markup=models.CharacterSkillsModel.C_RANK)
468         column.set_property("expand", True)
469         treeview.append_column(column)
470
471         #Column 2
472         column = gtk.TreeViewColumn('Points', renderer,
473                 markup=models.CharacterSkillsModel.C_SKILLPOINTS)
474         column.set_property("expand", True)
475         treeview.append_column(column)
476
477         #Column 3
478         column = gtk.TreeViewColumn('Level', renderer, 
479                 markup=models.CharacterSkillsModel.C_LEVEL)
480         column.set_property("expand", True)
481         treeview.append_column(column)
482
483
484     def refresh_clicked(self, button):
485         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
486         progress_bar.set_fraction(1)
487         self.skills_model.get_skills()
488         progress_bar.destroy()
489
490
491     def update_live_sp(self):
492         self.live_sp_val = self.live_sp_val + self.spps * (self.UPDATE_INTERVAL / 1000)
493         self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
494                                 util.comma(int(self.live_sp_val)))
495
496         return True
497
498
499 if __name__ == "__main__":
500     main()
501