IFindSupport::Result TreeViewFind::find(const QString &searchTxt, FindFlags findFlags, bool startFromCurrentIndex, bool *wrapped) { if (wrapped) *wrapped = false; if (searchTxt.isEmpty()) return IFindSupport::NotFound; QTextDocument::FindFlags flags = textDocumentFlagsForFindFlags(findFlags); QModelIndex resultIndex; QModelIndex currentIndex = d->m_view->currentIndex(); QModelIndex index = currentIndex; int currentRow = currentIndex.row(); bool backward = (flags & QTextDocument::FindBackward); if (wrapped) *wrapped = false; bool anyWrapped = false; bool stepWrapped = false; if (!startFromCurrentIndex) index = followingIndex(index, backward, &stepWrapped); else currentRow = -1; do { anyWrapped |= stepWrapped; // update wrapped state if we actually stepped to next/prev item if (index.isValid()) { const QString &text = d->m_view->model()->data( index, d->m_role).toString(); if (findFlags & FindRegularExpression) { bool sensitive = (findFlags & FindCaseSensitively); QRegExp searchExpr = QRegExp(searchTxt, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive)); if (searchExpr.indexIn(text) != -1 && d->m_view->model()->flags(index) & Qt::ItemIsSelectable && (index.row() != currentRow || index.parent() != currentIndex.parent())) resultIndex = index; } else { QTextDocument doc(text); if (!doc.find(searchTxt, 0, flags & (FindCaseSensitively | FindWholeWords)).isNull() && d->m_view->model()->flags(index) & Qt::ItemIsSelectable && (index.row() != currentRow || index.parent() != currentIndex.parent())) resultIndex = index; } } index = followingIndex(index, backward, &stepWrapped); } while (!resultIndex.isValid() && index.isValid() && index != currentIndex); if (resultIndex.isValid()) { d->m_view->setCurrentIndex(resultIndex); d->m_view->scrollTo(resultIndex); if (resultIndex.parent().isValid()) d->m_view->expand(resultIndex.parent()); if (wrapped) *wrapped = anyWrapped; return IFindSupport::Found; } return IFindSupport::NotFound; }
IFindSupport::Result ItemViewFind::find(const QString &searchTxt, FindFlags findFlags, bool startFromCurrentIndex, bool *wrapped) { if (wrapped) *wrapped = false; if (searchTxt.isEmpty()) return IFindSupport::NotFound; if (d->m_view->model()->rowCount() <= 0) // empty model return IFindSupport::NotFound; QModelIndex currentIndex = d->m_view->currentIndex(); if (!currentIndex.isValid()) // nothing selected, start from top currentIndex = d->m_view->model()->index(0, 0); QTextDocument::FindFlags flags = textDocumentFlagsForFindFlags(findFlags); QModelIndex resultIndex; QModelIndex index = currentIndex; int currentRow = currentIndex.row(); bool sensitive = (findFlags & FindCaseSensitively); QRegExp searchExpr; if (findFlags & FindRegularExpression) { searchExpr = QRegExp(searchTxt, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive)); } else if (findFlags & FindWholeWords) { const QString escapedSearchText = QRegExp::escape(searchTxt); const QString wordBoundary = QLatin1String("\b"); searchExpr = QRegExp(wordBoundary + escapedSearchText + wordBoundary, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive)); } else { searchExpr = QRegExp(searchTxt, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive), QRegExp::FixedString); } bool backward = (flags & QTextDocument::FindBackward); if (wrapped) *wrapped = false; bool anyWrapped = false; bool stepWrapped = false; if (!startFromCurrentIndex) index = followingIndex(index, backward, &stepWrapped); else currentRow = -1; do { anyWrapped |= stepWrapped; // update wrapped state if we actually stepped to next/prev item if (index.isValid()) { const QString &text = d->m_view->model()->data( index, d->m_role).toString(); if (d->m_view->model()->flags(index) & Qt::ItemIsSelectable && (index.row() != currentRow || index.parent() != currentIndex.parent()) && searchExpr.indexIn(text) != -1) resultIndex = index; } index = followingIndex(index, backward, &stepWrapped); } while (!resultIndex.isValid() && index.isValid() && index != currentIndex); if (resultIndex.isValid()) { d->m_view->setCurrentIndex(resultIndex); d->m_view->scrollTo(resultIndex); if (resultIndex.parent().isValid()) if (QTreeView *treeView = qobject_cast<QTreeView *>(d->m_view)) treeView->expand(resultIndex.parent()); if (wrapped) *wrapped = anyWrapped; return IFindSupport::Found; } return IFindSupport::NotFound; }