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