diff --git a/gui/builtinAdditionPanes/cargoView.py b/gui/builtinAdditionPanes/cargoView.py index d324701a8..c5eb20af8 100644 --- a/gui/builtinAdditionPanes/cargoView.py +++ b/gui/builtinAdditionPanes/cargoView.py @@ -148,9 +148,9 @@ class CargoView(d.Display): return self.original = fit.cargo if fit is not None else None - self.cargo = stuff = fit.cargo[:] if fit is not None else None - if stuff is not None: - stuff.sort(key=lambda c: (c.item.group.category.name, c.item.group.name, c.item.name)) + self.cargo = fit.cargo[:] if fit is not None else None + if self.cargo is not None: + self.cargo.sort(key=lambda c: (c.item.group.category.name, c.item.group.name, c.item.name)) if event.fitID != self.lastFitId: self.lastFitId = event.fitID @@ -162,8 +162,8 @@ class CargoView(d.Display): self.deselectItems() - self.populate(stuff) - self.refresh(stuff) + self.populate(self.cargo) + self.refresh(self.cargo) event.Skip() def removeItem(self, event): @@ -180,7 +180,7 @@ class CargoView(d.Display): if sel != -1: sFit = Fit.getInstance() fit = sFit.getFit(self.mainFrame.getActiveFit()) - cargo = fit.cargo[sel] + cargo = self.cargo[sel] sMkt = Market.getInstance() sourceContext = "cargoItem" diff --git a/gui/builtinAdditionPanes/droneView.py b/gui/builtinAdditionPanes/droneView.py index fe509f0b3..9b8e29931 100644 --- a/gui/builtinAdditionPanes/droneView.py +++ b/gui/builtinAdditionPanes/droneView.py @@ -184,10 +184,10 @@ class DroneView(Display): return self.original = fit.drones if fit is not None else None - self.drones = stuff = fit.drones[:] if fit is not None else None + self.drones = fit.drones[:] if fit is not None else None - if stuff is not None: - stuff.sort(key=self.droneKey) + if self.drones is not None: + self.drones.sort(key=self.droneKey) if event.fitID != self.lastFitId: self.lastFitId = event.fitID @@ -199,7 +199,7 @@ class DroneView(Display): self.deselectItems() - self.update(stuff) + self.update(self.drones) event.Skip() def addItem(self, event): diff --git a/gui/builtinAdditionPanes/implantView.py b/gui/builtinAdditionPanes/implantView.py index c3a294467..5a6db2ff4 100644 --- a/gui/builtinAdditionPanes/implantView.py +++ b/gui/builtinAdditionPanes/implantView.py @@ -156,9 +156,9 @@ class ImplantDisplay(d.Display): return self.original = fit.implants if fit is not None else None - self.implants = stuff = fit.appliedImplants[:] if fit is not None else None - if stuff is not None: - stuff.sort(key=lambda implant: implant.slot) + self.implants = fit.appliedImplants[:] if fit is not None else None + if self.implants is not None: + self.implants.sort(key=lambda implant: implant.slot) if event.fitID != self.lastFitId: self.lastFitId = event.fitID @@ -170,7 +170,7 @@ class ImplantDisplay(d.Display): self.deselectItems() - self.update(stuff) + self.update(self.implants) event.Skip() def addItem(self, event): diff --git a/gui/builtinContextMenus/amount.py b/gui/builtinContextMenus/amount.py index 3a70896f9..ba61afa38 100644 --- a/gui/builtinContextMenus/amount.py +++ b/gui/builtinContextMenus/amount.py @@ -48,7 +48,7 @@ class ChangeAmount(ContextMenu): cleanInput = re.sub(r'[^0-9.]', '', dlg.input.GetLineText(0).strip()) if isinstance(thing, es_Cargo): - self.mainFrame.command.Submit(cmd.GuiChangeCargoQty(fitID, fit.cargo.index(thing), int(float(cleanInput)))) + self.mainFrame.command.Submit(cmd.GuiChangeCargoQty(fitID, thing.itemID, int(float(cleanInput)))) return # no need for post event here elif isinstance(thing, Drone): if srcContext == "droneItem": diff --git a/gui/fitCommands/calc/fitAddBooster.py b/gui/fitCommands/calc/fitAddBooster.py index 4bb817f84..a460d05fe 100644 --- a/gui/fitCommands/calc/fitAddBooster.py +++ b/gui/fitCommands/calc/fitAddBooster.py @@ -38,10 +38,7 @@ class FitAddBoosterCommand(wx.Command): fit.boosters.insert(self.newPosition, newBooster) except HandledListActionError: pyfalog.warning('Failed to insert to list') - cmd = FitAddBoosterCommand( - fitID=self.fitID, - boosterInfo=self.oldBoosterInfo, - position=self.oldPosition) + cmd = FitAddBoosterCommand(fitID=self.fitID, boosterInfo=self.oldBoosterInfo, position=self.oldPosition) cmd.Do() return False else: @@ -49,10 +46,7 @@ class FitAddBoosterCommand(wx.Command): fit.boosters.append(newBooster) except HandledListActionError: pyfalog.warning('Failed to append to list') - cmd = FitAddBoosterCommand( - fitID=self.fitID, - boosterInfo=self.oldBoosterInfo, - position=self.oldPosition) + cmd = FitAddBoosterCommand(fitID=self.fitID, boosterInfo=self.oldBoosterInfo, position=self.oldPosition) cmd.Do() return False self.newPosition = fit.boosters.index(newBooster) @@ -63,10 +57,7 @@ class FitAddBoosterCommand(wx.Command): def Undo(self): pyfalog.debug('Undo addition of booster {} to fit {}'.format(self.newBoosterInfo, self.fitID)) if self.oldBoosterInfo and self.oldPosition: - cmd = FitAddBoosterCommand( - fitID=self.fitID, - boosterInfo=self.oldBoosterInfo, - position=self.oldPosition) + cmd = FitAddBoosterCommand(fitID=self.fitID, boosterInfo=self.oldBoosterInfo, position=self.oldPosition) return cmd.Do() from .fitRemoveBooster import FitRemoveBoosterCommand cmd = FitRemoveBoosterCommand(fitID=self.fitID, position=self.newPosition) diff --git a/gui/fitCommands/calc/fitAddCargo.py b/gui/fitCommands/calc/fitAddCargo.py index 2f334dc5c..91c1a5705 100644 --- a/gui/fitCommands/calc/fitAddCargo.py +++ b/gui/fitCommands/calc/fitAddCargo.py @@ -20,7 +20,9 @@ class FitAddCargoCommand(wx.Command): pyfalog.debug('Doing addition of cargo {} to fit {}'.format(self.cargoInfo, self.fitID)) fit = Fit.getInstance().getFit(self.fitID) cargo = next((c for c in fit.cargo if c.itemID == self.cargoInfo.itemID), None) - if cargo is None: + if cargo is not None: + cargo.amount += self.cargoInfo.amount + else: cargo = self.cargoInfo.toCargo() try: fit.cargo.append(cargo) @@ -28,8 +30,6 @@ class FitAddCargoCommand(wx.Command): pyfalog.warning('Failed to append to list') eos.db.commit() return False - else: - cargo.amount += self.cargoInfo.amount eos.db.commit() return True diff --git a/gui/fitCommands/calc/fitAddImplant.py b/gui/fitCommands/calc/fitAddImplant.py index ef5d8b37f..fa440bb9d 100644 --- a/gui/fitCommands/calc/fitAddImplant.py +++ b/gui/fitCommands/calc/fitAddImplant.py @@ -38,10 +38,7 @@ class FitAddImplantCommand(wx.Command): fit.implants.insert(self.newPosition, newImplant) except HandledListActionError: pyfalog.warning('Failed to insert to list') - cmd = FitAddImplantCommand( - fitID=self.fitID, - implantInfo=self.oldImplantInfo, - position=self.oldPosition) + cmd = FitAddImplantCommand(fitID=self.fitID, implantInfo=self.oldImplantInfo, position=self.oldPosition) cmd.Do() return False else: @@ -49,10 +46,7 @@ class FitAddImplantCommand(wx.Command): fit.implants.append(newImplant) except HandledListActionError: pyfalog.warning('Failed to append to list') - cmd = FitAddImplantCommand( - fitID=self.fitID, - implantInfo=self.oldImplantInfo, - position=self.oldPosition) + cmd = FitAddImplantCommand(fitID=self.fitID, implantInfo=self.oldImplantInfo, position=self.oldPosition) cmd.Do() return False self.newPosition = fit.implants.index(newImplant) @@ -62,10 +56,7 @@ class FitAddImplantCommand(wx.Command): def Undo(self): pyfalog.debug('Undo addition of implant {} to fit {}'.format(self.newImplantInfo, self.fitID)) if self.oldImplantInfo and self.oldPosition: - cmd = FitAddImplantCommand( - fitID=self.fitID, - implantInfo=self.oldImplantInfo, - position=self.oldPosition) + cmd = FitAddImplantCommand(fitID=self.fitID, implantInfo=self.oldImplantInfo, position=self.oldPosition) return cmd.Do() from .fitRemoveImplant import FitRemoveImplantCommand cmd = FitRemoveImplantCommand(fitID=self.fitID, position=self.newPosition) diff --git a/gui/fitCommands/calc/fitAddModule.py b/gui/fitCommands/calc/fitAddModule.py index 99860184f..2ec073853 100644 --- a/gui/fitCommands/calc/fitAddModule.py +++ b/gui/fitCommands/calc/fitAddModule.py @@ -33,10 +33,7 @@ class FitAddModuleCommand(wx.Command): for oldMod in fit.modules: if oldMod.getModifiedItemAttr('subSystemSlot') == newMod.getModifiedItemAttr('subSystemSlot') and newMod.slot == oldMod.slot: from .fitReplaceModule import FitReplaceModuleCommand - self.subsystemCmd = FitReplaceModuleCommand( - fitID=self.fitID, - position=oldMod.modPosition, - newModInfo=self.newModInfo) + self.subsystemCmd = FitReplaceModuleCommand(fitID=self.fitID, position=oldMod.modPosition, newModInfo=self.newModInfo) return self.subsystemCmd.Do() if not newMod.fits(fit): diff --git a/gui/fitCommands/calc/fitAddProjectedModule.py b/gui/fitCommands/calc/fitAddProjectedModule.py index e7194560e..401335387 100644 --- a/gui/fitCommands/calc/fitAddProjectedModule.py +++ b/gui/fitCommands/calc/fitAddProjectedModule.py @@ -52,14 +52,8 @@ class FitAddProjectedModuleCommand(wx.Command): def Undo(self): pyfalog.debug('Undoing addition of projected module {} onto: {}'.format(self.newModInfo, self.fitID)) if self.oldPosition is not None and self.oldModInfo is not None: - cmd = FitAddProjectedModuleCommand( - fitID=self.fitID, - modInfo=self.oldModInfo, - position=self.oldPosition) + cmd = FitAddProjectedModuleCommand(fitID=self.fitID, modInfo=self.oldModInfo, position=self.oldPosition) return cmd.Do() - if self.newPosition is None: - return False from gui.fitCommands.calc.fitRemoveProjectedModule import FitRemoveProjectedModuleCommand cmd = FitRemoveProjectedModuleCommand(self.fitID, self.newPosition) - cmd.Do() - return True + return cmd.Do() diff --git a/gui/fitCommands/calc/fitChangeCargoAmount.py b/gui/fitCommands/calc/fitChangeCargoAmount.py new file mode 100644 index 000000000..c468cd9ce --- /dev/null +++ b/gui/fitCommands/calc/fitChangeCargoAmount.py @@ -0,0 +1,43 @@ +import wx +from logbook import Logger + +import eos.db +from gui.fitCommands.helpers import CargoInfo +from service.fit import Fit + + +pyfalog = Logger(__name__) + + +class FitChangeCargoAmount(wx.Command): + + def __init__(self, fitID, cargoInfo): + wx.Command.__init__(self, True, 'Change Cargo Quantity') + self.fitID = fitID + self.cargoInfo = cargoInfo + self.savedCargoInfo = None + self.removeCommand = None + + def Do(self): + pyfalog.debug('Doing change of cargo {} for fit {}'.format(self.cargoInfo, self.fitID)) + fit = Fit.getInstance().getFit(self.fitID) + cargo = next((c for c in fit.cargo if c.itemID == self.cargoInfo.itemID), None) + if cargo is None: + pyfalog.warning('Cannot find cargo item') + return False + self.savedCargoInfo = CargoInfo.fromCargo(cargo) + if self.cargoInfo.amount > 0: + cargo.amount = self.cargoInfo.amount + eos.db.commit() + return True + else: + from .fitRemoveCargo import FitRemoveCargoCommand + self.removeCommand = FitRemoveCargoCommand(fitID=self.fitID, cargoInfo=self.savedCargoInfo) + return self.removeCommand.Do() + + def Undo(self): + pyfalog.debug('Undoing change of cargo {} for fit {}'.format(self.cargoInfo, self.fitID)) + if self.removeCommand is not None: + return self.removeCommand.Undo() + cmd = FitChangeCargoAmount(self.fitID, self.savedCargoInfo) + return cmd.Do() diff --git a/gui/fitCommands/calc/fitChangeCargoQty.py b/gui/fitCommands/calc/fitChangeCargoQty.py deleted file mode 100644 index 2ba30e085..000000000 --- a/gui/fitCommands/calc/fitChangeCargoQty.py +++ /dev/null @@ -1,27 +0,0 @@ -import wx -import eos.db -from logbook import Logger -pyfalog = Logger(__name__) - - -class FitChangeCargoQty(wx.Command): - def __init__(self, fitID, position, amount=1): - wx.Command.__init__(self, True, "Drone add") - self.fitID = fitID - self.position = position - self.amount = amount # add x amount. If this goes over amount, removes stack - self.old_amount = None - - def Do(self): - pyfalog.debug("Changing cargo ({0}) for fit ({1}) to amount: {2}", self.position, self.fitID, self.amount) - fit = eos.db.getFit(self.fitID) - cargo = fit.cargo[self.position] - self.old_amount = cargo.amount - cargo.amount = self.amount - - eos.db.commit() - return True - - def Undo(self): - cmd = FitChangeCargoQty(self.fitID, self.position, self.old_amount) - return cmd.Do() diff --git a/gui/fitCommands/calc/fitChangeDroneAmount.py b/gui/fitCommands/calc/fitChangeDroneAmount.py new file mode 100644 index 000000000..a0d6fc0dc --- /dev/null +++ b/gui/fitCommands/calc/fitChangeDroneAmount.py @@ -0,0 +1,53 @@ +import math + +import wx +from logbook import Logger + +import eos.db +from gui.fitCommands.helpers import DroneInfo +from service.fit import Fit + + +pyfalog = Logger(__name__) + + +class FitChangeDroneAmount(wx.Command): + + def __init__(self, fitID, position, amount): + wx.Command.__init__(self, True, 'Change Drone Quantity') + self.fitID = fitID + self.position = position + self.amount = amount + self.savedDroneInfo = None + self.removeCommand = None + + def Do(self): + pyfalog.debug('Doing change of drone quantity to {} at position {} on fit {}'.format(self.amount, self.position, self.fitID)) + if self.amount > 0: + fit = Fit.getInstance().getFit(self.fitID) + drone = fit.drones[self.position] + self.savedDroneInfo = DroneInfo.fromDrone(drone) + drone.amount = self.amount + if drone.amountActive > 0: + difference = self.amount - self.savedDroneInfo.amount + drone.amount = self.amount + drone.amountActive = max(min(drone.amountActive + difference, drone.amount), 0) + eos.db.commit() + return True + else: + from .fitRemoveDrone import FitRemoveDroneCommand + self.removeCommand = FitRemoveDroneCommand(fitID=self.fitID, position=self.position, amount=math.inf) + return self.removeCommand.Do() + + def Undo(self): + pyfalog.debug('Undoing change of drone quantity to {} at position {} on fit {}'.format(self.amount, self.position, self.fitID)) + if self.removeCommand is not None: + return self.removeCommand.Undo() + if self.savedDroneInfo is not None: + fit = Fit.getInstance().getFit(self.fitID) + drone = fit.drones[self.position] + drone.amount = self.savedDroneInfo.amount + drone.amountActive = self.savedDroneInfo.amountActive + eos.db.commit() + return True + return False diff --git a/gui/fitCommands/calc/fitChangeDroneQty.py b/gui/fitCommands/calc/fitChangeDroneQty.py deleted file mode 100644 index 8f7e8e801..000000000 --- a/gui/fitCommands/calc/fitChangeDroneQty.py +++ /dev/null @@ -1,27 +0,0 @@ -import wx -import eos.db -from logbook import Logger -pyfalog = Logger(__name__) - - -class FitChangeDroneQty(wx.Command): - def __init__(self, fitID, position, amount=1): - wx.Command.__init__(self, True, "Drone add") - self.fitID = fitID - self.position = position - self.amount = amount # add x amount. If this goes over amount, removes stack - self.old_amount = None - - def Do(self): - pyfalog.debug("Changing active fighters ({0}) for fit ({1}) to amount: {2}", self.position, self.fitID, self.amount) - fit = eos.db.getFit(self.fitID) - drone = fit.drones[self.position] - self.old_amount = drone.amount - drone.amount = self.amount - - eos.db.commit() - return True - - def Undo(self): - cmd = FitChangeDroneQty(self.fitID, self.position, self.old_amount) - return cmd.Do() diff --git a/gui/fitCommands/calc/fitRemoveDrone.py b/gui/fitCommands/calc/fitRemoveDrone.py index 00d8ee383..72973c65a 100644 --- a/gui/fitCommands/calc/fitRemoveDrone.py +++ b/gui/fitCommands/calc/fitRemoveDrone.py @@ -26,9 +26,9 @@ class FitRemoveDroneCommand(wx.Command): drone = fit.drones[self.position] self.savedDroneInfo = DroneInfo.fromDrone(drone) - drone.amount -= self.amountToRemove + drone.amount = max(drone.amount - self.amountToRemove, 0) if drone.amountActive > 0: - drone.amountActive -= self.amountToRemove + drone.amountActive = min(drone.amountActive, drone.amount) if drone.amount == 0: fit.drones.remove(drone) diff --git a/gui/fitCommands/guiAddBooster.py b/gui/fitCommands/guiAddBooster.py index 32adc3c48..89b4e1861 100644 --- a/gui/fitCommands/guiAddBooster.py +++ b/gui/fitCommands/guiAddBooster.py @@ -18,9 +18,7 @@ class GuiAddBoosterCommand(wx.Command): self.itemID = itemID def Do(self): - if self.internal_history.Submit(FitAddBoosterCommand( - fitID=self.fitID, - boosterInfo=BoosterInfo(itemID=self.itemID))): + if self.internal_history.Submit(FitAddBoosterCommand(fitID=self.fitID, boosterInfo=BoosterInfo(itemID=self.itemID))): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) return True diff --git a/gui/fitCommands/guiChangeCargoQty.py b/gui/fitCommands/guiChangeCargoQty.py index 80a906fc3..91a992d09 100644 --- a/gui/fitCommands/guiChangeCargoQty.py +++ b/gui/fitCommands/guiChangeCargoQty.py @@ -1,24 +1,26 @@ import wx import gui.mainFrame from gui import globalEvents as GE -from .calc.fitChangeCargoQty import FitChangeCargoQty +from .calc.fitChangeCargoAmount import FitChangeCargoAmount from service.fit import Fit +from gui.fitCommands.helpers import CargoInfo from logbook import Logger pyfalog = Logger(__name__) class GuiChangeCargoQty(wx.Command): - def __init__(self, fitID, position, amount=1): + + def __init__(self, fitID, itemID, amount): wx.Command.__init__(self, True, "") self.mainFrame = gui.mainFrame.MainFrame.getInstance() self.sFit = Fit.getInstance() self.fitID = fitID - self.position = position + self.itemID = itemID self.amount = amount self.internal_history = wx.CommandProcessor() def Do(self): - cmd = FitChangeCargoQty(self.fitID, self.position, self.amount) + cmd = FitChangeCargoAmount(self.fitID, CargoInfo(itemID=self.itemID, amount=self.amount)) if self.internal_history.Submit(cmd): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) diff --git a/gui/fitCommands/guiChangeDroneQty.py b/gui/fitCommands/guiChangeDroneQty.py index 5aa2ea12e..0d8923a2f 100644 --- a/gui/fitCommands/guiChangeDroneQty.py +++ b/gui/fitCommands/guiChangeDroneQty.py @@ -1,7 +1,7 @@ import wx import gui.mainFrame from gui import globalEvents as GE -from .calc.fitChangeDroneQty import FitChangeDroneQty +from .calc.fitChangeDroneAmount import FitChangeDroneAmount from service.fit import Fit from logbook import Logger pyfalog = Logger(__name__) @@ -18,7 +18,7 @@ class GuiChangeDroneQty(wx.Command): self.internal_history = wx.CommandProcessor() def Do(self): - cmd = FitChangeDroneQty(self.fitID, self.position, self.amount) + cmd = FitChangeDroneAmount(self.fitID, self.position, self.amount) if self.internal_history.Submit(cmd): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) diff --git a/gui/fitCommands/helpers.py b/gui/fitCommands/helpers.py index 6cb65ed3b..10a6ff0af 100644 --- a/gui/fitCommands/helpers.py +++ b/gui/fitCommands/helpers.py @@ -233,6 +233,15 @@ class CargoInfo: self.itemID = itemID self.amount = amount + @classmethod + def fromCargo(cls, cargo): + if cargo is None: + return None + info = cls( + itemID=cargo.itemID, + amount=cargo.amount) + return info + def toCargo(self): item = Market.getInstance().getItem(self.itemID) cargo = Cargo(item)