import typing import qtawesome from PyQt5 import QtGui,QtCore,QtWidgets from PyQt5.QtCore import QAbstractTableModel, QModelIndex, Qt, QVariant, QSize from PyQt5.QtWidgets import QItemDelegate, QHBoxLayout, QWidget, QTableView, QCheckBox, QMessageBox from model.UserModel.UserManage import UserManage from utils import Globals class QPushButton(QtWidgets.QPushButton): def __init__(self, *__args: any,clicked = None): super(QPushButton,self).__init__(*__args) self.setCursor(Qt.PointingHandCursor) self.setIconSize(QSize(25, 25)) class UserTableModel(QAbstractTableModel): def __init__(self, header, data: list, table = None): QAbstractTableModel.__init__(self, parent=None) self.table = table self.header = header self.datas = data self.checkList = ['Unchecked'] * len(self.datas) self.supportedDragActions() self.table = table self.editableList = [] # 表格中可编辑项 def initTable(self): self.datas = [] userDatas = UserManage.getAllUser() if not userDatas: # self.layoutChanged.emit() self.table.proxy.invalidate() return for x in userDatas: #x.append('') x += [''] self.datas.append(x) self.checkList = ['Unchecked'] * len(self.datas) # self.layoutChanged.emit() print(self.datas) self.table.proxy.invalidate() def append_data(self, x): self.datas.append(x) self.checkList.append('Unchecked') # self.layoutChanged.emit() self.table.proxy.invalidate() def insert_data(self, x, index): self.datas.insert(index, x) self.checkList.insert(index, 'Unchecked') print(self.datas) self.table.proxy.invalidate() def remove_row(self, row): self.datas.pop(row) self.checkList.pop(row) # self.layoutChanged.emit() self.table.proxy.invalidate() def rowCount(self, parent: QModelIndex = ...) -> int: if len(self.datas) > 0: return len(self.datas) return 0 def columnCount(self, parent: QModelIndex = ...) -> int: return len(self.header) def data(self, QModelIndex, role=None): # print(Qt.__dict__.items()) if role == Qt.TextAlignmentRole: return Qt.AlignCenter if not QModelIndex.isValid(): print("行或者列有问题") return QVariant() if role == Qt.BackgroundColorRole: return QtGui.QColor('#FFFFFF') if role == Qt.TextColorRole: if QModelIndex.column() == 6: return QtGui.QColor('#000000') return QtGui.QColor('#1A1A1A') elif role == Qt.CheckStateRole: if QModelIndex.column() == 0: return Qt.Checked if self.checkList[QModelIndex.row()] == 'Checked' else Qt.Unchecked else: return None elif role == Qt.ToolTipRole: if QModelIndex.column() == 0: return self.checkList[QModelIndex.row()] elif role != Qt.DisplayRole: return QVariant() elif QModelIndex.column() == 0: return QVariant(QModelIndex.row() + 1) return QVariant(self.datas[QModelIndex.row()][QModelIndex.column()]); def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any: if role != Qt.DisplayRole: return None if orientation == Qt.Horizontal: return self.header[section] def setData(self, index, value, role): row = index.row() col = index.column() if role == Qt.CheckStateRole and col == 0: self.checkList[row] = 'Checked' if value == Qt.Checked else 'Unchecked' return True if role == Qt.EditRole: self.datas[row][col] = value return True return True def flags(self, index): if index.column() == 0: return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable if index.row() in self.editableList and index.column() != 4: return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsEditable return Qt.ItemIsEnabled def headerClick(self, isOn): self.beginResetModel() if isOn: self.checkList = [] self.checkList = ['Checked'] * len(self.datas) else: self.checkList = [] self.checkList = ['UnChecked'] * len(self.datas) self.endResetModel() def dragMoveEvent(self, event): event.setDropAction(QtCore.Qt.MoveAction) event.accept() def moveRow(self, sourceParent: QModelIndex, sourceRow: int, destinationParent: QModelIndex, destinationChild: int) -> bool: if self.datas[destinationChild] == self.datas[sourceRow]: return self.datas[sourceRow], self.datas[destinationChild] = self.datas[destinationChild], self.datas[sourceRow] # self.layoutChanged.emit() self.table.proxy.invalidate() class UserButtonDelegate(QItemDelegate): """该类用于向单元格中添加按钮 任务表格""" def __init__(self, parent=None): super(UserButtonDelegate, self).__init__(parent) def paint(self, painter, option, index): if not self.parent().indexWidget(index): self.i = index # self.button1 = QPushButton( # qtawesome.icon('fa.play', color='#1fbb6f'), # "", # self.parent(), # clicked=self.start_action # ) # self.button1.setIconSize(QSize(15, 15)) # self.button1.setStyleSheet("border:none;") button2 = QPushButton( qtawesome.icon('fa.pencil', color='#4c8cf2'), "", self.parent(), clicked=self.edit_action ) button2.isEdit = True button2.oldName = False # self.button2.setStyleSheet("border:none;") button3 = QPushButton( qtawesome.icon('fa.trash', color='#ff6d6d'), "", self.parent(), clicked=self.delete_action ) # self.button3.setStyleSheet("border:none;") # self.button4 = QPushButton( # qtawesome.icon('fa.line-chart', color='#393c4e'), # "", # self.parent(), # clicked=self.copy_action # ) # self.button1.clicked.connect(self.start_action) button2.clicked.connect(self.edit_action) button3.clicked.connect(self.delete_action) # self.button4.clicked.connect(self.copy_action) # self.button4.setStyleSheet("border:none;") # self.button1.index = [index.row(), index.column()] button2.index = [index.row(), index.column()] button3.index = [index.row(), index.column()] # self.button4.index = [index.row(), index.column()] data = self.parent().model.datas[index.row()] for x in data[0:5]: if x != '': break else: button2.isEdit = False button2.setIcon(qtawesome.icon('fa.save', color='#1fbb6f')) self.parent().model.editableList.append(button2.index[0]) h_box_layout = QHBoxLayout() # h_box_layout.addWidget(self.button1) h_box_layout.addWidget(button2) h_box_layout.addWidget(button3) # h_box_layout.addWidget(self.button4) h_box_layout.setContentsMargins(0, 0, 0, 0) h_box_layout.setAlignment(Qt.AlignCenter) widget = QWidget() widget.setObjectName('userBtnWidget') widget.setLayout(h_box_layout) self.parent().setIndexWidget( index, widget ) # widget.setStyleSheet(''' # QWidget{background : #ffffff; # margin-bottom:4px; # margin-top:4px;}''') def edit_action(self): sender = self.sender() model = self.parent().model #paint.setAttribute(Qt.WA_TransparentForMouseEvents, False) if sender.isEdit: sender.setIcon(qtawesome.icon('fa.save', color='#1fbb6f')) sender.isEdit = False sender.oldName = model.datas[sender.index[0]][1] model.editableList.append(sender.index[0]) for i in range(5, 9): cbRow = str('cb' + str(sender.index[0]) + str(i)) index = self.parent().model.index(sender.index[0], i) delegate = self.parent().itemDelegate(index) checkbox = getattr(delegate, cbRow) if sender.index[0] == 0: checkbox.setAttribute(Qt.WA_TransparentForMouseEvents, True) else: checkbox.setAttribute(Qt.WA_TransparentForMouseEvents, False) else: userMes = model.datas[sender.index[0]] name, passwd, des, projectAuthority, protocolSetting, userAuthority, trendAuthority = str(userMes[1]), str(userMes[2]), str(userMes[3]), str(userMes[5]), str(userMes[6]), str(userMes[7]), str(userMes[8]) # print(name, passwd, des, projectAuthority, protocolSetting, userAuthority, trendAuthority) if not name or not passwd: reply = QMessageBox.question(self.parent(), '警告', "有字段为空", QMessageBox.Yes) return sender.isEdit = True model.editableList.remove(sender.index[0]) if sender.oldName: UserManage.editUser(userName = sender.oldName, Nname=name, userPwd = passwd, description = des, projectAuthority = projectAuthority, protocolSetting = protocolSetting, userAuthority = userAuthority, trendAuthority = trendAuthority) else: UserManage.createUser(userName = name, userPwd = passwd, description = des, projectAuthority = projectAuthority, protocolSetting = protocolSetting, userAuthority = userAuthority, trendAuthority = trendAuthority) sender.setIcon(qtawesome.icon('fa.pencil', color='#4c8cf2')) rowIndex = sender.index[0] userMes = UserManage.getByName(name) #varMes.append('') # UserMes.insert(1, '') #varMes.insert(2, '') userMes += [''] # if sender.index[0] in model.editableList: # # else: model.remove_row(rowIndex) model.insert_data(userMes, rowIndex) for i in range(5, 9): cbRow = str('cb' + str(sender.index[0]) + str(i)) index = self.parent().model.index(sender.index[0], i) delegate = self.parent().itemDelegate(index) checkbox = getattr(delegate, cbRow) checkbox.setAttribute(Qt.WA_TransparentForMouseEvents, True) def delete_action(self): sender = self.sender() model = self.parent().model name = str(model.datas[sender.index[0]][1]) UserManage.deleteUser(name = name) model.remove_row(sender.index[0]) class UserCheckBoxDelegate(QItemDelegate): """该类用于向单元格中添加复选框 任务表格""" def __init__(self, parent=None): super(UserCheckBoxDelegate, self).__init__(parent) def paint(self, painter, option, index): if not self.parent().indexWidget(index): data = self.parent().model.datas[index.row()] checkBox = str('cb' + str(index.row()) + str(index.column())) setattr(self, checkBox, QCheckBox('',self.parent())) cbBox = getattr(self, checkBox) cbBox.setObjectName('userBox') #checkBox = getattr(self, str(index.row() + index.column())) #getattr(self,str(index.row() + index.column())) #self.checkBox = QCheckBox('',self.parent()) cbBox.index = [index.row(), index.column()] if str(data[index.column()]) == '0': #button2.isEdit = False cbBox.setChecked(False) #self.parent().model.editableList.append(button2.index[0]) elif str(data[index.column()]) == '2': #button2.isEdit = True cbBox.setChecked(True) #self.parent().model.editableList.append(button2.index[0]) # if self.sender.index[0] in self.parent().model.editableList: # self.checkBox.setAttribute(Qt.WA_TransparentForMouseEvents, False); # else: # self.checkBox.setAttribute(Qt.WA_TransparentForMouseEvents); cbBox.stateChanged.connect(lambda: self.checkChanged(cbBox, data)) for x in data[1:5]: if x != '': cbBox.setAttribute(Qt.WA_TransparentForMouseEvents) break h_box_layout = QHBoxLayout() h_box_layout.addWidget(cbBox) h_box_layout.setContentsMargins(0, 0, 0, 0) h_box_layout.setAlignment(Qt.AlignCenter) widget = QWidget() widget.setLayout(h_box_layout) widget.setObjectName('userCheckWidget') self.parent().setIndexWidget( index, widget ) # self.checkBox.setFocusPolicy(Qt.NoFocus); # widget.setStyleSheet(''' # QWidget{background : #ffffff; # margin-bottom:4px; # margin-top:4px;}''') def checkChanged(self, cbBoxs, data): cbBoxs = cbBoxs if str(cbBoxs.checkState()) == '2': if cbBoxs.index[1] == 5: data[5] = 2 elif cbBoxs.index[1] == 6: data[6] = 2 elif cbBoxs.index[1] == 7: data[7] = 2 elif cbBoxs.index[1] == 8: data[8] = 2 elif str(cbBoxs.checkState()) == '0': if cbBoxs.index[1] == 5: data[5] = 0 elif cbBoxs.index[1] == 6: data[6] = 0 elif cbBoxs.index[1] == 7: data[7] = 0 elif cbBoxs.index[1] == 8: data[8] = 0 ''' print(str(self.checkBox.checkState())) print(self.checkBox.index[1]) if str(self.checkBox.checkState()) == '2': if self.checkBox.index[1] == 5: self.authority['工程管理权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 6: self.authority['变量强制权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 7: self.authority['用户管理权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 8: self.authority['趋势查看权限'] = str(self.checkBox.checkState()) elif str(self.checkBox.checkState()) == '0': if self.checkBox.index[1] == 5: self.authority['工程管理权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 6: self.authority['变量强制权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 7: self.authority['用户管理权限'] = str(self.checkBox.checkState()) elif self.checkBox.index[1] == 8: self.authority['趋势查看权限'] = str(self.checkBox.checkState()) '''