moved the code into src/ and added LICENSE file
[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://example.site.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         win = hildon.StackableWindow()
72         win.show_all() 
73         hildon.hildon_gtk_window_set_progress_indicator(win, 1)
74
75         # Create menu
76         # NOTE: we probably want a window-specific menu for this page, but the
77         # main appmenu works for now
78         menu = self.create_menu(win)
79         # Attach menu to the window
80         win.set_app_menu(menu)
81
82         pannable_area = hildon.PannableArea()
83
84         model = treeview.get_model()
85         miter = model.get_iter(path)
86         
87         # column 0 is the portrait, column 1 is name
88
89         char_name = model.get_value(miter, 1)
90         char_id = self.controller.char_name2id(char_name)
91         sheet = self.controller.get_char_sheet(char_id)
92
93         win.set_title(char_name)
94         
95         skillLabel = gtk.Label("Skills")
96
97         name = gtk.Label("%s" % char_name)
98         name.set_alignment(0, 0.5)
99
100         race = gtk.Label("%s %s %s" % (sheet.gender, sheet.race,
101             sheet.bloodLine))
102         race.set_alignment(0, 0.5)
103         
104         corp = gtk.Label("Corp: %s" % sheet.corporationName)
105         corp.set_alignment(0, 0.5)
106
107         balance = gtk.Label("Balance: %s ISK" % sheet.balance)
108         balance.set_alignment(0, 0.5)
109
110         portrait = gtk.Image()
111         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
112         portrait.show()
113         
114         hbox = gtk.HBox(False, 0)
115
116         info_vbox = gtk.VBox(False, 0)
117         info_vbox.pack_start(name, False, False, 1)
118         info_vbox.pack_start(race, False, False, 1)
119         info_vbox.pack_start(corp, False, False, 1)
120         info_vbox.pack_start(balance, False, False, 1)
121
122         hbox.pack_start(portrait, False, False, 10)
123         hbox.pack_start(info_vbox, False, False, 5)
124         #hbox.pack_start(stats_vbox, False, False, 5)
125         
126         vbox = gtk.VBox(False, 0)
127         pannable_area.add_with_viewport(vbox)
128
129         vbox.pack_start(hbox, False, False, 0)
130         vbox.pack_start(skillLabel, False, False, 5)
131
132         win.add(pannable_area)
133         win.show_all()
134
135         hildon.hildon_gtk_window_set_progress_indicator(win, 0)
136
137     def create_char_model(self):
138         lstore = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
139
140         #get icon and name and put in a liststore
141         self.fill_char_model(lstore)
142
143         return lstore
144
145
146     def fill_char_model(self, lstore):
147         char_list = self.controller.get_characters()
148
149         for name, icon in char_list:
150             liter = lstore.append()
151             lstore.set(liter, 1, name, 0, self.set_pix(icon))
152     
153
154     def update_model(self, lstore):
155         lstore.clear()
156         self.fill_char_model(lstore)
157
158
159     def set_pix(self, filename):
160         pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
161         return pixbuf
162
163     
164     def add_columns_to_treeview(self, treeview):
165         #Column 0 for the treeview
166         renderer = gtk.CellRendererPixbuf()
167         column = gtk.TreeViewColumn()
168         column.pack_start(renderer, True)
169         column.add_attribute(renderer, "pixbuf", 0)
170         treeview.append_column(column)
171
172         #Column 1 for the treeview
173         renderer = gtk.CellRendererText()
174         column = gtk.TreeViewColumn('title', renderer, text=1)
175         column.set_property("expand", True)
176         treeview.append_column(column)
177  
178   
179     def settings_clicked(self, button, window):
180         dialog = gtk.Dialog()
181     
182         #get the vbox to pack all the settings into
183         vbox = dialog.vbox
184     
185         dialog.set_transient_for(window)
186         dialog.set_title("Settings")
187
188         uidLabel = gtk.Label("User ID:")
189         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
190         vbox.add(uidLabel)
191         
192         uidEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
193         uidEntry.set_placeholder("User ID")
194         uidEntry.set_text(self.controller.get_uid())
195         uidEntry.set_property('is_focus', False)
196         
197         vbox.add(uidEntry)
198
199         apiLabel = gtk.Label("API key:")
200         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
201         vbox.add(apiLabel)
202         
203         apiEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
204         apiEntry.set_placeholder("API Key")
205         apiEntry.set_text(self.controller.get_api_key())
206         apiEntry.set_property('is_focus', False)
207
208         vbox.add(apiEntry)
209        
210         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
211         help_button = dialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
212
213         dialog.show_all()
214         result = dialog.run()
215         if result == gtk.RESPONSE_OK:
216             self.controller.set_api_key(apiEntry.get_text())
217             self.controller.set_uid(uidEntry.get_text())
218             self.controller.set_auth()
219             self.update_model(self.char_model)
220
221         
222         dialog.destroy()
223
224         return result
225
226     
227     def about_clicked(self, button):
228         dialog = gtk.AboutDialog()
229         dialog.set_website(self.about_website)
230         dialog.set_website_label(self.about_website)
231         dialog.set_name(self.about_name)
232         dialog.set_authors(self.about_authors)
233         dialog.set_comments(self.about_text)
234         dialog.set_version(self.app_version)
235         dialog.run()
236         dialog.destroy()
237
238     
239     def refresh_clicked(self, button, window):
240         self.update_model(self.char_model)
241
242     
243     def create_menu(self, window): 
244         menu = hildon.AppMenu()
245
246         for command in self.menu_items:
247             # Create menu entries
248             button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
249             button.set_label(command)
250
251             if command == "About":
252                 button.connect("clicked", self.about_clicked)
253             elif command == "Settings":
254                 button.connect("clicked", self.settings_clicked, window)
255             elif command == "Refresh":
256                 button.connect("clicked", self.refresh_clicked, window)
257             else:
258                 assert False, command
259
260             # Add entry to the view menu
261             menu.append(button)
262         
263         menu.show_all()
264
265         return menu
266
267
268 if __name__ == "__main__":
269     main()
270