mirror-chatterino2/src/widgets/splits/SplitInput.cpp

1087 lines
33 KiB
C++
Raw Normal View History

#include "widgets/splits/SplitInput.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "common/QLogging.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/commands/CommandController.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
#include "messages/Link.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
#include "util/Clamp.hpp"
#include "util/Helpers.hpp"
2018-06-26 14:09:39 +02:00
#include "util/LayoutCreator.hpp"
#include "widgets/dialogs/EmotePopup.hpp"
#include "widgets/helper/ChannelView.hpp"
#include "widgets/helper/EffectLabel.hpp"
#include "widgets/helper/ResizingTextEdit.hpp"
#include "widgets/Notebook.hpp"
#include "widgets/Scrollbar.hpp"
#include "widgets/splits/InputCompletionPopup.hpp"
#include "widgets/splits/Split.hpp"
#include "widgets/splits/SplitContainer.hpp"
#include "widgets/splits/SplitInput.hpp"
2017-01-01 02:30:42 +01:00
#include <QCompleter>
2017-01-18 04:52:47 +01:00
#include <QPainter>
#include <QSignalBlocker>
2017-01-18 04:52:47 +01:00
#include <functional>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
SplitInput::SplitInput(Split *_chatWidget, bool enableInlineReplying)
: SplitInput(_chatWidget, _chatWidget, _chatWidget->view_,
enableInlineReplying)
{
}
SplitInput::SplitInput(QWidget *parent, Split *_chatWidget,
ChannelView *_channelView, bool enableInlineReplying)
: BaseWidget(parent)
2018-06-06 18:57:22 +02:00
, split_(_chatWidget)
, channelView_(_channelView)
, enableInlineReplying_(enableInlineReplying)
2017-01-01 02:30:42 +01:00
{
this->installEventFilter(this);
2018-01-25 20:49:49 +01:00
this->initLayout();
2018-08-06 21:17:03 +02:00
auto completer =
new QCompleter(&this->split_->getChannel().get()->completionModel);
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setCompleter(completer);
2018-01-25 20:49:49 +01:00
this->signalHolder_.managedConnect(this->split_->channelChanged, [this] {
auto channel = this->split_->getChannel();
auto completer = new QCompleter(&channel->completionModel);
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setCompleter(completer);
});
2018-01-25 20:49:49 +01:00
// misc
this->installKeyPressedEvent();
this->addShortcuts();
this->ui_.textEdit->focusLost.connect([this] {
this->hideCompletionPopup();
});
2018-11-21 21:37:41 +01:00
this->scaleChangedEvent(this->scale());
this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated,
[this]() {
this->clearShortcuts();
this->addShortcuts();
});
2018-01-25 20:49:49 +01:00
}
2018-01-25 20:49:49 +01:00
void SplitInput::initLayout()
{
auto app = getApp();
2018-06-26 17:06:17 +02:00
LayoutCreator<SplitInput> layoutCreator(this);
2018-06-06 18:57:22 +02:00
auto layout =
layoutCreator.setLayoutType<QVBoxLayout>().withoutMargin().assign(
&this->ui_.vbox);
// reply label stuff
auto replyWrapper =
layout.emplace<QWidget>().assign(&this->ui_.replyWrapper);
this->ui_.replyWrapper->setContentsMargins(0, 0, 0, 0);
auto replyHbox = replyWrapper.emplace<QHBoxLayout>().withoutMargin().assign(
&this->ui_.replyHbox);
auto replyLabel = replyHbox.emplace<QLabel>().assign(&this->ui_.replyLabel);
replyLabel->setAlignment(Qt::AlignLeft);
replyLabel->setFont(
app->fonts->getFont(FontStyle::ChatMedium, this->scale()));
replyHbox->addStretch(1);
auto replyCancelButton = replyHbox.emplace<EffectLabel>(nullptr, 4)
.assign(&this->ui_.cancelReplyButton);
replyCancelButton->getLabel().setTextFormat(Qt::RichText);
replyCancelButton->hide();
replyLabel->hide();
// hbox for input, right box
auto hboxLayout =
layout.emplace<QHBoxLayout>().withoutMargin().assign(&this->ui_.hbox);
2017-10-27 20:34:23 +02:00
2018-01-25 20:49:49 +01:00
// input
2018-08-06 21:17:03 +02:00
auto textEdit =
hboxLayout.emplace<ResizingTextEdit>().assign(&this->ui_.textEdit);
2018-01-25 20:49:49 +01:00
connect(textEdit.getElement(), &ResizingTextEdit::textChanged, this,
&SplitInput::editTextChanged);
2018-01-25 20:49:49 +01:00
// right box
auto box = hboxLayout.emplace<QVBoxLayout>().withoutMargin();
2018-01-25 20:49:49 +01:00
box->setSpacing(0);
{
2018-08-06 21:17:03 +02:00
auto textEditLength =
box.emplace<QLabel>().assign(&this->ui_.textEditLength);
2018-01-25 20:49:49 +01:00
textEditLength->setAlignment(Qt::AlignRight);
2017-09-15 17:23:49 +02:00
2018-01-25 20:49:49 +01:00
box->addStretch(1);
2018-08-08 15:35:54 +02:00
box.emplace<EffectLabel>().assign(&this->ui_.emoteButton);
2018-01-25 20:49:49 +01:00
}
2018-06-06 18:57:22 +02:00
this->ui_.emoteButton->getLabel().setTextFormat(Qt::RichText);
2018-01-25 20:49:49 +01:00
// ---- misc
2018-01-25 20:49:49 +01:00
// set edit font
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setFont(
2018-11-21 21:37:41 +01:00
app->fonts->getFont(FontStyle::ChatMedium, this->scale()));
QObject::connect(this->ui_.textEdit, &QTextEdit::cursorPositionChanged,
this, &SplitInput::onCursorPositionChanged);
QObject::connect(this->ui_.textEdit, &QTextEdit::textChanged, this,
&SplitInput::onTextChanged);
this->managedConnections_.managedConnect(app->fonts->fontChanged, [=]() {
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setFont(
2018-11-21 21:37:41 +01:00
app->fonts->getFont(FontStyle::ChatMedium, this->scale()));
this->ui_.replyLabel->setFont(
app->fonts->getFont(FontStyle::ChatMediumBold, this->scale()));
});
2018-01-25 20:49:49 +01:00
// open emote popup
QObject::connect(this->ui_.emoteButton, &EffectLabel::leftClicked, [=] {
this->openEmotePopup();
});
2017-09-15 17:23:49 +02:00
// clear input and remove reply thread
QObject::connect(this->ui_.cancelReplyButton, &EffectLabel::leftClicked,
[=] {
this->clearInput();
});
2018-01-25 20:49:49 +01:00
// clear channelview selection when selecting in the input
2018-08-06 21:17:03 +02:00
QObject::connect(this->ui_.textEdit, &QTextEdit::copyAvailable,
[this](bool available) {
2018-10-21 13:43:02 +02:00
if (available)
{
this->split_->view_->clearSelection();
2018-08-06 21:17:03 +02:00
}
});
2017-01-22 12:46:35 +01:00
2018-01-25 20:49:49 +01:00
// textEditLength visibility
getSettings()->showMessageLength.connect(
2018-08-06 21:17:03 +02:00
[this](const bool &value, auto) {
2018-10-20 19:15:15 +02:00
// this->ui_.textEditLength->setHidden(!value);
this->editTextChanged();
2018-08-06 21:17:03 +02:00
},
2018-06-06 18:57:22 +02:00
this->managedConnections_);
2018-01-25 20:49:49 +01:00
}
2017-01-22 12:46:35 +01:00
2018-01-25 20:49:49 +01:00
void SplitInput::scaleChangedEvent(float scale)
{
auto app = getApp();
// update the icon size of the buttons
2018-06-07 17:43:21 +02:00
this->updateEmoteButton();
this->updateCancelReplyButton();
2018-01-25 20:49:49 +01:00
// set maximum height
if (!this->hidden)
{
this->setMaximumHeight(this->scaledMaxHeight());
}
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setFont(
app->fonts->getFont(FontStyle::ChatMedium, this->scale()));
this->ui_.textEditLength->setFont(
app->fonts->getFont(FontStyle::ChatMedium, this->scale()));
this->ui_.replyLabel->setFont(
app->fonts->getFont(FontStyle::ChatMediumBold, this->scale()));
2018-01-25 20:49:49 +01:00
}
2018-07-06 17:11:37 +02:00
void SplitInput::themeChangedEvent()
2018-01-25 20:49:49 +01:00
{
2020-12-13 12:16:08 +01:00
QPalette palette, placeholderPalette;
2018-01-25 20:49:49 +01:00
2020-12-13 12:16:08 +01:00
palette.setColor(QPalette::WindowText, this->theme->splits.input.text);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
2020-12-13 12:16:08 +01:00
placeholderPalette.setColor(
QPalette::PlaceholderText,
this->theme->messages.textColors.chatPlaceholder);
#endif
2018-06-07 17:43:21 +02:00
this->updateEmoteButton();
this->updateCancelReplyButton();
2018-06-06 18:57:22 +02:00
this->ui_.textEditLength->setPalette(palette);
2018-01-25 20:49:49 +01:00
this->ui_.textEdit->setStyleSheet(this->theme->splits.input.styleSheet);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
2020-12-13 12:16:08 +01:00
this->ui_.textEdit->setPalette(placeholderPalette);
#endif
auto marginPx = (this->theme->isLightTheme() ? 4 : 2) * this->scale();
this->ui_.vbox->setContentsMargins(marginPx, marginPx, marginPx, marginPx);
2018-06-07 17:43:21 +02:00
this->ui_.emoteButton->getLabel().setStyleSheet("color: #000");
if (this->theme->isLightTheme())
{
this->ui_.replyLabel->setStyleSheet("color: #333");
}
else
{
this->ui_.replyLabel->setStyleSheet("color: #ccc");
}
2018-06-07 17:43:21 +02:00
}
void SplitInput::updateEmoteButton()
{
2018-11-21 21:37:41 +01:00
float scale = this->scale();
2018-06-07 17:43:21 +02:00
2018-08-02 14:23:27 +02:00
QString text = "<img src=':/buttons/emote.svg' width='xD' height='xD' />";
2018-06-07 17:43:21 +02:00
text.replace("xD", QString::number(int(12 * scale)));
2018-10-21 13:43:02 +02:00
if (this->theme->isLightTheme())
{
text.replace("emote", "emoteDark");
2018-06-07 17:43:21 +02:00
}
this->ui_.emoteButton->getLabel().setText(text);
this->ui_.emoteButton->setFixedHeight(int(18 * scale));
2018-01-25 20:49:49 +01:00
}
void SplitInput::updateCancelReplyButton()
{
float scale = this->scale();
QString text =
QStringLiteral(
"<img src=':/buttons/cancel.svg' width='%1' height='%1' />")
.arg(QString::number(int(12 * scale)));
if (this->theme->isLightTheme())
{
text.replace("cancel", "cancelDark");
}
this->ui_.cancelReplyButton->getLabel().setText(text);
this->ui_.cancelReplyButton->setFixedHeight(int(12 * scale));
}
2018-08-27 14:36:01 +02:00
void SplitInput::openEmotePopup()
{
2018-10-21 13:43:02 +02:00
if (!this->emotePopup_)
{
2019-08-13 16:39:22 +02:00
this->emotePopup_ = new EmotePopup(this);
this->emotePopup_->setAttribute(Qt::WA_DeleteOnClose);
2018-08-27 14:36:01 +02:00
this->emotePopup_->linkClicked.connect([this](const Link &link) {
2018-10-21 13:43:02 +02:00
if (link.type == Link::InsertText)
{
QTextCursor cursor = this->ui_.textEdit->textCursor();
QString textToInsert(link.value + " ");
// If symbol before cursor isn't space or empty
// Then insert space before emote.
if (cursor.position() > 0 &&
2018-10-21 13:43:02 +02:00
!this->getInputText()[cursor.position() - 1].isSpace())
{
textToInsert = " " + textToInsert;
}
this->insertText(textToInsert);
2018-08-27 14:36:01 +02:00
}
});
}
2018-11-21 21:37:41 +01:00
this->emotePopup_->resize(int(300 * this->emotePopup_->scale()),
int(500 * this->emotePopup_->scale()));
2018-08-27 14:36:01 +02:00
this->emotePopup_->loadChannel(this->split_->getChannel());
this->emotePopup_->show();
this->emotePopup_->raise();
2018-09-25 13:37:24 +02:00
this->emotePopup_->activateWindow();
2018-08-27 14:36:01 +02:00
}
QString SplitInput::handleSendMessage(std::vector<QString> &arguments)
{
auto c = this->split_->getChannel();
if (c == nullptr)
return "";
if (!c->isTwitchChannel() || this->replyThread_ == nullptr)
{
// standard message send behavior
QString message = ui_.textEdit->toPlainText();
message = message.replace('\n', ' ');
QString sendMessage =
getApp()->commands->execCommand(message, c, false);
c->sendMessage(sendMessage);
this->postMessageSend(message, arguments);
return "";
}
else
{
// Reply to message
auto tc = dynamic_cast<TwitchChannel *>(c.get());
if (!tc)
{
// this should not fail
return "";
}
QString message = this->ui_.textEdit->toPlainText();
if (this->enableInlineReplying_)
{
// Remove @username prefix that is inserted when doing inline replies
message.remove(0, this->replyThread_->root()->displayName.length() +
1); // remove "@username"
if (!message.isEmpty() && message.at(0) == ' ')
{
message.remove(0, 1); // remove possible space
}
}
message = message.replace('\n', ' ');
QString sendMessage =
getApp()->commands->execCommand(message, c, false);
// Reply within TwitchChannel
tc->sendReply(sendMessage, this->replyThread_->rootId());
this->postMessageSend(message, arguments);
return "";
}
}
void SplitInput::postMessageSend(const QString &message,
const std::vector<QString> &arguments)
{
// don't add duplicate messages and empty message to message history
if ((this->prevMsg_.isEmpty() || !this->prevMsg_.endsWith(message)) &&
!message.trimmed().isEmpty())
{
this->prevMsg_.append(message);
}
if (arguments.empty() || arguments.at(0) != "keepInput")
{
this->clearInput();
}
this->prevIndex_ = this->prevMsg_.size();
}
int SplitInput::scaledMaxHeight() const
{
return int(150 * this->scale());
}
void SplitInput::addShortcuts()
2018-01-25 20:49:49 +01:00
{
HotkeyController::HotkeyMap actions{
{"cursorToStart",
[this](std::vector<QString> arguments) -> QString {
if (arguments.size() != 1)
{
qCWarning(chatterinoHotkeys)
<< "Invalid cursorToStart arguments. Argument 0: select "
"(\"withSelection\" or \"withoutSelection\")";
return "Invalid cursorToStart arguments. Argument 0: select "
"(\"withSelection\" or \"withoutSelection\")";
}
QTextCursor cursor = this->ui_.textEdit->textCursor();
auto place = QTextCursor::Start;
auto stringTakeSelection = arguments.at(0);
bool select;
if (stringTakeSelection == "withSelection")
{
select = true;
}
else if (stringTakeSelection == "withoutSelection")
{
select = false;
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid cursorToStart select argument (0)!";
return "Invalid cursorToStart select argument (0)!";
}
cursor.movePosition(place,
select ? QTextCursor::MoveMode::KeepAnchor
: QTextCursor::MoveMode::MoveAnchor);
this->ui_.textEdit->setTextCursor(cursor);
return "";
}},
{"cursorToEnd",
[this](std::vector<QString> arguments) -> QString {
if (arguments.size() != 1)
{
qCWarning(chatterinoHotkeys)
<< "Invalid cursorToEnd arguments. Argument 0: select "
"(\"withSelection\" or \"withoutSelection\")";
return "Invalid cursorToEnd arguments. Argument 0: select "
"(\"withSelection\" or \"withoutSelection\")";
}
QTextCursor cursor = this->ui_.textEdit->textCursor();
auto place = QTextCursor::End;
auto stringTakeSelection = arguments.at(0);
bool select;
if (stringTakeSelection == "withSelection")
{
select = true;
}
else if (stringTakeSelection == "withoutSelection")
{
select = false;
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid cursorToEnd select argument (0)!";
return "Invalid cursorToEnd select argument (0)!";
}
cursor.movePosition(place,
select ? QTextCursor::MoveMode::KeepAnchor
: QTextCursor::MoveMode::MoveAnchor);
this->ui_.textEdit->setTextCursor(cursor);
return "";
}},
{"openEmotesPopup",
[this](std::vector<QString>) -> QString {
this->openEmotePopup();
return "";
}},
{"sendMessage",
[this](std::vector<QString> arguments) -> QString {
return this->handleSendMessage(arguments);
}},
{"previousMessage",
[this](std::vector<QString>) -> QString {
if (this->prevMsg_.size() && this->prevIndex_)
{
if (this->prevIndex_ == (this->prevMsg_.size()))
{
this->currMsg_ = ui_.textEdit->toPlainText();
}
this->prevIndex_--;
this->ui_.textEdit->setPlainText(
this->prevMsg_.at(this->prevIndex_));
this->ui_.textEdit->resetCompletion();
QTextCursor cursor = this->ui_.textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
this->ui_.textEdit->setTextCursor(cursor);
}
return "";
}},
{"nextMessage",
[this](std::vector<QString>) -> QString {
// If user did not write anything before then just do nothing.
if (this->prevMsg_.isEmpty())
{
return "";
}
bool cursorToEnd = true;
QString message = ui_.textEdit->toPlainText();
if (this->prevIndex_ != (this->prevMsg_.size() - 1) &&
this->prevIndex_ != this->prevMsg_.size())
{
this->prevIndex_++;
this->ui_.textEdit->setPlainText(
this->prevMsg_.at(this->prevIndex_));
this->ui_.textEdit->resetCompletion();
}
else
{
this->prevIndex_ = this->prevMsg_.size();
if (message == this->prevMsg_.at(this->prevIndex_ - 1))
{
// If user has just come from a message history
// Then simply get currMsg_.
this->ui_.textEdit->setPlainText(this->currMsg_);
this->ui_.textEdit->resetCompletion();
}
else if (message != this->currMsg_)
{
// If user are already in current message
// And type something new
// Then replace currMsg_ with new one.
this->currMsg_ = message;
}
// If user is already in current message
// Then don't touch cursos.
cursorToEnd =
(message == this->prevMsg_.at(this->prevIndex_ - 1));
}
if (cursorToEnd)
{
QTextCursor cursor = this->ui_.textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
this->ui_.textEdit->setTextCursor(cursor);
}
return "";
}},
{"undo",
[this](std::vector<QString>) -> QString {
this->ui_.textEdit->undo();
return "";
}},
{"redo",
[this](std::vector<QString>) -> QString {
this->ui_.textEdit->redo();
return "";
}},
{"copy",
[this](std::vector<QString> arguments) -> QString {
// XXX: this action is unused at the moment, a qt standard shortcut is used instead
if (arguments.size() == 0)
{
return "copy action takes only one argument: the source "
"of the copy \"split\", \"input\" or "
"\"auto\". If the source is \"split\", only text "
"from the chat will be copied. If it is "
"\"splitInput\", text from the input box will be "
"copied. Automatic will pick whichever has a "
"selection";
}
bool copyFromSplit = false;
auto mode = arguments.at(0);
if (mode == "split")
{
copyFromSplit = true;
}
else if (mode == "splitInput")
{
copyFromSplit = false;
}
else if (mode == "auto")
{
const auto &cursor = this->ui_.textEdit->textCursor();
copyFromSplit = !cursor.hasSelection();
}
if (copyFromSplit)
{
this->channelView_->copySelectedText();
}
else
{
this->ui_.textEdit->copy();
}
return "";
}},
{"paste",
[this](std::vector<QString>) -> QString {
this->ui_.textEdit->paste();
return "";
}},
{"clear",
[this](std::vector<QString>) -> QString {
this->clearInput();
return "";
}},
{"selectAll",
[this](std::vector<QString>) -> QString {
this->ui_.textEdit->selectAll();
return "";
}},
{"selectWord",
[this](std::vector<QString>) -> QString {
auto cursor = this->ui_.textEdit->textCursor();
cursor.select(QTextCursor::WordUnderCursor);
this->ui_.textEdit->setTextCursor(cursor);
return "";
}},
};
this->shortcuts_ = getApp()->hotkeys->shortcutsForCategory(
HotkeyCategory::SplitInput, actions, this->parentWidget());
}
bool SplitInput::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride ||
event->type() == QEvent::Shortcut)
{
if (auto popup = this->inputCompletionPopup_.get())
{
if (popup->isVisible())
{
// Stop shortcut from triggering by saying we will handle it ourselves
event->accept();
// Return false means the underlying event isn't stopped, it will continue to propagate
return false;
}
2018-10-21 13:43:02 +02:00
}
}
return BaseWidget::eventFilter(obj, event);
}
void SplitInput::installKeyPressedEvent()
{
this->ui_.textEdit->keyPressed.disconnectAll();
this->ui_.textEdit->keyPressed.connect([this](QKeyEvent *event) {
if (auto popup = this->inputCompletionPopup_.get())
2018-10-21 13:43:02 +02:00
{
if (popup->isVisible())
2018-10-21 13:43:02 +02:00
{
if (popup->eventFilter(nullptr, event))
2018-10-21 13:43:02 +02:00
{
event->accept();
2018-08-27 20:12:38 +02:00
return;
}
}
2018-10-21 13:43:02 +02:00
}
// One of the last remaining of it's kind, the copy shortcut.
// For some bizarre reason Qt doesn't want this key be rebound.
// TODO(Mm2PL): Revisit in Qt6, maybe something changed?
if ((event->key() == Qt::Key_C || event->key() == Qt::Key_Insert) &&
event->modifiers() == Qt::ControlModifier)
2018-10-21 13:43:02 +02:00
{
if (this->channelView_->hasSelection())
2018-10-21 13:43:02 +02:00
{
this->channelView_->copySelectedText();
2017-09-21 02:20:02 +02:00
event->accept();
}
2018-10-21 13:43:02 +02:00
}
2017-01-29 13:23:22 +01:00
});
2017-01-22 12:46:35 +01:00
}
void SplitInput::mousePressEvent(QMouseEvent *event)
{
if (this->hidden)
{
BaseWidget::mousePressEvent(event);
}
// else, don't call QWidget::mousePressEvent,
// which will call event->ignore()
}
void SplitInput::onTextChanged()
{
this->updateCompletionPopup();
}
void SplitInput::onCursorPositionChanged()
{
this->updateCompletionPopup();
}
void SplitInput::updateCompletionPopup()
{
auto channel = this->split_->getChannel().get();
auto tc = dynamic_cast<TwitchChannel *>(channel);
bool showEmoteCompletion = getSettings()->emoteCompletionWithColon;
bool showUsernameCompletion =
tc && getSettings()->showUsernameCompletionMenu;
if (!showEmoteCompletion && !showUsernameCompletion)
{
this->hideCompletionPopup();
return;
}
// check if in completion prefix
auto &edit = *this->ui_.textEdit;
auto text = edit.toPlainText();
auto position = edit.textCursor().position() - 1;
if (text.length() == 0 || position == -1)
{
this->hideCompletionPopup();
return;
}
for (int i = clamp(position, 0, text.length() - 1); i >= 0; i--)
{
if (text[i] == ' ')
{
this->hideCompletionPopup();
return;
}
else if (text[i] == ':' && showEmoteCompletion)
{
2020-08-22 12:34:19 +02:00
if (i == 0 || text[i - 1].isSpace())
this->showCompletionPopup(text.mid(i, position - i + 1).mid(1),
true);
2020-08-22 12:34:19 +02:00
else
this->hideCompletionPopup();
return;
}
else if (text[i] == '@' && showUsernameCompletion)
{
if (i == 0 || text[i - 1].isSpace())
this->showCompletionPopup(text.mid(i, position - i + 1).mid(1),
false);
else
this->hideCompletionPopup();
return;
}
}
this->hideCompletionPopup();
}
void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion)
{
if (!this->inputCompletionPopup_.get())
{
this->inputCompletionPopup_ = new InputCompletionPopup(this);
this->inputCompletionPopup_->setInputAction(
[that = QObjectRef(this)](const QString &text) mutable {
if (auto this2 = that.get())
{
this2->insertCompletionText(text);
this2->hideCompletionPopup();
}
});
}
auto popup = this->inputCompletionPopup_.get();
assert(popup);
if (emoteCompletion) // autocomplete emotes
popup->updateEmotes(text, this->split_->getChannel());
else // autocomplete usernames
popup->updateUsers(text, this->split_->getChannel());
auto pos = this->mapToGlobal({0, 0}) - QPoint(0, popup->height()) +
QPoint((this->width() - popup->width()) / 2, 0);
popup->move(pos);
popup->show();
}
void SplitInput::hideCompletionPopup()
{
if (auto popup = this->inputCompletionPopup_.get())
popup->hide();
}
void SplitInput::insertCompletionText(const QString &input_)
{
auto &edit = *this->ui_.textEdit;
2020-08-22 12:34:19 +02:00
auto input = input_ + ' ';
auto text = edit.toPlainText();
auto position = edit.textCursor().position() - 1;
for (int i = clamp(position, 0, text.length() - 1); i >= 0; i--)
{
bool done = false;
if (text[i] == ':')
{
done = true;
}
else if (text[i] == '@')
{
const auto userMention =
formatUserMention(input_, edit.isFirstWord(),
getSettings()->mentionUsersWithComma);
input = "@" + userMention + " ";
done = true;
}
if (done)
{
auto cursor = edit.textCursor();
edit.setPlainText(
text.remove(i, position - i + 1).insert(i, input));
cursor.setPosition(i + input.size());
edit.setTextCursor(cursor);
break;
}
}
}
bool SplitInput::hasSelection() const
2017-09-21 02:20:02 +02:00
{
return this->ui_.textEdit->textCursor().hasSelection();
}
2017-09-21 02:20:02 +02:00
void SplitInput::clearSelection() const
{
auto cursor = this->ui_.textEdit->textCursor();
cursor.clearSelection();
this->ui_.textEdit->setTextCursor(cursor);
2017-09-21 02:20:02 +02:00
}
bool SplitInput::isEditFirstWord() const
{
return this->ui_.textEdit->isFirstWord();
}
QString SplitInput::getInputText() const
{
2018-06-06 18:57:22 +02:00
return this->ui_.textEdit->toPlainText();
}
2018-01-24 20:58:53 +01:00
void SplitInput::insertText(const QString &text)
{
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->insertPlainText(text);
2017-01-01 02:30:42 +01:00
}
void SplitInput::hide()
{
if (this->isHidden())
{
return;
}
this->hidden = true;
this->setMaximumHeight(0);
this->updateGeometry();
}
void SplitInput::show()
{
if (!this->isHidden())
{
return;
}
this->hidden = false;
this->setMaximumHeight(this->scaledMaxHeight());
this->updateGeometry();
}
bool SplitInput::isHidden() const
{
return this->hidden;
}
2017-11-12 17:21:50 +01:00
void SplitInput::editTextChanged()
2017-01-29 13:23:22 +01:00
{
auto app = getApp();
2018-01-25 20:49:49 +01:00
// set textLengthLabel value
2018-06-06 18:57:22 +02:00
QString text = this->ui_.textEdit->toPlainText();
2017-12-17 02:40:05 +01:00
if (this->shouldPreventInput(text))
{
this->ui_.textEdit->setPlainText(text.left(TWITCH_MESSAGE_LIMIT));
this->ui_.textEdit->moveCursor(QTextCursor::EndOfBlock);
return;
}
2018-06-06 10:46:23 +02:00
if (text.startsWith("/r ", Qt::CaseInsensitive) &&
this->split_->getChannel()->isTwitchChannel())
2018-05-25 13:53:55 +02:00
{
QString lastUser = app->twitch->lastUserThatWhisperedMe.get();
2018-10-21 13:43:02 +02:00
if (!lastUser.isEmpty())
{
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setPlainText("/w " + lastUser + text.mid(2));
this->ui_.textEdit->moveCursor(QTextCursor::EndOfBlock);
2018-05-25 13:53:55 +02:00
}
2018-10-21 13:43:02 +02:00
}
else
{
2018-05-25 13:53:55 +02:00
this->textChanged.invoke(text);
2018-05-25 13:53:55 +02:00
text = text.trimmed();
2018-08-06 21:17:03 +02:00
text =
app->commands->execCommand(text, this->split_->getChannel(), true);
2018-05-25 13:53:55 +02:00
}
if (text.length() > 0 &&
getSettings()->messageOverflow.getValue() == MessageOverflow::Highlight)
{
QTextCursor cursor = this->ui_.textEdit->textCursor();
QTextCharFormat format;
QList<QTextEdit::ExtraSelection> selections;
cursor.setPosition(qMin(text.length(), TWITCH_MESSAGE_LIMIT),
QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor);
selections.append({cursor, format});
if (text.length() > TWITCH_MESSAGE_LIMIT)
{
cursor.setPosition(TWITCH_MESSAGE_LIMIT, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
format.setForeground(Qt::red);
selections.append({cursor, format});
}
// block reemit of QTextEdit::textChanged()
{
const QSignalBlocker b(this->ui_.textEdit);
this->ui_.textEdit->setExtraSelections(selections);
}
}
2017-12-17 02:40:05 +01:00
QString labelText;
if (text.length() > 0 && getSettings()->showMessageLength)
2018-10-21 13:43:02 +02:00
{
2018-11-21 21:37:41 +01:00
labelText = QString::number(text.length());
if (text.length() > TWITCH_MESSAGE_LIMIT)
{
this->ui_.textEditLength->setStyleSheet("color: red");
}
else
{
this->ui_.textEditLength->setStyleSheet("");
}
2018-10-21 13:43:02 +02:00
}
else
{
labelText = "";
2017-12-17 02:40:05 +01:00
}
2018-06-06 18:57:22 +02:00
this->ui_.textEditLength->setText(labelText);
bool hasReply = false;
if (this->enableInlineReplying_)
{
if (this->replyThread_ != nullptr)
{
// Check if the input still starts with @username. If not, don't reply.
//
// We need to verify that
// 1. the @username prefix exists and
// 2. if a character exists after the @username, it is a space
QString replyPrefix = "@" + this->replyThread_->root()->displayName;
if (!text.startsWith(replyPrefix) ||
(text.length() > replyPrefix.length() &&
text.at(replyPrefix.length()) != ' '))
{
this->replyThread_ = nullptr;
}
}
// Show/hide reply label if inline replies are possible
hasReply = this->replyThread_ != nullptr;
}
this->ui_.replyWrapper->setVisible(hasReply);
this->ui_.replyLabel->setVisible(hasReply);
this->ui_.cancelReplyButton->setVisible(hasReply);
2017-01-29 13:23:22 +01:00
}
void SplitInput::paintEvent(QPaintEvent * /*event*/)
2017-01-01 02:30:42 +01:00
{
QPainter painter(this);
int s;
QColor borderColor;
2018-10-21 13:43:02 +02:00
if (this->theme->isLightTheme())
{
s = int(3 * this->scale());
borderColor = QColor("#ccc");
2018-10-21 13:43:02 +02:00
}
else
{
s = int(1 * this->scale());
borderColor = QColor("#333");
}
2018-05-23 04:22:17 +02:00
QMargins removeMargins(s - 1, s - 1, s, s);
QRect baseRect = this->rect();
2018-05-23 04:22:17 +02:00
// completeAreaRect includes the reply label
QRect completeAreaRect = baseRect.marginsRemoved(removeMargins);
painter.fillRect(completeAreaRect, this->theme->splits.input.background);
painter.setPen(borderColor);
painter.drawRect(completeAreaRect);
if (this->enableInlineReplying_ && this->replyThread_ != nullptr)
{
// Move top of rect down to not include reply label
baseRect.setTop(baseRect.top() + this->ui_.replyWrapper->height());
2018-06-06 13:35:06 +02:00
QRect onlyInputRect = baseRect.marginsRemoved(removeMargins);
painter.setPen(borderColor);
painter.drawRect(onlyInputRect);
}
2017-01-01 02:30:42 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitInput::resizeEvent(QResizeEvent *)
{
2018-10-21 13:43:02 +02:00
if (this->height() == this->maximumHeight())
{
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
2018-10-21 13:43:02 +02:00
}
else
{
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
}
void SplitInput::giveFocus(Qt::FocusReason reason)
{
this->ui_.textEdit->setFocus(reason);
}
void SplitInput::setReply(std::shared_ptr<MessageThread> reply,
bool showReplyingLabel)
{
this->replyThread_ = std::move(reply);
if (this->enableInlineReplying_)
{
// Only enable reply label if inline replying
auto replyPrefix = "@" + this->replyThread_->root()->displayName;
auto plainText = this->ui_.textEdit->toPlainText().trimmed();
if (!plainText.startsWith(replyPrefix))
{
if (!plainText.isEmpty())
{
replyPrefix.append(' ');
}
this->ui_.textEdit->setPlainText(replyPrefix + plainText + " ");
this->ui_.textEdit->moveCursor(QTextCursor::EndOfBlock);
this->ui_.textEdit->resetCompletion();
}
this->ui_.replyLabel->setText("Replying to @" +
this->replyThread_->root()->displayName);
}
}
void SplitInput::setPlaceholderText(const QString &text)
{
this->ui_.textEdit->setPlaceholderText(text);
}
void SplitInput::clearInput()
{
this->currMsg_ = "";
this->ui_.textEdit->setText("");
this->ui_.textEdit->moveCursor(QTextCursor::Start);
if (this->enableInlineReplying_)
{
this->replyThread_ = nullptr;
}
}
bool SplitInput::shouldPreventInput(const QString &text) const
{
if (getSettings()->messageOverflow.getValue() != MessageOverflow::Prevent)
{
return false;
}
auto channel = this->split_->getChannel();
if (channel == nullptr)
{
return false;
}
if (!channel->isTwitchChannel())
{
// Don't respect this setting for IRC channels as the limits might be server-specific
return false;
}
return text.length() > TWITCH_MESSAGE_LIMIT;
}
} // namespace chatterino