
from qt import *
try:
    from qtlistview import *
except:
    pass

import coral
import propertyeditors as PE
L=coral.lang.CQUERY10

TM=coral.getTransactionManager()

class PropertyListItem(QCheckListItem):
    def __init__(self, parent, prop):
        QCheckListItem.__init__(self,parent,unicode(prop.getName()),QCheckListItem.CheckBox)
        self.prop=prop
        
class QueryElement(PE.QueryElementEditor):
    def __init__(self,parent,manager=None):
        PE.QueryElementEditor.__init__(self,parent,manager)
        self.items = {}

                
    def focusOn(self,element):
        
        if element is self.focus:
            PE.QueryElementEditor.focusOn(self,element)
            self.isActive.setChecked(element.isActive)
            self.updateCheckBox()
            return            
        else:
            PE.QueryElementEditor.focusOn(self,element)
            self.isActive.setChecked(element.isActive)
            self.items = {}
            self.itable.clear()
            if not element or not element.oclIsKindOf(coral.lang.CQUERY10.QueryElement):
                return
            
            if element.abstractElement:
                for prop in element.abstractElement.__properties__():
                    if prop.getMultLower() == 0 or prop.isAnonymous() or prop.getName() == "xmi_extension":
                        continue
                    pti=PropertyListItem(self.itable, prop )
                    self.items[prop] = pti
                    if prop.getCorrespondingElement() in element.ignore:
                        pti.setOn(True)
                                
            self.itable.sortColumn()
            self.itable.show()

            
    def valueChanged(self, item):
        assert self.focus
        if not item:
            return
        meta_prop=item.prop.getCorrespondingElement()
        
        TM.beginModifyCmd()
        try:
            
            if meta_prop in self.focus.ignore:
                self.focus.ignore.remove(meta_prop)
                item.setOn(False)
            else:
                self.focus.ignore.append(meta_prop)
                item.setOn(True)
        finally:
            TM.endModifyCmd()


    def updateCheckBox(self):
        set_select_all=True
        
        for item in self.items.values():
            meta_prop=item.prop.getCorrespondingElement()
            if meta_prop in self.focus.ignore:
                item.setOn(True)
            else:
                item.setOn(False)
                set_select_all=False

        self.selectAllBox.setChecked(set_select_all)
        
    def isActiveClicked(self, bool):
        TM.beginModifyCmd()
        try:
            self.focus.isActive = bool
        finally:
            TM.endModifyCmd()

    def selectAllClicked(self):
        is_checked=self.selectAllBox.isChecked()
        
        TM.beginModifyCmd()
        try:
            for item in self.items.values():
                meta_prop=item.prop.getCorrespondingElement()
                item.setOn(is_checked)

                if is_checked:
                    self.focus.ignore.append(meta_prop)

            if not is_checked:
                self.focus.ignore.resetSlot()
        finally:
            TM.endModifyCmd()
