From cc16b8779c16875f39cfffde9f148d77435accbe Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Sun, 5 Jan 2025 16:19:00 -0800 Subject: [PATCH] improve shortcut search to split by word (#5416) --- cockatrice/src/settings/shortcut_treeview.cpp | 28 ++++++++++++++++--- cockatrice/src/settings/shortcut_treeview.h | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cockatrice/src/settings/shortcut_treeview.cpp b/cockatrice/src/settings/shortcut_treeview.cpp index 58bb6dce8..02bc08164 100644 --- a/cockatrice/src/settings/shortcut_treeview.cpp +++ b/cockatrice/src/settings/shortcut_treeview.cpp @@ -10,15 +10,19 @@ ShortcutFilterProxyModel::ShortcutFilterProxyModel(QObject *parent) : QSortFilte } /** - * @return True if this row or its parent matches the search string + * Appends the parent and source row together before doing the regex match. */ bool ShortcutFilterProxyModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const { QModelIndex nameIndex = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent); QModelIndex parentIndex = sourceModel()->index(sourceParent.row(), filterKeyColumn(), sourceParent.parent()); - return sourceModel()->data(nameIndex).toString().contains(filterRegularExpression()) || - sourceModel()->data(parentIndex).toString().contains(filterRegularExpression()); + QString name = sourceModel()->data(nameIndex).toString(); + QString parentName = sourceModel()->data(parentIndex).toString(); + + QString searchedString = parentName + " " + name; + + return searchedString.contains(filterRegularExpression()); } ShortcutTreeView::ShortcutTreeView(QWidget *parent) : QTreeView(parent) @@ -140,8 +144,24 @@ void ShortcutTreeView::currentChanged(const QModelIndex ¤t, const QModelIn } } +/** + * The search string is split by word. + * A String is a match as long as it contains all the words in the search string in order + */ void ShortcutTreeView::updateSearchString(const QString &searchString) { - proxyModel->setFilterFixedString(searchString); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const auto skipEmptyParts = Qt::SkipEmptyParts; +#else + const auto skipEmptyParts = QString::SkipEmptyParts; +#endif + QStringList searchWords = searchString.split(" ", skipEmptyParts); + + auto escapeRegex = [](const QString &s) { return QRegularExpression::escape(s); }; + std::transform(searchWords.begin(), searchWords.end(), searchWords.begin(), escapeRegex); + + auto regex = QRegularExpression(searchWords.join(".*"), QRegularExpression::CaseInsensitiveOption); + + proxyModel->setFilterRegularExpression(regex); expandAll(); } diff --git a/cockatrice/src/settings/shortcut_treeview.h b/cockatrice/src/settings/shortcut_treeview.h index 19c9327e7..dc96d0360 100644 --- a/cockatrice/src/settings/shortcut_treeview.h +++ b/cockatrice/src/settings/shortcut_treeview.h @@ -7,7 +7,7 @@ #include /** - * Custom implementation of QSortFilterProxyModel that also searches in the parent's string when filtering + * Custom implementation of QSortFilterProxyModel that appends the source and parent strings together when filtering */ class ShortcutFilterProxyModel : public QSortFilterProxyModel {