def9c3b42d95a7192c38619297789c5c24bdaef4
[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         portrait = gtk.Image()
113         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
114         portrait.show()
115         
116         hbox = gtk.HBox(False, 0)
117
118         info_vbox = gtk.VBox(False, 0)
119         info_vbox.pack_start(name, False, False, 1)
120         info_vbox.pack_start(race, False, False, 1)
121         info_vbox.pack_start(corp, False, False, 1)
122         info_vbox.pack_start(balance, False, False, 1)
123
124         hbox.pack_start(portrait, False, False, 10)
125         hbox.pack_start(info_vbox, False, False, 5)
126         #hbox.pack_start(stats_vbox, False, False, 5)
127         
128         vbox = gtk.VBox(False, 0)
129         pannable_area.add_with_viewport(vbox)
130
131         vbox.pack_start(hbox, False, False, 0)
132         vbox.pack_start(skillLabel, False, False, 5)
133
134         skills_treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
135         
136         skills_model = self.create_skills_model(sheet)
137         skills_treeview.set_model(skills_model)
138
139         self.add_columns_to_skills_view(skills_treeview)
140
141         vbox.pack_start(skills_treeview, False, False, 0)
142
143         win.add(pannable_area)
144         win.show_all()
145
146         hildon.hildon_gtk_window_set_progress_indicator(win, 0)
147
148     def add_columns_to_skills_view(self, treeview):
149         #Column 0 for the treeview
150         renderer = gtk.CellRendererText()
151         column = gtk.TreeViewColumn('Skill Name', renderer, text=0)
152         column.set_property("expand", True)
153         treeview.append_column(column)
154         
155         #Column 1 for the treeview
156         renderer = gtk.CellRendererText()
157         column = gtk.TreeViewColumn('Skill Info', renderer, text=1)
158         column.set_property("expand", True)
159         treeview.append_column(column)
160
161     def create_skills_model(self, sheet):
162    
163         lstore = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
164
165         skilltree = self.controller.get_skill_tree()
166        
167         sp = [0, 250, 1414, 8000, 45255, 256000]
168
169         for g in skilltree.skillGroups:
170
171             skills_trained_in_this_group = False
172
173             for skill in g.skills:
174
175                 trained = sheet.skills.Get(skill.typeID, False)
176                 
177                 if trained:
178
179                     if not skills_trained_in_this_group:
180
181                         #TODO: add as a heading/category
182                         skills_trained_in_this_group = True
183                     
184                     # add row for this skill
185                     liter = lstore.append()
186                     lstore.set(liter, 0, "%s (Rank %d)" % (skill.typeName, skill.rank),
187                                       1, "SP: %d Level %d" %
188                                                  (trained.skillpoints,
189                                                   trained.level))
190
191
192         return lstore
193
194
195     def create_char_model(self):
196         lstore = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
197
198         #get icon and name and put in a liststore
199         self.fill_char_model(lstore)
200
201         return lstore
202
203
204     def fill_char_model(self, lstore):
205         char_list = self.controller.get_characters()
206
207         for name, icon in char_list:
208             liter = lstore.append()
209             lstore.set(liter, 1, name, 0, self.set_pix(icon))
210     
211
212     def update_model(self, lstore):
213         lstore.clear()
214         self.fill_char_model(lstore)
215
216
217     def set_pix(self, filename):
218         pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
219         return pixbuf
220
221     
222     def add_columns_to_treeview(self, treeview):
223         #Column 0 for the treeview
224         renderer = gtk.CellRendererPixbuf()
225         column = gtk.TreeViewColumn()
226         column.pack_start(renderer, True)
227         column.add_attribute(renderer, "pixbuf", 0)
228         treeview.append_column(column)
229
230         #Column 1 for the treeview
231         renderer = gtk.CellRendererText()
232         column = gtk.TreeViewColumn('Character Name', renderer, text=1)
233         column.set_property("expand", True)
234         treeview.append_column(column)
235  
236   
237     def settings_clicked(self, button, window):
238         dialog = gtk.Dialog()
239     
240         #get the vbox to pack all the settings into
241         vbox = dialog.vbox
242     
243         dialog.set_transient_for(window)
244         dialog.set_title("Settings")
245
246         uidLabel = gtk.Label("User ID:")
247         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
248         vbox.add(uidLabel)
249         
250         uidEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
251         uidEntry.set_placeholder("User ID")
252         uidEntry.set_text(self.controller.get_uid())
253         uidEntry.set_property('is_focus', False)
254         
255         vbox.add(uidEntry)
256
257         apiLabel = gtk.Label("API key:")
258         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
259         vbox.add(apiLabel)
260         
261         apiEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
262         apiEntry.set_placeholder("API Key")
263         apiEntry.set_text(self.controller.get_api_key())
264         apiEntry.set_property('is_focus', False)
265
266         vbox.add(apiEntry)
267        
268         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
269         help_button = dialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
270
271         dialog.show_all()
272         result = dialog.run()
273         if result == gtk.RESPONSE_OK:
274             self.controller.set_api_key(apiEntry.get_text())
275             self.controller.set_uid(uidEntry.get_text())
276             self.controller.set_auth()
277             self.update_model(self.char_model)
278
279         
280         dialog.destroy()
281
282         return result
283
284     
285     def about_clicked(self, button):
286         dialog = gtk.AboutDialog()
287         dialog.set_website(self.about_website)
288         dialog.set_website_label(self.about_website)
289         dialog.set_name(self.about_name)
290         dialog.set_authors(self.about_authors)
291         dialog.set_comments(self.about_text)
292         dialog.set_version(self.app_version)
293         dialog.run()
294         dialog.destroy()
295
296     
297     def refresh_clicked(self, button, window):
298         self.update_model(self.char_model)
299
300     
301     def create_menu(self, window): 
302         menu = hildon.AppMenu()
303
304         for command in self.menu_items:
305             # Create menu entries
306             button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
307             button.set_label(command)
308
309             if command == "About":
310                 button.connect("clicked", self.about_clicked)
311             elif command == "Settings":
312                 button.connect("clicked", self.settings_clicked, window)
313             elif command == "Refresh":
314                 button.connect("clicked", self.refresh_clicked, window)
315             else:
316                 assert False, command
317
318             # Add entry to the view menu
319             menu.append(button)
320         
321         menu.show_all()
322
323         return menu
324
325
326 if __name__ == "__main__":
327     main()
328