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));
    }
  }
}
Exemplo n.º 2
0
void WSortFilterProxyModel::updateItem(Item *item) const
{
  int sourceRowCount = sourceModel()->rowCount(item->sourceIndex_);
  item->sourceRowMap_.resize(sourceRowCount);
  item->proxyRowMap_.clear();

  /*
   * Filter...
   */
  for (int i = 0; i < sourceRowCount; ++i) {
    if (filterAcceptRow(i, item->sourceIndex_)) {
      item->sourceRowMap_[i] = item->proxyRowMap_.size();
      item->proxyRowMap_.push_back(i);
    } else
      item->sourceRowMap_[i] = -1;
  }

  /*
   * Sort...
   */
  if (sortKeyColumn_ != -1) {
    Utils::stable_sort(item->proxyRowMap_, Compare(this, item));

    rebuildSourceRowMap(item);
  }
}
Exemplo n.º 3
0
void WSortFilterProxyModel::sourceRowsAboutToBeRemoved
(const WModelIndex& parent, int start, int end)
{
  WModelIndex pparent = mapFromSource(parent);
  Item *item = itemFromIndex(pparent);

  for (int row = start; row <= end; ++row) {
    int mappedRow = item->sourceRowMap_[row];

    if (mappedRow != -1) {
      beginRemoveRows(pparent, mappedRow, mappedRow);
      item->proxyRowMap_.erase(item->proxyRowMap_.begin() + mappedRow);
      rebuildSourceRowMap(item); // erase may have shifted some
      endRemoveRows();
    }
  }
}
Exemplo n.º 4
0
void WSortFilterProxyModel::sourceRowsAboutToBeRemoved
(const WModelIndex& parent, int start, int end)
{
  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);

  for (int row = start; row <= end; ++row) {
    int mappedRow = item->sourceRowMap_[row];

    if (mappedRow != -1) {
      beginRemoveRows(pparent, mappedRow, mappedRow);
      item->proxyRowMap_.erase(item->proxyRowMap_.begin() + mappedRow);
      rebuildSourceRowMap(item); // erase may have shifted some
      endRemoveRows();
    }
  }
}
Exemplo n.º 5
0
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;
  }
}