mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
implement search bar in shortcuts menu (#5285)
* implement search bar in shortcuts menu * remove unneeded imports * use expandAll
This commit is contained in:
parent
a0e5871c6e
commit
5bdbd51fa8
5 changed files with 197 additions and 75 deletions
132
cockatrice/src/settings/shortcut_treeview.cpp
Normal file
132
cockatrice/src/settings/shortcut_treeview.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#include "shortcut_treeview.h"
|
||||
|
||||
#include "cache_settings.h"
|
||||
#include "shortcuts_settings.h"
|
||||
|
||||
#include <QHeaderView>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
ShortcutTreeView::ShortcutTreeView(QWidget *parent) : QTreeView(parent)
|
||||
{
|
||||
// model
|
||||
shortcutsModel = new QStandardItemModel(this);
|
||||
shortcutsModel->setColumnCount(3);
|
||||
populateShortcutsModel();
|
||||
|
||||
// filter proxy
|
||||
proxyModel = new QSortFilterProxyModel(this);
|
||||
proxyModel->setRecursiveFilteringEnabled(true);
|
||||
proxyModel->setSourceModel(shortcutsModel);
|
||||
proxyModel->setDynamicSortFilter(true);
|
||||
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
||||
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
||||
proxyModel->setFilterKeyColumn(0);
|
||||
|
||||
QTreeView::setModel(proxyModel);
|
||||
|
||||
// treeview
|
||||
hideColumn(2);
|
||||
|
||||
setUniformRowHeights(true);
|
||||
setAlternatingRowColors(true);
|
||||
|
||||
setSortingEnabled(true);
|
||||
proxyModel->sort(0, Qt::AscendingOrder);
|
||||
|
||||
header()->setSectionResizeMode(QHeaderView::Interactive);
|
||||
header()->setSortIndicator(0, Qt::AscendingOrder);
|
||||
setSelectionMode(SingleSelection);
|
||||
setSelectionBehavior(SelectRows);
|
||||
|
||||
expandAll();
|
||||
|
||||
connect(&SettingsCache::instance().shortcuts(), &ShortcutsSettings::shortCutChanged, this,
|
||||
&ShortcutTreeView::refreshShortcuts);
|
||||
}
|
||||
|
||||
void ShortcutTreeView::populateShortcutsModel()
|
||||
{
|
||||
QHash<QString, QStandardItem *> parentItems;
|
||||
QStandardItem *curParent = nullptr;
|
||||
for (const auto &key : SettingsCache::instance().shortcuts().getAllShortcutKeys()) {
|
||||
QString name = SettingsCache::instance().shortcuts().getShortcut(key).getName();
|
||||
QString group = SettingsCache::instance().shortcuts().getShortcut(key).getGroupName();
|
||||
QString shortcut = SettingsCache::instance().shortcuts().getShortcutString(key);
|
||||
|
||||
if (parentItems.contains(group)) {
|
||||
curParent = parentItems.value(group);
|
||||
} else {
|
||||
curParent = new QStandardItem(group);
|
||||
static QFont font = curParent->font();
|
||||
font.setBold(true);
|
||||
curParent->setFont(font);
|
||||
parentItems.insert(group, curParent);
|
||||
}
|
||||
|
||||
QList<QStandardItem *> list = {};
|
||||
list << new QStandardItem(name) << new QStandardItem(shortcut) << new QStandardItem(key);
|
||||
curParent->appendRow(list);
|
||||
}
|
||||
|
||||
for (const auto &parent : parentItems) {
|
||||
shortcutsModel->appendRow(parent);
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutTreeView::retranslateUi()
|
||||
{
|
||||
shortcutsModel->setHeaderData(0, Qt::Horizontal, tr("Action"));
|
||||
shortcutsModel->setHeaderData(1, Qt::Horizontal, tr("Shortcut"));
|
||||
refreshShortcuts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops over the model and reloads all rows
|
||||
*/
|
||||
static void loopOverModel(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex())
|
||||
{
|
||||
for (int r = 0; r < model->rowCount(parent); ++r) {
|
||||
const auto friendlyNameIndex = model->index(r, 0, parent);
|
||||
|
||||
if (model->hasChildren(friendlyNameIndex)) {
|
||||
const auto childIndex = model->index(0, 2, friendlyNameIndex);
|
||||
const auto key = model->data(childIndex).toString();
|
||||
const auto shortcutGroupName = SettingsCache::instance().shortcuts().getShortcut(key).getGroupName();
|
||||
model->setData(friendlyNameIndex, shortcutGroupName);
|
||||
|
||||
loopOverModel(model, friendlyNameIndex);
|
||||
} else {
|
||||
const auto shortcutSequenceIndex = model->index(r, 1, parent);
|
||||
const auto keyIndex = model->index(r, 2, parent);
|
||||
const auto key = model->data(keyIndex).toString();
|
||||
|
||||
const auto shortcutKey = SettingsCache::instance().shortcuts().getShortcut(key).getName();
|
||||
const auto shortcutSequence = SettingsCache::instance().shortcuts().getShortcutString(key);
|
||||
model->setData(friendlyNameIndex, shortcutKey);
|
||||
model->setData(shortcutSequenceIndex, shortcutSequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutTreeView::refreshShortcuts()
|
||||
{
|
||||
loopOverModel(shortcutsModel);
|
||||
}
|
||||
|
||||
void ShortcutTreeView::currentChanged(const QModelIndex ¤t, const QModelIndex & /* previous */)
|
||||
{
|
||||
QTreeView::scrollTo(current, QAbstractItemView::EnsureVisible);
|
||||
if (current.parent().isValid()) {
|
||||
auto shortcutName = model()->data(model()->index(current.row(), 2, current.parent())).toString();
|
||||
emit currentItemChanged(shortcutName);
|
||||
} else {
|
||||
// emit empty string if the selection is a category header
|
||||
emit currentItemChanged("");
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutTreeView::updateSearchString(const QString &searchString)
|
||||
{
|
||||
proxyModel->setFilterFixedString(searchString);
|
||||
expandAll();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue