def addField(self): """ add new field to the end of field table """ m = self.fields.model() newRow = m.rowCount() m.insertRows(newRow, 1) indexName = m.index(newRow, 0, QModelIndex()) indexType = m.index(newRow, 1, QModelIndex()) indexNull = m.index(newRow, 2, QModelIndex()) m.setData(indexName, "new_field") colType = self.fieldTypes[0] if newRow == 0: # adding the first row, use auto-incrementing column type if any if "serial" in self.fieldTypes: # PostgreSQL colType = "serial" m.setData(indexType, colType) m.setData(indexNull, None, Qt.DisplayRole) m.setData(indexNull, Qt.Unchecked, Qt.CheckStateRole) # selects the new row sel = self.fields.selectionModel() sel.select( indexName, QItemSelectionModel.Rows | QItemSelectionModel.ClearAndSelect) # starts editing self.fields.edit(indexName) self.updatePkeyCombo(0 if newRow == 0 else None)
def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() parentItem = parent.internalPointer() if parent.isValid() else self.rootItem childItem = parentItem.child(row) if childItem: return self.createIndex(row, column, childItem) return QModelIndex()
def parent(self, index): if not index.isValid(): return QModelIndex() childItem = index.internalPointer() parentItem = childItem.parent() if parentItem == self.rootItem: return QModelIndex() return self.createIndex(parentItem.row(), 0, parentItem)
def _rItem2Index(self, item, parent=None): if parent is None: parent = QModelIndex() if item == self.getItem(parent): return parent if not parent.isValid() or parent.internalPointer().populated: for i in range(self.rowCount(parent)): index = self.index(i, 0, parent) index = self._rItem2Index(item, index) if index.isValid(): return index return QModelIndex()
def removeRows(self, row, count, index=QModelIndex()): self.beginRemoveRows(index, row, row + count - 1) for i in xrange(row + count - 1, row + 1): self._mapping.pop(i) self._errors.pop(i) self.endRemoveRows() return True
def insertRows(self, row, count, index=QModelIndex()): self.beginInsertRows(index, row, row + count - 1) for i in xrange(count): field = self.newField() self._mapping.insert(row + i, field) self._errors.insert(row + i, None) self.testExpression(row) self.endInsertRows() return True
def _rPath2Index(self, path, parent=None, n=0): if parent is None: parent = QModelIndex() if path is None or len(path) == 0: return parent for i in range(self.rowCount(parent)): index = self.index(i, 0, parent) if self._getPath(index)[n] == path[0]: return self._rPath2Index(path[1:], index, n + 1) return parent
def fieldDown(self): """ move selected field down """ row = self.selectedField() if row is None: QMessageBox.information(self, self.tr("DB Manager"), self.tr("No field selected")) return if row == self.fields.model().rowCount() - 1: QMessageBox.information(self, self.tr("DB Manager"), self.tr("field is at bottom already")) return # take row and reinsert it rowdata = self.fields.model().takeRow(row) self.fields.model().insertRow(row + 1, rowdata) # set selection again index = self.fields.model().index(row + 1, 0, QModelIndex()) self.fields.selectionModel().select( index, QItemSelectionModel.Rows | QItemSelectionModel.ClearAndSelect) self.updatePkeyCombo()
def rowCount(self, parent=QModelIndex()): if parent.isValid(): return 0 return self._mapping.__len__()
def columnCount(self, parent=QModelIndex()): if parent.isValid(): return 0 return len(self.columns)