mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
improve shortcut search to split by word (#5416)
This commit is contained in:
parent
62f7c7f9ce
commit
cc16b8779c
2 changed files with 25 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,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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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