src/gui/itemviews/qsortfilterproxymodel.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   732             : q->filterAcceptsColumn(i, source_parent)) {
   732             : q->filterAcceptsColumn(i, source_parent)) {
   733             source_items.append(i);
   733             source_items.append(i);
   734         }
   734         }
   735     }
   735     }
   736 
   736 
       
   737     if (model->rowCount(source_parent) == delta_item_count) {
       
   738         // Items were inserted where there were none before.
       
   739         // If it was new rows make sure to create mappings for columns so that a
       
   740         // valid mapping can be retreived later and vice-versa.
       
   741 
       
   742         QVector<int> &orthogonal_proxy_to_source = (orient == Qt::Horizontal) ? m->source_rows : m->source_columns;
       
   743         QVector<int> &orthogonal_source_to_proxy = (orient == Qt::Horizontal) ? m->proxy_rows : m->proxy_columns;
       
   744 
       
   745         if (orthogonal_source_to_proxy.isEmpty()) {
       
   746             const int ortho_end = (orient == Qt::Horizontal) ? model->rowCount(source_parent) : model->columnCount(source_parent);
       
   747 
       
   748             for (int ortho_item = 0; ortho_item < ortho_end; ++ortho_item) {
       
   749                 if ((orient == Qt::Horizontal) ? q->filterAcceptsRow(ortho_item, source_parent)
       
   750                         : q->filterAcceptsColumn(ortho_item, source_parent)) {
       
   751                     orthogonal_proxy_to_source.append(ortho_item);
       
   752                 }
       
   753             }
       
   754             orthogonal_source_to_proxy.resize(orthogonal_proxy_to_source.size());
       
   755 
       
   756             if (orient == Qt::Horizontal) {
       
   757                 // We're reacting to columnsInserted, but we've just inserted new rows. Sort them.
       
   758                 sort_source_rows(orthogonal_proxy_to_source, source_parent);
       
   759             }
       
   760             build_source_to_proxy_mapping(orthogonal_proxy_to_source, orthogonal_source_to_proxy);
       
   761         }
       
   762     }
       
   763 
   737     // Sort and insert the items
   764     // Sort and insert the items
   738     if (orient == Qt::Vertical) // Only sort rows
   765     if (orient == Qt::Vertical) // Only sort rows
   739         sort_source_rows(source_items, source_parent);
   766         sort_source_rows(source_items, source_parent);
   740     insert_source_items(source_to_proxy, proxy_to_source, source_items, source_parent, orient);
   767     insert_source_items(source_to_proxy, proxy_to_source, source_items, source_parent, orient);
   741 }
   768 }
  1151 
  1178 
  1152 void QSortFilterProxyModelPrivate::_q_sourceAboutToBeReset()
  1179 void QSortFilterProxyModelPrivate::_q_sourceAboutToBeReset()
  1153 {
  1180 {
  1154     Q_Q(QSortFilterProxyModel);
  1181     Q_Q(QSortFilterProxyModel);
  1155     q->beginResetModel();
  1182     q->beginResetModel();
       
  1183     invalidatePersistentIndexes();
       
  1184     clear_mapping();
  1156 }
  1185 }
  1157 
  1186 
  1158 void QSortFilterProxyModelPrivate::_q_sourceReset()
  1187 void QSortFilterProxyModelPrivate::_q_sourceReset()
  1159 {
  1188 {
  1160     Q_Q(QSortFilterProxyModel);
  1189     Q_Q(QSortFilterProxyModel);
  1167 
  1196 
  1168 void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged()
  1197 void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged()
  1169 {
  1198 {
  1170     Q_Q(QSortFilterProxyModel);
  1199     Q_Q(QSortFilterProxyModel);
  1171     saved_persistent_indexes.clear();
  1200     saved_persistent_indexes.clear();
       
  1201     emit q->layoutAboutToBeChanged();
  1172     if (persistent.indexes.isEmpty())
  1202     if (persistent.indexes.isEmpty())
  1173         return;
  1203         return;
  1174     emit q->layoutAboutToBeChanged();
  1204 
  1175     saved_persistent_indexes = store_persistent_indexes();
  1205     saved_persistent_indexes = store_persistent_indexes();
  1176 }
  1206 }
  1177 
  1207 
  1178 void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged()
  1208 void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged()
  1179 {
  1209 {
  1180     Q_Q(QSortFilterProxyModel);
  1210     Q_Q(QSortFilterProxyModel);
  1181     if (saved_persistent_indexes.isEmpty()) {
  1211     if (saved_persistent_indexes.isEmpty()) {
  1182         q->invalidate();
  1212         clear_mapping();
       
  1213         emit q->layoutChanged();
  1183         return;
  1214         return;
  1184     }
  1215     }
  1185 
  1216 
  1186     qDeleteAll(source_index_mapping);
  1217     qDeleteAll(source_index_mapping);
  1187     source_index_mapping.clear();
  1218     source_index_mapping.clear();
  1468 */
  1499 */
  1469 void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
  1500 void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
  1470 {
  1501 {
  1471     Q_D(QSortFilterProxyModel);
  1502     Q_D(QSortFilterProxyModel);
  1472 
  1503 
       
  1504     beginResetModel();
       
  1505 
  1473     disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  1506     disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  1474                this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
  1507                this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
  1475 
  1508 
  1476     disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
  1509     disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
  1477                this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int)));
  1510                this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int)));
  1549 
  1582 
  1550     connect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset()));
  1583     connect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset()));
  1551     connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));
  1584     connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));
  1552 
  1585 
  1553     d->clear_mapping();
  1586     d->clear_mapping();
  1554     reset();
  1587     endResetModel();
  1555     if (d->update_source_sort_column() && d->dynamic_sortfilter)
  1588     if (d->update_source_sort_column() && d->dynamic_sortfilter)
  1556         d->sort();
  1589         d->sort();
  1557 }
  1590 }
  1558 
  1591 
  1559 /*!
  1592 /*!