feat: Warn for commands with duplicate triggers (#4322)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
askepticaldreamer
2024-03-17 04:46:58 -07:00
committed by GitHub
parent 0322d37650
commit 46c5609736
4 changed files with 102 additions and 11 deletions
+91 -10
View File
@@ -11,6 +11,7 @@
#include "util/StandardItemHelper.hpp"
#include "widgets/helper/EditableModelView.hpp"
#include <QColor>
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
@@ -22,26 +23,73 @@
#define TEXT "{1} => first word &nbsp;&nbsp;&nbsp; {1+} => first word and after &nbsp;&nbsp;&nbsp; {{ => { &nbsp;&nbsp;&nbsp; <a href='https://chatterino.com/help/commands'>more info</a>"
// clang-format on
namespace chatterino {
namespace {
QString c1settingsPath()
using namespace chatterino;
QString c1settingsPath()
{
return combinePath(qgetenv("appdata"), "Chatterino\\Custom\\Commands.txt");
}
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++)
{
return combinePath(qgetenv("appdata"),
"Chatterino\\Custom\\Commands.txt");
QString commandTrigger =
view->getModel()->index(i, 0).data().toString();
commands[commandTrigger].push_back(i);
}
for (const auto &[commandTrigger, rowIndices] : commands)
{
assert(!rowIndices.empty());
if (rowIndices.size() > 1)
{
foundDuplicateTrigger = true;
for (const auto &rowIndex : rowIndices)
{
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);
}
}
if (foundDuplicateTrigger)
{
duplicateWarning->show();
}
else
{
duplicateWarning->hide();
}
}
} // namespace
namespace chatterino {
CommandPage::CommandPage()
{
auto *app = getApp();
LayoutCreator<CommandPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();
EditableModelView *view = layout
.emplace<EditableModelView>(
app->getCommands()->createModel(nullptr))
.getElement();
auto *view = layout
.emplace<EditableModelView>(
getIApp()->getCommands()->createModel(nullptr))
.getElement();
view->setTitles({"Trigger", "Command", "Show In\nMessage Menu"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
@@ -83,6 +131,39 @@ CommandPage::CommandPage()
text->setStyleSheet("color: #bbb");
text->setOpenExternalLinks(true);
auto *duplicateWarning =
layout
.emplace<QLabel>("Multiple commands with the same trigger found. "
"Only one of the commands will work.")
.getElement();
duplicateWarning->setStyleSheet("color: yellow");
// 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::rowsRemoved, this,
[view, duplicateWarning]() {
checkCommandDuplicates(view, duplicateWarning);
});
QObject::connect(view->getModel(), &QAbstractItemModel::dataChanged, this,
[view, 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);
// ---- end of layout
this->commandsEditTimer_.setSingleShot(true);
}
@@ -2,7 +2,6 @@
#include "widgets/settingspages/SettingsPage.hpp"
#include <QTextEdit>
#include <QTimer>
namespace chatterino {