d75358ccca1b5c1fab5feb9c43d7ba55bbb86612
[mevemon] / src / ui / diablo / ui.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 # Based on Ry's Fremantle Python code. --danny
20
21 import sys
22
23 import gtk
24 import hildon
25 import gobject
26
27 class mEveMonUI():
28
29     about_name = 'mEveMon'
30     about_text = ('Mobile character monitor for EVE Online')
31     about_authors = ['Ryan Campbell','Danny Campbell']
32     about_website = 'http://mevemon.garage.maemo.org'
33     app_version = '0.1'
34
35     menu_items = ("Settings", "About", "Refresh")
36
37     def __init__(self, controller):
38         self.controller = controller
39    
40         gtk.set_application_name("mEveMon")
41     
42         # create the main window
43         win = hildon.Window()
44         win.connect("destroy", self.controller.quit)
45         win.show_all()
46         progress_bar = hildon.hildon_banner_show_progress(win, None, "Loading overview...")
47         progress_bar.set_fraction( 0.4 )
48
49         # Create menu
50         menu = self.create_menu(win)
51         # Attach menu to the window
52         win.set_menu(menu)
53
54         # will probably need to refer to http://maemo.org/community/maemo-developers/gtktreeview_issue/ for sample code again when we make these clickable --danny
55         self.char_model = self.create_char_model()
56         treeview = gtk.TreeView( model = self.char_model )
57         treeview.connect( 'row-activated', self.build_window )
58         treeview.set_model(self.char_model)
59         self.add_columns_to_treeview(treeview)
60
61         win.add_with_scrollbar(treeview)
62         win.show_all()
63
64         progress_bar.set_fraction( 1 )
65         progress_bar.destroy()
66   
67     def build_window(self, treeview, path, view_column):
68         win = hildon.Window()
69         win.show_all() 
70
71         progress_bar = hildon.hildon_banner_show_progress(win, None, "Loading character sheet...")
72         progress_bar.set_fraction( 0.4 )
73
74         # Create menu
75         # NOTE: we probably want a window-specific menu for this page, but the
76         # main appmenu works for now
77         menu = self.create_menu(win)
78         # Attach menu to the window
79         win.set_menu(menu)
80
81         #pannable_area = hildon.PannableArea()
82
83         model = treeview.get_model()
84         miter = model.get_iter(path)
85         
86         # column 0 is the portrait, column 1 is name
87
88         char_name = model.get_value(miter, 1)
89         char_id = self.controller.char_name2id(char_name)
90         sheet = self.controller.get_char_sheet(char_id)
91
92         win.set_title(char_name)
93         
94         skillLabel = gtk.Label("Skills")
95
96         # TODO: replace these with api calls
97         corp_name = ""
98         skill_points = 0
99
100         name = gtk.Label("Name: %s" % char_name)
101         name.set_alignment(0, 0.5)
102
103         race = gtk.Label("%s %s %s" % (sheet.gender, sheet.race,
104                                        sheet.bloodLine))
105         race.set_alignment(0, 0.5)
106         
107         corp = gtk.Label("Corp: %s" % sheet.corporationName)
108         corp.set_alignment(0, 0.5)
109         
110         balance = gtk.Label("Balance: %s ISK" % sheet.balance)
111         balance.set_alignment(0, 0.5)
112
113         # attributes need to be lower. --danny
114         intel = gtk.Label("Intelligence: %d" % sheet.attributes.intelligence)
115         intel.set_alignment(0, 0.5)
116         mem = gtk.Label("Memory: %d" % sheet.attributes.memory)
117         mem.set_alignment(0, 0.5)
118         char = gtk.Label("Charisma: %d" % sheet.attributes.charisma)
119         char.set_alignment(0, 0.5)
120         percep = gtk.Label("Perception: %d" % sheet.attributes.perception)
121         percep.set_alignment(0, 0.5)
122         wp = gtk.Label("Willpower: %d" % sheet.attributes.willpower)
123         wp.set_alignment(0, 0.5)
124         blank_label = gtk.Label('')
125         blank_label.set_alignment(0, 0.5)
126
127         portrait = gtk.Image()
128         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
129         portrait.show()
130         
131         hbox = gtk.HBox(False, 0)
132
133         info_vbox = gtk.VBox(False, 0)
134         info_vbox.pack_start(name, False, False, 1)
135         info_vbox.pack_start(race, False, False, 1)
136         info_vbox.pack_start(corp, False, False, 1)
137         info_vbox.pack_start(balance, False, False, 1)
138
139         info_vbox.pack_start(blank_label, False, False, 1)
140
141         #stats_vbox = gtk.VBox(False, 0)
142         info_vbox.pack_start(intel, False, False, 1)
143         info_vbox.pack_start(mem, False, False, 1)
144         info_vbox.pack_start(char, False, False, 1)
145         info_vbox.pack_start(percep, False, False, 1)
146         info_vbox.pack_start(wp, False, False, 1)
147
148         hbox.pack_start(portrait, False, False, 10)
149         hbox.pack_start(info_vbox, False, False, 5)
150         #hbox.pack_start(stats_vbox, False, False, 5)
151         
152         vbox = gtk.VBox(False, 0)
153         #pannable_area.add(vbox)
154
155         vbox.pack_start(hbox, False, False, 0)
156         vbox.pack_start(skillLabel, False, False, 5)
157
158         # need to make scrollable --danny
159         skills_model = self.create_skills_model(sheet)
160         skills_treeview = gtk.TreeView( model = skills_model )
161         skills_treeview.set_model(skills_model)
162         self.add_columns_to_skills_view(skills_treeview)
163
164         vbox.pack_start(skills_treeview, False, False, 0)
165
166         win.add_with_scrollbar(vbox)
167         win.show_all()
168
169         progress_bar.set_fraction( 1 )
170         progress_bar.destroy()
171
172     def add_columns_to_skills_view(self, treeview):
173         #Column 0 for the treeview
174         renderer = gtk.CellRendererText()
175         column = gtk.TreeViewColumn('Skill Name', renderer, text=0)
176         column.set_property("expand", True)
177         treeview.append_column(column)
178         
179         #Column 1 for the treeview
180         renderer = gtk.CellRendererText()
181         column = gtk.TreeViewColumn('Skill Info', renderer, text=1)
182         column.set_property("expand", True)
183         treeview.append_column(column)
184
185     def create_skills_model(self, sheet):
186    
187         lstore = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
188
189         skilltree = self.controller.get_skill_tree()
190        
191         sp = [0, 250, 1414, 8000, 45255, 256000]
192
193         for g in skilltree.skillGroups:
194
195             skills_trained_in_this_group = False
196
197             for skill in g.skills:
198
199                 trained = sheet.skills.Get(skill.typeID, False)
200                 
201                 if trained:
202
203                     if not skills_trained_in_this_group:
204
205                         #TODO: add as a heading/category
206                         skills_trained_in_this_group = True
207                     
208                     # add row for this skill
209                     liter = lstore.append()
210                     lstore.set(liter, 0, "%s (Rank %d)" % (skill.typeName, skill.rank), 1, "SP: %d Level %d" % (trained.skillpoints, trained.level))
211
212         return lstore
213
214     def create_char_model(self):
215         lstore = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
216         #get icon and name and put in a liststore
217         self.fill_char_model(lstore)
218         return lstore
219
220     def fill_char_model(self, lstore):
221         char_list = self.controller.get_characters()
222         for name, icon in char_list:
223             liter = lstore.append()
224             lstore.set(liter, 1, name, 0, self.set_pix(icon))
225
226     def update_model(self, lstore):
227         lstore.clear()
228         self.fill_char_model(lstore)
229
230     def set_pix(self, filename):
231         pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
232         return pixbuf
233
234     def add_columns_to_treeview(self, treeview):
235         #Column 0 for the treeview
236         renderer = gtk.CellRendererPixbuf()
237         column = gtk.TreeViewColumn()
238         column.pack_start(renderer, True)
239         column.add_attribute(renderer, "pixbuf", 0)
240         treeview.append_column(column)
241
242         #Column 1 for the treeview
243         renderer = gtk.CellRendererText()
244         column = gtk.TreeViewColumn('Character Name', renderer, text=1)
245         column.set_property("expand", True)
246         treeview.append_column(column)
247  
248
249     def settings_clicked(self, button, window):
250    
251         dialog = gtk.Dialog()
252     
253         #get the vbox to pack all the settings into
254         vbox = dialog.vbox
255     
256         dialog.set_transient_for(window)
257         dialog.set_title("Settings")
258
259         uidLabel = gtk.Label("User ID:")
260         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
261         vbox.add(uidLabel)
262         
263         uidEntry = gtk.Entry()
264         uidEntry.set_text(self.controller.get_uid())
265         uidEntry.set_property('is_focus', False)
266         
267         vbox.add(uidEntry)
268
269         apiLabel = gtk.Label("API key:")
270         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
271         vbox.add(apiLabel)
272         
273         apiEntry = gtk.Entry()
274         apiEntry.set_text(self.controller.get_api_key())
275         apiEntry.set_property('is_focus', False)
276
277         vbox.add(apiEntry)
278            
279         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
280         help_button = dialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
281
282
283         dialog.show_all()
284         result = dialog.run()
285
286         if result == gtk.RESPONSE_OK:
287             self.controller.set_api_key(apiEntry.get_text())
288             self.controller.set_uid(uidEntry.get_text())
289             self.update_model(self.char_model)
290         
291         dialog.destroy()
292
293         return result
294
295     def about_clicked(self, button):
296     
297         dialog = gtk.AboutDialog()
298         dialog.set_website(self.about_website)
299         dialog.set_website_label(self.about_website)
300         dialog.set_name(self.about_name)
301         dialog.set_authors(self.about_authors)
302         dialog.set_comments(self.about_text)
303         dialog.set_version(self.app_version)
304         dialog.run()
305         dialog.destroy()
306
307     def refresh_clicked(self, button, window):
308         self.update_model(self.char_model)
309   
310     def create_menu(self, window):
311         menu = gtk.Menu()
312         for command in self.menu_items:
313             button = gtk.MenuItem( command )
314             if command == "About":
315                 button.connect("activate", self.about_clicked)
316             elif command == "Settings":
317                 button.connect("activate", self.settings_clicked, window)
318             elif command == "Refresh":
319                 button.connect("activate", self.refresh_clicked, window)
320             else:
321                 assert False, command
322             # Add entry to the view menu
323             menu.append(button)
324         menu.show_all()
325         return menu
326
327 if __name__ == "__main__":
328     main()
329