# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'query_widget.ui'
#
# Created: Mon Feb 27 21:37:40 2012
#      by: PyQt4 UI code generator 4.9
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import re
from UserNode import UserNode
from EdgeItem import EdgeItem

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_ToolBox(object):
    NUM_GROUPBOX = 3
    '''ids for tabs'''
    MODIFY_TAB = 0
    ADD_TAB = 1
    DELETE_TAB = 2
    '''none item in combobox'''
    NONE_ITEM = 'None'
    
    def setupUi(self, Form, main):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(214, 618)
        Form.setAutoFillBackground(True)
        self.tabId = 0
        self.main = main
        self.roleList = []
        self.userList = []
        self.dirList = []
        self.gridWidget = QtGui.QWidget()
        grid = QtGui.QGridLayout(self.gridWidget)
        grid.addWidget(self.createRoleHierarchyWhenClickExclusiveGroup(), 0, 0)
        grid.addWidget(self.createItemPanel(), 1, 0)
        self.gridWidget.setLayout(grid)

    def initToolBoxUISelection(self):
        self.roleHighlightSchemeGroupBox.setEnabled(False)
        self.noRHierRBtn.setChecked(True)
        self.rhierClickGroupBox.setEnabled(True)
        self.roleChildHighlightRBtn.setChecked(True)
        self.enableUsernodeBtn.setChecked(True)
        self.roleModifyRBtn.setChecked(True)
        self.roleAddRBtn.setChecked(True)
        self.roleDeleteRBtn.setChecked(True)
        self.enableRolePanel()
        
    def isNameEmpty(self, name):
        p = re.compile('^(\s)*$')
        if p.match(name):
            return True
        else:
            return False
        
    def nameHasSpace(self, name):
        if (re.search('\s', name)):
            return True
        else:
            return False
             
    def createRoleHierarchyWhenClickExclusiveGroup(self):
        self.rhierClickGroupBox = QtGui.QGroupBox('Highlight Nodes')
        rhierWidget = QtGui.QWidget(self.rhierClickGroupBox)
        self.noRHierRBtn = QtGui.QRadioButton('Without Role Hierarchy')
        self.withRHierRBtn = QtGui.QRadioButton('With Role Hierarchy')
        self.roleHighlightSchemeGroupBox = self.createRoleHighlightSchemeGroupBox()
        self.roleHighlightSchemeGroupBox.setEnabled(False)
        self.noRHierRBtn.clicked.connect(self.main.policyManager.enableHighlightNoRHier)
        self.withRHierRBtn.clicked.connect(self.main.policyManager.enableHighlightWithRHier)
        self.noRHierRBtn.setChecked(True)
        rhierVbox = QtGui.QVBoxLayout()
        rhierVbox.addWidget(self.noRHierRBtn)
        rhierVbox.addWidget(self.withRHierRBtn)
        rhierVbox.addWidget(self.roleHighlightSchemeGroupBox)
        rhierVbox.setMargin(0)
        rhierWidget.setLayout(rhierVbox)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(rhierWidget)
        line = self.createLineSeparator()
        vbox.addWidget(line)
        userGroupBox = self.createUserNodeToggleGroupBox()
        vbox.addWidget(userGroupBox)
        line1 = self.createLineSeparator()
        vbox.addWidget(line1)
        allGroupBox = self.createAllNodeHighlightGroupBox()
        vbox.addWidget(allGroupBox)
        vbox.addStretch(1)
        self.rhierClickGroupBox.setLayout(vbox)
        self.rhierClickGroupBox.setEnabled(True)
        return self.rhierClickGroupBox
    
    def createLineSeparator(self):
        line = QtGui.QFrame(self.rhierClickGroupBox)
        line.setFrameShape(QtGui.QFrame.HLine)
        line.setFrameShadow(QtGui.QFrame.Sunken)
        return line
    
    def createRoleHighlightSchemeGroupBox(self):
        schemeGroupBox = QtGui.QGroupBox(self.rhierClickGroupBox)
        schemeGroupBox.setTitle('Highlight Scheme')
        vbox = QtGui.QVBoxLayout()
        self.roleParentHighlightRBtn = QtGui.QRadioButton('Include Parent Roles')
        self.roleChildHighlightRBtn = QtGui.QRadioButton('Include Child Roles')
        self.roleBothHighlightRBtn = QtGui.QRadioButton('Include Parent and Child Roles')
        self.roleParentHighlightRBtn.clicked.connect(self.main.policyManager.enableRoleParentScheme)
        self.roleChildHighlightRBtn.clicked.connect(self.main.policyManager.enableRoleChildScheme)
        self.roleBothHighlightRBtn.clicked.connect(self.main.policyManager.enableRoleBothScheme)
        self.roleChildHighlightRBtn.setChecked(True)
        vbox.addWidget(self.roleChildHighlightRBtn)
        vbox.addWidget(self.roleParentHighlightRBtn)
        vbox.addWidget(self.roleBothHighlightRBtn)
        vbox.setMargin(0)
        schemeGroupBox.setLayout(vbox)
        return schemeGroupBox

    def createAllNodeHighlightGroupBox(self):
        allGroupBox = QtGui.QWidget(self.rhierClickGroupBox)
        allVbox = QtGui.QVBoxLayout()
        self.highlightAllBtn = QtGui.QPushButton('Highlight All')
        self.disableAllBtn = QtGui.QPushButton('Disable All')
        self.highlightAllBtn.clicked.connect(self.main.policyManager.highlightAllNodes)
        self.disableAllBtn.clicked.connect(self.main.policyManager.disableAllNodes)
        allVbox.addWidget(self.highlightAllBtn)
        allVbox.addWidget(self.disableAllBtn)
        allVbox.setMargin(0)
        allGroupBox.setLayout(allVbox)
        return allGroupBox
    
    def createUserNodeToggleGroupBox(self):
        usernodeToggleGroupBox = QtGui.QWidget(self.rhierClickGroupBox)
        userVbox = QtGui.QVBoxLayout()
        self.enableUsernodeBtn = QtGui.QRadioButton('Enable User Nodes')
        self.disableUsernodeBtn = QtGui.QRadioButton('Disable User Nodes')
        self.enableUsernodeBtn.clicked.connect(self.enableUserNodes)
        self.disableUsernodeBtn.clicked.connect(self.disableUserNodes)
        userVbox.addWidget(self.enableUsernodeBtn)
        userVbox.addWidget(self.disableUsernodeBtn)
        userVbox.setMargin(0)
        usernodeToggleGroupBox.setLayout(userVbox)
        self.enableUsernodeBtn.setChecked(True)
        return usernodeToggleGroupBox
    
    def createItemPanel(self):
        groupbox = QtGui.QGroupBox('Policy Editing')
        vbox = QtGui.QVBoxLayout()
        self.itemTabWidget = QtGui.QTabWidget()
        self.itemTabWidget.addTab(self.createModifyItemExclusiveGroup(), "Modify")
        self.itemTabWidget.addTab(self.createaddItemExclusiveGroup(), "Add")
        self.itemTabWidget.addTab(self.createDeleteItemExclusiveGroup(), "Delete")
        self.itemTabWidget.currentChanged.connect(self.tabCurrentIdChanged)
        vbox.addWidget(self.itemTabWidget)
        groupbox.setLayout(vbox)
        return groupbox
        
    def tabCurrentIdChanged(self, currentid):
        self.tabId = currentid
        self.getContentForComboBox()
    
    def setPermsCheckBoxStates(self, recurstate, rstate, wstate, xstate):
        self.modifyPermsCBox_recursive.setChecked(recurstate)
        self.modifyPermsCBox_read.setChecked(rstate)
        self.modifyPermsCBox_write.setChecked(wstate)
        self.modifyPermsCBox_exe.setChecked(xstate)
    
    def getPermsFromCheckBoxes(self):
        permset = set()
        recursive = False
        if self.modifyPermsCBox_recursive.isChecked():
            recursive = True
        if self.modifyPermsCBox_read.isChecked():
            permset.add('r')
        if self.modifyPermsCBox_write.isChecked():
            permset.add('w')
        if self.modifyPermsCBox_exe.isChecked():
            permset.add('x')
        return recursive, permset
        
    def setCheckBoxesFromPermset(self, recursive, permset):
        self.modifyPermsCBox_recursive.setChecked(recursive)
        if 'r' in permset:
            self.modifyPermsCBox_read.setChecked(True)
        else:
            self.modifyPermsCBox_read.setChecked(False)
        if 'w' in permset:
            self.modifyPermsCBox_write.setChecked(True)
        else:
            self.modifyPermsCBox_write.setChecked(False)
        if 'x' in permset:
            self.modifyPermsCBox_exe.setChecked(True)
        else:
            self.modifyPermsCBox_exe.setChecked(False)
            
    def getContentForPermsComboBox0(self, textIndex):
        if self.dirModifyRBtn.isChecked():
            itemname = str(self.relatedComboBox_modify1.itemText(textIndex))
            if not self.isNameEmpty(itemname):
                if itemname in self.main.role_res_mat.keys():
                    if self.itemname in self.main.role_res_mat[itemname].keys():
                        permSet = self.main.role_res_mat[itemname][self.itemname]
                        self.setCheckBoxesFromPermset(permSet.recursive, permSet.perms)
                        
    def getContentForModifyItem(self, textIndex):
        if self.roleModifyRBtn.isChecked():
            self.itemname = str(self.modifyItemNameComboBox2.itemText(textIndex))
            if self.isNameEmpty(self.itemname):
                return
            self.roleParentComboBox_modify.clear()
            self.roleChildrenComboBox_modify.clear()
            for r in self.main.roleCompactNodes:
                if r.name == self.itemname:
                    rolenode = r
            '''for parent nodes'''
            if self.modifyBtn_addRoleParent.isChecked():
                negRoleParentNodeSet = self.main.roleSet-rolenode.parents
                negRoleParentNodeSet.remove(self.itemname)
                self.addItemsToComboBox(negRoleParentNodeSet, self.roleParentComboBox_modify)
            elif self.modifyBtn_deleteRoleParent.isChecked():
                self.addItemsToComboBox(rolenode.parents, self.roleParentComboBox_modify)
            '''for children nodes'''
            if self.modifyBtn_addRoleChildren.isChecked():
                negRoleChildrenNodeSet = self.main.roleSet-rolenode.children
                negRoleChildrenNodeSet.remove(self.itemname)
                self.addItemsToComboBox(negRoleChildrenNodeSet, self.roleChildrenComboBox_modify)
            elif self.modifyBtn_deleteRoleChildren.isChecked():
                self.addItemsToComboBox(rolenode.children, self.roleChildrenComboBox_modify)
        elif self.userModifyRBtn.isChecked():
            self.itemname = str(self.modifyItemNameComboBox.itemText(textIndex))
            if self.isNameEmpty(self.itemname):
                return
            self.relatedComboBox_modify1.clear()
            userRoleNodeSet = set()
            if self.itemname in self.main.user_role_mat.keys():
                userRoleNodeSet = self.main.user_role_mat[self.itemname]
            if self.modifyRadioBtn_add1.isChecked():
                negUserRoleNodeSet = self.main.roleSet-userRoleNodeSet
                self.addItemsToComboBox(negUserRoleNodeSet, self.relatedComboBox_modify1)
            elif self.modifyRadioBtn_delete1.isChecked():
                self.addItemsToComboBox(userRoleNodeSet, self.relatedComboBox_modify1)
        elif self.dirModifyRBtn.isChecked():
            self.itemname = str(self.modifyItemNameComboBox.itemText(textIndex))
            if self.isNameEmpty(self.itemname):
                return
            self.relatedComboBox_modify1.clear()
            dirnode = None
            for d in self.main.dirNodes:
                if d.dir == self.itemname:
                    dirnode = d
            if dirnode is None:
                return
            dirRoleNodeSet = set()
            for r in dirnode.roleNodes:
                dirRoleNodeSet.add(r.name)
            if self.modifyRadioBtn_add1.isChecked():
                negDirRoleNodeSet = self.main.roleSet-dirRoleNodeSet
                self.addItemsToComboBox(negDirRoleNodeSet, self.relatedComboBox_modify1)
                self.setPermsCheckBoxStates(False, False, False, False)
            elif self.modifyRadioBtn_delete1.isChecked():
                self.addItemsToComboBox(dirRoleNodeSet, self.relatedComboBox_modify1)
                index = self.relatedComboBox_modify1.currentIndex()
                self.getContentForPermsComboBox0(index)
            
    def updateContentForParentChidlrenComboBox(self):
        index = self.modifyItemNameComboBox2.currentIndex()
        self.getContentForModifyItem(index) 
                
    def getContentForComboBox(self):
        if self.tabId == self.MODIFY_TAB:
            if self.roleModifyRBtn.isChecked():
#                 if self.main.roleList != self.roleList:
#                     self.modifyItemNameComboBox2.clear()
#                     self.addItemsToComboBox(self.main.roleList, self.modifyItemNameComboBox2) 
#                     self.roleList = self.main.roleList
                self.modifyItemNameComboBox2.clear()
                self.addItemsToComboBox(self.main.roleList, self.modifyItemNameComboBox2)
                self.updateContentForParentChidlrenComboBox()
            elif self.dirModifyRBtn.isChecked():
#                 if self.main.dirList != self.dirList:
#                     self.modifyItemNameComboBox.clear()
#                     self.addItemsToComboBox(self.main.dirList, self.modifyItemNameComboBox)
#                     self.dirList = self.main.dirList
                self.modifyItemNameComboBox.clear()
                self.addItemsToComboBox(self.main.dirList, self.modifyItemNameComboBox)
                index = self.modifyItemNameComboBox.currentIndex()
                self.getContentForModifyItem(index)  
            elif self.userModifyRBtn.isChecked():
#                 if self.main.userList != self.userList:
#                     self.modifyItemNameComboBox.clear()
#                     self.addItemsToComboBox(self.main.userList, self.modifyItemNameComboBox)
#                     self.userList = self.main.userList 
                self.modifyItemNameComboBox.clear()
                self.addItemsToComboBox(self.main.userList, self.modifyItemNameComboBox)
                index = self.modifyItemNameComboBox.currentIndex()
                self.getContentForModifyItem(index)
        elif self.tabId == self.DELETE_TAB:
            self.relatedComboBox_delete1.clear()
            if self.roleDeleteRBtn.isChecked():
                self.addItemsToComboBox(self.main.roleList, self.relatedComboBox_delete1)
            elif self.dirDeleteRBtn.isChecked():
                self.addItemsToComboBox(self.main.dirList, self.relatedComboBox_delete1)
            elif self.userDeleteRBtn.isChecked():
                self.addItemsToComboBox(self.main.userList, self.relatedComboBox_delete1)

    def getContentForAddDeleteComboBox(self):
        if self.tabId == self.MODIFY_TAB:
            if self.roleModifyRBtn.isChecked():
                self.updateContentForParentChidlrenComboBox()
            elif self.dirModifyRBtn.isChecked():
                index = self.modifyItemNameComboBox.currentIndex()
                self.getContentForModifyItem(index)  
            elif self.userModifyRBtn.isChecked():
                index = self.modifyItemNameComboBox.currentIndex()
                self.getContentForModifyItem(index)
                
    def createModifyItemExclusiveGroup(self):
        groupBox = QtGui.QGroupBox('')
        self.roleModifyRBtn = QtGui.QRadioButton('&Role')
        self.dirModifyRBtn = QtGui.QRadioButton('&Object')
        self.userModifyRBtn = QtGui.QRadioButton('&User')
        self.roleModifyRBtn.clicked.connect(self.enableRolePanel)
        self.dirModifyRBtn.clicked.connect(self.enableDirPanel)
        self.userModifyRBtn.clicked.connect(self.enableUserPanel)
        self.roleModifyRBtn.setChecked(True)
        vbox = QtGui.QVBoxLayout()
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.roleModifyRBtn)
        hbox.addWidget(self.dirModifyRBtn)
        hbox.addWidget(self.userModifyRBtn)
        hbox.addStretch(1)
        vbox.addItem(hbox)
        vbox.addWidget(self.createPanelModifyItem())
        vbox.addStretch(1)
        groupBox.setLayout(vbox)
        return groupBox
    
    def createaddItemExclusiveGroup(self):
        addItemGroupBox = QtGui.QGroupBox('')
        self.roleAddRBtn = QtGui.QRadioButton('&Role')
        self.dirAddRBtn = QtGui.QRadioButton('&Object')
        self.userAddRBtn = QtGui.QRadioButton('&User')
        self.roleAddRBtn.clicked.connect(self.enableRolePanel)
        self.dirAddRBtn.clicked.connect(self.enableDirPanel)
        self.userAddRBtn.clicked.connect(self.enableUserPanel)
        self.roleAddRBtn.setChecked(True)
        vbox = QtGui.QVBoxLayout()
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.roleAddRBtn)
        hbox.addWidget(self.dirAddRBtn)
        hbox.addWidget(self.userAddRBtn)
        hbox.addStretch(1)
        vbox.addItem(hbox)
        vbox.addWidget(self.createPanelAddItem())
        vbox.addStretch(1)
        addItemGroupBox.setLayout(vbox)
        return addItemGroupBox
    
    def createDeleteItemExclusiveGroup(self):
        self.deleteItemGroupBox = QtGui.QGroupBox('')
        self.roleDeleteRBtn = QtGui.QRadioButton('&Role')
        self.dirDeleteRBtn = QtGui.QRadioButton('&Object')
        self.userDeleteRBtn = QtGui.QRadioButton('&User')
        self.roleDeleteRBtn.clicked.connect(self.enableRolePanel)
        self.dirDeleteRBtn.clicked.connect(self.enableDirPanel)
        self.userDeleteRBtn.clicked.connect(self.enableUserPanel)
        self.roleDeleteRBtn.setChecked(True)
        vbox = QtGui.QVBoxLayout()
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.roleDeleteRBtn)
        hbox.addWidget(self.dirDeleteRBtn)
        hbox.addWidget(self.userDeleteRBtn)
        hbox.addStretch(1)
        vbox.addItem(hbox)
        vbox.addWidget(self.createPanelDeleteItem())
        vbox.addStretch(1)
        self.deleteItemGroupBox.setLayout(vbox)
        return self.deleteItemGroupBox
    
    def addItemsToComboBox(self, itemlist, comboBox):
        for i in itemlist:
            comboBox.addItem(i)
        
    def createConnectionPanelModifyItem(self):
        modifyItemPanelVbox = QtGui.QVBoxLayout()
        modifyItemPanelGroupBox = QtGui.QGroupBox('Assignments')
        self.modifyItemNameLabel = QtGui.QLabel()
        self.modifyItemNameComboBox = QtGui.QComboBox()
        self.modifyGroupbox1 = QtGui.QGroupBox('Users')
        vbox1 = QtGui.QVBoxLayout()
        self.relatedComboBox_modify1 = QtGui.QComboBox()
        hboxWidget1 = QtGui.QWidget(self.modifyGroupbox1)
        hbox1 = QtGui.QHBoxLayout()
        self.modifyRadioBtn_add1 = QtGui.QRadioButton('Add')
        self.modifyRadioBtn_delete1 = QtGui.QRadioButton('Delete')
        self.modifyConfirmBtn1 = QtGui.QPushButton('OK')
        self.modifyPermsLineEdit0 = QtGui.QLineEdit()
        self.modifyPermsCBox_recursive = QtGui.QCheckBox('Recursive')
        self.permsGroupBox = QtGui.QGroupBox('Permissions')
        permsVLayout = QtGui.QVBoxLayout()
        self.modifyPermsCBox_read = QtGui.QCheckBox('Read')
        self.modifyPermsCBox_write = QtGui.QCheckBox('Write')
        self.modifyPermsCBox_exe = QtGui.QCheckBox('Execute')
        permsVLayout.addWidget(self.modifyPermsCBox_recursive)
        line = QtGui.QFrame(self.permsGroupBox)
        line.setFrameShape(QtGui.QFrame.HLine)
        line.setFrameShadow(QtGui.QFrame.Sunken)
        permsVLayout.addWidget(line)
        permsVLayout.addWidget(self.modifyPermsCBox_read)
        permsVLayout.addWidget(self.modifyPermsCBox_write)
        permsVLayout.addWidget(self.modifyPermsCBox_exe)
        self.permsGroupBox.setLayout(permsVLayout)
        hbox1.addWidget(self.modifyRadioBtn_add1)
        hbox1.addWidget(self.modifyRadioBtn_delete1)
        hboxWidget1.setLayout(hbox1)
        hbox1.setMargin(0)
        vbox1.addWidget(hboxWidget1)
        vbox1.addWidget(self.relatedComboBox_modify1)
        vbox1.addWidget(self.permsGroupBox)
        vbox1.addWidget(self.modifyConfirmBtn1)
        self.modifyGroupbox1.setLayout(vbox1)
        self.modifyItemNameComboBox.currentIndexChanged.connect(self.getContentForModifyItem)
        self.relatedComboBox_modify1.currentIndexChanged.connect(self.getContentForPermsComboBox0)
        self.modifyRadioBtn_add1.clicked.connect(self.getContentForAddDeleteComboBox)
        self.modifyRadioBtn_delete1.clicked.connect(self.getContentForAddDeleteComboBox)
        self.modifyConfirmBtn1.clicked.connect(self.modifyItem1)
        modifyItemPanelVbox.addWidget(self.modifyItemNameLabel)
        modifyItemPanelVbox.addWidget(self.modifyItemNameComboBox)
        modifyItemPanelVbox.addWidget(self.modifyGroupbox1)
        modifyItemPanelGroupBox.setLayout(modifyItemPanelVbox)
        self.modifyConnectionPanelGroupBox = modifyItemPanelGroupBox
        return modifyItemPanelGroupBox
    
    def createHierarchyPanelModifyItem(self):
        modifyItemPanelVbox = QtGui.QVBoxLayout()
        self.modifyItemPanelGroupBox = QtGui.QGroupBox('Hierarchy')
        self.modifyItemNameLabel2 = QtGui.QLabel('Role Name')
        self.modifyItemNameComboBox2 = QtGui.QComboBox()
        self.roleParentGroupbox = QtGui.QGroupBox('Parents')
        vbox3 = QtGui.QVBoxLayout()
        self.roleParentComboBox_modify = QtGui.QComboBox()
        hboxWidget3 = QtGui.QWidget(self.roleParentGroupbox)
        hbox3 = QtGui.QHBoxLayout()
        self.modifyBtn_addRoleParent = QtGui.QRadioButton('Add')
        self.modifyBtn_deleteRoleParent = QtGui.QRadioButton('Delete')
        self.modifyConfirmBtnRoleParent = QtGui.QPushButton('OK')
        hbox3.addWidget(self.modifyBtn_addRoleParent)
        hbox3.addWidget(self.modifyBtn_deleteRoleParent)
        hboxWidget3.setLayout(hbox3)
        hbox3.setMargin(0)
        vbox3.addWidget(hboxWidget3)
        vbox3.addWidget(self.roleParentComboBox_modify)
        vbox3.addWidget(self.modifyConfirmBtnRoleParent)
        self.roleParentGroupbox.setLayout(vbox3)
        self.modifyItemNameComboBox2.currentIndexChanged.connect(self.getContentForModifyItem)
        self.modifyBtn_addRoleParent.clicked.connect(self.updateContentForParentChidlrenComboBox)
        self.modifyBtn_deleteRoleParent.clicked.connect(self.updateContentForParentChidlrenComboBox)
        self.modifyConfirmBtnRoleParent.clicked.connect(self.modifyItemRoleParent)
        
        self.roleChildrenGroupbox = QtGui.QGroupBox('Children')
        vbox4 = QtGui.QVBoxLayout()
        self.roleChildrenComboBox_modify = QtGui.QComboBox()
        hboxWidget4 = QtGui.QWidget(self.roleChildrenGroupbox)
        hbox4 = QtGui.QHBoxLayout()
        self.modifyBtn_addRoleChildren = QtGui.QRadioButton('Add')
        self.modifyBtn_deleteRoleChildren = QtGui.QRadioButton('Delete')
        self.modifyConfirmBtnRoleChildren = QtGui.QPushButton('OK')
        hbox4.addWidget(self.modifyBtn_addRoleChildren)
        hbox4.addWidget(self.modifyBtn_deleteRoleChildren)
        hboxWidget4.setLayout(hbox4)
        hbox4.setMargin(0)
        vbox4.addWidget(hboxWidget4)
        vbox4.addWidget(self.roleChildrenComboBox_modify)
        vbox4.addWidget(self.modifyConfirmBtnRoleChildren)
        self.roleChildrenGroupbox.setLayout(vbox4)
        self.modifyBtn_addRoleChildren.clicked.connect(self.updateContentForParentChidlrenComboBox)
        self.modifyBtn_deleteRoleChildren.clicked.connect(self.updateContentForParentChidlrenComboBox)
        self.modifyConfirmBtnRoleChildren.clicked.connect(self.modifyItemRoleChildren)
        modifyItemPanelVbox.addWidget(self.modifyItemNameLabel2)
        modifyItemPanelVbox.addWidget(self.modifyItemNameComboBox2)
        modifyItemPanelVbox.addWidget(self.roleParentGroupbox)
        modifyItemPanelVbox.addWidget(self.roleChildrenGroupbox)
        self.modifyItemPanelGroupBox.setLayout(modifyItemPanelVbox)
        return self.modifyItemPanelGroupBox
    
    def createPanelModifyItem(self):
        widget = QtGui.QWidget()
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.createConnectionPanelModifyItem())
        vbox.addWidget(self.createHierarchyPanelModifyItem())
        widget.setLayout(vbox)
        self.enableRolePanel()
        return widget
        
    def modifyItem1(self):
        if self.modifyRadioBtn_add1.isChecked():
            self.modifyItemAdd1()
        else:
            self.modifyItemDelete1()
        self.getContentForAddDeleteComboBox()
        self.main.specDialog.getSpecFromVisualization()
            
    def modifyItemAdd1(self):
        item2Change = str(self.modifyItemNameComboBox.currentText())
        itemName1 = str(self.relatedComboBox_modify1.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemName1):
            return
        if self.dirModifyRBtn.isChecked(): 
            if self.isNameEmpty(itemName1):
                QtGui.QMessageBox.warning(self.main, 'Warning', 'Please choose a role to relate to this object!')
            recursive, permset = self.getPermsFromCheckBoxes()
            if permset==set():
                QtGui.QMessageBox.warning(self.main, 'Warning', 'No permission information has been entered! No change applied!')
            else:
                self.main.policyManager.addRole_modifyDirInPolicy(item2Change, itemName1, recursive, permset)
        elif self.userModifyRBtn.isChecked():
            self.main.policyManager.addRole_modifyUserInPolicy(item2Change, itemName1)
    
    def modifyItemDelete1(self):
        item2Change = str(self.modifyItemNameComboBox.currentText())
        itemName1 = str(self.relatedComboBox_modify1.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemName1):
            return
        if self.dirModifyRBtn.isChecked():
            recursive, permset = self.getPermsFromCheckBoxes()
            self.main.policyManager.deleteRole_modifyDirInPolicy(item2Change, itemName1, recursive, permset)
        elif self.userModifyRBtn.isChecked():
            self.main.policyManager.deleteRole_modifyUserInPolicy(item2Change, itemName1)
        
    def modifyItemRoleParent(self):
        if self.modifyBtn_addRoleParent.isChecked():
            self.modifyRoleAddParent()
        elif self.modifyBtn_deleteRoleParent.isChecked():
            self.modifyRoleDeleteParent()
        self.getContentForAddDeleteComboBox()
        self.main.specDialog.getSpecFromVisualization()
            
    def modifyItemRoleChildren(self):
        if self.modifyBtn_addRoleChildren.isChecked():
            self.modifyRoleAddChildren()
        elif self.modifyBtn_deleteRoleChildren.isChecked():
            self.modifyRoleDeleteChildren()
        self.main.specDialog.getSpecFromVisualization()
            
    def modifyRoleAddParent(self):
        item2Change = str(self.modifyItemNameComboBox2.currentText())
        itemParent = str(self.roleParentComboBox_modify.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemParent):
            return
        if self.roleModifyRBtn.isChecked():
            self.main.policyManager.addParent_modifyRoleInPolicy(item2Change, itemParent)
            message = "Toolbox: Role("+item2Change+"): Add: Parent("+itemParent+")\n"
            self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)
            print message
    
    def modifyRoleAddChildren(self):
        item2Change = str(self.modifyItemNameComboBox2.currentText())
        itemChild = str(self.roleChildrenComboBox_modify.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemChild):
            return
        if self.roleModifyRBtn.isChecked():
            self.main.policyManager.addChild_modifyRoleInPolicy(item2Change, itemChild)
            message = "Toolbox: Role("+item2Change+"): Add: Child("+itemChild+")\n"
            self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)
            print message
    
    def modifyRoleDeleteParent(self):
        item2Change = str(self.modifyItemNameComboBox2.currentText())
        itemParent = str(self.roleParentComboBox_modify.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemParent):
            return
        if self.roleModifyRBtn.isChecked():
            self.main.policyManager.deleteParent_modifyRoleInPolicy(item2Change, itemParent) 
            message = "Toolbox: Role("+item2Change+"): Delete: Parent("+itemParent+")\n"
            self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)
            print message
    
    def modifyRoleDeleteChildren(self):
        item2Change = str(self.modifyItemNameComboBox2.currentText())
        itemChild = str(self.roleChildrenComboBox_modify.currentText())
        if self.isNameEmpty(item2Change) or self.isNameEmpty(itemChild):
            return
        if self.roleModifyRBtn.isChecked():
            self.main.policyManager.deleteChild_modifyRoleInPolicy(item2Change, itemChild)
            message = "Toolbox: Role("+item2Change+"): Delete: Child("+itemChild+")\n"
            self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)
            print message
    
    def createPanelAddItem(self):
        addItemPanelVbox = QtGui.QVBoxLayout()
        addItemPanelGroupBox = QtGui.QGroupBox('')
        self.addItemNameLabel = QtGui.QLabel()
        self.addItemNameLabel.setText('Role Name')
        self.addItemNameInputLineEdit = QtGui.QLineEdit(addItemPanelGroupBox)
        self.addBtn = QtGui.QPushButton('Add')
        self.addBtn.clicked.connect(self.addItem)
        addItemPanelVbox.addWidget(self.addItemNameLabel)
        addItemPanelVbox.addWidget(self.addItemNameInputLineEdit)
        addItemPanelVbox.addWidget(self.addBtn)
        self.enableRolePanel()
        addItemPanelGroupBox.setLayout(addItemPanelVbox)
        return addItemPanelGroupBox
    
    def lineEditInputEmptyWarning(self):
        warningText = 'Please input a '
        if self.roleAddRBtn.isChecked():
            warningText += 'role name!'
        elif self.dirAddRBtn.isChecked():
            warningText += 'object name!'
        elif self.userAddRBtn.isChecked():
            warningText += 'user name!'
        QtGui.QMessageBox.critical(self.main, 'Error', warningText)
        
    def lineEditInputContainSpace(self):
        warningText = 'Please remove the spaces in the '
        if self.roleAddRBtn.isChecked():
            warningText += 'role name!'
        elif self.dirAddRBtn.isChecked():
            warningText += 'object name!'
        elif self.userAddRBtn.isChecked():
            warningText += 'user name!'
        QtGui.QMessageBox.critical(self.main, 'Error', warningText)
        
    def addItem(self):
        text = str(self.addItemNameInputLineEdit.text())
        if self.isNameEmpty(text):
            self.lineEditInputEmptyWarning()
        elif self.nameHasSpace(text):
            self.lineEditInputContainSpace()
        else:
            if self.roleAddRBtn.isChecked():
                self.main.policyManager.addRoleToPolicy(text)
            elif self.dirAddRBtn.isChecked():
                self.main.policyManager.addDirToPolicy(text)
            elif self.userAddRBtn.isChecked():
                self.main.policyManager.addUserToPolicy(text)
        self.getContentForComboBox()
        self.main.specDialog.getSpecFromVisualization()
        self.addItemNameInputLineEdit.clear()
        
    def enableRolePanel(self):
        if self.tabId == self.MODIFY_TAB:
            self.modifyGroupbox1.setVisible(False)
            self.modifyConnectionPanelGroupBox.setVisible(False)
            self.modifyBtn_addRoleParent.setChecked(True)
            self.modifyBtn_addRoleChildren.setChecked(True)
            self.modifyItemPanelGroupBox.setVisible(True)
        elif self.tabId == self.ADD_TAB:
            self.addItemNameLabel.setText('Role Name')
            self.addItemNameInputLineEdit.clear()
        elif self.tabId == self.DELETE_TAB:
            self.deleteItemNameLabel.setText('Role Name')
        self.getContentForComboBox()
        
    def enableUserPanel(self):
        if self.tabId == self.MODIFY_TAB:
            self.modifyItemNameLabel.setText('User Name')
            self.modifyGroupbox1.setTitle('Roles')
            self.modifyRadioBtn_add1.setChecked(True)
            self.modifyGroupbox1.setVisible(True)
            self.permsGroupBox.setVisible(False)
            self.modifyConnectionPanelGroupBox.setVisible(True)
            self.modifyItemPanelGroupBox.setVisible(False)
        elif self.tabId == self.ADD_TAB:
            self.addItemNameLabel.setText('User Name')
            self.addItemNameInputLineEdit.clear()
        elif self.tabId == self.DELETE_TAB:
            self.deleteItemNameLabel.setText('User Name')
        self.getContentForComboBox()
    
    def enableDirPanel(self):
        if self.tabId == self.MODIFY_TAB:
            self.modifyItemNameLabel.setText('Object Name')
            self.modifyGroupbox1.setTitle('Roles')
            self.modifyGroupbox1.setVisible(True)
            self.modifyRadioBtn_add1.setChecked(True)
            self.permsGroupBox.setVisible(True)
            self.modifyConnectionPanelGroupBox.setVisible(True)
            self.modifyItemPanelGroupBox.setVisible(False)
        elif self.tabId == self.ADD_TAB:
            self.addItemNameLabel.setText('Object Name')
            self.addItemNameInputLineEdit.clear()
        elif self.tabId == self.DELETE_TAB:
            self.deleteItemNameLabel.setText('Object Name')
        self.getContentForComboBox()
        
    def createPanelDeleteItem(self):
        deleteItemPanelVbox = QtGui.QVBoxLayout()
        deleteItemPanelGroupBox = QtGui.QGroupBox('')
        self.deleteItemNameLabel = QtGui.QLabel()
        self.deleteItemNameLabel.setText('Role Name')
        self.relatedNameLabel_delete1 = QtGui.QLabel()
        self.relatedComboBox_delete1 = QtGui.QComboBox()
        self.deleteBtn = QtGui.QPushButton('Delete')
        self.deleteBtn.clicked.connect(self.deleteItem)
        deleteItemPanelVbox.addWidget(self.deleteItemNameLabel)
        deleteItemPanelVbox.addWidget(self.relatedComboBox_delete1)
        deleteItemPanelVbox.addWidget(self.deleteBtn)
        deleteItemPanelGroupBox.setLayout(deleteItemPanelVbox)
        self.enableRolePanel()
        return deleteItemPanelGroupBox
    
    def deleteItem(self):
        itemName = str(self.relatedComboBox_delete1.currentText())
        if self.roleDeleteRBtn.isChecked():
            self.main.policyManager.deleteRoleInPolicy(itemName)
        elif self.dirDeleteRBtn.isChecked():
            self.main.policyManager.deleteDirInPolicy(itemName)
        elif self.userDeleteRBtn.isChecked():
            self.main.policyManager.deleteUserInPolicy(itemName)
        self.getContentForComboBox()
        self.main.specDialog.getSpecFromVisualization()
    
    def enableDeleteDirPanel(self):
        self.deleteItemNameLabel.setText('Object Name')
        self.getContentForComboBox()
        return self.deleteItemPanelVbox
    
    def removeAllItemsInLayout(self, layout):
        for i in xrange(layout.count()):
            item = layout.itemAt(i)
            layout.removeWidget(item.widget())
            layout.removeItem(item)
            
    def enableUserNodes(self):
        self.main.allowUsernodeHighlight = True
        for u in self.main.scene.items():
            if isinstance(u, UserNode):
                u.setVisible(True)
            elif isinstance(u, EdgeItem) and u.type == EdgeItem.SEPARATE_ROLEUSER_CONN:
                u.setVisible(True)
        self.main.updateAllUserNodes() 
        self.main.updateAllScenes()
        message = "Toolbox: Show User nodes\n"
        self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)
    
    def disableUserNodes(self):
        self.main.allowUsernodeHighlight = False
        for u in self.main.scene.items():
            if isinstance(u, UserNode):
                u.setVisible(False)
                u.highlighted = False
                u.inheritHighlight = False
            elif isinstance(u, EdgeItem) and u.type == EdgeItem.SEPARATE_ROLEUSER_CONN:
                u.setVisible(False)
                u.highlighted = False
        self.main.updateAllScenes()
        message = "Toolbox: Hide User nodes\n"
        self.main.writeToFile(self.main.logFile, self.main.getCurrentTimeString()+message)

            