Convert module swap / clone to fit / gui commands
This commit is contained in:
49
gui/fitCommands/fitCloneModule.py
Normal file
49
gui/fitCommands/fitCloneModule.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import wx
|
||||
from service.fit import Fit
|
||||
|
||||
import gui.mainFrame
|
||||
from gui import globalEvents as GE
|
||||
#from .helpers import ModuleInfoCache
|
||||
from eos.saveddata.module import Module, State
|
||||
import eos.db
|
||||
from logbook import Logger
|
||||
pyfalog = Logger(__name__)
|
||||
import copy
|
||||
|
||||
class FitCloneModduleCommand(wx.Command):
|
||||
"""
|
||||
Clone a module from src to dst
|
||||
This will overwrite dst! Checking for empty module must be
|
||||
done at a higher level
|
||||
|
||||
from sFit.cloneModule
|
||||
"""
|
||||
def __init__(self, fitID, src, dst):
|
||||
wx.Command.__init__(self, True, "Module Clone")
|
||||
self.fitID = fitID
|
||||
self.src = src
|
||||
self.dst = dst
|
||||
|
||||
def Do(self):
|
||||
pyfalog.debug("Cloning modules from source ({0}) to destination ({1}) for fit ID: {1}", self.src, self.dst, self.fitID)
|
||||
fit = eos.db.getFit(self.fitID)
|
||||
# Gather modules
|
||||
srcMod = fit.modules[self.src]
|
||||
dstMod = fit.modules[self.dst] # should be a placeholder module
|
||||
|
||||
new = copy.deepcopy(srcMod)
|
||||
new.owner = fit
|
||||
if new.fits(fit):
|
||||
# insert copy if module meets hardpoint restrictions
|
||||
fit.modules.remove(dstMod)
|
||||
fit.modules.insert(self.dst, new)
|
||||
|
||||
eos.db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def Undo(self):
|
||||
from .fitRemoveModule import FitRemoveModuleCommand # Avoid circular import
|
||||
cmd = FitRemoveModuleCommand(self.fitID, [self.dst])
|
||||
cmd.Do()
|
||||
return True
|
||||
44
gui/fitCommands/fitSwapModule.py
Normal file
44
gui/fitCommands/fitSwapModule.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import wx
|
||||
from service.fit import Fit
|
||||
|
||||
import gui.mainFrame
|
||||
from gui import globalEvents as GE
|
||||
#from .helpers import ModuleInfoCache
|
||||
from eos.saveddata.module import Module, State
|
||||
import eos.db
|
||||
from logbook import Logger
|
||||
pyfalog = Logger(__name__)
|
||||
|
||||
|
||||
class FitSwapModuleCommand(wx.Command):
|
||||
""""
|
||||
from sFit.swapModules
|
||||
"""
|
||||
def __init__(self, fitID, src, dst):
|
||||
wx.Command.__init__(self, True, "Module Swap")
|
||||
self.fitID = fitID
|
||||
self.src = src
|
||||
self.dst = dst
|
||||
|
||||
def Do(self):
|
||||
self.__swap(self.fitID, self.src, self.dst)
|
||||
return True
|
||||
|
||||
def Undo(self):
|
||||
self.__swap(self.fitID, self.dst, self.src)
|
||||
return True
|
||||
|
||||
def __swap(self, fitID, src, dst):
|
||||
pyfalog.debug("Swapping modules from source ({0}) to destination ({1}) for fit ID: {1}", src, dst, fitID)
|
||||
fit = eos.db.getFit(fitID)
|
||||
# Gather modules
|
||||
srcMod = fit.modules[src]
|
||||
dstMod = fit.modules[dst]
|
||||
|
||||
# To swap, we simply remove mod and insert at destination.
|
||||
fit.modules.remove(srcMod)
|
||||
fit.modules.insert(dst, srcMod)
|
||||
fit.modules.remove(dstMod)
|
||||
fit.modules.insert(src, dstMod)
|
||||
|
||||
eos.db.commit()
|
||||
@@ -3,7 +3,8 @@ from service.fit import Fit
|
||||
|
||||
import gui.mainFrame
|
||||
from gui import globalEvents as GE
|
||||
|
||||
from .fitSwapModule import FitSwapModuleCommand
|
||||
from .fitCloneModule import FitCloneModduleCommand
|
||||
|
||||
class GuiModuleSwapOrCloneCommand(wx.Command):
|
||||
def __init__(self, fitID, srcPosition, dstPosition, clone=False):
|
||||
@@ -15,20 +16,21 @@ class GuiModuleSwapOrCloneCommand(wx.Command):
|
||||
self.srcPosition = srcPosition
|
||||
self.dstPosition = dstPosition
|
||||
self.clone = clone
|
||||
self.internal_history = wx.CommandProcessor()
|
||||
|
||||
def Do(self):
|
||||
result = None
|
||||
if self.clone:
|
||||
self.sFit.cloneModule(self.fitID, self.srcPosition, self.dstPosition)
|
||||
result = self.internal_history.Submit(FitCloneModduleCommand(self.fitID, self.srcPosition, self.dstPosition))
|
||||
else:
|
||||
self.sFit.swapModules(self.fitID, self.srcPosition, self.dstPosition)
|
||||
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
|
||||
return True
|
||||
result = self.internal_history.Submit(FitSwapModuleCommand(self.fitID, self.srcPosition, self.dstPosition))
|
||||
|
||||
if result:
|
||||
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
|
||||
return result
|
||||
|
||||
def Undo(self):
|
||||
if self.clone:
|
||||
# if we had cloned, the destinations was originally an empty slot, hence we can just remove the module
|
||||
self.sFit.removeModule(self.fitID, [self.dstPosition])
|
||||
else:
|
||||
self.sFit.swapModules(self.fitID, self.dstPosition, self.srcPosition)
|
||||
for _ in self.internal_history.Commands:
|
||||
self.internal_history.Undo()
|
||||
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
|
||||
return True
|
||||
|
||||
@@ -736,6 +736,7 @@ class Fit(object):
|
||||
self.recalc(fit)
|
||||
|
||||
@staticmethod
|
||||
@deprecated
|
||||
def swapModules(fitID, src, dst):
|
||||
pyfalog.debug("Swapping modules from source ({0}) to destination ({1}) for fit ID: {1}", src, dst, fitID)
|
||||
fit = eos.db.getFit(fitID)
|
||||
@@ -751,6 +752,7 @@ class Fit(object):
|
||||
|
||||
eos.db.commit()
|
||||
|
||||
@deprecated
|
||||
def cloneModule(self, fitID, src, dst):
|
||||
"""
|
||||
Clone a module from src to dst
|
||||
|
||||
Reference in New Issue
Block a user