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

Fuel loading works

File:
1 edited

Legend:

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

    r139 r141  
    55import mlx.const as const
    66import mlx.fs as fs
     7import mlx.acft as acft
    78from mlx.checks import PayloadChecker
    89import mlx.util as util
     
    967968    def _forwardClicked(self, button):
    968969        """Called when the forward button is clicked."""
     970        if not self._completed:
     971            gui = self._wizard.gui
     972            gui.beginBusy(xstr("fuel_get_busy"))
     973           
     974            gui.simulator.getFuel(gui.flight.aircraft.fuelTanks,
     975                                  self._handleFuel)
     976        else:
     977            self._wizard.nextPage()
     978
     979    def _handleFuel(self, fuelData):
     980        """Callback for the fuel query operation."""
     981        gobject.idle_add(self._processFuel, fuelData)
     982
     983    def _processFuel(self, fuelData):
     984        """Process the given fuel data."""
     985        self._wizard.gui.endBusy()
     986        self._wizard._fuelData = fuelData
    969987        self._wizard.nextPage()
     988       
     989#-----------------------------------------------------------------------------
     990
     991class FuelTank(gtk.VBox):
     992    """Widget for the fuel tank."""
     993    def __init__(self, fuelTank, name, capacity, currentWeight):
     994        """Construct the widget for the tank with the given name."""
     995        super(FuelTank, self).__init__()
     996
     997        self.fuelTank = fuelTank
     998        self.capacity = capacity
     999        self.currentWeight = currentWeight
     1000        self.expectedWeight = currentWeight
     1001
     1002        label = gtk.Label("<b>" + name + "</b>")
     1003        label.set_use_markup(True)
     1004        label.set_justify(JUSTIFY_CENTER)
     1005        label.set_alignment(0.5, 1.0)
     1006        self.pack_start(label, False, False, 4)
     1007
     1008        self._tankFigure = gtk.DrawingArea()
     1009        self._tankFigure.set_size_request(38, -1)
     1010
     1011        if pygobject:
     1012            self._tankFigure.connect("draw", self._drawTankFigure)
     1013        else:
     1014            self._tankFigure.connect("expose_event", self._drawTankFigure)
     1015
     1016        alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
     1017                                  xscale = 0.0, yscale = 1.0)
     1018        alignment.add(self._tankFigure)
     1019
     1020        self.pack_start(alignment, True, True, 4)
     1021
     1022        self._expectedButton = gtk.SpinButton()
     1023        self._expectedButton.set_numeric(True)
     1024        self._expectedButton.set_range(0, self.capacity)
     1025        self._expectedButton.set_increments(10, 100)
     1026        self._expectedButton.set_value(currentWeight)
     1027        self._expectedButton.set_alignment(1.0)
     1028        self._expectedButton.set_width_chars(5)
     1029        self._expectedButton.connect("value-changed", self._expectedChanged)
     1030
     1031        alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
     1032                                  xscale = 0.0, yscale = 1.0)
     1033        alignment.add(self._expectedButton)
     1034        self.pack_start(alignment, False, False, 4)
     1035
     1036    def setCurrent(self, currentWeight):
     1037        """Set the current weight."""
     1038        self.currentWeight = currentWeight
     1039        self._redraw()
     1040
     1041    def isCorrect(self):
     1042        """Determine if the contents of the fuel tank are as expected"""
     1043        return abs(self.expectedWeight - self.currentWeight)<=1
     1044
     1045    def disable(self):
     1046        """Disable the fuel tank."""
     1047        self._expectedButton.set_sensitive(False)
     1048
     1049    def _redraw(self):
     1050        """Redraw the tank figure."""
     1051        self._tankFigure.queue_draw()
     1052
     1053    def _drawTankFigure(self, connStateArea, eventOrContext):
     1054        """Draw the tank figure."""
     1055        triangleSize = 5
     1056       
     1057        context = eventOrContext if pygobject else connStateArea.window.cairo_create()
     1058
     1059        width = connStateArea.get_allocated_width() if pygobject \
     1060                else connStateArea.allocation.width
     1061        height = connStateArea.get_allocated_height() if pygobject \
     1062                 else connStateArea.allocation.height
     1063
     1064        rectangleX0 = triangleSize
     1065        rectangleY0 = triangleSize
     1066        rectangleX1 = width - 1 - triangleSize
     1067        rectangleY1 = height - 1 - triangleSize
     1068        rectangleLineWidth = 2.0
     1069
     1070        context.set_source_rgb(0.0, 0.0, 0.0)
     1071        context.set_line_width(rectangleLineWidth)
     1072        context.rectangle(rectangleX0 + rectangleLineWidth/2,
     1073                          rectangleY0 + rectangleLineWidth/2,
     1074                          rectangleX1 - rectangleX0 - rectangleLineWidth,
     1075                          rectangleY1 - rectangleY0 - rectangleLineWidth)
     1076        context.stroke()
     1077
     1078        rectangleInnerLeft   = rectangleX0 + rectangleLineWidth
     1079        rectangleInnerRight  = rectangleX1 - rectangleLineWidth
     1080        rectangleInnerTop    = rectangleY0 + rectangleLineWidth
     1081        rectangleInnerBottom = rectangleY1 - rectangleLineWidth
     1082
     1083        rectangleInnerWidth = rectangleInnerRight - rectangleInnerLeft
     1084        rectangleInnerHeight = rectangleInnerBottom - rectangleInnerTop
     1085
     1086        context.set_source_rgb(1.0, 0.9, 0.6)
     1087        currentHeight = self.currentWeight * rectangleInnerHeight / self.capacity
     1088        currentX = rectangleInnerTop + rectangleInnerHeight - currentHeight
     1089        context.rectangle(rectangleInnerLeft,
     1090                          rectangleInnerTop + rectangleInnerHeight - currentHeight,
     1091                          rectangleInnerWidth, currentHeight)
     1092        context.fill()
     1093
     1094        expectedHeight = self.expectedWeight * rectangleInnerHeight / self.capacity
     1095        expectedY = rectangleInnerTop + rectangleInnerHeight - expectedHeight
     1096
     1097        context.set_line_width(1.5)
     1098        context.set_source_rgb(0.0, 0.85, 0.85)
     1099        context.move_to(rectangleX0, expectedY)
     1100        context.line_to(rectangleX1, expectedY)
     1101        context.stroke()
     1102
     1103        context.set_line_width(0.0)
     1104        context.move_to(0, expectedY - triangleSize)
     1105        context.line_to(0, expectedY + triangleSize)
     1106        context.line_to(rectangleX0 + 1, expectedY)
     1107        context.line_to(0, expectedY - triangleSize)
     1108        context.fill()
     1109
     1110        context.set_line_width(0.0)
     1111        context.move_to(width, expectedY - triangleSize)
     1112        context.line_to(width, expectedY + triangleSize)
     1113        context.line_to(rectangleX1 - 1, expectedY)
     1114        context.line_to(width, expectedY - triangleSize)
     1115        context.fill()
     1116
     1117    def _expectedChanged(self, spinButton):
     1118        """Called when the expected value has changed."""
     1119        self.expectedWeight = spinButton.get_value_as_int()
     1120        self._redraw()       
     1121
     1122#-----------------------------------------------------------------------------
     1123
     1124class FuelPage(Page):
     1125    """The page containing the fuel tank filling."""
     1126    _pumpStep = 0.02
     1127   
     1128    def __init__(self, wizard):
     1129        """Construct the page."""
     1130        super(FuelPage, self).__init__(wizard, xstr("fuel_title"),
     1131                                       xstr("fuel_help"),
     1132                                       completedHelp = xstr("fuel_chelp"))
     1133
     1134        self._fuelTanks = []
     1135        self._fuelTable = None
     1136        self._fuelAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
     1137                                            xscale = 0.0, yscale = 1.0)
     1138        self.setMainWidget(self._fuelAlignment)
     1139
     1140        tanks = acft.MostFuelTankAircraft.fuelTanks
     1141        tankData = ((2500, 3900),) * len(tanks)
     1142        self._setupTanks(tanks, tankData)
     1143
     1144        self._backButton = self.addPreviousButton(clicked = self._backClicked)
     1145        self._button = self.addNextButton(clicked = self._forwardClicked)
     1146
     1147        self._pumpIndex = 0
     1148
     1149    def activate(self):
     1150        """Activate the page."""
     1151        gui = self._wizard.gui
     1152
     1153        self._setupTanks(gui.flight.aircraft.fuelTanks,
     1154                         self._wizard._fuelData)
     1155
     1156    def finalize(self):
     1157        """Finalize the page."""
     1158        for fuelTank in self._fuelTanks:
     1159            fuelTank.disable()
     1160
     1161    def _backClicked(self, button):
     1162        """Called when the Back button is pressed."""
     1163        self.goBack()
     1164       
     1165    def _forwardClicked(self, button):
     1166        """Called when the forward button is clicked."""
     1167        if not self._completed:
     1168            self._pumpIndex = 0
     1169            self._wizard.gui.beginBusy(xstr("fuel_pump_busy"))
     1170            self._pump()
     1171        else:
     1172            self._wizard.nextPage()       
     1173
     1174    def _setupTanks(self, tanks, tankData):
     1175        """Setup the tanks for the given data."""
     1176        numTanks = len(tanks)
     1177        if self._fuelTable is not None:
     1178            self._fuelAlignment.remove(self._fuelTable)
     1179
     1180        self._fuelTanks = []
     1181        self._fuelTable = gtk.Table(numTanks, 1)
     1182        self._fuelTable.set_col_spacings(16)
     1183        for i in range(0, numTanks):
     1184            tank = tanks[i]
     1185            (current, capacity) = tankData[i]
     1186
     1187            fuelTank = FuelTank(tank,
     1188                                xstr("fuel_tank_" +
     1189                                     const.fuelTank2string(tank)),
     1190                                capacity, current)
     1191            self._fuelTable.attach(fuelTank, i, i+1, 0, 1)
     1192            self._fuelTanks.append(fuelTank)
     1193           
     1194        self._fuelAlignment.add(self._fuelTable)
     1195        self.show_all()
     1196
     1197    def _pump(self):
     1198        """Perform one step of pumping.
     1199
     1200        It is checked, if the current tank's contents are of the right
     1201        quantity. If not, it is filled one step further to the desired
     1202        contents. Otherwise the next tank is started. If all tanks are are
     1203        filled, the next page is selected."""
     1204        numTanks = len(self._fuelTanks)
     1205
     1206        fuelTank = None
     1207        while self._pumpIndex < numTanks:
     1208            fuelTank = self._fuelTanks[self._pumpIndex]
     1209            if fuelTank.isCorrect():
     1210                self._pumpIndex += 1
     1211                fuelTank = None
     1212            else:
     1213                break
     1214
     1215        if fuelTank is None:
     1216            self._wizard.gui.endBusy()
     1217            self._wizard.nextPage()
     1218        else:
     1219            currentLevel = fuelTank.currentWeight / fuelTank.capacity
     1220            expectedLevel = fuelTank.expectedWeight / fuelTank.capacity
     1221            if currentLevel<expectedLevel:
     1222                currentLevel += FuelPage._pumpStep
     1223                if currentLevel>expectedLevel: currentLevel = expectedLevel
     1224            else:
     1225                currentLevel -= FuelPage._pumpStep
     1226                if currentLevel<expectedLevel: currentLevel = expectedLevel
     1227            fuelTank.setCurrent(currentLevel * fuelTank.capacity)
     1228            self._wizard.gui.simulator.setFuelLevel([(fuelTank.fuelTank,
     1229                                                      currentLevel)])
     1230            gobject.timeout_add(50, self._pump)
    9701231       
    9711232#-----------------------------------------------------------------------------
     
    9741235    """The page containing the route and the flight level."""
    9751236    def __init__(self, wizard):
     1237        """Construct the page."""
    9761238        super(RoutePage, self).__init__(wizard, xstr("route_title"),
    9771239                                        xstr("route_help"),
     
    20452307        self._payloadIndex = len(self._pages)
    20462308        self._pages.append(TimePage(self))
     2309        self._pages.append(FuelPage(self))
    20472310        self._routePage = RoutePage(self)
    20482311        self._pages.append(self._routePage)
     
    22422505        self._bookedFlight = None
    22432506        self._departureGate = "-"
     2507        self._fuelData = None
    22442508        self._departureNOTAMs = None
    22452509        self._departureMETAR = None
Note: See TracChangeset for help on using the changeset viewer.