def __init__(self, config, showTitle = True, showButtons = True, parent = None): super(KTablePanel, self).__init__(parent) self.ktableModel = KTableModel(config) ktableDelegate = KTableDelegate(self) # self.ktables = self.ktableModel.tables self.config = config label = QLabel('Tables:') self.ktableView = KTableView() self.ktableView.setModel(self.ktableModel) self.ktableView.setItemDelegate(ktableDelegate) self.addTableButton = QPushButton("Add") self.removeTableButton = QPushButton("Remove") """CONNECTIONS""" self.connect(self.ktableView, SIGNAL('tableSelected'), self.tableSelected) self.connect(self.addTableButton, SIGNAL("clicked()"), self.addTable) self.connect(self.removeTableButton, SIGNAL("clicked()"), self.removeTable) """LAYOUT""" buttonsLayout = QHBoxLayout() buttonsLayout.addStretch() buttonsLayout.addWidget(self.addTableButton) buttonsLayout.addWidget(self.removeTableButton) layout = QVBoxLayout() if showTitle: layout.addWidget(label) layout.addWidget(self.ktableView) if showButtons: layout.addLayout(buttonsLayout) self.setLayout(layout)
class KTablePanel(QWidget): def __init__(self, config, showTitle = True, showButtons = True, parent = None): super(KTablePanel, self).__init__(parent) self.ktableModel = KTableModel(config) ktableDelegate = KTableDelegate(self) # self.ktables = self.ktableModel.tables self.config = config label = QLabel('Tables:') self.ktableView = KTableView() self.ktableView.setModel(self.ktableModel) self.ktableView.setItemDelegate(ktableDelegate) self.addTableButton = QPushButton("Add") self.removeTableButton = QPushButton("Remove") """CONNECTIONS""" self.connect(self.ktableView, SIGNAL('tableSelected'), self.tableSelected) self.connect(self.addTableButton, SIGNAL("clicked()"), self.addTable) self.connect(self.removeTableButton, SIGNAL("clicked()"), self.removeTable) """LAYOUT""" buttonsLayout = QHBoxLayout() buttonsLayout.addStretch() buttonsLayout.addWidget(self.addTableButton) buttonsLayout.addWidget(self.removeTableButton) layout = QVBoxLayout() if showTitle: layout.addWidget(label) layout.addWidget(self.ktableView) if showButtons: layout.addLayout(buttonsLayout) self.setLayout(layout) def addTable(self): # print "addTable" row = self.ktableModel.rowCount() self.ktableModel.insertRows(row) index = self.ktableModel.index(row, 0) self.ktableView.setFocus() self.ktableView.setCurrentIndex(index) self.ktableView.edit(index) def removeTable(self): # print "removeTable" index = self.ktableView.currentIndex() if not index.isValid(): return selectedTable = self.ktableModel.getTableByIndex(index) answer = QMessageBox.question(self, self.trUtf8(u'Remove table?'), self.trUtf8(u'Are you sure you want to remove the \'%1\' table?').arg(selectedTable.label), QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self.config.tablesToDelete.append(selectedTable) row = index.row() self.ktableModel.removeRows(row) def tableSelected(self, ktable): # print "tableSelected" self.emit(SIGNAL('tableSelected'), ktable) def selectTableAtIndex(self, rowIndex): modelIndex = self.ktableModel.index(rowIndex) if modelIndex.isValid(): self.ktableView.setCurrentIndex(modelIndex)