Ignore:
Timestamp:
05/16/12 18:04:55 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added support for the entrance exam

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/gui/flight.py

    r170 r184  
    1111from mlx.i18n import xstr
    1212from mlx.sound import startSound
     13import mlx.web as web
    1314
    1415import datetime
     
    215216                                  xscale = 0.0, yscale = 0.0)
    216217
    217         table = gtk.Table(2, 3)
     218        table = gtk.Table(4, 2)
    218219        table.set_row_spacings(4)
    219220        table.set_col_spacings(32)
     
    229230
    230231        self._pilotID = gtk.Entry()
    231         self._pilotID.connect("changed", self._setLoginButton)
     232        self._pilotID.connect("changed", self._setControls)
    232233        self._pilotID.set_tooltip_text(xstr("login_pilotID_tooltip"))
    233234        table.attach(self._pilotID, 1, 2, 0, 1)
     
    243244        self._password = gtk.Entry()
    244245        self._password.set_visibility(False)
    245         self._password.connect("changed", self._setLoginButton)
     246        self._password.connect("changed", self._setControls)
    246247        self._password.set_tooltip_text(xstr("login_password_tooltip"))
    247248        table.attach(self._password, 1, 2, 1, 2)
     
    253254        table.attach(self._rememberButton, 1, 2, 2, 3, ypadding = 8)
    254255
     256        self._entranceExam = gtk.CheckButton(xstr("login_entranceExam"))
     257        self._entranceExam.set_use_underline(True)
     258        self._entranceExam.set_tooltip_text(xstr("login_entranceExam_tooltip"))
     259        self._entranceExam.connect("toggled", self._setControls)
     260        table.attach(self._entranceExam, 1, 2, 3, 4, ypadding = 12)
     261
    255262        self._loginButton = self.addButton(xstr("button_login"), default = True)
    256         self._loginButton.set_sensitive(False)
    257263        self._loginButton.connect("clicked", self._loginClicked)
    258         self._loginButton.set_tooltip_text(xstr("login_button_tooltip"))       
     264        self._loginButton.set_tooltip_text(xstr("login_button_tooltip"))
     265
     266
     267    @property
     268    def entranceExam(self):
     269        """Get whether an entrance exam is being performed."""
     270        return self._entranceExam.get_active() and \
     271               self._pilotID.get_text()!=""
    259272
    260273    def activate(self):
     
    264277        self._password.set_text(config.password)
    265278        self._rememberButton.set_active(config.rememberPassword)
    266 
    267     def _setLoginButton(self, entry):
    268         """Set the login button's sensitivity.
    269 
    270         The button is sensitive only if both the pilot ID and the password
    271         fields contain values."""
    272         self._loginButton.set_sensitive(self._pilotID.get_text()!="" and
    273                                         self._password.get_text()!="")
     279        self._setControls(None)   
     280
     281    def _setControls(self, entry = None):
     282        """Set the sensitivity of the various controls.
     283
     284        The login button is sensitive only if both the pilot ID and the
     285        password fields contain values.
     286
     287        The password field is sensitive only, if the entrance exam checkbox is
     288        not selected.
     289
     290        The remember password checkbox is sensitive only, if the password field
     291        contains text.
     292
     293        The entrance exam checkbox is senstive only, if the pilot ID is not
     294        empty."""
     295        pilotID = self._pilotID.get_text()
     296        password = self._password.get_text()
     297        entranceExam = self._entranceExam.get_active()
     298        self._password.set_sensitive(not entranceExam)
     299        self._rememberButton.set_sensitive(password!="" and not entranceExam)
     300        self._entranceExam.set_sensitive(pilotID!="")
     301        self._loginButton.set_sensitive(pilotID!="" and
     302                                        (password!="" or entranceExam))
    274303
    275304    def _loginClicked(self, button):
    276305        """Called when the login button was clicked."""
    277         self._loginButton.set_sensitive(False)
    278306        gui = self._wizard.gui
    279307        gui.beginBusy(xstr("login_busy"))
    280308        gui.webHandler.login(self._loginResultCallback,
    281                             self._pilotID.get_text(),
    282                             self._password.get_text())
     309                             self._pilotID.get_text(),
     310                             self._password.get_text(),
     311                             entranceExam = self.entranceExam)
    283312
    284313    def _loginResultCallback(self, returned, result):
     
    306335                self._wizard.nextPage()
    307336            else:
     337                message = xstr("login_entranceExam_invalid"
     338                               if self.entranceExam else
     339                               xstr("login_invalid"))
    308340                dialog = gtk.MessageDialog(parent = self._wizard.gui.mainWindow,
    309341                                           type = MESSAGETYPE_ERROR,
    310                                            message_format = xstr("login_invalid"))
     342                                           message_format = message)
    311343                dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
    312344                dialog.set_title(WINDOW_TITLE_BASE)
    313                 dialog.format_secondary_markup(xstr("login_invalid_sec"))
     345                secondary = xstr("login_entranceExam_invalid_sec"
     346                                 if self.entranceExam else
     347                                 xstr("login_invalid_sec"))
     348                dialog.format_secondary_markup(secondary)
    314349                dialog.run()
    315350                dialog.hide()
     
    375410        self.setMainWidget(alignment)
    376411
     412        self._loadButton = self.addButton(xstr("flightsel_load"),
     413                                          sensitive = True,
     414                                          tooltip = xstr("flightsel_load_tooltip"))
     415        self._loadButton.connect("clicked", self._loadButtonClicked)
     416        self._loadDialog = None
     417       
    377418        self._button = self.addNextButton(sensitive = False,
    378419                                          clicked =  self._forwardClicked)
     420
     421        self._flights = []
    379422
    380423    def activate(self):
    381424        """Fill the flight list."""
    382425        self._flightList.set_sensitive(True)
     426        self._flights = []
    383427        self._listStore.clear()
    384428        for flight in self._wizard.loginResult.flights:
    385             self._listStore.append([str(flight.departureTime),
    386                                     flight.callsign,
    387                                     flight.departureICAO,
    388                                     flight.arrivalICAO])
    389 
     429            self._addFlight(flight)
     430           
    390431    def finalize(self):
    391432        """Finalize the page."""
    392433        self._flightList.set_sensitive(False)
    393434
     435    def _addFlight(self, flight):
     436        """Add the given file to the list of flights."""
     437        self._flights.append(flight)
     438        self._listStore.append([str(flight.departureTime),
     439                                flight.callsign,
     440                                flight.departureICAO,
     441                                flight.arrivalICAO])
     442
    394443    def _selectionChanged(self, selection):
    395444        """Called when the selection is changed."""
    396445        self._button.set_sensitive(selection.count_selected_rows()==1)
    397446
     447    def _loadButtonClicked(self, loadButton):
     448        """Called when the load a flight button is clicked."""
     449        dialog = self._getLoadDialog()
     450        dialog.show_all()
     451        response = dialog.run()
     452        dialog.hide()
     453
     454        if response==RESPONSETYPE_OK:
     455            fileName = dialog.get_filename()
     456            print "Loading", fileName
     457            bookedFlight = web.BookedFlight()
     458            try:
     459                with open(fileName, "rt") as f:
     460                    bookedFlight.readFromFile(f)
     461                self._addFlight(bookedFlight)               
     462            except Exception, e:
     463                print "Failed to load flight:", str(e)
     464                dialog = gtk.MessageDialog(parent = self._wizard.gui.mainWindow,
     465                                           type = MESSAGETYPE_ERROR,
     466                                           message_format =
     467                                           xstr("flightsel_load_failed"))
     468                dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
     469                dialog.set_title(WINDOW_TITLE_BASE)
     470                secondary = xstr("flightsel_load_failed_sec")
     471                dialog.format_secondary_markup(secondary)
     472                dialog.run()
     473                dialog.hide()               
     474           
    398475    def _forwardClicked(self, button):
    399476        """Called when the forward button was clicked."""
     
    406483            [index] = path.get_indices() if pygobject else path
    407484
    408             flight = self._wizard.loginResult.flights[index]
     485            flight = self._flights[index]
    409486            self._wizard._bookedFlight = flight
    410487            self._wizard.gui.enableFlightInfo()
     
    415492        """Update the departure gate for the booked flight."""
    416493        flight = self._wizard._bookedFlight
    417         if self._wizard.gui.config.onlineGateSystem:
     494        if self._wizard.gui.config.onlineGateSystem and \
     495           not self._wizard.entranceExam:
    418496            if flight.departureICAO=="LHBP":
    419497                self._wizard.getFleet(self._fleetRetrieved)
     
    449527        self._nextDistance = 2
    450528        self._wizard.jumpPage(2)
    451        
     529
     530    def _getLoadDialog(self):
     531        """Get the dialog to load a flight file."""
     532        if self._loadDialog is not None:
     533            return self._loadDialog
     534       
     535        gui = self._wizard.gui
     536        dialog = gtk.FileChooserDialog(title = WINDOW_TITLE_BASE + " - " +
     537                                       xstr("flightsel_load_title"),
     538                                       action = FILE_CHOOSER_ACTION_OPEN,
     539                                       buttons = (gtk.STOCK_CANCEL,
     540                                                  RESPONSETYPE_CANCEL,
     541                                                  gtk.STOCK_OK, RESPONSETYPE_OK),
     542                                       parent = gui.mainWindow)
     543        dialog.set_modal(True)           
     544
     545        filter = gtk.FileFilter()
     546        filter.set_name(xstr("flightsel_filter_flights"))
     547        filter.add_pattern("*.vaflight")
     548        dialog.add_filter(filter)
     549           
     550        filter = gtk.FileFilter()
     551        filter.set_name(xstr("file_filter_all"))
     552        filter.add_pattern("*.*")
     553        dialog.add_filter(filter)
     554
     555        self._loadDialog = dialog
     556       
     557        return dialog       
     558   
    452559#-----------------------------------------------------------------------------
    453560
     
    20362143        if self._wizard.gui.config.onlineGateSystem and \
    20372144           not self._completed and \
    2038            self._wizard.bookedFlight.arrivalICAO=="LHBP":
     2145           self._wizard.bookedFlight.arrivalICAO=="LHBP" and \
     2146           not self._wizard.entranceExam:
    20392147            self._wizard.getFleet(callback = self._fleetRetrieved,
    20402148                                  force = True)
     
    22382346        self._gatesModel.clear()
    22392347        if self._wizard.gui.config.onlineGateSystem and \
    2240            self._wizard.bookedFlight.arrivalICAO=="LHBP":
     2348           self._wizard.bookedFlight.arrivalICAO=="LHBP" and \
     2349           not self._wizard.entranceExam:
    22412350            occupiedGates = self._wizard._fleet.getOccupiedGateNumbers()
    22422351            for gateNumber in const.lhbpGateNumbers:
     
    22612370       
    22622371        self._saveButton.set_sensitive(sensitive)
    2263         self._sendButton.set_sensitive(sensitive)       
     2372        self._sendButton.set_sensitive(sensitive and
     2373                                       self._wizard.bookedFlight.id is not None)
    22642374
    22652375    def _flightTypeChanged(self, comboBox):
     
    23392449           
    23402450            filter = gtk.FileFilter()
    2341             filter.set_name(xstr("loadPIREP_filter_pireps"))
     2451            filter.set_name(xstr("file_filter_pireps"))
    23422452            filter.add_pattern("*.pirep")
    23432453            dialog.add_filter(filter)
    23442454           
    23452455            filter = gtk.FileFilter()
    2346             filter.set_name(xstr("loadPIREP_filter_all"))
     2456            filter.set_name(xstr("file_filter_all"))
    23472457            filter.add_pattern("*.*")
    23482458            dialog.add_filter(filter)
     
    23612471    def _handlePIREPSent(self, returned, result):
    23622472        """Callback for the PIREP sending result."""
    2363         if self._wizard.gui.config.onlineGateSystem and returned and result.success:
     2473        if self._wizard.gui.config.onlineGateSystem and \
     2474           not self._wizard.entranceExam and \
     2475           returned and result.success:
    23642476            bookedFlight = self._wizard.bookedFlight
    23652477            if bookedFlight.arrivalICAO=="LHBP":
     
    23942506        self._pages = []
    23952507        self._currentPage = None
    2396        
    2397         self._pages.append(LoginPage(self))
     2508
     2509        self._loginPage = LoginPage(self)
     2510        self._pages.append(self._loginPage)
    23982511        self._pages.append(FlightSelectionPage(self))
    23992512        self._pages.append(GateSelectionPage(self))
     
    24322545
    24332546        self._initialize()
     2547
     2548    @property
     2549    def entranceExam(self):
     2550        """Get whether an entrance exam is about to be taken."""
     2551        return self._loginPage.entranceExam
    24342552       
    24352553    @property
Note: See TracChangeset for help on using the changeset viewer.