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 nicknamespage [3/9] #5886

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
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
39 changes: 23 additions & 16 deletions src/widgets/settingspages/NicknamesPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,38 @@ NicknamesPage::NicknamesPage()
"filters."
"\nWith those features you will still need to use the user's original "
"name.");
EditableModelView *view =
layout
.emplace<EditableModelView>(
(new NicknamesModel(nullptr))
->initialized(&getSettings()->nicknames))
.getElement();

view->setTitles({"Username", "Nickname", "Enable regex", "Case-sensitive"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
this->view_ = layout
.emplace<EditableModelView>(
(new NicknamesModel(nullptr))
->initialized(&getSettings()->nicknames))
.getElement();

this->view_->setTitles(
{"Username", "Nickname", "Enable regex", "Case-sensitive"});
this->view_->getTableView()->horizontalHeader()->setSectionResizeMode(
QHeaderView::Fixed);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
this->view_->getTableView()->horizontalHeader()->setSectionResizeMode(
0, QHeaderView::Stretch);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
this->view_->getTableView()->horizontalHeader()->setSectionResizeMode(
1, QHeaderView::Stretch);

// We can safely ignore this signal connection since we own the view
std::ignore = view->addButtonPressed.connect([] {
// We can safely ignore this signal connection since we own the this->view_
std::ignore = this->view_->addButtonPressed.connect([] {
getSettings()->nicknames.append(
Nickname{"Username", "Nickname", false, false});
});

QTimer::singleShot(1, [view] {
view->getTableView()->resizeColumnsToContents();
view->getTableView()->setColumnWidth(0, 200);
QTimer::singleShot(1, [this] {
this->view_->getTableView()->resizeColumnsToContents();
this->view_->getTableView()->setColumnWidth(0, 200);
});
}

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

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

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

namespace chatterino {

class EditableModelView;

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

private:
EditableModelView *view_;
};

} // namespace chatterino
Loading