source: src/mlx/gui/flight.py@ 48:53b2648cfb30

Last change on this file since 48:53b2648cfb30 was 48:53b2648cfb30, checked in by István Váradi <locvais@…>, 12 years ago

Flight selection works

File size: 13.1 KB
Line 
1# The flight handling "wizard"
2
3from mlx.gui.common import *
4
5#-----------------------------------------------------------------------------
6
7class Page(gtk.Alignment):
8 """A page in the flight wizard."""
9 def __init__(self, wizard, title, help):
10 """Construct the page."""
11 super(Page, self).__init__(xalign = 0.0, yalign = 0.0,
12 xscale = 1.0, yscale = 1.0)
13 self.set_padding(padding_top = 4, padding_bottom = 4,
14 padding_left = 12, padding_right = 12)
15
16 frame = gtk.Frame()
17 self.add(frame)
18
19 style = self.get_style() if pygobject else self.rc_get_style()
20
21 self._vbox = gtk.VBox()
22 self._vbox.set_homogeneous(False)
23 frame.add(self._vbox)
24
25 eventBox = gtk.EventBox()
26 eventBox.modify_bg(0, style.bg[3])
27
28 alignment = gtk.Alignment(xalign = 0.0, xscale = 0.0)
29
30 label = gtk.Label(title)
31 label.modify_fg(0, style.fg[3])
32 label.modify_font(pango.FontDescription("bold 24"))
33 alignment.set_padding(padding_top = 4, padding_bottom = 4,
34 padding_left = 6, padding_right = 0)
35
36 alignment.add(label)
37 eventBox.add(alignment)
38
39 self._vbox.pack_start(eventBox, False, False, 0)
40
41 table = gtk.Table(3, 1)
42 table.set_homogeneous(True)
43
44 alignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
45 xscale = 1.0, yscale = 1.0)
46 alignment.set_padding(padding_top = 16, padding_bottom = 16,
47 padding_left = 16, padding_right = 16)
48 alignment.add(table)
49 self._vbox.pack_start(alignment, True, True, 0)
50
51 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
52 xscale = 0, yscale = 0.0)
53
54 label = gtk.Label(help)
55 label.set_justify(gtk.Justification.CENTER if pygobject
56 else gtk.JUSTIFY_CENTER)
57 alignment.add(label)
58 table.attach(alignment, 0, 1, 0, 1)
59
60 self._mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
61 xscale = 1.0, yscale = 1.0)
62 table.attach(self._mainAlignment, 0, 1, 1, 3)
63
64 buttonAlignment = gtk.Alignment(xalign = 1.0, xscale=0.0, yscale = 0.0)
65 buttonAlignment.set_padding(padding_top = 4, padding_bottom = 10,
66 padding_left = 16, padding_right = 16)
67
68 self._buttonBox = gtk.HButtonBox()
69 self._defaultButton = None
70 buttonAlignment.add(self._buttonBox)
71
72 self._vbox.pack_start(buttonAlignment, False, False, 0)
73
74 self._wizard = wizard
75
76 def setMainWidget(self, widget):
77 """Set the given widget as the main one."""
78 self._mainAlignment.add(widget)
79
80 def addButton(self, label, default = False):
81 """Add a button with the given label.
82
83 Return the button object created."""
84 button = gtk.Button(label)
85 self._buttonBox.add(button)
86 button.set_use_underline(True)
87 if default:
88 button.set_can_default(True)
89 self._defaultButton = button
90 return button
91
92 def activate(self):
93 """Called when this page becomes active.
94
95 This default implementation does nothing."""
96 pass
97
98 def grabDefault(self):
99 """If the page has a default button, make it the default one."""
100 if self._defaultButton is not None:
101 self._defaultButton.grab_default()
102
103#-----------------------------------------------------------------------------
104
105class LoginPage(Page):
106 """The login page."""
107 def __init__(self, wizard):
108 """Construct the login page."""
109 help = "Enter your MAVA pilot's ID and password to\n" \
110 "log in to the MAVA website and download\n" \
111 "your booked flights."
112 super(LoginPage, self).__init__(wizard, "Login", help)
113
114 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
115 xscale = 0.0, yscale = 0.0)
116
117 table = gtk.Table(2, 3)
118 table.set_row_spacings(4)
119 table.set_col_spacings(32)
120 alignment.add(table)
121 self.setMainWidget(alignment)
122
123 labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
124 label = gtk.Label("Pilot _ID:")
125 label.set_use_underline(True)
126 labelAlignment.add(label)
127 table.attach(labelAlignment, 0, 1, 0, 1)
128
129 self._pilotID = gtk.Entry()
130 self._pilotID.connect("changed", self._setLoginButton)
131 self._pilotID.set_tooltip_text("Enter your MAVA pilot's ID. This "
132 "usually starts with a "
133 "'P' followed by 3 digits.")
134 table.attach(self._pilotID, 1, 2, 0, 1)
135 label.set_mnemonic_widget(self._pilotID)
136
137 labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
138 label = gtk.Label("_Password:")
139 label.set_use_underline(True)
140 labelAlignment.add(label)
141 table.attach(labelAlignment, 0, 1, 1, 2)
142
143 self._password = gtk.Entry()
144 self._password.set_visibility(False)
145 self._password.connect("changed", self._setLoginButton)
146 self._password.set_tooltip_text("Enter the password for your pilot's ID")
147 table.attach(self._password, 1, 2, 1, 2)
148 label.set_mnemonic_widget(self._password)
149
150 self._rememberButton = gtk.CheckButton("_Remember password")
151 self._rememberButton.set_use_underline(True)
152 self._rememberButton.set_tooltip_text("If checked, your password will "
153 "be stored, so that you should "
154 "not have to enter it every time. "
155 "Note, however, that the password "
156 "is stored as text, and anybody "
157 "who can access your files will "
158 "be able to read it.")
159 table.attach(self._rememberButton, 1, 2, 2, 3, ypadding = 8)
160
161 self._loginButton = self.addButton("_Login", default = True)
162 self._loginButton.set_sensitive(False)
163 self._loginButton.connect("clicked", self._loginClicked)
164 self._loginButton.set_tooltip_text("Click to log in.")
165
166 config = self._wizard.gui.config
167 self._pilotID.set_text(config.pilotID)
168 self._password.set_text(config.password)
169 self._rememberButton.set_active(config.rememberPassword)
170
171 def _setLoginButton(self, entry):
172 """Set the login button's sensitivity.
173
174 The button is sensitive only if both the pilot ID and the password
175 fields contain values."""
176 self._loginButton.set_sensitive(self._pilotID.get_text()!="" and
177 self._password.get_text()!="")
178
179 def _loginClicked(self, button):
180 """Called when the login button was clicked."""
181 self._wizard.gui.webHandler.login(self._pilotID.get_text(),
182 self._password.get_text(),
183 self._loginResultCallback)
184
185 def _loginResultCallback(self, returned, result):
186 """The login result callback, called in the web handler's thread."""
187 gobject.idle_add(self._handleLoginResult, returned, result)
188
189 def _handleLoginResult(self, returned, result):
190 """Handle the login result."""
191 if returned:
192 if result.loggedIn:
193 config = self._wizard.gui.config
194
195 config.pilotID = self._pilotID.get_text()
196
197 rememberPassword = self._rememberButton.get_active()
198 config.password = self._password.get_text() if rememberPassword \
199 else ""
200
201 config.rememberPassword = rememberPassword
202
203 config.save()
204 self._wizard._loginResult = result
205 self._wizard.nextPage()
206 else:
207 dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
208 buttons = BUTTONSTYPE_OK,
209 message_format =
210 "Invalid pilot's ID or password.")
211 dialog.format_secondary_markup("Check the ID and try to reenter"
212 " the password.")
213 dialog.run()
214 dialog.hide()
215 else:
216 dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
217 buttons = BUTTONSTYPE_OK,
218 message_format =
219 "Failed to connect to the MAVA website.")
220 dialog.format_secondary_markup("Try again in a few minutes.")
221 dialog.run()
222 dialog.hide()
223
224#-----------------------------------------------------------------------------
225
226class FlightSelectionPage(Page):
227 """The page to select the flight."""
228 def __init__(self, wizard):
229 """Construct the flight selection page."""
230 super(FlightSelectionPage, self).__init__(wizard, "Flight selection",
231 "Select the flight you want "
232 "to perform.")
233
234
235 self._listStore = gtk.ListStore(str, str, str, str)
236 self._flightList = gtk.TreeView(self._listStore)
237 column = gtk.TreeViewColumn("Flight no.", gtk.CellRendererText(),
238 text = 1)
239 column.set_expand(True)
240 self._flightList.append_column(column)
241 column = gtk.TreeViewColumn("Departure time [UTC]", gtk.CellRendererText(),
242 text = 0)
243 column.set_expand(True)
244 self._flightList.append_column(column)
245 column = gtk.TreeViewColumn("From", gtk.CellRendererText(),
246 text = 2)
247 column.set_expand(True)
248 self._flightList.append_column(column)
249 column = gtk.TreeViewColumn("To", gtk.CellRendererText(),
250 text = 3)
251 column.set_expand(True)
252 self._flightList.append_column(column)
253
254 flightSelection = self._flightList.get_selection()
255 flightSelection.connect("changed", self._selectionChanged)
256
257 scrolledWindow = gtk.ScrolledWindow()
258 scrolledWindow.add(self._flightList)
259 scrolledWindow.set_size_request(400, -1)
260 scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
261 else gtk.POLICY_AUTOMATIC,
262 gtk.PolicyType.ALWAYS if pygobject
263 else gtk.POLICY_ALWAYS)
264
265 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.0, xscale = 0.0, yscale = 1.0)
266 alignment.add(scrolledWindow)
267
268 self.setMainWidget(alignment)
269
270 self._button = self.addButton(gtk.STOCK_GO_FORWARD, default = True)
271 self._button.set_use_stock(True)
272 self._button.set_sensitive(False)
273
274 self._activated = False
275
276 def activate(self):
277 """Fill the flight list."""
278 if not self._activated:
279 for flight in self._wizard.loginResult.flights:
280 self._listStore.append([str(flight.departureTime),
281 flight.callsign,
282 flight.departureICAO,
283 flight.arrivalICAO])
284 self._activated = True
285
286 def _selectionChanged(self, selection):
287 """Called when the selection is changed."""
288 self._button.set_sensitive(selection.count_selected_rows()==1)
289
290#-----------------------------------------------------------------------------
291
292class Wizard(gtk.VBox):
293 """The flight wizard."""
294 def __init__(self, gui):
295 """Construct the wizard."""
296 super(Wizard, self).__init__()
297
298 self.gui = gui
299
300 self._pages = []
301 self._currentPage = None
302
303 self._pages.append(LoginPage(self))
304 self._pages.append(FlightSelectionPage(self))
305
306 self._loginResult = None
307
308 self.setCurrentPage(0)
309
310 @property
311 def loginResult(self):
312 """Get the login result."""
313 return self._loginResult
314
315 def setCurrentPage(self, index):
316 """Set the current page to the one with the given index."""
317 assert index < len(self._pages)
318
319 if self._currentPage is not None:
320 self.remove(self._pages[self._currentPage])
321
322 self._currentPage = index
323 self.add(self._pages[index])
324 self._pages[index].activate()
325 self.show_all()
326
327 def nextPage(self):
328 """Go to the next page."""
329 self.setCurrentPage(self._currentPage + 1)
330 self.grabDefault()
331
332 def grabDefault(self):
333 """Make the default button of the current page the default."""
334 self._pages[self._currentPage].grabDefault()
335
336#-----------------------------------------------------------------------------
337
Note: See TracBrowser for help on using the repository browser.