1+ import wx
2+
3+ import gui .fitCommands as cmd
4+ import gui .mainFrame
5+ from gui .contextMenu import ContextMenuSingle
6+ from service .fit import Fit
7+
8+ _t = wx .GetTranslation
9+
10+
11+ class FillCargoWithItem (ContextMenuSingle ):
12+ def __init__ (self ):
13+ self .mainFrame = gui .mainFrame .MainFrame .getInstance ()
14+
15+ def display (self , callingWindow , srcContext , mainItem ):
16+ if srcContext not in ("marketItemGroup" , "marketItemMisc" ):
17+ return False
18+
19+ if mainItem is None :
20+ return False
21+
22+ if self .mainFrame .getActiveFit () is None :
23+ return False
24+
25+ # Only allow items that can be stored in cargo
26+ if not (mainItem .isCharge or mainItem .isCommodity ):
27+ return False
28+
29+ return True
30+
31+ def getText (self , callingWindow , itmContext , mainItem ):
32+ return _t ("Fill Cargo With {0}" ).format (itmContext )
33+
34+ def activate (self , callingWindow , fullContext , mainItem , i ):
35+ fitID = self .mainFrame .getActiveFit ()
36+ fit = Fit .getInstance ().getFit (fitID )
37+
38+ # Get the item's volume
39+ itemVolume = mainItem .attributes ['volume' ].value
40+ if itemVolume is None or itemVolume <= 0 :
41+ return
42+
43+ # Calculate how many items can fit in the cargo
44+ cargoCapacity = fit .ship .getModifiedItemAttr ("capacity" )
45+ currentCargoVolume = fit .cargoBayUsed
46+ availableVolume = cargoCapacity - currentCargoVolume
47+
48+ if availableVolume <= 0 :
49+ return
50+
51+ # Calculate maximum amount that can fit
52+ maxAmount = int (availableVolume / itemVolume )
53+ if maxAmount <= 0 :
54+ return
55+
56+ # Add the items to cargo
57+ command = cmd .GuiAddCargoCommand (fitID = fitID , itemID = int (mainItem .ID ), amount = maxAmount )
58+ if self .mainFrame .command .Submit (command ):
59+ self .mainFrame .additionsPane .select ("Cargo" , focus = False )
60+
61+
62+ FillCargoWithItem .register ()
0 commit comments