def editEntry(self): """ Edit an entry in the addressbook. """ tableView = self.currentWidget() proxyModel = tableView.model() selectionModel = tableView.selectionModel() # Get the name and address of the currently selected row. indexes = selectionModel.selectedRows() for index in indexes: row = proxyModel.mapToSource(index).row() ix = self.tableModel.index(row, 0, QModelIndex()) name = self.tableModel.data(ix, Qt.DisplayRole) ix = self.tableModel.index(row, 1, QModelIndex()) address = self.tableModel.data(ix, Qt.DisplayRole) # Open an addDialogWidget, and only allow the user to edit the address. addDialog = AddDialogWidget() addDialog.setWindowTitle("Edit a Contact") addDialog.nameText.setReadOnly(True) addDialog.nameText.setText(name) addDialog.addressText.setText(address) # If the address is different, add it to the model. if addDialog.exec_(): newAddress = addDialog.address if newAddress != address: ix = self.tableModel.index(row, 1, QModelIndex()) self.tableModel.setData(ix, newAddress, Qt.EditRole)
def insertRows(self, row, count, parent=QModelIndex()): """QAbstractItemModel virtual """ debug_print('Model.insertRows row [{0}] count [{1}]'.format( row, count)) if row < 0 or row > len(self._data) or count < 1: raise ValueError('Bad row [{0}] or count [{1}]'.format(row, count)) else: upper = row + count - 1 self.beginInsertRows(QModelIndex(), row, upper) # Create list of new rows. Cannot use [{whatever}] * count because # this will create the same dict instance repeated 'count' times, # not 'count' different dict instances new_rows = [None] * count for i in xrange(0, count): new_rows[i] = { "metadata": {}, "rect": QtCore.QRect(0, 0, 0, 0), "rotation": 0 } self._data[row:row] = new_rows self._modified = True self.endInsertRows() self.dataChanged.emit(self.index(row, 0), self.index(upper, 0)) return True
def clear(self): self.beginRemoveRows(QModelIndex(), 0, self.rowCount() - 1) self.beginRemoveColumns(QModelIndex(), 0, 0) self.table = [] self.references = [] self.endRemoveColumns() self.endRemoveRows()
def index(self, row, column, parent=QModelIndex()): """QAbstractItemModel virtual """ if self.hasIndex(row, column, parent): return self.createIndex(row, column, self._data[row]) else: return QModelIndex()
def removeRows(self, position, rows=1, index=QModelIndex()): """ Remove a row from the model. """ self.beginRemoveRows(QModelIndex(), position, position + rows - 1) del self.addresses[position:position + rows] self.endRemoveRows() return True
def index(self, row, column, parent=QModelIndex()): if not self.hasIndex(row, column, parent): return QModelIndex() p = parent.internalPointer() if parent.isValid() else self._root c = p.get_child(row) return self.createIndex(row, column, c)
def insertRows(self, position, rows=1, index=QModelIndex()): """ Insert a row into the model. """ self.beginInsertRows(QModelIndex(), position, position + rows - 1) for row in range(rows): self.addresses.insert(position + row, {"name": "", "address": ""}) self.endInsertRows() return True
def parent(self, index): # this method is the reverse of index(). It returns indexes to parents, not to children if not index.isValid(): return QModelIndex() item = self.getItemAt(index) parent = item.parent() if parent is self._root_item: return QModelIndex() else: return self.createIndex(parent.childNumber(), 0, parent)
def removeRows(self, position, rows=1, index=QModelIndex()): self.beginRemoveRows(QModelIndex(), position, position + rows - 1) for row in range(position, rows): compartment = self.compartments[row] self.mainModel.SbmlModel.removeCompartment(compartment) self.endRemoveRows() self.Dirty = True return True
def clear(self): if self.rowCount() == 0: return self.beginRemoveRows(QModelIndex(),0,max(0,self.rowCount()-1)) self.beginRemoveColumns(QModelIndex(),0,max(0,self.columnCount()-1)) self.objects = [] self.table_backgrounds = [] self.endRemoveColumns() self.endRemoveRows()
def removeRows(self, row, count=1, parent=None): if parent is None: parent = QModelIndex() self.beginRemoveRows(QModelIndex(), row, row + count - 1) for index in reversed(range(row, row + count)): self._forcings.pop(index) self.endRemoveRows() return True
def insertRows(self, row, count=1, parent=None): if parent is None: parent = QModelIndex() self.beginInsertRows(QModelIndex(), row, row + count - 1) for _ in range(count): self._forcings.insert(row, [ELECTRON, DELTA, -1, 0.0, 1.0]) self.endInsertRows() return True
def on_data_changed(self, *_): root = self.invisibleRootItem() for i in range(root.rowCount()): file_item = root.child(i) file_item.apply(convert(self.pattern_model.pattern_data(), file_item.text())) for j in range(file_item.rowCount()): take_item = file_item.child(j) take_item.apply(convert(self.pattern_model.pattern_data(), take_item.text())) self.dataChanged.emit(QModelIndex(), QModelIndex())
def insertRows(self, position, rows=1, index=QModelIndex()): self.beginInsertRows(QModelIndex(), position, position + rows - 1) # mandatory Qt call for row in range( rows ): # we don't take the actual row into account as the libSBML always appends at the end newCompartment = libsbml.Compartment() self.mainModel.SbmlModel.addCompartment(SBMLEntity(newCompartment)) self.endInsertRows() # mandatory Qt call self.Dirty = True return True
def parent(self, childIndex): if not childIndex.isValid(): return QModelIndex() child = self.nodeFromIndex(childIndex) parent = child.Parent if parent is self.root: return QModelIndex() row = parent.getRowOfChild(child) # if row: return self.createIndex(row, 0, parent)
def parent(self, index): if not index.isValid(): return QModelIndex() childItem = index.internalPointer() if not childItem: return QModelIndex() parentItem = childItem.parent() if parentItem == self.rootItem: return QModelIndex() return self.createIndex(parentItem.row(), 0, parentItem)
def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parentItem = self.rootItem else: parentItem = parent.internalPointer() childItem = parentItem.child(row) if childItem: return self.createIndex(row, column, childItem) else: return QModelIndex()
def index(self, row, column, parentIndex): # I think this method is here to pack data into different indexes if not self.hasIndex(row, column, parentIndex): return QModelIndex() # get the parent item from the index. if not parentIndex.isValid(): parent_item = self._root_item else: parent_item = parentIndex.internalPointer() child = parent_item.child(row) if child: return self.createIndex(row, column, child) else: return QModelIndex()
def parent(self, index): '''Return parent of *index*.''' if not index.isValid(): return QModelIndex() item = index.internalPointer() if not item: return QModelIndex() parent = item.parent if not parent or parent == self.root: return QModelIndex() return self.createIndex(parent.row, 0, parent)
def index(self, row, column, parentIndex): if row < 0 or column < 0: return QModelIndex() # we are only interested in the first (0th) column if parentIndex.isValid() and parentIndex.column() != 0: return QModelIndex() parent = self.nodeFromIndex(parentIndex) child = parent.getChild(row) if child: return self.createIndex(row, column, child) else: return QModelIndex()
def addSpiderGroup(self, groupName="New launch group"): group_count = self._root_item.childCount() self.beginInsertRows(QModelIndex(), group_count - 1, group_count) new_group = SpiderTreeItem(groupName, [], parent=self._root_item) #new_group.appendChild(SpiderTreeItem("Drag and drop spider(s) here...", [], parent=new_group)) # used to crash before adding [] self._root_item.appendChild(new_group) self.endInsertRows()
def loadCache(self): self.beginInsertRows(QModelIndex(), len(self._photos), len(self._photos) + len(self._cache)) self._photos += self._cache self.endInsertRows() self._cache = [] if self._done: self._timer.stop()
def swap_row(self, ndx, ndx2): if ndx >= 0 and ndx2 >= 0 and ndx < self.rowCount( ) and ndx2 < self.rowCount() and ndx != ndx2: mainlog.debug("ObjectModel : swap_row {} {}".format(ndx, ndx2)) p = QModelIndex() if self.beginMoveRows(p, ndx, ndx, p, ndx2): mainlog.debug("ObjectModel : swap_row Done") # FIXME use rowsAboutToBeMoved o = self._objects[ndx2] self._objects[ndx2] = self._objects[ndx] self._objects[ndx] = o # The order change is a also change ! self._changed_objects.add(self._objects[ndx]) self._changed_objects.add(self._objects[ndx2]) self.endMoveRows() self.dataChanged.emit(self.index(ndx, 0), self.index(ndx, self.columnCount() - 1)) self.dataChanged.emit(self.index(ndx2, 0), self.index(ndx2, self.columnCount() - 1)) return True else: mainlog.debug("beginMoveRows didn't want to work :-(") return False
def insert_objects(self, row_ndx, objects, parentIndex=QModelIndex()): """ Insert new objects in the model. We assume those objects will *need* to be saved in the database (unless deleted first) """ if not objects or len(objects) == 0: return if row_ndx < 0: row_ndx = 0 self.beginInsertRows(parentIndex, row_ndx, row_ndx + len(objects) - 1) for obj in objects: if obj in self._objects: mainlog.error( "Trying to add the same object twice into object model.") self._created_objects.add(obj) # mainlog.debug("insert_objects: inserting object {} at row {}".format(obj, row_ndx)) self._objects.insert(row_ndx, obj) row_ndx += 1 self.endInsertRows()
def pathIndex(self, path): '''Return index of item with *path*.''' sourceModel = self.sourceModel() if not sourceModel: return QModelIndex() return self.mapFromSource(sourceModel.pathIndex(path))
def index(self, row, column, parent): '''Return index for *row* and *column* under *parent*.''' if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): item = self.root else: item = parent.internalPointer() try: child = item.children[row] except IndexError: return QModelIndex() else: return self.createIndex(row, column, child)
def database_remove_node(self, node_name): model_id = self.get_node_model_id(node_name) self.beginRemoveRows(QModelIndex(), model_id.row(), model_id.row()) c = self.db.cursor() c.execute( 'SELECT FileId ' 'FROM NodesTable LEFT JOIN FilesTable USING (FileId) ' 'WHERE Name=?', (node_name, )) file_id = c.fetchone()[0] c.execute('SELECT InstanceCount ' 'FROM FilesTable ' 'WHERE FileId=?', (file_id, )) if c.fetchone()[0] == 1: c.execute('DELETE FROM FilesTable ' 'WHERE FileId=?', (file_id, )) else: c.execute( 'UPDATE FilesTable ' 'SET InstanceCount=InstanceCount - 1 ' 'WHERE FileId=?', (file_id, )) c.execute('DELETE FROM NodesTable WHERE Name=?', (node_name, )) self.endRemoveRows()
def invertSelection(self): self.emit(SIGNAL("layoutAboutToBeChanged()")) for column in xrange(1, self.columnCount()): for row in xrange(self.rowCount()): if self.dataMatrix[column][row] == STATE.NODATA: continue if self.dataMatrix[column][row] == STATE.CHECKED: self.dataMatrix[column][row] = STATE.UNCHECKED else: self.dataMatrix[column][row] = STATE.CHECKED self.emit(SIGNAL("layoutChanged()")) self.dataChanged.emit(QModelIndex(), QModelIndex())
def addParameter(self): """ Add a standard parameter to the end of the list. """ standardParameter = Parameter("ParameterName", "Value") self.parameters.append(standardParameter) self.insertRows(len(self.parameters), 1, QModelIndex())
def addEntry(self, name=None, address=None): """ Add an entry to the addressbook. """ if name is None and address is None: addDialog = AddDialogWidget() if addDialog.exec_(): name = addDialog.name address = addDialog.address address = {"name": name, "address": address} addresses = self.tableModel.addresses[:] # The QT docs for this example state that what we're doing here # is checking if the entered name already exists. What they # (and we here) are actually doing is checking if the whole # name/address pair exists already - ok for the purposes of this # example, but obviously not how a real addressbook application # should behave. try: addresses.remove(address) QMessageBox.information(self, "Duplicate Name", "The name \"%s\" already exists." % name) except ValueError: # The address didn't already exist, so let's add it to the model. # Step 1: create the row self.tableModel.insertRows(0) # Step 2: get the index of the newly created row and use it. # to set the name ix = self.tableModel.index(0, 0, QModelIndex()) self.tableModel.setData(ix, address["name"], Qt.EditRole) # Step 3: lather, rinse, repeat for the address. ix = self.tableModel.index(0, 1, QModelIndex()) self.tableModel.setData(ix, address["address"], Qt.EditRole) # Remove the newAddressTab, as we now have at least one # address in the model. self.removeTab(self.indexOf(self.newAddressTab)) # The screenshot for the QT example shows nicely formatted # multiline cells, but the actual application doesn't behave # quite so nicely, at least on Ubuntu. Here we resize the newly # created row so that multiline addresses look reasonable. tableView = self.currentWidget() tableView.resizeRowToContents(ix.row())