# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

from coral.modeler.extension import ContextMenuEntry
import coral
import qt

from coral import log
logger=log.getLogger("executeOperation")


class ExecuteOperation(ContextMenuEntry):
    
    def createMenuItem(self,menu,element):
        logger.info(unicode(element))
        logger.info(unicode(element.__type__()))
        self.element=element
        opMenu=qt.QPopupMenu(menu,"op")
        menu.insertItem("Execute Operation",opMenu)
        id=0
        for o in element.__type__().__operations__():
            opMenu.insertItem(o.getFuncName(),self.doOperation,0,id)
            id+=1

    def doOperation(self,opId):
        coral.getTransactionManager().beginModifyCmd()
        try:
            op=self.element.__type__().getOperations()[opId]
            logger.debug(unicode(op))
            logger.debug("result="+unicode(op.getFunc()(self.element)))
        finally:
            coral.getTransactionManager().endModifyCmd()
        
