From 966763aaa4592193797ad946455298378af9cbe3 Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Tue, 30 Apr 2019 00:44:54 +0300 Subject: [PATCH] Make toggling ignore of restrictions undoable --- eos/saveddata/module.py | 2 + gui/builtinViews/fittingView.py | 34 +++++++++------- gui/fitCommands/__init__.py | 1 + gui/fitCommands/gui/fitRestrictionToggle.py | 45 +++++++++++++++++++++ gui/mainFrame.py | 3 +- service/fit.py | 15 ------- 6 files changed, 68 insertions(+), 32 deletions(-) create mode 100644 gui/fitCommands/gui/fitRestrictionToggle.py diff --git a/eos/saveddata/module.py b/eos/saveddata/module.py index 59ece8eb4..7b48a09a2 100644 --- a/eos/saveddata/module.py +++ b/eos/saveddata/module.py @@ -550,6 +550,8 @@ class Module(HandledItem, HandledCharge, ItemAttrShortcut, ChargeAttrShortcut): if not fits and fit.ignoreRestrictions: self.restrictionOverridden = True fits = True + elif fits and fit.ignoreRestrictions: + self.restrictionOverridden = False return fits diff --git a/gui/builtinViews/fittingView.py b/gui/builtinViews/fittingView.py index a54fbba32..4accf3bf7 100644 --- a/gui/builtinViews/fittingView.py +++ b/gui/builtinViews/fittingView.py @@ -758,21 +758,25 @@ class FittingView(d.Display): self.SetItemBackgroundColour(i, self.GetBackgroundColour()) # only consider changing color if we're dealing with a Module - if type(mod) is Module: - hasRestrictionOverriden = getattr(mod, 'restrictionOverridden', None) - # If module had broken fitting restrictions but now doesn't, - # ensure it is now valid, and remove restrictionOverridden - # variable. More in #1519 - if not fit.ignoreRestrictions and hasRestrictionOverriden: - clean = False - if mod.fits(fit, False): - if not mod.hardpoint: - clean = True - elif fit.getHardpointsFree(mod.hardpoint) >= 0: - clean = True - if clean: - del mod.restrictionOverridden - hasRestrictionOverriden = not hasRestrictionOverriden + if isinstance(mod, Module): + hasRestrictionOverriden = False + if not mod.isEmpty: + fits = mod.fits(fit, False) + hasRestrictionOverriden = getattr(mod, 'restrictionOverridden', None) + # If module had broken fitting restrictions but now doesn't, + # ensure it is now valid, and remove restrictionOverridden + # variable. More in #1519 + if not fit.ignoreRestrictions and hasRestrictionOverriden: + clean = False + if fits: + if not mod.hardpoint: + clean = True + elif fit.getHardpointsFree(mod.hardpoint) >= 0: + clean = True + if clean: + del mod.restrictionOverridden + hasRestrictionOverriden = not hasRestrictionOverriden + if slotMap[mod.slot] or hasRestrictionOverriden: # Color too many modules as red self.SetItemBackgroundColour(i, wx.Colour(204, 51, 51)) diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 06ca2f458..24c0f7d52 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -11,6 +11,7 @@ from .gui.commandFit.add import GuiAddCommandFitCommand from .gui.commandFit.remove import GuiRemoveCommandFitsCommand from .gui.commandFit.toggleStates import GuiToggleCommandFitStatesCommand from .gui.fitRename import GuiRenameFitCommand +from .gui.fitRestrictionToggle import GuiToggleFittingRestrictionsCommand from .gui.implant.add import GuiAddImplantCommand from .gui.implant.changeLocation import GuiChangeImplantLocationCommand from .gui.implant.changeMeta import GuiChangeImplantMetaCommand diff --git a/gui/fitCommands/gui/fitRestrictionToggle.py b/gui/fitCommands/gui/fitRestrictionToggle.py new file mode 100644 index 000000000..a1419001d --- /dev/null +++ b/gui/fitCommands/gui/fitRestrictionToggle.py @@ -0,0 +1,45 @@ +import wx +from service.fit import Fit + +import eos.db +import gui.mainFrame +from gui import globalEvents as GE +from gui.fitCommands.helpers import InternalCommandHistory +from gui.fitCommands.calc.module.localRemove import CalcRemoveLocalModulesCommand + + +class GuiToggleFittingRestrictionsCommand(wx.Command): + + def __init__(self, fitID): + wx.Command.__init__(self, True, 'Toggle Fitting Restrictions') + self.internalHistory = InternalCommandHistory() + self.fitID = fitID + + def Do(self): + fit = Fit.getInstance().getFit(self.fitID) + fit.ignoreRestrictions = not fit.ignoreRestrictions + + success = True + if not fit.ignoreRestrictions: + results = [] + for position, mod in sorted(enumerate(fit.modules), key=lambda i: i[0], reverse=True): + if not mod.isEmpty and not mod.fits(fit, hardpointLimit=False): + cmd = CalcRemoveLocalModulesCommand(fitID=self.fitID, positions=[position], commit=False) + results.append(self.internalHistory.submit(cmd)) + if len(results) > 0: + success = any(results) + + eos.db.commit() + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success + + def Undo(self): + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + fit.ignoreRestrictions = not fit.ignoreRestrictions + success = self.internalHistory.undoAll() + eos.db.commit() + sFit.recalc(self.fitID) + wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID)) + return success diff --git a/gui/mainFrame.py b/gui/mainFrame.py index 4b313c5ee..cdcd26f38 100644 --- a/gui/mainFrame.py +++ b/gui/mainFrame.py @@ -604,8 +604,7 @@ class MainFrame(wx.Frame): result = dlg.ShowModal() == wx.ID_YES dlg.Destroy() if result: - sFit.toggleRestrictionIgnore(fitID) - wx.PostEvent(self, GE.FitChanged(fitID=fitID)) + self.command.Submit(cmd.GuiToggleFittingRestrictionsCommand(fitID=fitID)) def eveFittings(self, event): dlg = EveFittings(self) diff --git a/service/fit.py b/service/fit.py index 832b317c9..60b921c5b 100644 --- a/service/fit.py +++ b/service/fit.py @@ -346,21 +346,6 @@ class Fit(FitDeprecated): eos.db.commit() return mutator.value - def toggleRestrictionIgnore(self, fitID): - pyfalog.debug("Toggling restriction ignore for fit ID: {0}", fitID) - fit = eos.db.getFit(fitID) - fit.ignoreRestrictions = not fit.ignoreRestrictions - - # remove invalid modules when switching back to enabled fitting restrictions - if not fit.ignoreRestrictions: - for m in fit.modules: - if not m.isEmpty and not m.fits(fit, False): - self.removeModule(fit.ID, m.modPosition) - - eos.db.commit() - self.recalc(fit) - return True - def changeChar(self, fitID, charID): pyfalog.debug("Changing character ({0}) for fit ID: {1}", charID, fitID) if fitID is None or charID is None: