-
- Q_ASSERT(false);
- }
-
- // we've got a sorted list of ranges now. so we can easily check if there is a possibility to merge
- for(int i = newRanges.count() - 1; i > 0; i--) {
- _RangeRect &a = newRanges[i - 1];
- _RangeRect &b = newRanges[i];
- if(a.left != b.left || a.right != b.right)
- continue;
-
- if(a.bottom < b.top - 1) {
- continue;
- }
-
- // all merge checks passed!
- if(b.bottom > a.bottom) {
- a.bottom = b.bottom;
- a.bottomItem = b.bottomItem;
- } // otherwise b is totally enclosed in a -> nothing to do but drop b.
- newRanges.removeAt(i);
- }
-
- QItemSelection proxySelection;
- for(int i = 0; i < newRanges.count(); i++) {
- _RangeRect &r = newRanges[i];
- proxySelection << QItemSelectionRange(createIndex(r.top, r.left, r.topItem), createIndex(r.bottom, r.right, r.bottomItem));
- }
- return proxySelection;
-}
-
-QItemSelection FlatProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const {
- QItemSelection sourceSelection;
-
- for(int i = 0; i < proxySelection.count(); i++) {
- const QItemSelectionRange &range = proxySelection[i];
-
- SourceItem *topLeftItem = 0;
- SourceItem *bottomRightItem = 0;
- SourceItem *currentItem = static_cast<SourceItem *>(range.topLeft().internalPointer());
- int row = range.topLeft().row();
- int left = range.topLeft().column();
- int right = range.bottomRight().column();
- while(currentItem && row <= range.bottomRight().row()) {
- Q_ASSERT(currentItem->pos() == row);
- if(!topLeftItem)
- topLeftItem = currentItem;
-
- if(currentItem->parent() == topLeftItem->parent()) {
- bottomRightItem = currentItem;
- } else {
- Q_ASSERT(topLeftItem && bottomRightItem);
- sourceSelection << QItemSelectionRange(mapToSource(createIndex(topLeftItem->pos(), left, topLeftItem)), mapToSource(createIndex(bottomRightItem->pos(), right, bottomRightItem)));
- topLeftItem = 0;
- bottomRightItem = 0;
- }
-
- // update loop vars
- currentItem = currentItem->next();
- row++;
- }
-
- Q_ASSERT(topLeftItem && bottomRightItem); // there should be one range left.
- sourceSelection << QItemSelectionRange(mapToSource(createIndex(topLeftItem->pos(), left, topLeftItem)), mapToSource(createIndex(bottomRightItem->pos(), right, bottomRightItem)));
- }
-
- return sourceSelection;
-}
-
-void FlatProxyModel::setSourceModel(QAbstractItemModel *sourceModel) {
- if(QAbstractProxyModel::sourceModel()) {
- disconnect(QAbstractProxyModel::sourceModel(), 0, this, 0);
- }
-
- QAbstractProxyModel::setSourceModel(sourceModel);
-
- emit layoutAboutToBeChanged();
- removeSubTree(QModelIndex(), false /* don't emit removeRows() */);
- insertSubTree(QModelIndex(), false /* don't emit insertRows() */);
- emit layoutChanged();
-
- if(sourceModel) {
- connect(sourceModel, SIGNAL(columnsAboutToBeInserted(const QModelIndex &, int, int)),
- this, SLOT(on_columnsAboutToBeInserted(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(columnsAboutToBeRemoved(const QModelIndex &, int, int)),
- this, SLOT(on_columnsAboutToBeRemoved(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
- this, SLOT(on_columnsInserted(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(columnsRemoved(const QModelIndex &, int, int)),
- this, SLOT(on_columnsRemoved(const QModelIndex &, int, int)));
-
- connect(sourceModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
- this, SLOT(on_dataChanged(const QModelIndex &, const QModelIndex &)));
- // on_headerDataChanged(Qt::Orientation orientation, int first, int last)
-
- connect(sourceModel, SIGNAL(layoutAboutToBeChanged()),
- this, SLOT(on_layoutAboutToBeChanged()));
- connect(sourceModel, SIGNAL(layoutChanged()),
- this, SLOT(on_layoutChanged()));
-
- connect(sourceModel, SIGNAL(modelAboutToBeReset()),
- this, SLOT(on_modelAboutToBeReset()));
- // void on_modelReset()
-
- connect(sourceModel, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
- this, SLOT(on_rowsAboutToBeInserted(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
- this, SLOT(on_rowsAboutToBeRemoved(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
- this, SLOT(on_rowsInserted(const QModelIndex &, int, int)));
- connect(sourceModel, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
- this, SLOT(on_rowsRemoved(const QModelIndex &, int, int)));
- }
-
-}
-
-void FlatProxyModel::insertSubTree(const QModelIndex &source_idx, bool emitInsert) {
- SourceItem *newSubTree = new SourceItem(source_idx.row(), sourceToInternal(sourceModel()->parent(source_idx)));
-
- if(newSubTree->parent()) {
- newSubTree->setPos(newSubTree->parent()->pos() + source_idx.row() + 1);
- }
- SourceItem *lastItem = insertSubTreeHelper(newSubTree, newSubTree, source_idx);
-
- Q_ASSERT(lastItem);
- Q_ASSERT(lastItem->next() == 0);
-
- if(emitInsert)
- beginInsertRows(QModelIndex(), newSubTree->pos(), lastItem->pos());
-
- if(newSubTree->parent()) {
- if(newSubTree->parent()->childCount() > source_idx.row()) {
- SourceItem *next = newSubTree->parent()->child(source_idx.row());
- lastItem->setNext(next);
- int nextPos = lastItem->pos() + 1;
- while(next) {
- next->setPos(nextPos);
- next = next->next();