diff --git a/gui/fitCommands/calc/fitChangeModuleCharges.py b/gui/fitCommands/calc/fitChangeModuleCharges.py new file mode 100644 index 000000000..4bee92ba4 --- /dev/null +++ b/gui/fitCommands/calc/fitChangeModuleCharges.py @@ -0,0 +1,54 @@ +import wx +from logbook import Logger + +import eos.db +from service.fit import Fit +from service.market import Market + + +pyfalog = Logger(__name__) + + +class FitChangeModuleChargesCommand(wx.Command): + + def __init__(self, fitID, chargeMap, projected=False): + wx.Command.__init__(self, True, 'Change Module Charges') + self.fitID = fitID + self.chargeMap = chargeMap + self.projected = projected + self.savedChargeMap = None + + def Do(self): + pyfalog.debug('Doing change of module charges according to map {} on fit {}'.format(self.chargeMap, self.fitID)) + fit = Fit.getInstance().getFit(self.fitID) + container = fit.modules if not self.projected else fit.projectedModules + changes = False + self.savedChargeMap = {} + sMkt = Market.getInstance() + for position, chargeItemID in self.chargeMap.items(): + mod = container[position] + if mod.isEmpty: + continue + if mod.chargeID is None and chargeItemID is None: + continue + if mod.chargeID == chargeItemID: + continue + chargeItem = sMkt.getItem(chargeItemID) if chargeItemID is not None else None + if chargeItem is not None and not chargeItem.isCharge: + continue + if not mod.isValidCharge(chargeItem): + pyfalog.warning('Invalid charge {} for {}'.format(chargeItem, mod)) + continue + pyfalog.debug('Setting charge {} for {} on fit {}'.format(chargeItem, mod, self.fitID)) + self.savedChargeMap[position] = mod.chargeID + changes = True + mod.charge = chargeItem + if changes: + eos.db.commit() + return True + return False + + def Undo(self): + pyfalog.debug('Undoing change of module charges according to map {} on fit {}'.format(self.chargeMap, self.fitID)) + cmd = FitChangeModuleChargesCommand(fitID=self.fitID, chargeMap=self.savedChargeMap, projected=self.projected) + return cmd.Do() diff --git a/gui/fitCommands/calc/fitRebaseItem.py b/gui/fitCommands/calc/fitRebaseItem.py index 6b62868f3..796a1a87b 100644 --- a/gui/fitCommands/calc/fitRebaseItem.py +++ b/gui/fitCommands/calc/fitRebaseItem.py @@ -1,7 +1,7 @@ import wx - from logbook import Logger +import eos.db from service.fit import Fit from service.market import Market @@ -11,13 +11,14 @@ pyfalog = Logger(__name__) class FitRebaseItemCommand(wx.Command): - def __init__(self, fitID, containerName, position, itemID): + def __init__(self, fitID, containerName, position, itemID, commit=True): wx.Command.__init__(self, True, 'Rebase Item') self.fitID = fitID self.containerName = containerName self.position = position self.itemID = itemID self.savedItemID = None + self.commit = commit def Do(self): pyfalog.debug('Doing rebase of item in {} at position {} to {}'.format(self.containerName, self.position, self.itemID)) @@ -32,6 +33,8 @@ class FitRebaseItemCommand(wx.Command): pyfalog.warning('Unable to fetch new item') return False obj.rebase(newItem) + if self.commit: + eos.db.commit() return True def Undo(self): diff --git a/gui/fitCommands/calc/fitSetCharge.py b/gui/fitCommands/calc/fitSetCharge.py deleted file mode 100644 index 91a4abe8d..000000000 --- a/gui/fitCommands/calc/fitSetCharge.py +++ /dev/null @@ -1,48 +0,0 @@ -import wx -from logbook import Logger - -import eos.db -import gui.mainFrame -from service.fit import Fit - -pyfalog = Logger(__name__) - - -class FitSetChargeCommand(wx.Command): - def __init__(self, fitID, positions, chargeID=None, projected=False): - # todo: determine if this command really should be used with a group of modules, or a simple per module basis - wx.Command.__init__(self, True, "Module Charge Add") - self.mainFrame = gui.mainFrame.MainFrame.getInstance() - self.sFit = Fit.getInstance() - self.fitID = fitID - self.chargeID = chargeID - self.positions = positions - self.projected = projected - self.cache = None - - def Do(self): - return self.__setAmmo(self.positions, self.chargeID) - - def Undo(self): - for position, chargeID in self.cache.items(): - self.__setAmmo([position], chargeID) - return True - - def __setAmmo(self, positions, chargeID): - fit = eos.db.getFit(self.fitID) - source = fit.modules if not self.projected else fit.projectedModules - self.cache = {source[i].modPosition: source[i].chargeID for i in positions} - ammo = eos.db.getItem(chargeID) if chargeID else None - - if ammo is not None and not ammo.isCharge: - return False - result = False - - for pos in positions: - mod = source[pos] - if not mod.isEmpty and mod.isValidCharge(ammo): - pyfalog.debug("Set ammo {} for {} on fit {}", ammo, mod, self.fitID) - result = True - mod.charge = ammo - eos.db.commit() - return result diff --git a/gui/fitCommands/guiAddCharge.py b/gui/fitCommands/guiAddCharge.py index ae2af59aa..feba85cd9 100644 --- a/gui/fitCommands/guiAddCharge.py +++ b/gui/fitCommands/guiAddCharge.py @@ -3,7 +3,7 @@ from service.fit import Fit import gui.mainFrame from gui import globalEvents as GE -from .calc.fitSetCharge import FitSetChargeCommand +from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand class GuiModuleAddChargeCommand(wx.Command): @@ -18,7 +18,7 @@ class GuiModuleAddChargeCommand(wx.Command): self.projected = modules[0].isProjected def Do(self): - if self.internal_history.Submit(FitSetChargeCommand(self.fitID, self.positions, self.itemID, self.projected)): + if self.internal_history.Submit(FitChangeModuleChargesCommand(self.fitID, {p: self.itemID for p in self.positions}, self.projected)): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) return True diff --git a/gui/fitCommands/guiAddModule.py b/gui/fitCommands/guiAddModule.py index 1e386206a..22a5d00d1 100644 --- a/gui/fitCommands/guiAddModule.py +++ b/gui/fitCommands/guiAddModule.py @@ -8,7 +8,7 @@ from gui import globalEvents as GE from gui.fitCommands.helpers import ModuleInfo from .calc.fitAddModule import FitAddModuleCommand from .calc.fitReplaceModule import FitReplaceModuleCommand -from .calc.fitSetCharge import FitSetChargeCommand +from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand pyfalog = Logger(__name__) @@ -38,7 +38,7 @@ class GuiModuleAddCommand(wx.Command): # Charge if item.isCharge and self.position is not None: pyfalog.debug("Trying to add a charge") - success = self.internalHistory.Submit(FitSetChargeCommand(self.fitID, [self.position], self.itemID)) + success = self.internalHistory.Submit(FitChangeModuleChargesCommand(self.fitID, {self.position: self.itemID})) if not success: pyfalog.debug(" Failed") return False # if it's a charge item and this failed, nothing more we can try. diff --git a/gui/fitCommands/guiCargoToModule.py b/gui/fitCommands/guiCargoToModule.py index 48c693818..1c7020daa 100644 --- a/gui/fitCommands/guiCargoToModule.py +++ b/gui/fitCommands/guiCargoToModule.py @@ -3,7 +3,7 @@ from service.fit import Fit import gui.mainFrame from gui import globalEvents as GE -from gui.fitCommands.calc.fitSetCharge import FitSetChargeCommand +from gui.fitCommands.calc.fitChangeModuleCharges import FitChangeModuleChargesCommand from gui.fitCommands.calc.fitReplaceModule import FitReplaceModuleCommand from gui.fitCommands.calc.fitRemoveCargo import FitRemoveCargoCommand from gui.fitCommands.helpers import ModuleInfo @@ -39,8 +39,8 @@ class GuiCargoToModuleCommand(wx.Command): result = False # We're trying to move a charge from cargo to a slot. Use SetCharge command (don't respect move vs copy) - if sFit.isAmmo(cargo.item.ID): - result = self.internal_history.Submit(FitSetChargeCommand(self.fitID, [module.modPosition], cargo.item.ID)) + if sFit.isAmmo(cargo.itemID): + result = self.internal_history.Submit(FitChangeModuleChargesCommand(self.fitID, {module.modPosition: cargo.itemID})) else: pyfalog.debug("Moving cargo item to module for fit ID: {0}", self.fitID) diff --git a/gui/fitCommands/guiRebaseItems.py b/gui/fitCommands/guiRebaseItems.py index ac6e663e1..166b3ecd5 100644 --- a/gui/fitCommands/guiRebaseItems.py +++ b/gui/fitCommands/guiRebaseItems.py @@ -6,7 +6,7 @@ from gui import globalEvents as GE from gui.fitCommands.helpers import CargoInfo from service.fit import Fit from .calc.fitRebaseItem import FitRebaseItemCommand -from .calc.fitSetCharge import FitSetChargeCommand +from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand from .calc.fitAddCargo import FitAddCargoCommand from .calc.fitRemoveCargo import FitRemoveCargoCommand @@ -25,14 +25,14 @@ class GuiRebaseItemsCommand(wx.Command): fit = eos.db.getFit(self.fitID) for mod in fit.modules: if mod.itemID in self.rebaseMap: - self.internal_history.Submit(FitRebaseItemCommand(self.fitID, "modules", mod.modPosition, self.rebaseMap[mod.itemID])) + self.internal_history.Submit(FitRebaseItemCommand(fitID=self.fitID, containerName="modules", position=mod.modPosition, itemID=self.rebaseMap[mod.itemID], commit=False)) if mod.chargeID in self.rebaseMap: - self.internal_history.Submit(FitSetChargeCommand(self.fitID, [mod.modPosition], self.rebaseMap[mod.chargeID])) + self.internal_history.Submit(FitChangeModuleChargesCommand(fitID=self.fitID, chargeMap={mod.modPosition: self.rebaseMap[mod.chargeID]})) for containerName in ("drones", "fighters", "implants", "boosters"): container = getattr(fit, containerName) for obj in container: if obj.itemID in self.rebaseMap: - self.internal_history.Submit(FitRebaseItemCommand(self.fitID, containerName, container.index(obj), self.rebaseMap[obj.itemID])) + self.internal_history.Submit(FitRebaseItemCommand(fitID=self.fitID, containerName=containerName, position=container.index(obj), itemID=self.rebaseMap[obj.itemID], commit=False)) # Need to process cargo separately as we want to merge items when needed, # e.g. FN iron and CN iron into single stack of CN iron for cargo in fit.cargo: