| 8 | |
| 9 | |
| 10 | class CalcAddBoosterCommand(wx.Command): |
| 11 | |
| 12 | def __init__(self, fitID, boosterInfo, position=None): |
| 13 | wx.Command.__init__(self, True, 'Add Booster') |
| 14 | self.fitID = fitID |
| 15 | self.newBoosterInfo = boosterInfo |
| 16 | self.newPosition = position |
| 17 | self.oldBoosterInfo = None |
| 18 | self.oldPosition = None |
| 19 | |
| 20 | def Do(self): |
| 21 | pyfalog.debug('Doing addition of booster {} to fit {}'.format(self.newBoosterInfo, self.fitID)) |
| 22 | fit = Fit.getInstance().getFit(self.fitID) |
| 23 | |
| 24 | if any(self.newBoosterInfo.itemID == b.itemID for b in fit.boosters): |
| 25 | pyfalog.debug('Skipping as such booster is already on the fit') |
| 26 | return False |
| 27 | |
| 28 | newBooster = self.newBoosterInfo.toBooster() |
| 29 | if newBooster is None: |
| 30 | return False |
| 31 | |
| 32 | self.oldPosition, self.oldBoosterInfo = fit.boosters.makeRoom(newBooster) |
| 33 | |
| 34 | if self.newPosition is not None: |
| 35 | fit.boosters.insert(self.newPosition, newBooster) |
| 36 | if newBooster not in fit.boosters: |
| 37 | pyfalog.warning('Failed to insert to list') |
| 38 | cmd = CalcAddBoosterCommand( |
| 39 | fitID=self.fitID, |
| 40 | boosterInfo=self.oldBoosterInfo, |
| 41 | position=self.oldPosition) |
| 42 | cmd.Do() |
| 43 | return False |
| 44 | else: |
| 45 | fit.boosters.append(newBooster) |
| 46 | if newBooster not in fit.boosters: |
| 47 | pyfalog.warning('Failed to append to list') |
| 48 | cmd = CalcAddBoosterCommand( |
| 49 | fitID=self.fitID, |
| 50 | boosterInfo=self.oldBoosterInfo, |
| 51 | position=self.oldPosition) |
| 52 | cmd.Do() |
| 53 | return False |
| 54 | self.newPosition = fit.boosters.index(newBooster) |
| 55 | |
| 56 | return True |
| 57 | |
| 58 | def Undo(self): |
| 59 | pyfalog.debug('Undo addition of booster {} to fit {}'.format(self.newBoosterInfo, self.fitID)) |
| 60 | if self.oldBoosterInfo is not None and self.oldPosition is not None: |
| 61 | cmd = CalcAddBoosterCommand(fitID=self.fitID, boosterInfo=self.oldBoosterInfo, position=self.oldPosition) |
| 62 | return cmd.Do() |
| 63 | from .remove import CalcRemoveBoosterCommand |
| 64 | cmd = CalcRemoveBoosterCommand(fitID=self.fitID, position=self.newPosition) |
| 65 | return cmd.Do() |