from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL, Qt, QString, QSignalMapper, SIGNAL, SLOT
from PyQt4.QtGui import QWidget, QSortFilterProxyModel, \
QAbstractItemView, QMenu, QStandardItemModel
from Proyect.presentation.view.ui_Groups import Ui_Groups
from Proyect.presentation.model.standard_model import StandardModel
from Proyect.presentation.config.tree_menu import ElementType, tree_menu
class Groups(QWidget):
'''
classdocs
'''
def __init__(self, parent=None, flags=0):
'''
Constructor
'''
QWidget.__init__(self)
self.ui = Ui_Groups()
self.ui.setupUi(self)
if parent is not None:
self.setParent(parent)
self.model = None
self.og_domain = None
self.menu = None
self.menu_signal = None
self.sort_model = None
self.load_component()
self.load_connection()
def load_connection(self):
'''
'''
self.connect(self.ui.btn_add, SIGNAL("clicked()"), self.new_row)
self.connect(self.ui.btn_delete, SIGNAL("clicked()"), self.delete_row)
self.connect(self.ui.btn_cancel, SIGNAL("clicked()"), self.cancel)
self.connect(self.ui.btn_save, SIGNAL("clicked()"), self.save)
""
self.connect(self.ui.tree_view, SIGNAL("customContextMenuRequested(const QPoint &)"), self.show_menu)
def load_component(self):
'''
Modelo del Arbol
'''
self.model = StandardModel()
self.model.setRowCount(1)
headers = [self.trUtf8("Grupos"), self.trUtf8("Descripción")]
self.model.setHorizontalHeaderLabels(headers)
self.sort_model = QSortFilterProxyModel()
self.sort_model.setSourceModel(self.model)
self.ui.table_view.setModel(self.sort_model)
self.ui.table_view.resizeColumnsToContents()
self.ui.table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
self.ui.table_view.setSortingEnabled(True)
i = 0
while i < 2:
self.ui.table_view.setColumnHidden(i, True)
i += 1
self.ui.tree_view.setModel(self.sort_model)
self.ui.tree_view.setSortingEnabled(True)
self.ui.tree_view.setContextMenuPolicy(Qt.CustomContextMenu)
'''
Modelo de la tabla
'''
self.modelo = StandardModel()
self.modelo.setRowCount(1)
headers1 = [ "Grupo", " Usuario "," Nombre y Apellido ",self.trUtf8("Identificación")]
self.modelo.setHorizontalHeaderLabels(headers1)
self.sort_model = QSortFilterProxyModel()
self.sort_model.setSourceModel(self.modelo)
self.ui.table_view.setModel(self.sort_model)
self.ui.table_view.resizeColumnsToContents()
self.ui.table_view.setSelectionMode(QAbstractItemView.SingleSelection)
self.ui.table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
self.ui.table_view.setSortingEnabled(True)
i = 5
while i < len(headers):
self.ui.table_view.setColumnHidden(i, True)
i += 1
self.ui.table_view.setModel(self.sort_model)
self.ui.table_view.setSortingEnabled(True)
self.ui.table_view.setContextMenuPolicy(Qt.CustomContextMenu)
def insertChild(self): #Funcion que permite crear los pozos
index = self.ui.tree_view.selectionModel().currentIndex()
model = self.ui.tree_view.model()
if model.columnCount(index) == 0:
if not model.insertColumn(0, index):
return
if not model.insertRow(0, index):
return
for column in range(model.columnCount(index)):
child = model.index(0, column, index)
model.setData(child, "[No data]", QtCore.Qt.EditRole)
if model.headerData(column, QtCore.Qt.Horizontal) is None:
model.setHeaderData(column, QtCore.Qt.Horizontal,
"[No header]", QtCore.Qt.EditRole)
self.ui.tree_view.selectionModel().setCurrentIndex(model.index(0, 0, index),
QtGui.QItemSelectionModel.ClearAndSelect)
def new_row(self): #Funcion que permite insetar nueva fila
'''
'''
self.model.setRowCount(self.model.rowCount() + 1)
def delete_row(self):
'''
'''
if len(self.ui.table_view.selectedIndexes()) == 0:
return
index = self.ui.table_view.selectedIndexes() [0]
row = self.sort_model.mapToSource (index).row ()
self.model.takeRow(row)
def save(self):
'''
'''
pass
def cancel(self):
'''
'''
self.close()
def show_menu(self, point):
'''
'''
level = 0
self.menu_signal = QSignalMapper()
index = self.ui.tree_view.selectedIndexes()
if len(index) > 0:
level = 0
indexe = index[0]
while indexe.parent().isValid():
indexe = indexe.parent()
level +=1
self.menu = QMenu()
if level == 0:
if len(index) > 0:
level = self.model.level(index[0])
for x in tree_menu[level].keys():
action = self.menu.addAction(self.trUtf8(x))
self.menu_signal.setMapping(action, tree_menu[level][x])
self.connect(action, SIGNAL("triggered()"), self.menu_signal, SLOT("map()"))
# elif level == 1:
# if len(index) > 1:
# level = self.model.level(index[0])
#
# for x in tree_menu[level+1].keys():
# action = self.menu.addAction(self.trUtf8(x))
# self.menu_signal.setMapping(action, tree_menu[level+1][x])
# self.connect(action, SIGNAL("triggered()"), self.menu_signal, SLOT("map()"))
elif level == 2:
if len(index) > 0:
level = self.model.level(index[0])
for x in tree_menu[level+2].keys():
action = self.menu.addAction(self.trUtf8(x))
self.menu_signal.setMapping(action, tree_menu[level+2][x])
self.connect(action, SIGNAL("triggered()"), self.menu_signal, SLOT("map()"))
self.connect(self.menu_signal, SIGNAL("mapped(int)"), self.actions)
self.menu.exec_(self.ui.tree_view.viewport().mapToGlobal(point))
def actions(self, action):
'''
'''
if action == ElementType.ADD_G:
self.new_row() #Llamado a funcion que crea un nuevo grupo de ajuste
print "Add G"
if action == ElementType.EDIT_G:
#self.new_row()
print "Edit G"
if action == ElementType.DELETE_G:
print "Delete G"
if action == ElementType.ADD_USR:
self.insertChild()
print "Add User"
if action == ElementType.EDIT_USR:
print "Edit User"
#self.insertChild()
if action == ElementType.DELETE_USR:
print "Delete User"