| 12 | |
| 13 | |
| 14 | class CalcCloneLocalModuleCommand(wx.Command): |
| 15 | |
| 16 | def __init__(self, fitID, srcPosition, dstPosition): |
| 17 | wx.Command.__init__(self, True, 'Clone Local Module') |
| 18 | self.fitID = fitID |
| 19 | self.srcPosition = srcPosition |
| 20 | self.dstPosition = dstPosition |
| 21 | self.savedStateCheckChanges = None |
| 22 | |
| 23 | def Do(self): |
| 24 | pyfalog.debug('Doing cloning of local module from position {} to position {} for fit ID {}'.format(self.srcPosition, self.dstPosition, self.fitID)) |
| 25 | sFit = Fit.getInstance() |
| 26 | fit = sFit.getFit(self.fitID) |
| 27 | srcMod = fit.modules[self.srcPosition] |
| 28 | copyMod = copy.deepcopy(srcMod) |
| 29 | if not copyMod.fits(fit): |
| 30 | return False |
| 31 | if not fit.modules[self.dstPosition].isEmpty: |
| 32 | return False |
| 33 | fit.modules.replace(self.dstPosition, copyMod) |
| 34 | if copyMod not in fit.modules: |
| 35 | pyfalog.warning('Failed to replace module') |
| 36 | return False |
| 37 | # Need to flush because checkStates sometimes relies on module->fit |
| 38 | # relationship via .owner attribute, which is handled by SQLAlchemy |
| 39 | eos.db.flush() |
| 40 | sFit.recalc(fit) |
| 41 | self.savedStateCheckChanges = sFit.checkStates(fit, copyMod) |
| 42 | return True |
| 43 | |
| 44 | def Undo(self): |
| 45 | pyfalog.debug('Undoing cloning of local module from position {} to position {} for fit ID {}'.format(self.srcPosition, self.dstPosition, self.fitID)) |
| 46 | from .localRemove import CalcRemoveLocalModulesCommand |
| 47 | cmd = CalcRemoveLocalModulesCommand(fitID=self.fitID, positions=[self.dstPosition], recalc=False) |
| 48 | if not cmd.Do(): |
| 49 | return False |
| 50 | restoreCheckedStates(Fit.getInstance().getFit(self.fitID), self.savedStateCheckChanges) |
| 51 | return True |
| 52 | |
| 53 | @property |
| 54 | def needsGuiRecalc(self): |
| 55 | if self.savedStateCheckChanges is None: |
| 56 | return True |
| 57 | for container in self.savedStateCheckChanges: |
| 58 | if len(container) > 0: |
| 59 | return True |
| 60 | return False |