From 8693203c6d4aced3b2b23b9ae33527616387733e Mon Sep 17 00:00:00 2001 From: fourtf Date: Thu, 4 Jan 2018 04:03:51 +0100 Subject: [PATCH] added command tab to settings but they don't save --- src/singletons/commandmanager.cpp | 8 ++++---- src/singletons/commandmanager.hpp | 2 +- src/widgets/helper/splitinput.cpp | 9 ++++++++- src/widgets/settingsdialog.cpp | 33 +++++++++++++++++++++++++++++-- src/widgets/settingsdialog.hpp | 2 ++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/singletons/commandmanager.cpp b/src/singletons/commandmanager.cpp index dfc41b07c..998449967 100644 --- a/src/singletons/commandmanager.cpp +++ b/src/singletons/commandmanager.cpp @@ -38,9 +38,9 @@ void CommandManager::setCommands(const QStringList &_commands) continue; } - if (command.at(0) != '/') { - command = QString("/") + command; - } + // if (command.at(0) != '/') { + // command = QString("/") + command; + // } QString commandName = command.mid(0, command.indexOf(' ')); @@ -58,7 +58,7 @@ QStringList CommandManager::getCommands() return this->commandsStringList; } -QString CommandManager::execCommand(QString text) +QString CommandManager::execCommand(QString text, bool dryRun) { Command command; QStringList words = text.split(' ', QString::SkipEmptyParts); diff --git a/src/singletons/commandmanager.hpp b/src/singletons/commandmanager.hpp index 86e2c3239..ddddb57e3 100644 --- a/src/singletons/commandmanager.hpp +++ b/src/singletons/commandmanager.hpp @@ -18,7 +18,7 @@ class CommandManager public: static CommandManager &getInstance(); - QString execCommand(QString text); + QString execCommand(QString text, bool dryRun); void loadCommands(); void saveCommands(); diff --git a/src/widgets/helper/splitinput.cpp b/src/widgets/helper/splitinput.cpp index 4ec8e6274..af02ea872 100644 --- a/src/widgets/helper/splitinput.cpp +++ b/src/widgets/helper/splitinput.cpp @@ -1,4 +1,5 @@ #include "widgets/helper/splitinput.hpp" +#include "singletons/commandmanager.hpp" #include "singletons/completionmanager.hpp" #include "singletons/ircmanager.hpp" #include "singletons/settingsmanager.hpp" @@ -82,7 +83,11 @@ SplitInput::SplitInput(Split *_chatWidget) } QString message = textInput.toPlainText(); - c->sendMessage(message.replace('\n', ' ')); + QString sendMessage = + singletons::CommandManager::getInstance().execCommand(message, false); + sendMessage = sendMessage.replace('\n', ' '); + + c->sendMessage(sendMessage); prevMsg.append(message); event->accept(); @@ -229,6 +234,8 @@ void SplitInput::editTextChanged() static QRegularExpression spaceRegex("\\s\\s+"); text = text.replace(spaceRegex, " "); + text = singletons::CommandManager::getInstance().execCommand(text, true); + QString labelText; if (text.length() == 0) { diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 5032ed5bf..5788162b2 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -2,6 +2,7 @@ #include "const.hpp" #include "debug/log.hpp" #include "singletons/accountmanager.hpp" +#include "singletons/commandmanager.hpp" #include "singletons/windowmanager.hpp" #include "twitch/twitchmessagebuilder.hpp" #include "twitch/twitchuser.hpp" @@ -87,8 +88,7 @@ void SettingsDialog::addTabs() this->addTab(this->createBehaviourTab(), "Behaviour", ":/images/behave.svg"); - // this->addTab(this->createCommandsTab(), "Commands", - // ":/images/CustomActionEditor_16x.png"); + this->addTab(this->createCommandsTab(), "Commands", ":/images/CustomActionEditor_16x.png"); this->addTab(this->createEmotesTab(), "Emotes", ":/images/emote.svg"); @@ -361,8 +361,37 @@ QVBoxLayout *SettingsDialog::createBehaviourTab() QVBoxLayout *SettingsDialog::createCommandsTab() { + singletons::CommandManager &commandManager = singletons::CommandManager::getInstance(); + + this->commandsTextChangedDelay.setSingleShot(true); + auto layout = this->createTabLayout(); + layout->addWidget(new QLabel("One command per line. Commands don't save right now")); + layout->addWidget(new QLabel("\"/cmd example command\" will print " + "\"example command\" when you type /cmd in chat.")); + layout->addWidget(new QLabel("{1} will be replaced with the first word you type after the " + "command, {2} with the second and so on.")); + layout->addWidget(new QLabel("{1+} will be replaced with first word and everything after, {2+} " + "with everything after the second word and so on")); + layout->addWidget(new QLabel("Duplicate commands will be ignored.")); + + QTextEdit *textEdit = new QTextEdit(); + textEdit->setPlainText(QString(commandManager.getCommands().join('\n'))); + + layout->addWidget(textEdit); + + QObject::connect(textEdit, &QTextEdit::textChanged, + [this] { this->commandsTextChangedDelay.start(200); }); + + QObject::connect(&this->commandsTextChangedDelay, &QTimer::timeout, + [textEdit, &commandManager] { + QString text = textEdit->toPlainText(); + QStringList lines = text.split(QRegularExpression("(\r?\n|\r\n?)")); + + commandManager.setCommands(lines); + }); + return layout; } diff --git a/src/widgets/settingsdialog.hpp b/src/widgets/settingsdialog.hpp index 2e1092f02..d8e4bda50 100644 --- a/src/widgets/settingsdialog.hpp +++ b/src/widgets/settingsdialog.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ private: std::vector tabs; pajlada::Settings::Setting usernameDisplayMode; + QTimer commandsTextChangedDelay; struct { QVBoxLayout tabs;