mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-18 08:22:15 -07:00
improve shortcut search to split by word
This commit is contained in:
parent
38e99f2e87
commit
7e9bfd8fed
2 changed files with 20 additions and 5 deletions
|
|
@ -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,19 @@ 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);
|
||||
QStringList searchWords = searchString.split(" ", Qt::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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
#include <QTreeView>
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue