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 highlightingpage [6/9] #5889

Open
wants to merge 6 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
41 changes: 34 additions & 7 deletions src/widgets/settingspages/HighlightingPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ HighlightingPage::HighlightingPage()

// TABS
auto tabs = layout.emplace<QTabWidget>();
this->tabWidget_ = &*tabs;
{
// HIGHLIGHTS
auto highlights = tabs.appendTab(new QVBoxLayout, "Messages");
Expand All @@ -64,13 +65,14 @@ HighlightingPage::HighlightingPage()
"Message highlights are prioritized over badge highlights "
"and user highlights.");

auto *view =
EditableModelView *view =
highlights
.emplace<EditableModelView>(
(new HighlightModel(nullptr))
->initialized(
&getSettings()->highlightedMessages))
.getElement();
this->viewMessages_ = view;
view->addRegexHelpLink();
view->setTitles({"Pattern", "Show in\nMentions",
"Flash\ntaskbar", "Enable\nregex",
Expand Down Expand Up @@ -118,6 +120,7 @@ HighlightingPage::HighlightingPage()
(new UserHighlightModel(nullptr))
->initialized(&getSettings()->highlightedUsers))
.getElement();
this->viewUsers_ = view;

view->addRegexHelpLink();
view->getTableView()->horizontalHeader()->hideSection(
Expand Down Expand Up @@ -167,12 +170,14 @@ HighlightingPage::HighlightingPage()
"user badges.\n"
"Badge highlights are prioritzed under user and message "
"highlights.");
auto *view = badgeHighlights
.emplace<EditableModelView>(
(new BadgeHighlightModel(nullptr))
->initialized(
&getSettings()->highlightedBadges))
.getElement();
EditableModelView *view =
badgeHighlights
.emplace<EditableModelView>(
(new BadgeHighlightModel(nullptr))
->initialized(
&getSettings()->highlightedBadges))
.getElement();
this->viewBadges_ = view;
view->setTitles({"Name", "Show In\nMentions", "Flash\ntaskbar",
"Play\nsound", "Custom\nsound", "Color"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
Expand Down Expand Up @@ -230,6 +235,7 @@ HighlightingPage::HighlightingPage()
(new HighlightBlacklistModel(nullptr))
->initialized(&getSettings()->blacklistedUsers))
.getElement();
this->viewBlacklistedUsers_ = view;

view->addRegexHelpLink();
view->setTitles({"Username", "Enable\nregex"});
Expand Down Expand Up @@ -392,4 +398,25 @@ void HighlightingPage::tableCellClicked(const QModelIndex &clicked,
}
}

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

bool matchMessages =
this->viewMessages_->filterSearchResults(query, fields);
this->tabWidget_->setTabVisible(0, matchMessages);

bool matchUsers = this->viewUsers_->filterSearchResults(query, fields);
this->tabWidget_->setTabVisible(1, matchUsers);

bool matchBadges = this->viewBadges_->filterSearchResults(query, fields);
this->tabWidget_->setTabVisible(2, matchBadges);

bool matchBlacklistedUsers =
this->viewBlacklistedUsers_->filterSearchResults(query, fields);
this->tabWidget_->setTabVisible(3, matchBlacklistedUsers);

return matchMessages || matchUsers || matchBadges || matchBlacklistedUsers;
}

} // namespace chatterino
8 changes: 8 additions & 0 deletions src/widgets/settingspages/HighlightingPage.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "util/LayoutCreator.hpp"
#include "widgets/settingspages/SettingsPage.hpp"

#include <QAbstractTableModel>
Expand All @@ -16,10 +17,17 @@ class HighlightingPage : public SettingsPage
{
public:
HighlightingPage();
bool filterElements(const QString &query) override;

private:
enum HighlightTab { Messages = 0, Users = 1, Badges = 2, Blacklist = 3 };

QTabWidget *tabWidget_;
EditableModelView *viewMessages_;
EditableModelView *viewUsers_;
EditableModelView *viewBadges_;
EditableModelView *viewBlacklistedUsers_;

QTimer disabledUsersChangedTimer_;

void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
Expand Down
Loading