Interactive/MenusAndHotkeysCustom.py

# (C) Copyright 2012 Autodesk, Inc.  All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy
# form.

#
# You will need Autodesk Showcase 2009 or later.
#
 
#
# You will get Help -> All Menu Items that show all the menu items.
# and Help -> All Hotkeys that show all the assigned hotkeys.
#

from MessageInterpreter     import MessageInterpreter
from UserCustomization      import UserCustomBase, CustomInfo
from awSupportApi           import Product
from Dialogs.AboutDialog    import AboutDialog

def instantiate():
    return DumpMenusCustomization()

class DumpMenusCustomization(UserCustomBase):
    def __init__(self):
        self.__myInterpreter = LocalInterpreter()
        self.__myMenu = None
        self.__myDumpId = None
        self.__myLocalMenu = None

    def getInterpreter(self, isInteractive):
        return self.__myInterpreter

    def appendMenu(self, menuInterpreter):
        self.__myMenuInterpreter = menuInterpreter
        
    def appendMenuItems(self,id,menu):
        if "Help" == id:
            self.__myMenu = menu
            menu.appendItem(_( 'Show All Menu Items' ), self.__onAllMenuItems )
            menu.appendItem(_( 'Show All Hotkeys' ), self.__onAllHotkeys )


    def __onAllMenuItems( self, event ):
        self.__myInterpreter.AllMenuItems(self.__myMenuInterpreter, self.__myMenu)


    def __onAllHotkeys( self, event ):
        self.__myInterpreter.AllHotkeys(self.__myMenuInterpreter, self.__myMenu)

        
class LocalInterpreter(MessageInterpreter):
    def __init__(self):
        MessageInterpreter.__init__(self)
        self.myMenus = []
        self.myHotkeys = {}
        self.kSpacing = "    "


    def assembleItem( self, prefix, item ):
        return "%s%s<br>\n" % (prefix,item)


    def assembleFirst( self, prefix, item ):
        return "%s<b>%s</b><br>\n" % (prefix,item)


    def assembleMenu( self, prefix, menu ):
        first = True
        res = ""
        for item in menu:
            if isinstance(item,list):
                res += self.assembleMenu(prefix + self.kSpacing, item)
            elif first:
                res += self.assembleFirst(prefix,item)
            else:
                res += self.assembleItem(prefix + self.kSpacing, item)
            first = False
        return res


    def assembleHotkeys( self ):
        res = "<table border=1>\n"
        for (k,l) in sorted(self.myHotkeys.iteritems()):
            res += "<tr><td>%s</td><td>%s</td></tr>\n" % (k,l)
        res += "</table>\n"
        return res


    def AllMenuItems(self, menuInterpreter, menu):
        self.__recompute( menuInterpreter )

        product = Product.instance()
        productName = product.getFullNameNoFlavour()
        yearId = "%s" % (product.getCutId()[0:4])

        md = "<html>\n"
        for one in self.myMenus:
            md += "<br>\n"
            md += self.assembleMenu( "", one )
        md += "</html>\n"

        msgs = AboutDialog(scrollHeight=500,doHtml=True,printTitle=productName)
        msgs.showRes( menu.getParentWindow(),
                      '%s Menu Items' % (productName),
                      product.getBrandingResourceFile(),
                      product.getBrandingIndexAboutImage(),
                      "",
                      "Copyright %s Autodesk, Inc. All rights reserved. Do not distribute." % (yearId),
                      "",
                      md )


    def AllHotkeys(self, menuInterpreter, menu):
        self.__recompute( menuInterpreter )
        product = Product.instance()
        productName = product.getFullNameNoFlavour()
        yearId = "%s" % (product.getCutId()[0:4])

        hd = "<html>\n"
        hd += self.assembleHotkeys()
        hd += "</html>\n"

        msgs = AboutDialog(scrollHeight=500,doHtml=True,printTitle=productName)
        msgs.showRes( menu.getParentWindow(),
                      '%s Hotkeys' % (productName),
                      product.getBrandingResourceFile(),
                      product.getBrandingIndexAboutImage(),
                      "",
                      "Copyright %s Autodesk, Inc. All rights reserved. Do not distribute." % (yearId),
                      "",
                      hd )


    def __invoke(self,top, val):
        if val:
            return '&nbsp;&nbsp;<font color="red" size="-1">Access with MENU_INVOKE_ITEM(%s,%s)</font>' % (str(top),str(val))
        else:
            return ""

    def __recompute( self, menuInterpreter ):
        self.myMenus = []
        self.myHotkeys = {}
        for menu in menuInterpreter:
            top = menu
            active = [str(menu.getLabel())]
            self.__dumpOne( top,
                            menu,
                            menu.getLabel(),
                            active )
            self.myMenus.append(active)


    def __dumpOne( self, top, menu, path, active ):
        for item in menu.GetMenuItems():
            fullid = ""
            if hasattr(top,'myMenuItems'):
                for id in top.myMenuItems:
                    if top.myMenuItems[id] == item.GetId():
                        fullid = self.__invoke(top.getMenuId(), id)
                        break
            if item.IsSeparator():
                active.append( "--------" )
            elif item.IsSubMenu():
                newOne = []
                newOne.append( str(item.GetLabel()) + fullid )
                self.__dumpOne( top, item.GetSubMenu(),
                                "%s --> %s" % (path,item.GetLabel()),
                                newOne )
                active.append( newOne )

            else:
                label = item.GetText()
                if label != "Show All Menu Items" and \
                   label != "Show All Hotkeys":
                    hkl = label.split('\t')
                    if 2 == len(hkl):
                        hk = str(hkl[1])
                        self.myHotkeys[hk] = \
                            "%s --> %s" % (path,item.GetLabel())
                    label.replace('\t','XXXX')
                    active.append( label + fullid )


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '1.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Show all menu items and all hotkeys."
    customInfo.longInfo = \
"""Add two menu items under "Help" menu to show all menu items and all hotkeys.  The menu items list also shows the syntax for the MENU_INVOKE_ITEM message to access it.
"""
    return customInfo