Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Search KeyboardSettingsPage [1/9] #5884

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Minor: When (re-)connecting, visible channels are now joined first. (#5850)
- Minor: Added the ability to filter on messages by the author's user ID (example: `author.user_id == "22484632"`). (#5862)
- Minor: Improved error messaging of the `/clip` command. (#5879)
- Minor: Added searchable hotkeys to global search and make keybinds searchable in the Hotkeys settings. (#5884)
- Bugfix: Fixed a potential way to escape the Lua Plugin sandbox. (#5846)
- Bugfix: Fixed a crash relating to Lua HTTP. (#5800)
- Bugfix: Fixed a crash that could occur on Linux and macOS when clicking "Install" from the update prompt. (#5818)
Expand Down
56 changes: 56 additions & 0 deletions src/widgets/helper/EditableModelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,62 @@ void EditableModelView::addRegexHelpLink()
this->addCustomButton(regexHelpLabel);
}

bool EditableModelView::filterSearchResults(const QString &query,
std::span<const int> columnSelect)
{
bool searchFoundSomething = false;
auto rowAmount = this->model_->rowCount();

// make sure to show the page even if the table is empty,
// but only if we aren't search something
if (rowAmount == 0 && query.isEmpty())
{
return true;
}

for (int i = 0; i < rowAmount; i++)
{
tableView_->hideRow(i);
}
for (int j : columnSelect)
{
for (int i = 0; i < rowAmount; i++)
{
QModelIndex idx = model_->index(i, j);
QVariant a = model_->data(idx);
if (a.toString().contains(query, Qt::CaseInsensitive))
{
tableView_->showRow(i);
searchFoundSomething = true;
}
}
}
return searchFoundSomething;
}

void EditableModelView::filterSearchResultsHotkey(
const QKeySequence &keySequenceQuery)
{
auto rowAmount = this->model_->rowCount();
for (int i = 0; i < rowAmount; i++)
{
tableView_->hideRow(i);
}
for (int i = 0; i < rowAmount; i++)
{
QModelIndex idx = model_->index(i, 1);
QVariant a = model_->data(idx);
auto seq = qvariant_cast<QKeySequence>(a);

// todo: Make this fuzzy match, right now only exact matches happen
// so ctrl+f won't match ctrl+shift+f shortcuts
if (keySequenceQuery.matches(seq) != QKeySequence::NoMatch)
{
tableView_->showRow(i);
}
}
}

void EditableModelView::moveRow(int dir)
{
auto selected = this->getTableView()->selectionModel()->selectedRows(0);
Expand Down
7 changes: 7 additions & 0 deletions src/widgets/helper/EditableModelView.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once

#include <pajlada/signals/signal.hpp>
#include <QKeySequence>
#include <QWidget>

#include <span>

class QAbstractTableModel;
class QTableView;
class QHBoxLayout;
Expand All @@ -25,6 +28,10 @@ class EditableModelView : public QWidget
void addCustomButton(QWidget *widget);
void addRegexHelpLink();

bool filterSearchResults(const QString &query,
std::span<const int> columnSelect);
void filterSearchResultsHotkey(const QKeySequence &keySequenceQuery);

private:
QTableView *tableView_{};
QAbstractTableModel *model_{};
Expand Down
27 changes: 26 additions & 1 deletion src/widgets/settingspages/KeyboardSettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

#include <QFormLayout>
#include <QHeaderView>
#include <QKeySequenceEdit>
#include <QLabel>
#include <QMessageBox>
#include <QTableView>

#include <array>

namespace {

using namespace chatterino;
Expand Down Expand Up @@ -51,6 +54,7 @@ KeyboardSettingsPage::KeyboardSettingsPage()
auto *model = getApp()->getHotkeys()->createModel(nullptr);
EditableModelView *view =
layout.emplace<EditableModelView>(model).getElement();
this->view_ = view;

view->setTitles({"Hotkey name", "Keybinding"});
view->getTableView()->horizontalHeader()->setVisible(true);
Expand All @@ -73,11 +77,25 @@ KeyboardSettingsPage::KeyboardSettingsPage()
}
});

QObject::connect(view->getTableView(), &QTableView::doubleClicked,
QObject::connect(view_->getTableView(), &QTableView::doubleClicked,
[view, model](const QModelIndex &clicked) {
tableCellClicked(clicked, view, model);
});

auto *keySequenceInput = new QKeySequenceEdit(this);

#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
keySequenceInput->setClearButtonEnabled(true);
#endif
auto *searchText = new QLabel("Search keybind:", this);

QObject::connect(keySequenceInput, &QKeySequenceEdit::keySequenceChanged,
[view](const QKeySequence &keySequence) {
view->filterSearchResultsHotkey(keySequence);
});
view->addCustomButton(searchText);
view->addCustomButton(keySequenceInput);

auto *resetEverything = new QPushButton("Reset to defaults");
QObject::connect(resetEverything, &QPushButton::clicked, [this]() {
auto reply = QMessageBox::question(
Expand Down Expand Up @@ -129,4 +147,11 @@ KeyboardSettingsPage::KeyboardSettingsPage()
}
}

bool KeyboardSettingsPage::filterElements(const QString &query)
{
std::array fields{0};

return this->view_->filterSearchResults(query, fields);
}

} // namespace chatterino
4 changes: 4 additions & 0 deletions src/widgets/settingspages/KeyboardSettingsPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class KeyboardSettingsPage : public SettingsPage
{
public:
KeyboardSettingsPage();
bool filterElements(const QString &query) override;

private:
EditableModelView *view_;
};

} // namespace chatterino
Loading