QModelIndex ItemViewFind::nextIndex(const QModelIndex &idx, bool *wrapped) const { if (wrapped) *wrapped = false; QAbstractItemModel *model = d->m_view->model(); // pathological if (!idx.isValid()) return model->index(0, 0); // same parent has more columns, go to next column if (idx.column() + 1 < model->columnCount(idx.parent())) return model->index(idx.row(), idx.column() + 1, idx.parent()); // tree views have their children attached to first column // make sure we are at first column QModelIndex current = model->index(idx.row(), 0, idx.parent()); // check for children if (d->m_option == FetchMoreWhileSearching && model->canFetchMore(current)) model->fetchMore(current); if (model->rowCount(current) > 0) return current.child(0, 0); // no more children, go up and look for parent with more children QModelIndex nextIndex; while (!nextIndex.isValid()) { int row = current.row(); current = current.parent(); if (d->m_option == FetchMoreWhileSearching && model->canFetchMore(current)) model->fetchMore(current); if (row + 1 < model->rowCount(current)) { // Same parent has another child nextIndex = model->index(row + 1, 0, current); } else { // go up one parent if (!current.isValid()) { // we start from the beginning if (wrapped) *wrapped = true; nextIndex = model->index(0, 0); } } } return nextIndex; }
QModelIndex ItemViewFind::prevIndex(const QModelIndex &idx, bool *wrapped) const { if (wrapped) *wrapped = false; QAbstractItemModel *model = d->m_view->model(); // if same parent has earlier columns, just move there if (idx.column() > 0) return model->index(idx.row(), idx.column() - 1, idx.parent()); QModelIndex current = idx; bool checkForChildren = true; if (current.isValid()) { int row = current.row(); if (row > 0) { current = model->index(row - 1, 0, current.parent()); } else { current = current.parent(); checkForChildren = !current.isValid(); if (checkForChildren && wrapped) { // we start from the end *wrapped = true; } } } if (checkForChildren) { // traverse down the hierarchy if (d->m_option == FetchMoreWhileSearching && model->canFetchMore(current)) model->fetchMore(current); while (int rc = model->rowCount(current)) { current = model->index(rc - 1, 0, current); } } // set to last column current = model->index(current.row(), model->columnCount(current.parent()) - 1, current.parent()); return current; }