#include "commandpage.hpp" #include #include #include #include #include #include "singletons/commandmanager.hpp" #include "util/layoutcreator.hpp" #include "util/standarditemhelper.hpp" //#include "widgets/helper/comboboxitemdelegate.hpp" // clang-format off #define TEXT "{1} => first word, {2} => second word, ...\n"\ "{1+} => first word and after, {2+} => second word and after, ...\n"\ "{{1} => {1}" // clang-format on namespace chatterino { namespace widgets { namespace settingspages { CommandPage::CommandPage() : SettingsPage("Commands", ":/images/commands.svg") { auto &settings = singletons::SettingManager::getInstance(); util::LayoutCreator layoutCreator(this); auto layout = layoutCreator.emplace().withoutMargin(); QTableView *view = *layout.emplace(); QStandardItemModel *model = new QStandardItemModel(0, 2, view); view->setModel(model); model->setHeaderData(0, Qt::Horizontal, "Trigger"); model->setHeaderData(1, Qt::Horizontal, "Command"); view->setSelectionMode(QAbstractItemView::ExtendedSelection); view->setSelectionBehavior(QAbstractItemView::SelectRows); view->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); for (const QString &string : singletons::CommandManager::getInstance().getCommands()) { int index = string.indexOf(' '); if (index == -1) { model->appendRow({util::stringItem(string), util::stringItem("")}); } else { model->appendRow( {util::stringItem(string.mid(0, index)), util::stringItem(string.mid(index + 1))}); } } QObject::connect( model, &QStandardItemModel::dataChanged, [model](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { QStringList list; for (int i = 0; i < model->rowCount(); i++) { QString command = model->item(i, 0)->data(Qt::EditRole).toString(); // int index = command.indexOf(' '); // if (index != -1) { // command = command.mid(index); // } list.append(command + " " + model->item(i, 1)->data(Qt::EditRole).toString()); } singletons::CommandManager::getInstance().setCommands(list); }); auto buttons = layout.emplace().withoutMargin(); { auto add = buttons.emplace("Add"); QObject::connect(*add, &QPushButton::clicked, [model, view] { model->appendRow({util::stringItem("/command"), util::stringItem("")}); view->scrollToBottom(); }); auto remove = buttons.emplace("Remove"); QObject::connect(*remove, &QPushButton::clicked, [view, model] { std::vector indices; for (const QModelIndex &index : view->selectionModel()->selectedRows(0)) { indices.push_back(index.row()); } std::sort(indices.begin(), indices.end()); for (int i = indices.size() - 1; i >= 0; i--) { model->removeRow(indices[i]); } }); buttons->addStretch(1); } layout.append(this->createCheckBox("Also match the trigger at the end of the message", settings.allowCommandsAtEnd)); QLabel *text = *layout.emplace(TEXT); text->setWordWrap(true); text->setStyleSheet("color: #bbb"); // ---- end of layout this->commandsEditTimer.setSingleShot(true); } QTextEdit *CommandPage::getCommandsTextEdit() { singletons::CommandManager &commandManager = singletons::CommandManager::getInstance(); // cancel QStringList currentCommands = commandManager.getCommands(); this->onCancel.connect( [currentCommands, &commandManager] { commandManager.setCommands(currentCommands); }); // create text edit QTextEdit *textEdit = new QTextEdit; textEdit->setPlainText(QString(commandManager.getCommands().join('\n'))); QObject::connect(textEdit, &QTextEdit::textChanged, [this] { this->commandsEditTimer.start(200); }); QObject::connect(&this->commandsEditTimer, &QTimer::timeout, [textEdit, &commandManager] { QString text = textEdit->toPlainText(); QStringList lines = text.split(QRegularExpression("(\r?\n|\r\n?)")); commandManager.setCommands(lines); }); return textEdit; } } // namespace settingspages } // namespace widgets } // namespace chatterino