from coral.modeler.extension import ContextMenuAction
import coral
import qt
from qt import *
from coral.modeler.coralmodeler import CoralModeler
from coral.modeler import modeler, iconcache
DI = coral.lang.XMIDI20
import coral.modeler.diagram.xmidi_diagrams as diagrams
from coral.modeler import extension,action
import engine as E

class Menu(ContextMenuAction):
    def createMenuItem(self,menu,lElement,lPresentation=[], diagram=None, position=None):

        self.diagram = diagram
        self.elements = lElement
        self.position = position
        self.menu=menu
        if lElement:
            self.element=lElement[0]
        else:
            self.element=None
            
        self.presentation=lPresentation
        self.constraintTab=None
        
        if self.element and (type(self.element) != coral.kernel.ProjectPtr):
            if self.element.oclIsKindOf(coral.lang.CONSTRAINT10.ConstraintModel):

                
                if E.isActive(self.element):
                    menu.insertItem("Deactivate Checking",self.deActivateEngine)
                else:
                    menu.insertItem("Activate Checking",self.activateEngine)

                menu.insertItem("Open in Constraint Tab", self.openInConstraintTab)

    def deActivateEngine(self):
        E.deActivateCM(self.element)
        allConstraintTabs = self.findConstraintTabs()
        for ct in allConstraintTabs:
            if ct.modelWidget and ct.modelWidget.cm == self.element:
                ct.updateIcons(self.element)
                ct.activateDeactivateButton.setIconSet(QIconSet(iconcache.getIcon("enable", "constraint")))
        
    def activateEngine(self):
        allConstraintTabs = self.findConstraintTabs()
        for ct in allConstraintTabs:
            if ct.modelWidget and ct.modelWidget.cm == self.element:
                ct.setProgressPictureOn()
                ct.progressLabel.repaint()
        E.loadConstraintModel(self.element)
        E.activateCM(self.element)
        for ct in allConstraintTabs:
            if ct.modelWidget and ct.modelWidget.cm == self.element:
                ct.setProgressPictureOff()
                ct.updateIcons(self.element)
                ct.activateDeactivateButton.setIconSet(QIconSet(iconcache.getIcon("disable", "constraint")))
    def openInConstraintTab(self):
        self.allConstraintTabs = self.findConstraintTabs()
        

        #The CM is already loaded in the current tab
        currentPage = action.getCurrentHandler("modeler").tabs[(1,1)].currentPage()
        if isinstance(currentPage,coral.addins.constraint.constraintTab.ConstraintTab):
            if currentPage.modelWidget and self.element == currentPage.modelWidget.cm:
                return
            
        for ct in self.allConstraintTabs:
            if ct.modelWidget and ct.modelWidget.cm == self.element:
                #The CM is loaded in a tab that has been closed
                if action.getCurrentHandler("modeler").tabs[(1,1)].indexOf(ct) < 0:
                    action.getCurrentHandler("modeler").addToolToTab((1,1),ct)
                    return
                #The CM is loaded in a tab that is still open, but is not the current page
                else:
                    action.getCurrentHandler("modeler").tabs[(1,1)].showPage(ct)
                    return

        #The CM has not previously been loaded in a constraint tab, looking for empty tab to open CM in
        for ct in self.allConstraintTabs:
            if not ct.modelWidget:
                ct.loadConstraintModel(self.element)
                return
            
        #There are no empty constraint tabs to open CM in, creating a new one
        action["openConstraint"].__call__()
        self.allConstraintTabs = self.findConstraintTabs()
        for ct in self.allConstraintTabs:
            if not ct.modelWidget:
                ct.loadConstraintModel(self.element)

    def findConstraintTabs(self):
        """Looks for the Constraint Tab from the set of editors of the current handler"""
        #self.constraintTab=None
        tabs = []
        self.window = action.getCurrentHandler("modeler")
        for e in self.window.editorSet.getEditors():
            if isinstance(e,coral.addins.constraint.constraintTab.ConstraintTab):
                tabs.append(e)
        return tabs


        
