source: src/mlx/gui/flight.py@ 49:1b5b3d34dd84

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

Implemented busy state notification

File size: 13.2 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.beginBusy("Logging in...")
182 self._wizard.gui.webHandler.login(self._pilotID.get_text(),
183 self._password.get_text(),
184 self._loginResultCallback)
185
186 def _loginResultCallback(self, returned, result):
187 """The login result callback, called in the web handler's thread."""
188 gobject.idle_add(self._handleLoginResult, returned, result)
189
190 def _handleLoginResult(self, returned, result):
191 """Handle the login result."""
192 self._wizard.gui.endBusy()
193 if returned:
194 if result.loggedIn:
195 config = self._wizard.gui.config
196
197 config.pilotID = self._pilotID.get_text()
198
199 rememberPassword = self._rememberButton.get_active()
200 config.password = self._password.get_text() if rememberPassword \
201 else ""
202
203 config.rememberPassword = rememberPassword
204
205 config.save()
206 self._wizard._loginResult = result
207 self._wizard.nextPage()
208 else:
209 dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
210 buttons = BUTTONSTYPE_OK,
211 message_format =
212 "Invalid pilot's ID or password.")
213 dialog.format_secondary_markup("Check the ID and try to reenter"
214 " the password.")
215 dialog.run()
216 dialog.hide()
217 else:
218 dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
219 buttons = BUTTONSTYPE_OK,
220 message_format =
221 "Failed to connect to the MAVA website.")
222 dialog.format_secondary_markup("Try again in a few minutes.")
223 dialog.run()
224 dialog.hide()
225
226#-----------------------------------------------------------------------------
227
228class FlightSelectionPage(Page):
229 """The page to select the flight."""
230 def __init__(self, wizard):
231 """Construct the flight selection page."""
232 super(FlightSelectionPage, self).__init__(wizard, "Flight selection",
233 "Select the flight you want "
234 "to perform.")
235
236
237 self._listStore = gtk.ListStore(str, str, str, str)
238 self._flightList = gtk.TreeView(self._listStore)
239 column = gtk.TreeViewColumn("Flight no.", gtk.CellRendererText(),
240 text = 1)
241 column.set_expand(True)
242 self._flightList.append_column(column)
243 column = gtk.TreeViewColumn("Departure time [UTC]", gtk.CellRendererText(),
244 text = 0)
245 column.set_expand(True)
246 self._flightList.append_column(column)
247 column = gtk.TreeViewColumn("From", gtk.CellRendererText(),
248 text = 2)
249 column.set_expand(True)
250 self._flightList.append_column(column)
251 column = gtk.TreeViewColumn("To", gtk.CellRendererText(),
252 text = 3)
253 column.set_expand(True)
254 self._flightList.append_column(column)
255
256 flightSelection = self._flightList.get_selection()
257 flightSelection.connect("changed", self._selectionChanged)
258
259 scrolledWindow = gtk.ScrolledWindow()
260 scrolledWindow.add(self._flightList)
261 scrolledWindow.set_size_request(400, -1)
262 scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
263 else gtk.POLICY_AUTOMATIC,
264 gtk.PolicyType.ALWAYS if pygobject
265 else gtk.POLICY_ALWAYS)
266
267 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.0, xscale = 0.0, yscale = 1.0)
268 alignment.add(scrolledWindow)
269
270 self.setMainWidget(alignment)
271
272 self._button = self.addButton(gtk.STOCK_GO_FORWARD, default = True)
273 self._button.set_use_stock(True)
274 self._button.set_sensitive(False)
275
276 self._activated = False
277
278 def activate(self):
279 """Fill the flight list."""
280 if not self._activated:
281 for flight in self._wizard.loginResult.flights:
282 self._listStore.append([str(flight.departureTime),
283 flight.callsign,
284 flight.departureICAO,
285 flight.arrivalICAO])
286 self._activated = True
287
288 def _selectionChanged(self, selection):
289 """Called when the selection is changed."""
290 self._button.set_sensitive(selection.count_selected_rows()==1)
291
292#-----------------------------------------------------------------------------
293
294class Wizard(gtk.VBox):
295 """The flight wizard."""
296 def __init__(self, gui):
297 """Construct the wizard."""
298 super(Wizard, self).__init__()
299
300 self.gui = gui
301
302 self._pages = []
303 self._currentPage = None
304
305 self._pages.append(LoginPage(self))
306 self._pages.append(FlightSelectionPage(self))
307
308 self._loginResult = None
309
310 self.setCurrentPage(0)
311
312 @property
313 def loginResult(self):
314 """Get the login result."""
315 return self._loginResult
316
317 def setCurrentPage(self, index):
318 """Set the current page to the one with the given index."""
319 assert index < len(self._pages)
320
321 if self._currentPage is not None:
322 self.remove(self._pages[self._currentPage])
323
324 self._currentPage = index
325 self.add(self._pages[index])
326 self._pages[index].activate()
327 self.show_all()
328
329 def nextPage(self):
330 """Go to the next page."""
331 self.setCurrentPage(self._currentPage + 1)
332 self.grabDefault()
333
334 def grabDefault(self):
335 """Make the default button of the current page the default."""
336 self._pages[self._currentPage].grabDefault()
337
338#-----------------------------------------------------------------------------
339
Note: See TracBrowser for help on using the repository browser.