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

import traceback
from coral.modeler.dlg.ActionBrowserDlg import ActionBrowserDlg
from coral.modeler.extension import ModelerTool
import coral
import qt
from coral.modeler import action
from coral.modeler.window import CoralMainWindow
import copy

class ActionBrowser(ActionBrowserDlg,ModelerTool):
    def __init__(self,parent):
        ModelerTool.__init__(self)
        ActionBrowserDlg.__init__(self,parent)
        self.setCaption("Coral Action Browser")
        self.connect(self.bInvoke,qt.SIGNAL('clicked()'),self.onInvoke)
        self.hide()
        self.resize(600,400)
        self.current=None
        self.doIt()
        
    def onCurrentChanged(self,e):
        pass
        
    def doIt(self):
        top=self
        while(top.parent()):
            top=top.parent()
        self.actionList.clear()
        l=copy.copy(action.actions.values())
        l.sort()        
        for a in l:
            self.actionList.insertItem(a.name)
        self.current=None
        

    def onInvoke(self):
        name=unicode(self.actionList.currentText())
        if not action.__exists__(name):
            qt.QMessageBox.critical(self,"Invoke Action","Action "+name+" has been deleted from the application.")
            self.doIt()                               
        else:        
            action[unicode(self.actionList.currentText())].dispatchProtected()

            


