2018-01-12 23:09:05 +01:00
|
|
|
#include "commandpage.hpp"
|
|
|
|
|
|
|
|
#include <QLabel>
|
2018-04-27 01:11:09 +02:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QTableView>
|
2018-01-12 23:09:05 +01:00
|
|
|
#include <QTextEdit>
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include "application.hpp"
|
2018-01-12 23:09:05 +01:00
|
|
|
#include "singletons/commandmanager.hpp"
|
2018-01-16 00:26:04 +01:00
|
|
|
#include "util/layoutcreator.hpp"
|
2018-04-27 01:11:09 +02:00
|
|
|
#include "util/standarditemhelper.hpp"
|
|
|
|
//#include "widgets/helper/comboboxitemdelegate.hpp"
|
2018-01-12 23:09:05 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QTextEdit>
|
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
// clang-format off
|
2018-04-27 01:11:09 +02:00
|
|
|
#define TEXT "{1} => first word, {2} => second word, ...\n"\
|
|
|
|
"{1+} => first word and after, {2+} => second word and after, ...\n"\
|
|
|
|
"{{1} => {1}"
|
2018-01-12 23:09:05 +01:00
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
namespace settingspages {
|
|
|
|
|
|
|
|
CommandPage::CommandPage()
|
2018-04-26 23:07:02 +02:00
|
|
|
: SettingsPage("Commands", ":/images/commands.svg")
|
2018-01-12 23:09:05 +01:00
|
|
|
{
|
2018-04-28 13:48:49 +02:00
|
|
|
auto app = getApp();
|
2018-04-27 01:11:09 +02:00
|
|
|
|
2018-01-12 23:09:05 +01:00
|
|
|
util::LayoutCreator<CommandPage> layoutCreator(this);
|
2018-01-13 00:04:47 +01:00
|
|
|
auto layout = layoutCreator.emplace<QVBoxLayout>().withoutMargin();
|
2018-01-12 23:09:05 +01:00
|
|
|
|
2018-04-27 01:11:09 +02:00
|
|
|
QTableView *view = *layout.emplace<QTableView>();
|
|
|
|
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);
|
|
|
|
|
2018-04-28 13:48:49 +02:00
|
|
|
for (const QString &string : app->commands->getCommands()) {
|
2018-04-27 01:11:09 +02:00
|
|
|
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<int> &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());
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:48:49 +02:00
|
|
|
getApp()->commands->setCommands(list);
|
2018-04-27 01:11:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
auto buttons = layout.emplace<QHBoxLayout>().withoutMargin();
|
|
|
|
{
|
|
|
|
auto add = buttons.emplace<QPushButton>("Add");
|
|
|
|
QObject::connect(*add, &QPushButton::clicked, [model, view] {
|
|
|
|
model->appendRow({util::stringItem("/command"), util::stringItem("")});
|
|
|
|
view->scrollToBottom();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto remove = buttons.emplace<QPushButton>("Remove");
|
|
|
|
QObject::connect(*remove, &QPushButton::clicked, [view, model] {
|
|
|
|
std::vector<int> 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",
|
2018-04-28 13:48:49 +02:00
|
|
|
app->settings->allowCommandsAtEnd));
|
2018-01-12 23:09:05 +01:00
|
|
|
|
2018-04-27 01:11:09 +02:00
|
|
|
QLabel *text = *layout.emplace<QLabel>(TEXT);
|
|
|
|
text->setWordWrap(true);
|
|
|
|
text->setStyleSheet("color: #bbb");
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
// ---- end of layout
|
|
|
|
this->commandsEditTimer.setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEdit *CommandPage::getCommandsTextEdit()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
// cancel
|
2018-04-27 22:11:19 +02:00
|
|
|
QStringList currentCommands = app->commands->getCommands();
|
2018-01-12 23:09:05 +01:00
|
|
|
|
2018-04-28 13:48:49 +02:00
|
|
|
this->onCancel.connect([currentCommands, app] { app->commands->setCommands(currentCommands); });
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
// create text edit
|
|
|
|
QTextEdit *textEdit = new QTextEdit;
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
textEdit->setPlainText(QString(app->commands->getCommands().join('\n')));
|
2018-01-12 23:09:05 +01:00
|
|
|
|
|
|
|
QObject::connect(textEdit, &QTextEdit::textChanged,
|
|
|
|
[this] { this->commandsEditTimer.start(200); });
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
QObject::connect(&this->commandsEditTimer, &QTimer::timeout, [textEdit, app] {
|
2018-01-12 23:09:05 +01:00
|
|
|
QString text = textEdit->toPlainText();
|
|
|
|
QStringList lines = text.split(QRegularExpression("(\r?\n|\r\n?)"));
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
app->commands->setCommands(lines);
|
2018-01-12 23:09:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return textEdit;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace settingspages
|
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|