# -*- 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.QtCore import *
from PyQt4.QtGui import *
try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_RoleHierViewWindow(object):
    def setupUi(self, Form, scene):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(350, 618)
        Form.setAutoFillBackground(True)
        hbox = QHBoxLayout()
        splitter = QSplitter(Qt.Horizontal)
        splitter.addWidget(self.createRoleUserGroupBox())
        splitter.addWidget(self.createRolePermGroupBox())
#         hbox.addWidget(self.createRoleUserGroupBox())
#         hbox.addWidget(self.createRolePermGroupBox())
        hbox.addWidget(splitter)
        Form.setLayout(hbox)
        self.hbox = hbox
        QMetaObject.connectSlotsByName(Form)
        self.rolePermLists = []
        self.roleUserLists = []
    
    '''role and permissions'''
    def addRolePermList(self, roleName, rolePermLists):
        roleTableView = TableViewForPermDockWidget(self.rolePermGroupBox, roleName, rolePermLists)
        self.rolePermVerticalLayout.addWidget(roleTableView)
        self.rolePermLists.append(roleTableView)

    def removeRolePermList(self, roleName):
        for r in self.rolePermLists:
            if r.roleName == roleName:
                r.close()
                self.rolePermVerticalLayout.removeWidget(r)
                self.rolePermLists.remove(r)
    
    def createRolePermGroupBox(self):
        self.rolePermGroupBox = QGroupBox('Permissions')
        #self.rolePermGroupBox.setGeometry(self.rolePermGroupBox.geometry().y(), 127, self.rolePermGroupBox.geometry().x(), self.rolePermGroupBox.geometry().height())
        self.rolePermVerticalLayout = QVBoxLayout(self.rolePermGroupBox)
        #self.rolePermVerticalLayout.setGeometry(QRect(self.rolePermVerticalLayout.geometry().x(), self.rolePermVerticalLayout.geometry().y(), 127, self.rolePermVerticalLayout.geometry().height()))
        self.rolePermGroupBox.setLayout(self.rolePermVerticalLayout)
        self.rolePermVerticalLayout.setMargin(0)
        return self.rolePermGroupBox
    
    '''role and users'''  
    def addRoleUserList(self, roleName, roleUserList):
        roleTableView = TableViewForUserDockWidget(self.roleUserGroupBox, roleName, roleUserList)
        self.roleUserVerticalLayout.addWidget(roleTableView)
        self.roleUserLists.append(roleTableView)
                
    def removeRoleUserList(self, roleName):
        for r in self.roleUserLists:
            if r.roleName == roleName:
                r.close()
                self.roleUserVerticalLayout.removeWidget(r)
                self.roleUserLists.remove(r)
                
    def createRoleUserGroupBox(self):
        self.roleUserGroupBox = QGroupBox('Users')
        #self.roleUserGroupBox.setGeometry(self.roleUserGroupBox.geometry().y(), 127, self.roleUserGroupBox.geometry().x(), self.roleUserGroupBox.geometry().height())
        self.roleUserVerticalLayout = QVBoxLayout(self.roleUserGroupBox)
        self.roleUserGroupBox.setLayout(self.roleUserVerticalLayout)
        self.roleUserVerticalLayout.setMargin(0)
        #self.roleUserVerticalLayout.setGeometry(QRect(self.roleUserVerticalLayout.geometry().x(), self.roleUserVerticalLayout.geometry().y(), 127, self.roleUserVerticalLayout.geometry().height()))
        return self.roleUserGroupBox
    
'''role and users'''   
class TableViewForUserDockWidget(QWidget):
    def __init__(self, parent, roleName, roleUserList):
        QWidget.__init__(self, parent)
        self.roleName = roleName
        parentWidth = parent.geometry().width()-2
        parentHeight = parent.geometry().height()
        tableView = QTableView(self)
        tableView.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        tableView.verticalHeader().hide()
        tableView.setGeometry(QRect(0, 0, parentWidth, parentHeight))
        numUsers = len(roleUserList)
        model = QStandardItemModel(numUsers,1,tableView)
        model.setHorizontalHeaderItem(0, QStandardItem(QString(roleName)))
        for i in xrange(numUsers):
            item = QStandardItem(QString(roleUserList[i]))
            item.setTextAlignment(Qt.AlignCenter)
            model.setItem(i,0, item)
        tableView.setModel(model)
        self.tableView = tableView
        
    def resize(self, geometry):
        self.setGeometry(geometry)
        self.tableView.setGeometry(geometry)
        
    def changeSize(self):
        self.adjustSize()
        self.tableView.adjustSize()
        
'''role and permissions'''    
class TableViewForPermDockWidget(QWidget):
    def __init__(self, parent, roleName, rolePermLists):
        QWidget.__init__(self, parent)
        self.roleName = roleName
        parentWidth = parent.geometry().width()-5
        parentHeight = parent.geometry().height()
        
        self.headerTableView = QTableView(self)
        self.header = self.headerTableView.horizontalHeader()
        self.headerTableView.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        self.headerTableView.verticalHeader().hide()
        self.headerTableView.setGeometry(QRect(0, 0, parentWidth, self.headerTableView.height()))
        model = QStandardItemModel(0,1,self.headerTableView)
        model.setHorizontalHeaderItem(0, QStandardItem(QString(roleName)))
        self.headerTableView.setModel(model)
        
        self.dataTableView = QTableView(self)
        self.dataTableView.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        self.dataTableView.verticalHeader().hide()
        self.dataTableView.horizontalHeader().hide()
        self.dataTableView.setGeometry(QRect(0, self.headerTableView.height()-5, parentWidth, parentHeight-self.headerTableView.height()+5))
        if rolePermLists!= []:
            numPerms = len(rolePermLists[0])
            model = QStandardItemModel(numPerms,2,self.dataTableView)
            for i in xrange(numPerms):
                item = QStandardItem(QString(rolePermLists[0][i]))
                item.setTextAlignment(Qt.AlignCenter)
                model.setItem(i,0, item)
                item = QStandardItem(QString(rolePermLists[1][i]))
                item.setTextAlignment(Qt.AlignCenter)
                model.setItem(i,1, item)
        self.dataTableView.setModel(model)
    
    def changeSize(self):
        self.adjustSize()
        self.headerTableView.adjustSize()
        self.dataTableView.adjustSize()
        
    def resize(self, geometry):
        self.setGeometry(geometry)
        self.headerTableView.setGeometry(geometry)
        self.dataTableView.setGeometry(geometry)