9e444f295cb052354c4b0315226c7ee9c5e611c5
[mevemon] / ui / fremantle / ui.py
1
2 # Based on C code from:
3 # "Hildon Tutorial" version 2009-04-28
4 # Example 3.1, "Example of a Hildon application menu"
5
6 import sys
7
8 import gtk
9 import hildon
10 import gobject
11
12 class mEveMonUI():
13
14     about_name = 'mEveMon'
15     about_text = ('Mobile character monitor for EVE Online')
16     about_authors = ['Ryan Campbell', 'Danny Campbell']
17     about_website = 'http://example.site.org'
18     app_version = '0.1'
19
20     menu_items = ("Settings", "About", "Refresh")
21
22     def __init__(self, controller):
23         self.controller = controller
24         gtk.set_application_name("mEveMon")
25     
26         #create the main window
27         win = hildon.StackableWindow()
28         win.connect("destroy", self.controller.quit)
29         win.show_all()
30         hildon.hildon_gtk_window_set_progress_indicator(win, 1)
31
32         # Create menu
33         menu = self.create_menu(win)
34         # Attach menu to the window
35         win.set_app_menu(menu)
36
37         pannable_area = hildon.PannableArea()
38
39         # gtk.HILDON_UI_MODE_NORMAL -> not selection in the treeview
40         # gtk.HILDON_UI_MODE_EDIT -> selection in the treeview
41         treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
42         treeview.connect('row-activated', self.build_window)
43
44         self.char_model = self.create_char_model()
45         treeview.set_model(self.char_model)
46
47         self.add_columns_to_treeview(treeview)
48
49         pannable_area.add(treeview)
50
51         win.add(pannable_area);
52         
53         win.show_all()
54
55         hildon.hildon_gtk_window_set_progress_indicator(win, 0)
56
57     def build_window(self, treeview, path, view_column):
58         win = hildon.StackableWindow()
59         win.show_all() 
60         hildon.hildon_gtk_window_set_progress_indicator(win, 1)
61
62         # Create menu
63         # NOTE: we probably want a window-specific menu for this page, but the
64         # main appmenu works for now
65         menu = self.create_menu(win)
66         # Attach menu to the window
67         win.set_app_menu(menu)
68
69         pannable_area = hildon.PannableArea()
70
71         model = treeview.get_model()
72         miter = model.get_iter(path)
73         
74         # column 0 is the portrait, column 1 is name
75
76         char_name = model.get_value(miter, 1)
77         char_id = self.controller.char_name2id(char_name)
78         sheet = self.controller.get_char_sheet(char_id)
79
80         win.set_title(char_name)
81         
82         skillLabel = gtk.Label("Skills")
83
84         name = gtk.Label("%s" % char_name)
85         name.set_alignment(0, 0.5)
86
87         race = gtk.Label("%s %s %s" % (sheet.gender, sheet.race,
88             sheet.bloodLine))
89         race.set_alignment(0, 0.5)
90         
91         corp = gtk.Label("Corp: %s" % sheet.corporationName)
92         corp.set_alignment(0, 0.5)
93
94         balance = gtk.Label("Balance: %s ISK" % sheet.balance)
95         balance.set_alignment(0, 0.5)
96
97         portrait = gtk.Image()
98         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
99         portrait.show()
100         
101         hbox = gtk.HBox(False, 0)
102
103         info_vbox = gtk.VBox(False, 0)
104         info_vbox.pack_start(name, False, False, 1)
105         info_vbox.pack_start(race, False, False, 1)
106         info_vbox.pack_start(corp, False, False, 1)
107         info_vbox.pack_start(balance, False, False, 1)
108
109         hbox.pack_start(portrait, False, False, 10)
110         hbox.pack_start(info_vbox, False, False, 5)
111         #hbox.pack_start(stats_vbox, False, False, 5)
112         
113         vbox = gtk.VBox(False, 0)
114         pannable_area.add_with_viewport(vbox)
115
116         vbox.pack_start(hbox, False, False, 0)
117         vbox.pack_start(skillLabel, False, False, 5)
118
119         win.add(pannable_area)
120         win.show_all()
121
122         hildon.hildon_gtk_window_set_progress_indicator(win, 0)
123
124     def create_char_model(self):
125         lstore = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
126
127         #get icon and name and put in a liststore
128         self.fill_char_model(lstore)
129
130         return lstore
131
132
133     def fill_char_model(self, lstore):
134         char_list = self.controller.get_characters()
135
136         for name, icon in char_list:
137             liter = lstore.append()
138             lstore.set(liter, 1, name, 0, self.set_pix(icon))
139     
140
141     def update_model(self, lstore):
142         lstore.clear()
143         self.fill_char_model(lstore)
144
145
146     def set_pix(self, filename):
147         pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
148         return pixbuf
149
150     
151     def add_columns_to_treeview(self, treeview):
152         #Column 0 for the treeview
153         renderer = gtk.CellRendererPixbuf()
154         column = gtk.TreeViewColumn()
155         column.pack_start(renderer, True)
156         column.add_attribute(renderer, "pixbuf", 0)
157         treeview.append_column(column)
158
159         #Column 1 for the treeview
160         renderer = gtk.CellRendererText()
161         column = gtk.TreeViewColumn('title', renderer, text=1)
162         column.set_property("expand", True)
163         treeview.append_column(column)
164  
165   
166     def settings_clicked(self, button, window):
167         dialog = gtk.Dialog()
168     
169         #get the vbox to pack all the settings into
170         vbox = dialog.vbox
171     
172         dialog.set_transient_for(window)
173         dialog.set_title("Settings")
174
175         uidLabel = gtk.Label("User ID:")
176         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
177         vbox.add(uidLabel)
178         
179         uidEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
180         uidEntry.set_placeholder("User ID")
181         uidEntry.set_text(self.controller.get_uid())
182         uidEntry.set_property('is_focus', False)
183         
184         vbox.add(uidEntry)
185
186         apiLabel = gtk.Label("API key:")
187         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
188         vbox.add(apiLabel)
189         
190         apiEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
191         apiEntry.set_placeholder("API Key")
192         apiEntry.set_text(self.controller.get_api_key())
193         apiEntry.set_property('is_focus', False)
194
195         vbox.add(apiEntry)
196        
197         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
198         help_button = dialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
199
200         dialog.show_all()
201         result = dialog.run()
202         if result == gtk.RESPONSE_OK:
203             self.controller.set_api_key(apiEntry.get_text())
204             self.controller.set_uid(uidEntry.get_text())
205             self.controller.set_auth()
206             self.update_model(self.char_model)
207
208         
209         dialog.destroy()
210
211         return result
212
213     
214     def about_clicked(self, button):
215         dialog = gtk.AboutDialog()
216         dialog.set_website(self.about_website)
217         dialog.set_website_label(self.about_website)
218         dialog.set_name(self.about_name)
219         dialog.set_authors(self.about_authors)
220         dialog.set_comments(self.about_text)
221         dialog.set_version(self.app_version)
222         dialog.run()
223         dialog.destroy()
224
225     
226     def refresh_clicked(self, button, window):
227         self.update_model(self.char_model)
228
229     
230     def create_menu(self, window): 
231         menu = hildon.AppMenu()
232
233         for command in self.menu_items:
234             # Create menu entries
235             button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
236             button.set_label(command)
237
238             if command == "About":
239                 button.connect("clicked", self.about_clicked)
240             elif command == "Settings":
241                 button.connect("clicked", self.settings_clicked, window)
242             elif command == "Refresh":
243                 button.connect("clicked", self.refresh_clicked, window)
244             else:
245                 assert False, command
246
247             # Add entry to the view menu
248             menu.append(button)
249         
250         menu.show_all()
251
252         return menu
253
254
255 if __name__ == "__main__":
256     main()
257