From 8671b2079027f020d8aa8d2a29edaa8dbee7cbe9 Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Thu, 25 Apr 2019 15:20:20 +0300 Subject: [PATCH] Rework implant state switch command to accept multiple positions --- gui/builtinAdditionPanes/implantView.py | 2 +- gui/fitCommands/__init__.py | 2 +- gui/fitCommands/calc/implant/toggleState.py | 32 ----------- gui/fitCommands/calc/implant/toggleStates.py | 54 +++++++++++++++++++ .../{toggleState.py => toggleStates.py} | 9 ++-- 5 files changed, 61 insertions(+), 38 deletions(-) delete mode 100644 gui/fitCommands/calc/implant/toggleState.py create mode 100644 gui/fitCommands/calc/implant/toggleStates.py rename gui/fitCommands/gui/implant/{toggleState.py => toggleStates.py} (70%) diff --git a/gui/builtinAdditionPanes/implantView.py b/gui/builtinAdditionPanes/implantView.py index 506b66b0c..44cc5452b 100644 --- a/gui/builtinAdditionPanes/implantView.py +++ b/gui/builtinAdditionPanes/implantView.py @@ -241,7 +241,7 @@ class ImplantDisplay(d.Display): if implant in self.original: position = self.original.index(implant) self.mainFrame.command.Submit(cmd.GuiToggleImplantStateCommand( - fitID=fitID, position=position)) + fitID=fitID, mainPosition=position, positions=[position])) def spawnMenu(self, event): sel = self.GetFirstSelected() diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index f04fbd546..794403054 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -16,7 +16,7 @@ from .gui.implant.changeLocation import GuiChangeImplantLocationCommand from .gui.implant.changeMeta import GuiChangeImplantMetaCommand from .gui.implant.remove import GuiRemoveImplantsCommand from .gui.implant.setAdd import GuiAddImplantSetCommand -from .gui.implant.toggleState import GuiToggleImplantStateCommand +from .gui.implant.toggleStates import GuiToggleImplantStateCommand from .gui.itemsRebase import GuiRebaseItemsCommand from .gui.localDrone.add import GuiAddLocalDroneCommand from .gui.localDrone.changeAmount import GuiChangeLocalDroneAmountCommand diff --git a/gui/fitCommands/calc/implant/toggleState.py b/gui/fitCommands/calc/implant/toggleState.py deleted file mode 100644 index 34fe62a7a..000000000 --- a/gui/fitCommands/calc/implant/toggleState.py +++ /dev/null @@ -1,32 +0,0 @@ -import wx -from logbook import Logger - -import eos.db -from service.fit import Fit - - -pyfalog = Logger(__name__) - - -class CalcToggleImplantStateCommand(wx.Command): - - def __init__(self, fitID, position, forceState=None): - wx.Command.__init__(self, True, 'Toggle Implant State') - self.fitID = fitID - self.position = position - self.forceState = forceState - self.savedState = None - - def Do(self): - pyfalog.debug('Doing toggling of implant state at position {} for fit {}'.format(self.position, self.fitID)) - fit = Fit.getInstance().getFit(self.fitID) - implant = fit.implants[self.position] - self.savedState = implant.active - implant.active = not implant.active if self.forceState is None else self.forceState - eos.db.commit() - return True - - def Undo(self): - pyfalog.debug('Undoing toggling of implant state at position {} for fit {}'.format(self.position, self.fitID)) - cmd = CalcToggleImplantStateCommand(fitID=self.fitID, position=self.position, forceState=self.savedState) - return cmd.Do() diff --git a/gui/fitCommands/calc/implant/toggleStates.py b/gui/fitCommands/calc/implant/toggleStates.py new file mode 100644 index 000000000..3cd14f4a1 --- /dev/null +++ b/gui/fitCommands/calc/implant/toggleStates.py @@ -0,0 +1,54 @@ +import wx +from logbook import Logger + +import eos.db +from service.fit import Fit + + +pyfalog = Logger(__name__) + + +class CalcToggleImplantStatesCommand(wx.Command): + + def __init__(self, fitID, mainPosition, positions, forceStates=None): + wx.Command.__init__(self, True, 'Toggle Implant States') + self.fitID = fitID + self.mainPosition = mainPosition + self.positions = positions + self.forceStates = forceStates + self.savedStates = None + + def Do(self): + pyfalog.debug('Doing toggling of implant state at position {}/{} for fit {}'.format(self.mainPosition, self.positions, self.fitID)) + fit = Fit.getInstance().getFit(self.fitID) + + positions = self.positions[:] + if self.mainPosition not in positions: + positions.append(self.mainPosition) + self.savedStates = {p: fit.implants[p].active for p in positions} + + if self.forceStates is not None: + for position, active in self.forceStates.items(): + implant = fit.implants[position] + implant.active = active + elif fit.implants[self.mainPosition].active: + for position in positions: + implant = fit.implants[position] + if implant.active: + implant.active = False + else: + for position in positions: + implant = fit.implants[position] + if not implant.active: + implant.active = True + eos.db.commit() + return True + + def Undo(self): + pyfalog.debug('Undoing toggling of implant state at position {}/{} for fit {}'.format(self.mainPosition, self.positions, self.fitID)) + cmd = CalcToggleImplantStatesCommand( + fitID=self.fitID, + positions=self.positions, + mainPosition=self.mainPosition, + forceStates=self.savedStates) + return cmd.Do() diff --git a/gui/fitCommands/gui/implant/toggleState.py b/gui/fitCommands/gui/implant/toggleStates.py similarity index 70% rename from gui/fitCommands/gui/implant/toggleState.py rename to gui/fitCommands/gui/implant/toggleStates.py index bcecacbe9..e824d90b1 100644 --- a/gui/fitCommands/gui/implant/toggleState.py +++ b/gui/fitCommands/gui/implant/toggleStates.py @@ -2,21 +2,22 @@ import wx import gui.mainFrame from gui import globalEvents as GE -from gui.fitCommands.calc.implant.toggleState import CalcToggleImplantStateCommand +from gui.fitCommands.calc.implant.toggleStates import CalcToggleImplantStatesCommand from gui.fitCommands.helpers import InternalCommandHistory from service.fit import Fit class GuiToggleImplantStateCommand(wx.Command): - def __init__(self, fitID, position): + def __init__(self, fitID, mainPosition, positions): wx.Command.__init__(self, True, 'Toggle Implant State') self.internalHistory = InternalCommandHistory() self.fitID = fitID - self.position = position + self.mainPosition = mainPosition + self.positions = positions def Do(self): - cmd = CalcToggleImplantStateCommand(fitID=self.fitID, position=self.position) + cmd = CalcToggleImplantStatesCommand(fitID=self.fitID, mainPosition=self.mainPosition, positions=self.positions) success = self.internalHistory.submit(cmd) Fit.getInstance().recalc(self.fitID) wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))