|
|
|
|
@@ -1,4 +1,5 @@
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
|
|
# noinspection PyPackageRequirements
|
|
|
|
|
import wx
|
|
|
|
|
@@ -20,7 +21,7 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
sTR = svc_TargetProfile.getInstance()
|
|
|
|
|
self.profiles = sTR.getUserTargetProfileList()
|
|
|
|
|
self.profiles = list(chain(sTR.getBuiltinTargetProfileList(), sTR.getUserTargetProfileList()))
|
|
|
|
|
self.profiles.sort(key=lambda p: (p.name in ['None'], p.name))
|
|
|
|
|
|
|
|
|
|
return len(self.profiles) > 0
|
|
|
|
|
@@ -30,7 +31,7 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
|
|
|
|
return 'Target Resists'
|
|
|
|
|
|
|
|
|
|
def handleResistSwitch(self, event):
|
|
|
|
|
profile = self.profileIds.get(event.Id, False)
|
|
|
|
|
profile = self.profileEventMap.get(event.Id, False)
|
|
|
|
|
if profile is False:
|
|
|
|
|
event.Skip()
|
|
|
|
|
return
|
|
|
|
|
@@ -40,77 +41,63 @@ class TargetProfileSwitcher(ContextMenuUnconditional):
|
|
|
|
|
sFit.setTargetProfile(fitID, profile)
|
|
|
|
|
wx.PostEvent(self.mainFrame, GE.FitChanged(fitIDs=(fitID,)))
|
|
|
|
|
|
|
|
|
|
def addProfile(self, rootMenu, profile):
|
|
|
|
|
def _addProfile(self, parentMenu, profile, name):
|
|
|
|
|
id = ContextMenuUnconditional.nextID()
|
|
|
|
|
name = getattr(profile, '_name', profile.name) if profile is not None else 'No Profile'
|
|
|
|
|
|
|
|
|
|
self.profileIds[id] = profile
|
|
|
|
|
item = wx.MenuItem(rootMenu, id, name, kind=wx.ITEM_CHECK)
|
|
|
|
|
rootMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, item)
|
|
|
|
|
self.profileEventMap[id] = profile
|
|
|
|
|
menuItem = wx.MenuItem(parentMenu, id, name, kind=wx.ITEM_CHECK)
|
|
|
|
|
parentMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, menuItem)
|
|
|
|
|
|
|
|
|
|
# determine active profile
|
|
|
|
|
sFit = Fit.getInstance()
|
|
|
|
|
fitID = self.mainFrame.getActiveFit()
|
|
|
|
|
f = sFit.getFit(fitID)
|
|
|
|
|
tr = f.targetProfile
|
|
|
|
|
checked = sFit.getFit(fitID).targetProfile is profile
|
|
|
|
|
return menuItem, checked
|
|
|
|
|
|
|
|
|
|
checked = tr == profile
|
|
|
|
|
|
|
|
|
|
return item, checked
|
|
|
|
|
def _addCategory(self, parentMenu, name):
|
|
|
|
|
id = ContextMenuUnconditional.nextID()
|
|
|
|
|
menuItem = wx.MenuItem(parentMenu, id, name)
|
|
|
|
|
parentMenu.Bind(wx.EVT_MENU, self.handleResistSwitch, menuItem)
|
|
|
|
|
return menuItem
|
|
|
|
|
|
|
|
|
|
def getSubMenu(self, callingWindow, context, rootMenu, i, pitem):
|
|
|
|
|
self.profileIds = {}
|
|
|
|
|
self.subMenus = OrderedDict()
|
|
|
|
|
self.singles = []
|
|
|
|
|
self.profileEventMap = {}
|
|
|
|
|
|
|
|
|
|
sub = wx.Menu()
|
|
|
|
|
self.items = (OrderedDict(), OrderedDict())
|
|
|
|
|
for profile in self.profiles:
|
|
|
|
|
start, end = profile.name.find('['), profile.name.find(']')
|
|
|
|
|
if start is not -1 and end is not -1:
|
|
|
|
|
currBase = profile.name[start + 1:end]
|
|
|
|
|
name = profile.name[end + 1:].strip()
|
|
|
|
|
if not name:
|
|
|
|
|
self.singles.append(profile)
|
|
|
|
|
continue
|
|
|
|
|
# set helper attr
|
|
|
|
|
setattr(profile, '_name', name)
|
|
|
|
|
if currBase not in self.subMenus:
|
|
|
|
|
self.subMenus[currBase] = []
|
|
|
|
|
self.subMenus[currBase].append(profile)
|
|
|
|
|
else:
|
|
|
|
|
self.singles.append(profile)
|
|
|
|
|
# Add reset
|
|
|
|
|
msw = 'wxMSW' in wx.PlatformInfo
|
|
|
|
|
mitem, checked = self.addProfile(rootMenu if msw else sub, None)
|
|
|
|
|
sub.Append(mitem)
|
|
|
|
|
mitem.Check(checked)
|
|
|
|
|
sub.AppendSeparator()
|
|
|
|
|
remainingName = profile.name.strip()
|
|
|
|
|
container = self.items
|
|
|
|
|
while True:
|
|
|
|
|
start, end = remainingName.find('['), remainingName.find(']')
|
|
|
|
|
if start == -1 or end == -1:
|
|
|
|
|
container[0][remainingName] = profile
|
|
|
|
|
break
|
|
|
|
|
container = container[1].setdefault(remainingName[start + 1:end], (OrderedDict(), OrderedDict()))
|
|
|
|
|
remainingName = remainingName[end + 1:].strip()
|
|
|
|
|
|
|
|
|
|
# Single items, no parent
|
|
|
|
|
for profile in self.singles:
|
|
|
|
|
mitem, checked = self.addProfile(rootMenu if msw else sub, profile)
|
|
|
|
|
sub.Append(mitem)
|
|
|
|
|
mitem.Check(checked)
|
|
|
|
|
# Category as menu item - expands further
|
|
|
|
|
msw = "wxMSW" in wx.PlatformInfo
|
|
|
|
|
|
|
|
|
|
# Items that have a parent
|
|
|
|
|
for menuName, profiles in list(self.subMenus.items()):
|
|
|
|
|
# Create parent item for root menu that is simply name of parent
|
|
|
|
|
item = wx.MenuItem(rootMenu, ContextMenuUnconditional.nextID(), menuName)
|
|
|
|
|
|
|
|
|
|
# Create menu for child items
|
|
|
|
|
grandSub = wx.Menu()
|
|
|
|
|
|
|
|
|
|
# Apply child menu to parent item
|
|
|
|
|
item.SetSubMenu(grandSub)
|
|
|
|
|
|
|
|
|
|
# Append child items to child menu
|
|
|
|
|
for profile in profiles:
|
|
|
|
|
mitem, checked = self.addProfile(rootMenu if msw else grandSub, profile)
|
|
|
|
|
grandSub.Append(mitem)
|
|
|
|
|
def makeMenu(container, parentMenu, first=False):
|
|
|
|
|
menu = wx.Menu()
|
|
|
|
|
if first:
|
|
|
|
|
mitem, checked = self._addProfile(rootMenu if msw else parentMenu, None, 'No Profile')
|
|
|
|
|
menu.Append(mitem)
|
|
|
|
|
mitem.Check(checked)
|
|
|
|
|
sub.Append(item) # finally, append parent item to root menu
|
|
|
|
|
menu.AppendSeparator()
|
|
|
|
|
for name, pattern in container[0].items():
|
|
|
|
|
menuItem, checked = self._addProfile(rootMenu if msw else parentMenu, pattern, name)
|
|
|
|
|
menu.Append(menuItem)
|
|
|
|
|
menuItem.Check(checked)
|
|
|
|
|
for name, subcontainer in container[1].items():
|
|
|
|
|
menuItem = self._addCategory(rootMenu if msw else parentMenu, name)
|
|
|
|
|
subMenu = makeMenu(subcontainer, menu)
|
|
|
|
|
menuItem.SetSubMenu(subMenu)
|
|
|
|
|
menu.Append(menuItem)
|
|
|
|
|
menu.Bind(wx.EVT_MENU, self.handleResistSwitch)
|
|
|
|
|
return menu
|
|
|
|
|
|
|
|
|
|
return sub
|
|
|
|
|
subMenu = makeMenu(self.items, rootMenu, first=True)
|
|
|
|
|
return subMenu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TargetProfileSwitcher.register()
|
|
|
|
|
|