split ui classes into separate modules
[mevemon] / package / src / ui / fremantle / base.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 hildon
20 import gtk
21
22 import ui.models as models
23
24 class BaseUI():
25     menu_items = ("Settings", "About", "Refresh")
26
27     def create_menu(self, window):
28         menu = hildon.AppMenu()
29
30         for command in self.menu_items:
31             # Create menu entries
32             button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
33             button.set_label(command)
34
35             if command == "About":
36                 button.connect("clicked", self.about_clicked)
37             elif command == "Settings":
38                 button.connect("clicked", self.settings_clicked, window)
39             elif command == "Refresh":
40                 button.connect("clicked", self.refresh_clicked)
41             else:
42                 assert False, command
43
44             # Add entry to the view menu
45             menu.append(button)
46
47         menu.show_all()
48
49         return menu
50
51     def settings_clicked(self, button, window):
52
53         RESPONSE_NEW, RESPONSE_EDIT, RESPONSE_DELETE = range(3)
54
55         dialog = gtk.Dialog()
56         dialog.set_transient_for(window)
57         dialog.set_title("Settings")
58
59         pannable_area = hildon.PannableArea()
60
61         dialog_vbox = dialog.vbox
62
63         vbox = gtk.VBox(False, 1)
64
65         self.accounts_model = models.AccountsModel(self.controller)
66         
67         accounts_treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
68         accounts_treeview.set_model(self.accounts_model)
69         accounts_treeview.set_headers_visible(True)
70         self.add_columns_to_accounts(accounts_treeview)
71         vbox.pack_start(accounts_treeview, False, False, 1)
72
73         # all stock responses are negative, so we can use any positive value
74         new_button = dialog.add_button("New", RESPONSE_NEW)
75         #TODO: get edit button working
76         #edit_button = dialog.add_button("Edit", RESPONSE_EDIT)
77         delete_button = dialog.add_button("Delete", RESPONSE_DELETE)
78         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
79         
80         pannable_area.add_with_viewport(vbox)
81         
82         dialog_vbox.pack_start(pannable_area, True, True, 1)
83       
84       
85         dialog.show_all()
86
87         result = dialog.run()
88
89         while(result != gtk.RESPONSE_DELETE_EVENT):
90             if result == RESPONSE_NEW:
91                 self.new_account_clicked(window)
92             #elif result == RESPONSE_EDIT:
93             #    # get the selected treeview item and pop up the account_box
94             #    self.edit_account(accounts_treeview)
95             elif result == RESPONSE_DELETE:
96                 # get the selected treeview item, and delete the gconf keys
97                 self.delete_account(accounts_treeview)
98             elif result == gtk.RESPONSE_OK:
99                 self.char_model.get_characters()
100                 break
101         
102             result = dialog.run()
103
104         dialog.destroy()
105
106
107
108     def get_selected_item(self, treeview, column):
109         selection = treeview.get_selection()
110         model, miter = selection.get_selected()
111
112         value = model.get_value(miter, column)
113
114         return value
115
116     def edit_account(self, treeview):
117         uid = self.get_selected_item(treeview, 0)
118         # pop up the account dialog
119
120         self.accounts_model.get_accounts()
121
122     def delete_account(self, treeview):
123         uid = self.get_selected_item(treeview, 0)
124         self.controller.remove_account(uid)
125         # refresh model
126         self.accounts_model.get_accounts()
127
128
129     def add_columns_to_accounts(self, treeview):
130         #Column 0 for the treeview
131         renderer = gtk.CellRendererText()
132         column = gtk.TreeViewColumn('User ID', renderer, 
133                 text=models.AccountsModel.C_UID)
134         column.set_property("expand", True)
135         treeview.append_column(column)
136
137         #Column 2 (characters) for the treeview
138         column = gtk.TreeViewColumn('Characters', renderer, 
139                 markup=models.AccountsModel.C_CHARS)
140         column.set_property("expand", True)
141         treeview.append_column(column)
142
143
144     def new_account_clicked(self, window):
145         dialog = gtk.Dialog()
146     
147         #get the vbox to pack all the settings into
148         vbox = dialog.vbox
149     
150         dialog.set_transient_for(window)
151         dialog.set_title("New Account")
152
153         uidLabel = gtk.Label("User ID:")
154         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
155         vbox.add(uidLabel)
156         
157         uidEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
158         uidEntry.set_placeholder("User ID")
159         uidEntry.set_property('is_focus', False)
160         
161         vbox.add(uidEntry)
162
163         apiLabel = gtk.Label("API key:")
164         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
165         vbox.add(apiLabel)
166         
167         apiEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
168         apiEntry.set_placeholder("API Key")
169         apiEntry.set_property('is_focus', False)
170
171         vbox.add(apiEntry)
172        
173         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
174
175         dialog.show_all()
176         result = dialog.run()
177         
178         valid_credentials = False
179
180         while not valid_credentials:
181             if result == gtk.RESPONSE_OK:
182                 uid = uidEntry.get_text()
183                 api_key = apiEntry.get_text()
184                 try:
185                     validation.uid(uid)
186                     validation.api_key(api_key)
187                 except validation.ValidationError, e:
188                     self.report_error(e.message)
189                     result = dialog.run()
190                 else:
191                     valid_credentials = True
192                     self.controller.add_account(uid, api_key)
193                     self.accounts_model.get_accounts()
194             else:
195                 break
196
197         dialog.destroy()
198
199
200     def report_error(self, error):
201         hildon.hildon_banner_show_information(self.win, '', error)
202     
203     def about_clicked(self, button):
204         dialog = gtk.AboutDialog()
205         dialog.set_website(self.controller.about_website)
206         dialog.set_website_label(self.controller.about_website)
207         dialog.set_name(self.controller.about_name)
208         dialog.set_authors(self.controller.about_authors)
209         dialog.set_comments(self.controller.about_text)
210         dialog.set_version(self.controller.app_version)
211         dialog.run()
212         dialog.destroy()
213
214     def add_label(self, text, box, markup=True, align="left", padding=1):
215         label = gtk.Label(text)
216         if markup:
217             label.set_use_markup(True)
218         if align == "left":
219             label.set_alignment(0, 0.5)
220
221         box.pack_start(label, False, False, padding)
222
223         return label