def __init__(self, parent, tableName, isReport = None): super(ViewTables, self).__init__(parent) self.ui = Ui_ViewTables() self.ui.setupUi(self) self.ui.addRecordButton.clicked.connect(self.addRecord) self.ui.editRecordButton.clicked.connect(self.editRecord) self.ui.deleteRecordButton.clicked.connect(self.deleteRecord) self.ui.tableWidget.itemSelectionChanged.connect(self.disableButtons) self.tableName = tableName self.setWindowTitle(convertTableNameToTitle[tableName]) self.isReport = isReport appInst.tables.append(self) self.fillHeaders() self.fillCells() self.disableButtons()
class ViewTables(QtGui.QWidget): def __init__(self, parent, tableName, isReport = None): super(ViewTables, self).__init__(parent) self.ui = Ui_ViewTables() self.ui.setupUi(self) self.ui.addRecordButton.clicked.connect(self.addRecord) self.ui.editRecordButton.clicked.connect(self.editRecord) self.ui.deleteRecordButton.clicked.connect(self.deleteRecord) self.ui.tableWidget.itemSelectionChanged.connect(self.disableButtons) self.tableName = tableName self.setWindowTitle(convertTableNameToTitle[tableName]) self.isReport = isReport appInst.tables.append(self) self.fillHeaders() self.fillCells() self.disableButtons() def fillHeaders(self): #self.headers = appInst.getHeadersWithForeignValues(self.tableName) self.ui.tableWidget.setColumnCount(len(self.headers)) self.ui.tableWidget.verticalHeader().setVisible(False) self.ui.tableWidget.setHorizontalHeaderLabels(self.headers) if not appInst.isAdmin(): self.ui.addRecordButton.setDisabled(True) def fillCells(self): self.ui.tableWidget.clearContents() fields = appInst.getVisibleHeaders(appInst.getTable(self.tableName)) values = appInst.selectAllWithForeignValues(self.tableName, self.isReport) self.ui.tableWidget.setRowCount(len(values) + (1 if self.isReport else 0)) row = -1 for value in values: row = row + 1 column = -1 for item in value: column = column + 1 it = item if isEnum(fields[column]): it = globals()[fields[column].name][int(item)] newitem = QtGui.QTableWidgetItem(str(it)) self.ui.tableWidget.setItem(row, column, newitem) if self.isReport: newitem = QtGui.QTableWidgetItem(str(appInst.cntSum())) self.ui.tableWidget.setItem(row + 1, column, newitem) self.primaryKeys = self.findPrimaryKeys() def findPrimaryKeys(self): fields = appInst.getHeaders(self.tableName) values = appInst.selectAll(self.tableName) result = list() for value in values: pk = list() column = -1 for item in value: column += 1 if fields[column].primary_key: pk.append({'name': fields[column].name, 'value': item}) result.append(pk) return result def disableButtons(self): disable = not (appInst.isAdmin() and len(self.ui.tableWidget.selectedItems())) self.ui.addRecordButton.setDisabled(not appInst.isAdmin()) self.ui.editRecordButton.setDisabled(disable) self.ui.deleteRecordButton.setDisabled(disable) def addRecord(self): rec = ChangeRecord(self, self.tableName) rec.open() def editRecord(self): row = self.ui.tableWidget.currentRow() rec = ChangeRecord(self, self.tableName, self.primaryKeys[row]) rec.open() def deleteRecord(self): msg = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to delete this record ?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if msg == QtGui.QMessageBox.Yes: row = self.ui.tableWidget.currentRow() appInst.delete(self.tableName, self.primaryKeys[row]) appInst.updateTableViews() def closeEvent(self, event): appInst.tables.remove(self) event.accept()