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