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 commandpage [8/9] #5891

Open
wants to merge 3 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
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
5 changes: 5 additions & 0 deletions src/widgets/helper/EditableModelView.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

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

class QAbstractTableModel;
Expand All @@ -25,6 +26,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
37 changes: 23 additions & 14 deletions src/widgets/settingspages/CommandPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ CommandPage::CommandPage()
LayoutCreator<CommandPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();

auto *view = layout
.emplace<EditableModelView>(
getApp()->getCommands()->createModel(nullptr))
.getElement();
EditableModelView *view =
layout
.emplace<EditableModelView>(
getApp()->getCommands()->createModel(nullptr))
.getElement();
this->view_ = view;

view->setTitles({"Trigger", "Command", "Show In\nMessage Menu"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
Expand Down Expand Up @@ -137,32 +139,39 @@ CommandPage::CommandPage()

// NOTE: These signals mean that the duplicate check happens in the middle of a row being moved, where he index can be wrong.
// This should be reconsidered, or potentially changed in the signalvectormodel. Or maybe we rely on a SignalVectorModel signal instead
QObject::connect(view->getModel(), &QAbstractItemModel::rowsInserted, this,
[view, duplicateWarning]() {
checkCommandDuplicates(view, duplicateWarning);
QObject::connect(view_->getModel(), &QAbstractItemModel::rowsInserted, this,
[this, duplicateWarning]() {
checkCommandDuplicates(view_, duplicateWarning);
});

QObject::connect(view->getModel(), &QAbstractItemModel::rowsRemoved, this,
[view, duplicateWarning]() {
checkCommandDuplicates(view, duplicateWarning);
QObject::connect(view_->getModel(), &QAbstractItemModel::rowsRemoved, this,
[this, duplicateWarning]() {
checkCommandDuplicates(view_, duplicateWarning);
});

QObject::connect(view->getModel(), &QAbstractItemModel::dataChanged, this,
[view, duplicateWarning](const QModelIndex &topLeft,
QObject::connect(view_->getModel(), &QAbstractItemModel::dataChanged, this,
[this, duplicateWarning](const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles) {
(void)topLeft;
(void)bottomRight;
if (roles.contains(Qt::EditRole))
{
checkCommandDuplicates(view, duplicateWarning);
checkCommandDuplicates(view_, duplicateWarning);
}
});

checkCommandDuplicates(view, duplicateWarning);
checkCommandDuplicates(view_, duplicateWarning);

// ---- end of layout
this->commandsEditTimer_.setSingleShot(true);
}

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

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

} // namespace chatterino
4 changes: 4 additions & 0 deletions src/widgets/settingspages/CommandPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

namespace chatterino {

class EditableModelView;

class CommandPage : public SettingsPage
{
public:
CommandPage();
bool filterElements(const QString &query) override;

private:
QTimer commandsEditTimer_;
EditableModelView *view_;
};

} // namespace chatterino
Loading