From 97ac0804c589a6288e9b52dbc1dfc1c2e211dee4 Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Sat, 15 Aug 2015 19:35:31 +0100 Subject: [PATCH 1/6] Add Submenu of variations of module type. Non functional so far. --- gui/builtinContextMenus/__init__.py | 1 + gui/builtinContextMenus/metaSwap.py | 51 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 gui/builtinContextMenus/metaSwap.py diff --git a/gui/builtinContextMenus/__init__.py b/gui/builtinContextMenus/__init__.py index d1da03023d..785e3eebf9 100644 --- a/gui/builtinContextMenus/__init__.py +++ b/gui/builtinContextMenus/__init__.py @@ -19,4 +19,5 @@ "targetResists", "priceClear", "amount", + "metaSwap", ] diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py new file mode 100644 index 0000000000..f2f611134c --- /dev/null +++ b/gui/builtinContextMenus/metaSwap.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +from gui.contextMenu import ContextMenu +from gui.itemStats import ItemStatsDialog +import gui.mainFrame +import service +import wx + +class MetaSwap(ContextMenu): + def __init__(self): + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + + def display(self, srcContext, selection): + + if self.mainFrame.getActiveFit() is None or srcContext not in ("fittingModule", "projectedModule"): + return False + + self.module = selection[0] + + return True + + def getText(self, itmContext, selection): + return "Variations" + + def getSubMenu(self, context, selection, rootMenu, i, pitem): + def get_metalevel(x): + return x.attributes["metaLevel"].value + + m = wx.Menu() + mkt = service.Market.getInstance() + items = list(mkt.getVariationsByItems([selection[0].item])) + items.sort(key=get_metalevel) + group = None + for item in items: + # Apparently no metaGroup for the Tech I variant: + if item.metaGroup is None: + thisgroup = "Tech I" + else: + thisgroup = item.metaGroup.name + + if thisgroup != group: + group = thisgroup + id = wx.NewId() + m.Append(id, u'─ %s ─' % group) + m.Enable(id, False) + + id = wx.NewId() + name = item.name + m.AppendItem(wx.MenuItem(rootMenu, id, name)) + return m + +MetaSwap.register() From 7f100353e2fc59bd75a0a636cc965386ccc860bd Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Sun, 16 Aug 2015 18:30:27 +0100 Subject: [PATCH 2/6] Make variations menu actually swap out module Functional, but not handling multiple selections well --- eos/effectHandlerHelpers.py | 4 ++++ gui/builtinContextMenus/metaSwap.py | 29 ++++++++++++++++++++++++-- service/fit.py | 32 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/eos/effectHandlerHelpers.py b/eos/effectHandlerHelpers.py index 3bf576a4c1..eca4d64379 100644 --- a/eos/effectHandlerHelpers.py +++ b/eos/effectHandlerHelpers.py @@ -159,6 +159,10 @@ def toDummy(self, index): dummy.position = index self[index] = dummy + def toModule(self, index, mod): + mod.position = index + self[index] = mod + def freeSlot(self, slot): for i in range(len(self) -1, -1, -1): mod = self[i] diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index f2f611134c..84bb865430 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -4,6 +4,12 @@ import gui.mainFrame import service import wx +import gui.globalEvents as GE + +# TODO: +# Handle multiple selection better +# Icons? +# Submenu for officer? class MetaSwap(ContextMenu): def __init__(self): @@ -22,6 +28,8 @@ def getText(self, itmContext, selection): return "Variations" def getSubMenu(self, context, selection, rootMenu, i, pitem): + self.moduleLookup = {} + def get_metalevel(x): return x.attributes["metaLevel"].value @@ -44,8 +52,25 @@ def get_metalevel(x): m.Enable(id, False) id = wx.NewId() - name = item.name - m.AppendItem(wx.MenuItem(rootMenu, id, name)) + mitem = wx.MenuItem(rootMenu, id, item.name) + rootMenu.Bind(wx.EVT_MENU, self.handleModule, mitem) + self.moduleLookup[id] = item + m.AppendItem(mitem) return m + def handleModule(self, event): + item = self.moduleLookup.get(event.Id, None) + if item is None: + event.Skip() + return + + sFit = service.Fit.getInstance() + fitID = self.mainFrame.getActiveFit() + fit = sFit.getFit(fitID) + + pos = fit.modules.index(self.module) + sFit.changeModule(fitID, pos, item.ID) + + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + MetaSwap.register() diff --git a/service/fit.py b/service/fit.py index ee6294cd7e..ce400bdf10 100644 --- a/service/fit.py +++ b/service/fit.py @@ -440,6 +440,38 @@ def removeModule(self, fitID, position): eos.db.commit() return numSlots != len(fit.modules) + def changeModule(self, fitID, position, newItemID): + fit = eos.db.getFit(fitID) + if fit.modules[position].isEmpty: + return None + + # Dummy it out in case the next bit fails + fit.modules.toDummy(position) + + item = eos.db.getItem(newItemID, eager=("attributes", "group.category")) + try: + m = eos.types.Module(item) + except ValueError: + return False + + if m.fits(fit): + m.owner = fit + fit.modules.toModule(position, m) + if m.isValidState(State.ACTIVE): + m.state = State.ACTIVE + + # As some items may affect state-limiting attributes of the ship, calculate new attributes first + self.recalc(fit) + # Then, check states of all modules and change where needed. This will recalc if needed + self.checkStates(fit, m) + + fit.fill() + eos.db.commit() + + return True + else: + return None + def moveCargoToModule(self, fitID, moduleIdx, cargoIdx, copyMod=False): """ Moves cargo to fitting window. Can either do a copy, move, or swap with current module From 6a4b2ffe69fef040a11d4a7700fe5329fa465872 Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Sun, 16 Aug 2015 21:45:20 +0100 Subject: [PATCH 3/6] Make variations menu handle multiple selections Ensure the variations menu only shows when you've got a matching set of items selected. --- gui/builtinContextMenus/metaSwap.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index 84bb865430..a408081323 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -10,6 +10,7 @@ # Handle multiple selection better # Icons? # Submenu for officer? +# Officer/Deadspace sorting class MetaSwap(ContextMenu): def __init__(self): @@ -17,10 +18,22 @@ def __init__(self): def display(self, srcContext, selection): - if self.mainFrame.getActiveFit() is None or srcContext not in ("fittingModule", "projectedModule"): + if self.mainFrame.getActiveFit() is None or srcContext not in ("fittingModule",): return False - self.module = selection[0] + # Check if list of variations is same for all of selection + # If not - don't show the menu + mkt = service.Market.getInstance() + self.variations = None + for i in selection: + variations = mkt.getVariationsByItems([i.item]) + if self.variations is None: + self.variations = variations + else: + if variations != self.variations: + return False + + self.selection = selection return True @@ -34,9 +47,10 @@ def get_metalevel(x): return x.attributes["metaLevel"].value m = wx.Menu() - mkt = service.Market.getInstance() - items = list(mkt.getVariationsByItems([selection[0].item])) + + items = list(self.variations) items.sort(key=get_metalevel) + group = None for item in items: # Apparently no metaGroup for the Tech I variant: @@ -68,8 +82,9 @@ def handleModule(self, event): fitID = self.mainFrame.getActiveFit() fit = sFit.getFit(fitID) - pos = fit.modules.index(self.module) - sFit.changeModule(fitID, pos, item.ID) + for mod in self.selection: + pos = fit.modules.index(mod) + sFit.changeModule(fitID, pos, item.ID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) From 8fe97180ed40040c4d7b5dcfac7ce888c5ed6dfe Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Sun, 16 Aug 2015 21:56:39 +0100 Subject: [PATCH 4/6] Sort higher metalevel items properly in variations menu --- gui/builtinContextMenus/metaSwap.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index a408081323..7536681d87 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -6,12 +6,6 @@ import wx import gui.globalEvents as GE -# TODO: -# Handle multiple selection better -# Icons? -# Submenu for officer? -# Officer/Deadspace sorting - class MetaSwap(ContextMenu): def __init__(self): self.mainFrame = gui.mainFrame.MainFrame.getInstance() @@ -46,10 +40,15 @@ def getSubMenu(self, context, selection, rootMenu, i, pitem): def get_metalevel(x): return x.attributes["metaLevel"].value + def get_metagroup(x): + return x.metaGroup.ID if x.metaGroup is not None else 0 + m = wx.Menu() + # Sort items by metalevel, and group within that metalevel items = list(self.variations) items.sort(key=get_metalevel) + items.sort(key=get_metagroup) group = None for item in items: @@ -57,6 +56,7 @@ def get_metalevel(x): if item.metaGroup is None: thisgroup = "Tech I" else: + print item.metaGroup.ID thisgroup = item.metaGroup.name if thisgroup != group: From c763595cc47a458aa11a6342323b413dcc9dcff3 Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Mon, 17 Aug 2015 15:41:58 +0100 Subject: [PATCH 5/6] Remove debug print statement --- gui/builtinContextMenus/metaSwap.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index 7536681d87..e233060d6c 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -56,7 +56,6 @@ def get_metagroup(x): if item.metaGroup is None: thisgroup = "Tech I" else: - print item.metaGroup.ID thisgroup = item.metaGroup.name if thisgroup != group: From 2904ab6afa06d7182d983adba16b3cb2384c2103 Mon Sep 17 00:00:00 2001 From: Will Wykeham Date: Tue, 18 Aug 2015 15:59:02 +0100 Subject: [PATCH 6/6] Non-windows platform fix for variations menu --- gui/builtinContextMenus/metaSwap.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gui/builtinContextMenus/metaSwap.py b/gui/builtinContextMenus/metaSwap.py index e233060d6c..35ae4b36ff 100644 --- a/gui/builtinContextMenus/metaSwap.py +++ b/gui/builtinContextMenus/metaSwap.py @@ -45,6 +45,13 @@ def get_metagroup(x): m = wx.Menu() + # If on Windows we need to bind out events into the root menu, on other + # platforms they need to go to our sub menu + if "wxMSW" in wx.PlatformInfo: + bindmenu = rootMenu + else: + bindmenu = m + # Sort items by metalevel, and group within that metalevel items = list(self.variations) items.sort(key=get_metalevel) @@ -66,7 +73,7 @@ def get_metagroup(x): id = wx.NewId() mitem = wx.MenuItem(rootMenu, id, item.name) - rootMenu.Bind(wx.EVT_MENU, self.handleModule, mitem) + bindmenu.Bind(wx.EVT_MENU, self.handleModule, mitem) self.moduleLookup[id] = item m.AppendItem(mitem) return m