1 | # The flight handling "wizard"
|
---|
2 |
|
---|
3 | from mlx.gui.common import *
|
---|
4 |
|
---|
5 | #-----------------------------------------------------------------------------
|
---|
6 |
|
---|
7 | class 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 | print self.get_style()
|
---|
20 | style = self.get_style() if pygobject else self.rc_get_style()
|
---|
21 |
|
---|
22 | self._vbox = gtk.VBox()
|
---|
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 | alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
42 | xscale = 0, yscale = 0.3)
|
---|
43 | label = gtk.Label(help)
|
---|
44 | label.set_justify(gtk.Justification.CENTER if pygobject
|
---|
45 | else gtk.JUSTIFY_CENTER)
|
---|
46 | alignment.add(label)
|
---|
47 | self._vbox.pack_start(alignment, True, True, 0)
|
---|
48 |
|
---|
49 | self._mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
50 | xscale = 0, yscale = 0.0)
|
---|
51 | self._vbox.pack_start(self._mainAlignment, True, True, 0)
|
---|
52 |
|
---|
53 | buttonAlignment = gtk.Alignment(xalign = 1.0, xscale=0.0)
|
---|
54 | buttonAlignment.set_padding(padding_top = 4, padding_bottom = 10,
|
---|
55 | padding_left = 16, padding_right = 16)
|
---|
56 |
|
---|
57 | self._buttonBox = gtk.HButtonBox()
|
---|
58 | buttonAlignment.add(self._buttonBox)
|
---|
59 |
|
---|
60 | self._vbox.pack_start(buttonAlignment, False, False, 0)
|
---|
61 |
|
---|
62 | self._wizard = wizard
|
---|
63 |
|
---|
64 | def setMainWidget(self, widget):
|
---|
65 | """Set the given widget as the main one."""
|
---|
66 | self._mainAlignment.add(widget)
|
---|
67 |
|
---|
68 | def addButton(self, label):
|
---|
69 | """Add a button with the given label.
|
---|
70 |
|
---|
71 | Return the button object created."""
|
---|
72 | button = gtk.Button(label)
|
---|
73 | self._buttonBox.add(button)
|
---|
74 | return button
|
---|
75 |
|
---|
76 | #-----------------------------------------------------------------------------
|
---|
77 |
|
---|
78 | class LoginPage(Page):
|
---|
79 | """The login page."""
|
---|
80 | def __init__(self, wizard):
|
---|
81 | """Construct the login page."""
|
---|
82 | help = "Enter your MAVA pilot's ID and password to\n" \
|
---|
83 | "log in to the MAVA website and download\n" \
|
---|
84 | "your booked flights."
|
---|
85 | super(LoginPage, self).__init__(wizard, "Login", help)
|
---|
86 |
|
---|
87 | table = gtk.Table(2, 2)
|
---|
88 | table.set_row_spacings(4)
|
---|
89 | table.set_col_spacings(32)
|
---|
90 | self.setMainWidget(table)
|
---|
91 |
|
---|
92 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
93 | labelAlignment.add(gtk.Label("Pilot ID:"))
|
---|
94 | table.attach(labelAlignment, 0, 1, 0, 1)
|
---|
95 |
|
---|
96 | self._pilotID = gtk.Entry()
|
---|
97 | self._pilotID.connect("changed", self._setLoginButton)
|
---|
98 | table.attach(self._pilotID, 1, 2, 0, 1)
|
---|
99 |
|
---|
100 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
101 | labelAlignment.add(gtk.Label("Password:"))
|
---|
102 | table.attach(labelAlignment, 0, 1, 1, 2)
|
---|
103 |
|
---|
104 | self._password = gtk.Entry()
|
---|
105 | self._password.set_visibility(False)
|
---|
106 | self._password.connect("changed", self._setLoginButton)
|
---|
107 | table.attach(self._password, 1, 2, 1, 2)
|
---|
108 |
|
---|
109 | self._loginButton = self.addButton("Login")
|
---|
110 | self._loginButton.set_sensitive(False)
|
---|
111 | self._loginButton.connect("clicked", self._loginClicked)
|
---|
112 |
|
---|
113 | config = self._wizard.gui.config
|
---|
114 | self._pilotID.set_text(config.pilotID)
|
---|
115 | self._password.set_text(config.password)
|
---|
116 |
|
---|
117 | def _setLoginButton(self, entry):
|
---|
118 | """Set the login button's sensitivity.
|
---|
119 |
|
---|
120 | The button is sensitive only if both the pilot ID and the password
|
---|
121 | fields contain values."""
|
---|
122 | self._loginButton.set_sensitive(self._pilotID.get_text()!="" and
|
---|
123 | self._password.get_text()!="")
|
---|
124 |
|
---|
125 | def _loginClicked(self, button):
|
---|
126 | """Called when the login button was clicked."""
|
---|
127 | self._wizard.gui.webHandler.login(self._pilotID.get_text(),
|
---|
128 | self._password.get_text(),
|
---|
129 | self._loginResultCallback)
|
---|
130 |
|
---|
131 | def _loginResultCallback(self, returned, result):
|
---|
132 | """The login result callback, called in the web handler's thread."""
|
---|
133 | gobject.idle_add(self._handleLoginResult, returned, result)
|
---|
134 |
|
---|
135 | def _handleLoginResult(self, returned, result):
|
---|
136 | """Handle the login result."""
|
---|
137 | if returned:
|
---|
138 | if result.loggedIn:
|
---|
139 | config = self._wizard.gui.config
|
---|
140 | config.pilotID = self._pilotID.get_text()
|
---|
141 | config.password = self._password.get_text()
|
---|
142 | config.save()
|
---|
143 | self._wizard.nextPage()
|
---|
144 | else:
|
---|
145 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
146 | buttons = BUTTONSTYPE_OK,
|
---|
147 | message_format =
|
---|
148 | "Invalid pilot's ID or password.")
|
---|
149 | dialog.format_secondary_markup("Check the ID and try to reenter"
|
---|
150 | " the password.")
|
---|
151 | dialog.run()
|
---|
152 | dialog.hide()
|
---|
153 | else:
|
---|
154 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
155 | buttons = BUTTONSTYPE_OK,
|
---|
156 | message_format =
|
---|
157 | "Failed to connect to the MAVA website.")
|
---|
158 | dialog.format_secondary_markup("Try again in a few minutes.")
|
---|
159 | dialog.run()
|
---|
160 | dialog.hide()
|
---|
161 |
|
---|
162 | #-----------------------------------------------------------------------------
|
---|
163 |
|
---|
164 | class FlightSelectionPage(Page):
|
---|
165 | """The page to select the flight."""
|
---|
166 | def __init__(self, wizard):
|
---|
167 | """Construct the flight selection page."""
|
---|
168 | super(FlightSelectionPage, self).__init__(wizard, "Flight selection",
|
---|
169 | "Hello, te lo!")
|
---|
170 |
|
---|
171 | #-----------------------------------------------------------------------------
|
---|
172 |
|
---|
173 | class Wizard(gtk.VBox):
|
---|
174 | """The flight wizard."""
|
---|
175 | def __init__(self, gui):
|
---|
176 | """Construct the wizard."""
|
---|
177 | super(Wizard, self).__init__()
|
---|
178 |
|
---|
179 | self.gui = gui
|
---|
180 |
|
---|
181 | self._pages = []
|
---|
182 | self._currentPage = None
|
---|
183 |
|
---|
184 | self._pages.append(LoginPage(self))
|
---|
185 | self._pages.append(FlightSelectionPage(self))
|
---|
186 |
|
---|
187 | self.setCurrentPage(0)
|
---|
188 |
|
---|
189 | def setCurrentPage(self, index):
|
---|
190 | """Set the current page to the one with the given index."""
|
---|
191 | assert index < len(self._pages)
|
---|
192 |
|
---|
193 | if self._currentPage is not None:
|
---|
194 | self.remove(self._pages[self._currentPage])
|
---|
195 |
|
---|
196 | self._currentPage = index
|
---|
197 | self.add(self._pages[index])
|
---|
198 | self.show_all()
|
---|
199 |
|
---|
200 | def nextPage(self):
|
---|
201 | """Go to the next page."""
|
---|
202 | self.setCurrentPage(self._currentPage + 1)
|
---|
203 |
|
---|
204 | #-----------------------------------------------------------------------------
|
---|
205 |
|
---|