added license header to our code
[mevemon] / 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://example.site.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(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
91         win.set_title(char_name)
92         
93         skillLabel = gtk.Label("Skills")
94
95         # TODO: replace these with api calls
96         corp_name = ""
97         skill_points = 0
98
99         name = gtk.Label("Name: %s" % char_name)
100         name.set_alignment(0, 0.5)
101
102         corp = gtk.Label("Corp: %s" % corp_name)
103         corp.set_alignment(0, 0.5)
104
105         balance = gtk.Label("Balance: %s ISK" % 
106                 self.controller.get_account_balance(char_id))
107         balance.set_alignment(0, 0.5)
108
109         sp = gtk.Label("Skill points: %s" % skill_points)
110         sp.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(corp, False, False, 1)
121         info_vbox.pack_start(balance, False, False, 1)
122         info_vbox.pack_start(sp, 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(vbox)
130
131         vbox.pack_start(hbox, False, False, 0)
132         vbox.pack_start(skillLabel, False, False, 5)
133
134         win.add(vbox)
135         win.show_all()
136
137         progress_bar.set_fraction( 1 )
138         progress_bar.destroy()
139
140     def create_char_model(self):
141         lstore = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
142         #get icon and name and put in a liststore
143         self.fill_char_model(lstore)
144         return lstore
145
146     def fill_char_model(self, lstore):
147         char_list = self.controller.get_characters()
148         for name, icon in char_list:
149             liter = lstore.append()
150             lstore.set(liter, 1, name, 0, self.set_pix(icon))
151
152     def update_model(self, lstore):
153         lstore.clear()
154         self.fill_char_model(lstore)
155
156     def set_pix(self, filename):
157         pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
158         return pixbuf
159
160     def add_columns_to_treeview(self, treeview):
161         #Column 0 for the treeview
162         renderer = gtk.CellRendererPixbuf()
163         column = gtk.TreeViewColumn()
164         column.pack_start(renderer, True)
165         column.add_attribute(renderer, "pixbuf", 0)
166         treeview.append_column(column)
167
168         #Column 1 for the treeview
169         renderer = gtk.CellRendererText()
170         column = gtk.TreeViewColumn('title', renderer, text=1)
171         column.set_property("expand", True)
172         treeview.append_column(column)
173  
174
175     def settings_clicked(self, button, window):
176    
177         dialog = gtk.Dialog()
178     
179         #get the vbox to pack all the settings into
180         vbox = dialog.vbox
181     
182         dialog.set_transient_for(window)
183         dialog.set_title("Settings")
184
185         uidLabel = gtk.Label("User ID:")
186         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
187         vbox.add(uidLabel)
188         
189         uidEntry = gtk.Entry()
190         uidEntry.set_text(self.controller.get_uid())
191         uidEntry.set_property('is_focus', False)
192         
193         vbox.add(uidEntry)
194
195         apiLabel = gtk.Label("API key:")
196         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
197         vbox.add(apiLabel)
198         
199         apiEntry = gtk.Entry()
200         apiEntry.set_text(self.controller.get_api_key())
201         apiEntry.set_property('is_focus', False)
202
203         vbox.add(apiEntry)
204            
205         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
206         help_button = dialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
207
208
209         dialog.show_all()
210         result = dialog.run()
211
212         if result == gtk.RESPONSE_OK:
213             self.controller.set_api_key(apiEntry.get_text())
214             self.controller.set_uid(uidEntry.get_text())
215             self.update_model(self.char_model)
216         
217         dialog.destroy()
218
219         return result
220
221     def about_clicked(self, button):
222     
223         dialog = gtk.AboutDialog()
224         dialog.set_website(self.about_website)
225         dialog.set_website_label(self.about_website)
226         dialog.set_name(self.about_name)
227         dialog.set_authors(self.about_authors)
228         dialog.set_comments(self.about_text)
229         dialog.set_version(self.app_version)
230         dialog.run()
231         dialog.destroy()
232
233     def refresh_clicked(self, button, window):
234         self.update_model(self.char_model)
235   
236     def create_menu(self, window):
237         menu = gtk.Menu()
238         for command in self.menu_items:
239             button = gtk.MenuItem( command )
240             if command == "About":
241                 button.connect("activate", self.about_clicked)
242             elif command == "Settings":
243                 button.connect("activate", self.settings_clicked, window)
244             elif command == "Refresh":
245                 button.connect("activate", self.refresh_clicked, window)
246             else:
247                 assert False, command
248             # Add entry to the view menu
249             menu.append(button)
250         menu.show_all()
251         return menu
252
253 if __name__ == "__main__":
254     main()
255