Skip to content

Commit

Permalink
Added more pages to the global search function
Browse files Browse the repository at this point in the history
  • Loading branch information
apa420 committed Jan 30, 2025
1 parent ae11191 commit f2fa585
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 181 deletions.
23 changes: 17 additions & 6 deletions src/widgets/helper/EditableModelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,37 @@ void EditableModelView::addRegexHelpLink()
this->addCustomButton(regexHelpLabel);
}

void EditableModelView::filterSearchResults(QString *query,
std::vector<int> *columnSelect)
bool EditableModelView::filterSearchResults(const QString &query,
std::vector<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_->showRow(i);
tableView_->hideRow(i);
}
for (int j : *columnSelect)
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))
if (a.toString().contains(query, Qt::CaseInsensitive))
{
tableView_->hideRow(i);
tableView_->showRow(i);
searchFoundSomething = true;
}
}
}
return searchFoundSomething;
}

void EditableModelView::filterSearchResultsHotkey(
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/helper/EditableModelView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class EditableModelView : public QWidget
void addCustomButton(QWidget *widget);
void addRegexHelpLink();

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

private:
Expand Down
61 changes: 35 additions & 26 deletions src/widgets/settingspages/CommandPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ QString c1settingsPath()
return combinePath(qgetenv("appdata"), "Chatterino\\Custom\\Commands.txt");
}

void checkCommandDuplicates(EditableModelView *view, QLabel *duplicateWarning)
void checkCommandDuplicates(EditableModelView *view_, QLabel *duplicateWarning)
{
bool foundDuplicateTrigger = false;

// Maps command triggers to model row indices
std::unordered_map<QString, std::vector<int>> commands;

for (int i = 0; i < view->getModel()->rowCount(); i++)
for (int i = 0; i < view_->getModel()->rowCount(); i++)
{
QString commandTrigger =
view->getModel()->index(i, 0).data().toString();
view_->getModel()->index(i, 0).data().toString();
commands[commandTrigger].push_back(i);
}

Expand All @@ -57,14 +57,16 @@ void checkCommandDuplicates(EditableModelView *view, QLabel *duplicateWarning)

for (const auto &rowIndex : rowIndices)
{
view->getModel()->setData(view->getModel()->index(rowIndex, 0),
QColor("yellow"), Qt::ForegroundRole);
view_->getModel()->setData(
view_->getModel()->index(rowIndex, 0), QColor("yellow"),
Qt::ForegroundRole);
}
}
else
{
view->getModel()->setData(view->getModel()->index(rowIndices[0], 0),
QColor("white"), Qt::ForegroundRole);
view_->getModel()->setData(
view_->getModel()->index(rowIndices[0], 0), QColor("white"),
Qt::ForegroundRole);
}
}

Expand All @@ -87,16 +89,16 @@ CommandPage::CommandPage()
LayoutCreator<CommandPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();

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

view->setTitles({"Trigger", "Command", "Show In\nMessage Menu"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
view_->setTitles({"Trigger", "Command", "Show In\nMessage Menu"});
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 view_
std::ignore = view_->addButtonPressed.connect([] {
getApp()->getCommands()->items.append(
Command{"/command", "I made a new command HeyGuys"});
});
Expand All @@ -105,7 +107,7 @@ CommandPage::CommandPage()
if (QFile(c1settingsPath()).exists())
{
auto *button = new QPushButton("Import commands from Chatterino 1");
view->addCustomButton(button);
view_->addCustomButton(button);

QObject::connect(button, &QPushButton::clicked, this, [] {
QFile c1settings(c1settingsPath());
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)
{
auto *fields = new std::vector<int>{0, 1};

return 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

0 comments on commit f2fa585

Please sign in to comment.