void WSortFilterProxyModel::sourceDataChanged(const WModelIndex& topLeft, const WModelIndex& bottomRight) { bool refilter = dynamic_ && (filterKeyColumn_ >= topLeft.column() && filterKeyColumn_ <= bottomRight.column()); bool resort = dynamic_ && (sortKeyColumn_ >= topLeft.column() && sortKeyColumn_ <= bottomRight.column()); WModelIndex parent = mapFromSource(topLeft.parent()); Item *item = itemFromIndex(parent); for (int row = topLeft.row(); row <= bottomRight.row(); ++row) { int oldMappedRow = item->sourceRowMap_[row]; bool propagateDataChange = oldMappedRow != -1; if (refilter || resort) { // Determine new insertion point: erase it temporarily for this if (oldMappedRow != -1) item->proxyRowMap_.erase(item->proxyRowMap_.begin() + oldMappedRow); int newMappedRow = mappedInsertionPoint(row, item); if (oldMappedRow != -1) item->proxyRowMap_.insert(item->proxyRowMap_.begin() + oldMappedRow, row); if (newMappedRow != oldMappedRow) { if (oldMappedRow != -1) { beginRemoveRows(parent, oldMappedRow, oldMappedRow); item->proxyRowMap_.erase (item->proxyRowMap_.begin() + oldMappedRow); rebuildSourceRowMap(item); endRemoveRows(); } if (newMappedRow != -1) { beginInsertRows(parent, newMappedRow, newMappedRow); item->proxyRowMap_.insert (item->proxyRowMap_.begin() + newMappedRow, row); rebuildSourceRowMap(item); endInsertRows(); } propagateDataChange = false; } } if (propagateDataChange) { WModelIndex l = sourceModel()->index(row, topLeft.column(), topLeft.parent()); WModelIndex r = sourceModel()->index(row, bottomRight.column(), topLeft.parent()); dataChanged().emit(mapFromSource(l), mapFromSource(r)); } } }
void WSortFilterProxyModel::sourceRowsInserted(const WModelIndex& parent, int start, int end) { startShiftModelIndexes(parent, end + 1, (end - start + 1), mappedIndexes_); if (inserting_) return; int count = end - start + 1; WModelIndex pparent = mapFromSource(parent); // distinguish between invalid parent being root item or being filtered out if (parent.isValid() && !pparent.isValid()) return; Item *item = itemFromIndex(pparent); // Shift existing entries in proxyRowMap, and reserve place in sourceRowMap // After this step, existing rows are okay again. for (unsigned i = 0; i < item->proxyRowMap_.size(); ++i) { if (item->proxyRowMap_[i] >= start) item->proxyRowMap_[i] += count; } item->sourceRowMap_.insert(item->sourceRowMap_.begin() + start, count, -1); if (!dynamic_) return; for (int row = start; row <= end; ++row) { int newMappedRow = mappedInsertionPoint(row, item); if (newMappedRow != -1) { beginInsertRows(pparent, newMappedRow, newMappedRow); item->proxyRowMap_.insert (item->proxyRowMap_.begin() + newMappedRow, row); rebuildSourceRowMap(item); // insertion may have shifted some endInsertRows(); } else item->sourceRowMap_[row] = -1; } }