diff --git a/eos/saveddata/fit.py b/eos/saveddata/fit.py index ebeb17e8f..f4d5122bf 100644 --- a/eos/saveddata/fit.py +++ b/eos/saveddata/fit.py @@ -924,8 +924,10 @@ class Fit(object): recalc. Figure out a way to keep track of any changes to slot layout and call this automatically """ if self.ship is None: - return + return {} + # Look for any dummies of that type to remove + posToRemove = {} for slotType in (FittingSlot.LOW.value, FittingSlot.MED.value, FittingSlot.HIGH.value, FittingSlot.RIG.value, FittingSlot.SUBSYSTEM.value, FittingSlot.SERVICE.value): amount = self.getSlotsFree(slotType, True) if amount > 0: @@ -933,16 +935,17 @@ class Fit(object): self.modules.append(Module.buildEmpty(slotType)) if amount < 0: - # Look for any dummies of that type to remove - toRemove = [] for mod in self.modules: if mod.isEmpty and mod.slot == slotType: - toRemove.append(mod) + pos = self.modules.index(mod) + posToRemove[pos] = slotType amount += 1 if amount == 0: break - for mod in toRemove: - self.modules.remove(mod) + for pos in sorted(posToRemove, reverse=True): + mod = self.modules[pos] + self.modules.remove(mod) + return posToRemove def unfill(self): for i in range(len(self.modules) - 1, -1, -1): diff --git a/gui/fitCommands/calc/module/localRemove.py b/gui/fitCommands/calc/module/localRemove.py index c3969b830..28e5b9056 100644 --- a/gui/fitCommands/calc/module/localRemove.py +++ b/gui/fitCommands/calc/module/localRemove.py @@ -2,6 +2,7 @@ import wx from logbook import Logger import eos.db +from eos.const import FittingSlot from gui.fitCommands.helpers import ModuleInfo, restoreCheckedStates from service.fit import Fit @@ -16,6 +17,7 @@ class CalcRemoveLocalModulesCommand(wx.Command): self.fitID = fitID self.positions = positions self.commit = commit + self.savedSubInfos = None self.savedModInfos = None self.savedStateCheckChanges = None @@ -24,14 +26,18 @@ class CalcRemoveLocalModulesCommand(wx.Command): sFit = Fit.getInstance() fit = sFit.getFit(self.fitID) + self.savedSubInfos = {} self.savedModInfos = {} for position in self.positions: mod = fit.modules[position] if not mod.isEmpty: - self.savedModInfos[position] = ModuleInfo.fromModule(mod) + if mod.slot == FittingSlot.SUBSYSTEM: + self.savedSubInfos[position] = ModuleInfo.fromModule(mod) + else: + self.savedModInfos[position] = ModuleInfo.fromModule(mod) fit.modules.free(position) - if len(self.savedModInfos) == 0: + if len(self.savedSubInfos) == 0 and len(self.savedModInfos) == 0: return False # Need to flush because checkStates sometimes relies on module->fit @@ -48,14 +54,22 @@ class CalcRemoveLocalModulesCommand(wx.Command): pyfalog.debug('Undoing removal of local modules {} on fit {}'.format(self.savedModInfos, self.fitID)) results = [] from .localReplace import CalcReplaceLocalModuleCommand + # Restore subsystems 1st + if len(self.savedSubInfos) > 0: + for position, modInfo in self.savedSubInfos.items(): + cmd = CalcReplaceLocalModuleCommand( + fitID=self.fitID, position=position, newModInfo=modInfo, commit=False) + results.append(cmd.Do()) + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + sFit.recalc(fit) for position, modInfo in self.savedModInfos.items(): - # Do not commit in any case to not worsen performance, we will commit later anyway cmd = CalcReplaceLocalModuleCommand( fitID=self.fitID, position=position, newModInfo=modInfo, commit=False) results.append(cmd.Do()) if not any(results): return False - restoreCheckedStates(Fit.getInstance().getFit(self.fitID), self.savedStateCheckChanges) + restoreCheckedStates(fit, self.savedStateCheckChanges) if self.commit: eos.db.commit() return True diff --git a/service/fit.py b/service/fit.py index 60b921c5b..67b745402 100644 --- a/service/fit.py +++ b/service/fit.py @@ -480,5 +480,6 @@ class Fit(FitDeprecated): fit.clear() fit.calculateModifiedAttributes() - fit.fill() + removedDummies = fit.fill() pyfalog.info("=" * 10 + "recalc time: " + str(time() - start_time) + "=" * 10) + return removedDummies