Skip to content

Commit

Permalink
Merge pull request #621 from blitzmann/context-menu-ids
Browse files Browse the repository at this point in the history
Context menu ids
  • Loading branch information
blitzmann committed May 22, 2016
2 parents de6c843 + d71b050 commit f9c5954
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gui/builtinContextMenus/changeAffectingSkills.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def addSkill(self, rootMenu, skill, i):
else:
label = "Level %s" % i

id = wx.NewId()
id = ContextMenu.nextID()
self.skillIds[id] = (skill, i)
menuItem = wx.MenuItem(rootMenu, id, label, kind=wx.ITEM_RADIO)
rootMenu.Bind(wx.EVT_MENU, self.handleSkillChange, menuItem)
Expand All @@ -72,7 +72,7 @@ def getSubMenu(self, context, selection, rootMenu, i, pitem):
sub = wx.Menu()

for skill in self.skills:
skillItem = wx.MenuItem(sub, wx.NewId(), skill.item.name)
skillItem = wx.MenuItem(sub, ContextMenu.nextID(), skill.item.name)
grandSub = wx.Menu()
skillItem.SetSubMenu(grandSub)
if skill.learned:
Expand Down
2 changes: 1 addition & 1 deletion gui/builtinContextMenus/damagePattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def getText(self, itmContext, selection):
return self.m

def addPattern(self, rootMenu, pattern):
id = wx.NewId()
id = ContextMenu.nextID()
name = getattr(pattern, "_name", pattern.name) if pattern is not None else "No Profile"

self.patternIds[id] = pattern
Expand Down
2 changes: 1 addition & 1 deletion gui/builtinContextMenus/fighterAbilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def getText(self, itmContext, selection):

def addAbility(self, menu, ability):
label = ability.name
id = wx.NewId()
id = ContextMenu.nextID()
self.abilityIds[id] = ability
menuItem = wx.MenuItem(menu, id, label, kind=wx.ITEM_CHECK)
menu.Bind(wx.EVT_MENU, self.handleMode, menuItem)
Expand Down
2 changes: 1 addition & 1 deletion gui/builtinContextMenus/implantSets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def getSubMenu(self, context, selection, rootMenu, i, pitem):
self.idmap = {}

for set in implantSets:
id = wx.NewId()
id = ContextMenu.nextID()
mitem = wx.MenuItem(rootMenu, id, set.name)
bindmenu.Bind(wx.EVT_MENU, self.handleSelection, mitem)
self.idmap[id] = set
Expand Down
4 changes: 2 additions & 2 deletions gui/builtinContextMenus/metaSwap.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def get_metagroup(x):

if thisgroup != group:
group = thisgroup
id = wx.NewId()
id = ContextMenu.nextID()
m.Append(id, u'─ %s ─' % group)
m.Enable(id, False)

id = wx.NewId()
id = ContextMenu.nextID()
mitem = wx.MenuItem(rootMenu, id, item.name)
bindmenu.Bind(wx.EVT_MENU, self.handleModule, mitem)
self.moduleLookup[id] = item
Expand Down
4 changes: 2 additions & 2 deletions gui/builtinContextMenus/moduleAmmoPicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def nameSorter(self, charge):
return map(self.numericConverter, parts)

def addCharge(self, menu, charge):
id = wx.NewId()
id = ContextMenu.nextID()
name = charge.name if charge is not None else "Empty"
self.chargeIds[id] = charge
item = wx.MenuItem(menu, id, name)
Expand All @@ -112,7 +112,7 @@ def addCharge(self, menu, charge):
return item

def addSeperator(self, m, text):
id = wx.NewId()
id = ContextMenu.nextID()
m.Append(id, u'─ %s ─' % text)
m.Enable(id, False)

Expand Down
2 changes: 1 addition & 1 deletion gui/builtinContextMenus/tacticalMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def getText(self, itmContext, selection):

def addMode(self, menu, mode):
label = mode.item.name.rsplit()[-2]
id = wx.NewId()
id = ContextMenu.nextID()
self.modeIds[id] = mode
menuItem = wx.MenuItem(menu, id, label, kind=wx.ITEM_RADIO)
menu.Bind(wx.EVT_MENU, self.handleMode, menuItem)
Expand Down
4 changes: 2 additions & 2 deletions gui/builtinContextMenus/targetResists.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def handleResistSwitch(self, event):
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID))

def addPattern(self, rootMenu, pattern):
id = wx.NewId()
id = ContextMenu.nextID()
name = getattr(pattern, "_name", pattern.name) if pattern is not None else "No Profile"

self.patternIds[id] = pattern
Expand Down Expand Up @@ -89,7 +89,7 @@ def getSubMenu(self, context, selection, rootMenu, i, pitem):
# Items that have a parent
for menuName, patterns in self.subMenus.items():
# Create parent item for root menu that is simply name of parent
item = wx.MenuItem(rootMenu, wx.NewId(), menuName)
item = wx.MenuItem(rootMenu, ContextMenu.nextID(), menuName)

# Create menu for child items
grandSub = wx.Menu()
Expand Down
2 changes: 1 addition & 1 deletion gui/builtinContextMenus/whProjector.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def getSubMenu(self, context, selection, rootMenu, i, pitem):
sub.AppendItem(subItem)

for swData in sorted(effdata[swType], key=lambda tpl: tpl[2]):
wxid = wx.NewId()
wxid = ContextMenu.nextID()
swObj, swName, swClass = swData
self.idmap[wxid] = (swObj, swName)
grandSubItem = wx.MenuItem(grandSub, wxid, swClass)
Expand Down
29 changes: 28 additions & 1 deletion gui/contextMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
#===============================================================================

import wx
import logging

logger = logging.getLogger(__name__)

class ContextMenu(object):
menus = []
_ids = [] #[wx.NewId() for x in xrange(200)] # init with decent amount
_idxid = -1

@classmethod
def register(cls):
Expand All @@ -44,6 +49,8 @@ def getMenu(cls, selection, *fullContexts):
(('marketItemGroup', 'Implant'),)
(('fittingShip', 'Ship'),)
"""
cls._idxid = -1
debug_start = len(cls._ids)

rootMenu = wx.Menu()
rootMenu.info = {}
Expand All @@ -68,7 +75,7 @@ def getMenu(cls, selection, *fullContexts):
bitmap = m.getBitmap(srcContext, selection)
multiple = not isinstance(bitmap, wx.Bitmap)
for it, text in enumerate(texts):
id = wx.NewId()
id = cls.nextID()
rootItem = wx.MenuItem(rootMenu, id, text)
rootMenu.info[id] = (m, fullContext, it)

Expand Down Expand Up @@ -109,6 +116,10 @@ def getMenu(cls, selection, *fullContexts):
if amount > 0 and i != len(fullContexts) - 1:
rootMenu.AppendSeparator()

debug_end = len(cls._ids)
if (debug_end - debug_start):
logger.debug("%d new IDs created for this menu" % (debug_end - debug_start))

return rootMenu if empty is False else None

@classmethod
Expand All @@ -134,6 +145,22 @@ def activate(self, fullContext, selection, i):
def getSubMenu(self, context, selection, rootMenu, i, pitem):
return None

@classmethod
def nextID(cls):
"""
Fetches an ID from the pool of IDs allocated to Context Menu.
If we don't have enough ID's to fulfill request, create new
ID and add it to the pool.
See GH Issue #589
"""
cls._idxid += 1

if cls._idxid >= len(cls._ids): # We don't ahve an ID for this index, create one
cls._ids.append(wx.NewId())

return cls._ids[cls._idxid]

def getText(self, context, selection):
"""
getText should be implemented in child classes, and should return either
Expand Down

0 comments on commit f9c5954

Please sign in to comment.