From aa282990a5a3a08d85fb90e1edc90b308a3bafe5 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Fri, 3 Aug 2018 01:42:06 -0400 Subject: [PATCH] Fix the state change command to not store the modules themselves, but the positions --- gui/builtinViews/fittingView.py | 6 ++- gui/fitCommands/calc/fitChangeState.py | 59 ++++++++++++++++---------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/gui/builtinViews/fittingView.py b/gui/builtinViews/fittingView.py index 247003b55..74d02295c 100644 --- a/gui/builtinViews/fittingView.py +++ b/gui/builtinViews/fittingView.py @@ -268,7 +268,9 @@ class FittingView(d.Display): sel = [] row = self.GetFirstSelected() while row != -1: - sel.append(self.mods[self.GetItemData(row)]) + mod = self.mods[self.GetItemData(row)] + if mod and not isinstance(mod, Rack): + sel.append(mod) row = self.GetNextSelected(row) return sel @@ -629,7 +631,7 @@ class FittingView(d.Display): ctrl = event.cmdDown or event.middleIsDown click = "ctrl" if ctrl is True else "right" if event.GetButton() == 3 else "left" - self.mainFrame.command.Submit(cmd.GuiModuleStateChangeCommand(fitID, self.mods[self.GetItemData(row)], mods, click)) + self.mainFrame.command.Submit(cmd.GuiModuleStateChangeCommand(fitID, self.mods[self.GetItemData(row)].modPosition, [mod.modPosition for mod in mods], click)) # update state tooltip tooltip = self.activeColumns[col].getToolTip(self.mods[self.GetItemData(row)]) diff --git a/gui/fitCommands/calc/fitChangeState.py b/gui/fitCommands/calc/fitChangeState.py index 06ad7bfbd..a6a1548cd 100644 --- a/gui/fitCommands/calc/fitChangeState.py +++ b/gui/fitCommands/calc/fitChangeState.py @@ -9,40 +9,53 @@ pyfalog = Logger(__name__) import eos.db class FitChangeStatesCommand(wx.Command): - def __init__(self, fitID, baseMod, modules, click): + """ + Fitting command that trys to change the state of modules in [positions]. We use the base module to determine the + state that we will try to apply for all modules. + + + """ + def __init__(self, fitID, baseModPos, positions, click): # todo: instead of modules, needs to be positions. Dead objects are a thing wx.Command.__init__(self, True, "Module State Change") self.mainFrame = gui.mainFrame.MainFrame.getInstance() self.sFit = Fit.getInstance() self.fitID = fitID - self.baseMod = baseMod - self.modules = modules + self.baseModPos = baseModPos + self.positions = positions self.click = click self.changed = None self.old_states = {} - for mod in modules: - # we don't use the actual module as the key, because it may have been deleted in subsequent calls (even if - # we undo a deletion, wouldn't be the same obj). So, we store the position - self.old_states[mod.modPosition] = mod.state def Do(self): - pyfalog.debug("Toggle module state for fit ID: {0}", self.fitID) - changed = False - proposedState = Module.getProposedState(self.baseMod, self.click) + fit = eos.db.getFit(self.fitID) - if proposedState != self.baseMod.state: - changed = True - self.baseMod.state = proposedState - for mod in self.modules: - if mod != self.baseMod: - p = Module.getProposedState(mod, self.click, proposedState) - mod.state = p - if p != mod.state: - changed = True + baseMod = fit.modules[self.baseModPos] + + # make sure positions only include non-empty positions + self.positions = [x for x in self.positions if not fit.modules[x].isEmpty] + + for x in self.positions: + self.old_states[x] = fit.modules[x].state + + proposedState = Module.getProposedState(baseMod, self.click) + pyfalog.debug("Attempting to change modules to {}", proposedState) + + if proposedState != baseMod.state: + pyfalog.debug("Toggle {} state: {} for fit ID: {}", baseMod, proposedState, self.fitID) + + self.changed = True + baseMod.state = proposedState + for i in [x for x in self.positions if x != self.baseModPos]: # dont consider base module position + mod = fit.modules[i] + p = Module.getProposedState(mod, self.click, proposedState) + mod.state = p + if p != mod.state: + pyfalog.debug("Toggle {} state: {} for fit ID: {}", mod, p, self.fitID) + self.changed = True # if we haven't change the state (eg, overheat -> overheat), simply fail the command - if changed: - self.changed = changed + if self.changed: eos.db.commit() # As some items may affect state-limiting attributes of the ship, calculate new attributes first # self.recalc(fit) @@ -55,5 +68,7 @@ class FitChangeStatesCommand(wx.Command): # todo: some sanity checking to make sure that we are applying state back to the same modules? fit = self.sFit.getFit(self.fitID) for k, v in self.old_states.items(): - fit.modules[k].state = v + mod = fit.modules[k] + pyfalog.debug("Reverting {} to state {} for fit ID", mod, v, self.fitID) + mod.state = v return True