Edit detail for baseLeoPlugin revision 1 of 1

1
Editor: ktenney
Time: 2006/06/25 04:59:36 GMT+0
Note:

changed:
-
import leoPlugins

class baseLeoPlugin(object):
    """A Convenience class to simplify plugin authoring
    """
    
    import leoGlobals as g
    
    def __init__(self, tag, keywords):
                    
        self.c = keywords['c']
        self.commandNames = []
        
    def setCommand(self, commandName, handler, 
                    shortcut = None, pane = 'all', verbose = True):
        
        """Associate a command name with handler code, 
        optionally defining a keystroke shortcut
        """
        self.commandNames.append(commandName)
        
        self.commandName = commandName
        self.shortcut = shortcut
        self.handler = handler
        self.c.k.registerCommand (commandName, shortcut, handler, 
                                pane, verbose)
                                
    def setMenuItem(self, menu, commandName = None, handler = None):
        
        """Create a menu item in 'menu' using text 'commandName' calling handler 'handler'
        if commandName and handler are none, use the most recently defined values
        """
        
        # setMenuItem can create a command, or use a previously defined one.
        if commandName is None:
            commandName = self.commandName
        # make sure commandName is in the list of commandNames                        
        else:
            if commandName not in self.commandNames:
                self.commandNames.append(commandName) 
                       
        if handler is None:
            handler = self.handler
            
        table = ((commandName, None, handler),)
        self.c.frame.menu.createMenuItemsFromTable(menu, table)

    def setButton(self, buttonText = None, commandName = None, color = None):
        
        """Associate an existing command with a 'button'
        """
        if buttonText is None:
            buttonText = self.commandName
            
        if commandName is None:
            commandName = self.commandName       
        else:
            if commandName not in self.commandNames:
                raise NameError, "setButton error, %s is not a commandName" % commandName
            
        if color is None:
            color = 'grey'
        script = "c.k.simulateCommand('%s')" % self.commandName
        self.g.makeScriptButton(self.c, script=script, 
                                buttonText = buttonText, bg = color)
        
class Hello(baseLeoPlugin):
    """Say hello from <current node>
    """
    def __init__(self, tag, keywords):
        
        # call parent __init__
        baseLeoPlugin.__init__(self, tag, keywords)
        
        # if the plugin object defines only one command, 
        # just give it a name. You can then create a button and menu entry
        self.setCommand('Hello', self.hello)
        self.setButton()
        self.setMenuItem('Cmds')
        
        # create a command with a shortcut
        self.setCommand('Hola', self.hola, 'Alt-Ctrl-H')
        
        # create a button using different text than commandName
        self.setButton('Hello in Spanish')
        
        # create a menu item with default text
        self.setMenuItem('Cmds')
        
        # define a command using setMenuItem 
        self.setMenuItem('Cmds', 'Ciao baby', self.ciao)
                        
    def hello(self, event):
        self.g.es( "hello from node %s" % self.c.currentPosition().headString())

    def hola(self, event):
        self.g.es( "hola from node %s" % self.c.currentPosition().headString())
        
    def ciao(self, event):
        self.g.es( "ciao baby (%s)" % self.c.currentPosition().headString())
            
   
leoPlugins.registerHandler("after-create-leo-frame", Hello)




import leoPlugins

class baseLeoPlugin(object):

"""A Convenience class to simplify plugin authoring """

import leoGlobals as g

def __init__(self, tag, keywords):

self.c = keywords['c']? self.commandNames = []
def setCommand(self, commandName, handler,
shortcut = None, pane = 'all', verbose = True):

"""Associate a command name with handler code, optionally defining a keystroke shortcut """ self.commandNames.append(commandName)

self.commandName = commandName self.shortcut = shortcut self.handler = handler self.c.k.registerCommand (commandName, shortcut, handler,

pane, verbose)

def setMenuItem(self, menu, commandName = None, handler = None):

"""Create a menu item in 'menu' using text 'commandName' calling handler 'handler' if commandName and handler are none, use the most recently defined values """

# setMenuItem can create a command, or use a previously defined one. if commandName is None:

commandName = self.commandName

# make sure commandName is in the list of commandNames else:

if commandName not in self.commandNames:
self.commandNames.append(commandName)
if handler is None:
handler = self.handler

table = ((commandName, None, handler),) self.c.frame.menu.createMenuItemsFromTable(menu, table)

def setButton(self, buttonText = None, commandName = None, color = None):

"""Associate an existing command with a 'button' """ if buttonText is None:

buttonText = self.commandName
if commandName is None:
commandName = self.commandName
else:
if commandName not in self.commandNames:
raise NameError?, "setButton error, %s is not a commandName" % commandName
if color is None:
color = 'grey'

script = "c.k.simulateCommand('%s')" % self.commandName self.g.makeScriptButton(self.c, script=script,

buttonText = buttonText, bg = color)
class Hello(baseLeoPlugin):

"""Say hello from <current node> """ def __init__(self, tag, keywords):

# call parent __init__ baseLeoPlugin.__init__(self, tag, keywords)

# if the plugin object defines only one command, # just give it a name. You can then create a button and menu entry self.setCommand('Hello', self.hello) self.setButton() self.setMenuItem('Cmds')

# create a command with a shortcut self.setCommand('Hola', self.hola, 'Alt-Ctrl-H')

# create a button using different text than commandName self.setButton('Hello in Spanish')

# create a menu item with default text self.setMenuItem('Cmds')

# define a command using setMenuItem self.setMenuItem('Cmds', 'Ciao baby', self.ciao)

def hello(self, event):
self.g.es( "hello from node %s" % self.c.currentPosition().headString())
def hola(self, event):
self.g.es( "hola from node %s" % self.c.currentPosition().headString())
def ciao(self, event):
self.g.es( "ciao baby (%s)" % self.c.currentPosition().headString())

leoPlugins.registerHandler("after-create-leo-frame", Hello)