diff --git a/gui/builtinContextMenus/cargo.py b/gui/builtinContextMenus/cargo.py index 32d6370cf..a0b1c6e80 100644 --- a/gui/builtinContextMenus/cargo.py +++ b/gui/builtinContextMenus/cargo.py @@ -5,6 +5,7 @@ import gui.globalEvents as GE import wx from service.fit import Fit from service.settings import ContextMenuSettings +import gui.fitCommands as cmd class Cargo(ContextMenu): @@ -32,13 +33,12 @@ class Cargo(ContextMenu): return "Add {0} to Cargo".format(itmContext) def activate(self, fullContext, selection, i): - sFit = Fit.getInstance() fitID = self.mainFrame.getActiveFit() typeID = int(selection[0].ID) - sFit.addCargo(fitID, typeID) + + self.mainFrame.command.Submit(cmd.GuiAddCargoCommand(fitID, typeID)) self.mainFrame.additionsPane.select("Cargo") - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) Cargo.register() diff --git a/gui/builtinContextMenus/itemRemove.py b/gui/builtinContextMenus/itemRemove.py index c1c11fde5..9eb741dda 100644 --- a/gui/builtinContextMenus/itemRemove.py +++ b/gui/builtinContextMenus/itemRemove.py @@ -49,7 +49,8 @@ class ItemRemove(ContextMenu): elif srcContext == "boosterItem": sFit.removeBooster(fitID, fit.boosters.index(selection[0])) elif srcContext == "cargoItem": - sFit.removeCargo(fitID, fit.cargo.index(selection[0])) + self.mainFrame.command.Submit(cmd.GuiRemoveCargoCommand(fitID, selection[0].itemID)) + return # the command takes care of the PostEvent elif srcContext in ("projectedFit", "projectedModule", "projectedDrone", "projectedFighter"): sFit.removeProjected(fitID, selection[0]) elif srcContext == "commandFit": diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index b3cb622fc..7e74e568e 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -2,4 +2,8 @@ from .moduleStateChange import GuiModuleStateChangeCommand from .moduleAdd import GuiModuleAddCommand from .moduleRemove import GuiModuleRemoveCommand from .moduleAddCharge import GuiModuleAddChargeCommand -from .moduleSwapOrClone import GuiModuleSwapOrCloneCommand \ No newline at end of file +from .moduleSwapOrClone import GuiModuleSwapOrCloneCommand +from .guiRemoveCargo import GuiRemoveCargoCommand +from .guiAddCargo import GuiAddCargoCommand +from .fitAddCargo import FitAddCargoCommand +from .fitRemoveCargo import FitRemoveCargoCommand \ No newline at end of file diff --git a/gui/fitCommands/fitAddCargo.py b/gui/fitCommands/fitAddCargo.py new file mode 100644 index 000000000..85bfae3eb --- /dev/null +++ b/gui/fitCommands/fitAddCargo.py @@ -0,0 +1,48 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +#from .helpers import ModuleInfoCache +from eos.saveddata.module import Module, State +import eos.db +from logbook import Logger +pyfalog = Logger(__name__) +from eos.saveddata.cargo import Cargo + +class FitAddCargoCommand(wx.Command): + """" + from sFit.addCargo + """ + def __init__(self, fitID, itemID, amount=1, replace=False): + wx.Command.__init__(self, True, "Cargo add") + self.fitID = fitID + self.itemID = itemID + self.amount = amount # add x amount. If this goes over amount, removes stack + self.replace = replace # if this is false, we increment. + + def Do(self): + pyfalog.debug("Adding cargo {0} (x{1}) for fit {2}", self.itemID, self.amount, self.fitID) + + fit = eos.db.getFit(self.fitID) + item = eos.db.getItem(self.itemID) + + cargo = next((x for x in fit.cargo if x.itemID == self.itemID), None) + + if cargo is None: + cargo = Cargo(item) + fit.cargo.append(cargo) + + if self.replace: + cargo.amount = self.amount + else: + cargo.amount += self.amount + + eos.db.commit() + return True + + def Undo(self): + from .fitRemoveCargo import FitRemoveCargoCommand # Avoid circular import + cmd = FitRemoveCargoCommand(self.fitID, self.itemID, self.amount) + cmd.Do() + return True diff --git a/gui/fitCommands/fitRemoveCargo.py b/gui/fitCommands/fitRemoveCargo.py new file mode 100644 index 000000000..41fb57b9e --- /dev/null +++ b/gui/fitCommands/fitRemoveCargo.py @@ -0,0 +1,53 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +#from .helpers import ModuleInfoCache +from eos.saveddata.module import Module, State +import eos.db +from logbook import Logger +pyfalog = Logger(__name__) + + +class FitRemoveCargoCommand(wx.Command): + """" + Fitting command that sets the amount for an item within the cargo. + + from sFit.removeCargo + """ + def __init__(self, fitID, itemID, amount=1, stack=False): + wx.Command.__init__(self, True, "Cargo remove") + self.fitID = fitID + self.itemID = itemID + self.stack = stack # remove entire stack + self.amount = amount # remove x amount. If this goes over amount, removes stack + self.old_amount = None + + def Do(self): + pyfalog.debug("Removing cargo {0} (x{1}) for fit {2}", self.itemID, self.amount, self.fitID) + fit = eos.db.getFit(self.fitID) + cargo = next((x for x in fit.cargo if x.itemID == self.itemID), None) + + if cargo is None: + return False + + self.old_amount = cargo.amount + + if self.amount >= cargo.amount: + self.stack = True # set to full stack, this allows easier logic in the Undo function + + if self.stack or self.amount >= cargo.amount: + fit.cargo.remove(cargo) + eos.db.commit() + return True + + cargo.amount -= self.amount + eos.db.commit() + return True + + def Undo(self): + from .fitAddCargo import FitAddCargoCommand # Avoid circular import + cmd = FitAddCargoCommand(self.fitID, self.itemID, self.old_amount, True) + cmd.Do() + return True diff --git a/gui/fitCommands/fitSetCharge.py b/gui/fitCommands/fitSetCharge.py index 0ae8a80fd..111959a18 100644 --- a/gui/fitCommands/fitSetCharge.py +++ b/gui/fitCommands/fitSetCharge.py @@ -37,7 +37,7 @@ class FitSetChargeCommand(wx.Command): result = False for mod in modules: - if mod.isValidCharge(ammo): + if not mod.isEmpty and mod.isValidCharge(ammo): result = True mod.charge = ammo eos.db.commit() diff --git a/gui/fitCommands/guiAddCargo.py b/gui/fitCommands/guiAddCargo.py new file mode 100644 index 000000000..98491505c --- /dev/null +++ b/gui/fitCommands/guiAddCargo.py @@ -0,0 +1,29 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from .fitAddCargo import FitAddCargoCommand + +class GuiAddCargoCommand(wx.Command): + def __init__(self, fitID, itemID): + wx.Command.__init__(self, True, "Cargo Add") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.internal_history = wx.CommandProcessor() + self.fitID = fitID + # can set his up no to not have to set variables on our object + self.cmd = FitAddCargoCommand(fitID, itemID) + + def Do(self): + if self.internal_history.Submit(self.cmd): + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + return False + + def Undo(self): + for x in self.internal_history.Commands: + self.internal_history.Undo() + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + diff --git a/gui/fitCommands/guiRemoveCargo.py b/gui/fitCommands/guiRemoveCargo.py new file mode 100644 index 000000000..f86b607d2 --- /dev/null +++ b/gui/fitCommands/guiRemoveCargo.py @@ -0,0 +1,29 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from .fitRemoveCargo import FitRemoveCargoCommand + +class GuiRemoveCargoCommand(wx.Command): + def __init__(self, fitID, itemID): + wx.Command.__init__(self, True, "Module Charge Add") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.internal_history = wx.CommandProcessor() + self.fitID = fitID + # can set his up no to not have to set variables on our object + self.cmd = FitRemoveCargoCommand(fitID, itemID, stack=True) + + def Do(self): + if self.internal_history.Submit(self.cmd): + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + return False + + def Undo(self): + for x in self.internal_history.Commands: + self.internal_history.Undo() + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + diff --git a/service/fit.py b/service/fit.py index 175292666..746990daf 100644 --- a/service/fit.py +++ b/service/fit.py @@ -672,6 +672,7 @@ class Fit(object): else: return None + @deprecated def moveCargoToModule(self, fitID, moduleIdx, cargoIdx, copyMod=False): """ Moves cargo to fitting window. Can either do a copy, move, or swap with current module @@ -775,6 +776,7 @@ class Fit(object): eos.db.commit() self.recalc(fit) + @deprecated def addCargo(self, fitID, itemID, amount=1, replace=False): """ Adds cargo via typeID of item. If replace = True, we replace amount with @@ -812,6 +814,7 @@ class Fit(object): return True + @deprecated def removeCargo(self, fitID, position): pyfalog.debug("Removing cargo from position ({0}) fit ID: {1}", position, fitID) if fitID is None: