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

490 lines
15 KiB
C++
Raw Normal View History

#include "widgets/splits/SplitInput.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "controllers/commands/CommandController.hpp"
#include "messages/Link.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.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"
2018-06-26 14:09:39 +02:00
#include "util/LayoutCreator.hpp"
#include "widgets/Notebook.hpp"
#include "widgets/dialogs/EmotePopup.hpp"
#include "widgets/helper/ChannelView.hpp"
#include "widgets/helper/EffectLabel.hpp"
#include "widgets/helper/ResizingTextEdit.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>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
2017-12-17 02:18:13 +01:00
SplitInput::SplitInput(Split *_chatWidget)
: BaseWidget(_chatWidget)
2018-06-06 18:57:22 +02:00
, split_(_chatWidget)
2017-01-01 02:30:42 +01:00
{
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
2018-06-06 18:57:22 +02:00
this->split_->channelChanged.connect([this] {
2018-08-06 21:17:03 +02:00
auto completer =
new QCompleter(&this->split_->getChannel()->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->scaleChangedEvent(this->getScale());
}
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 =
2018-08-06 21:17:03 +02:00
layoutCreator.setLayoutType<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 =
layout.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 = layout.emplace<QVBoxLayout>().withoutMargin();
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(
app->fonts->getFont(FontStyle::ChatMedium, this->getScale()));
2018-06-13 13:29:11 +02:00
this->managedConnections_.push_back(app->fonts->fontChanged.connect([=]() {
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setFont(
app->fonts->getFont(FontStyle::ChatMedium, this->getScale()));
2018-01-25 20:49:49 +01:00
}));
// open emote popup
2018-09-01 14:33:27 +02:00
QObject::connect(this->ui_.emoteButton, &EffectLabel::clicked,
[=] { this->openEmotePopup(); });
2017-09-15 17:23:49 +02:00
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)
{
// update the icon size of the emote button
2018-06-07 17:43:21 +02:00
this->updateEmoteButton();
2018-01-25 20:49:49 +01:00
// set maximum height
2018-05-25 13:53:55 +02:00
this->setMaximumHeight(int(150 * this->getScale()));
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setFont(
getApp()->fonts->getFont(FontStyle::ChatMedium, this->getScale()));
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
{
QPalette palette;
2018-07-06 17:11:37 +02:00
palette.setColor(QPalette::Foreground, this->theme->splits.input.text);
2018-06-07 17:43:21 +02:00
this->updateEmoteButton();
2018-06-06 18:57:22 +02:00
this->ui_.textEditLength->setPalette(palette);
2018-01-25 20:49:49 +01:00
2018-07-06 17:11:37 +02:00
this->ui_.textEdit->setStyleSheet(this->theme->splits.input.styleSheet);
2018-01-25 20:49:49 +01:00
2018-08-06 21:17:03 +02:00
this->ui_.hbox->setMargin(
int((this->theme->isLightTheme() ? 4 : 2) * this->getScale()));
2018-06-07 17:43:21 +02:00
this->ui_.emoteButton->getLabel().setStyleSheet("color: #000");
}
void SplitInput::updateEmoteButton()
{
float scale = this->getScale();
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
}
2018-08-27 14:36:01 +02:00
void SplitInput::openEmotePopup()
{
2018-10-21 13:43:02 +02:00
if (!this->emotePopup_)
{
2018-08-27 14:36:01 +02:00
this->emotePopup_ = std::make_unique<EmotePopup>();
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
}
});
}
this->emotePopup_->resize(int(300 * this->emotePopup_->getScale()),
int(500 * this->emotePopup_->getScale()));
this->emotePopup_->loadChannel(this->split_->getChannel());
this->emotePopup_->show();
2018-09-25 13:37:24 +02:00
this->emotePopup_->activateWindow();
2018-08-27 14:36:01 +02:00
}
2018-01-25 20:49:49 +01:00
void SplitInput::installKeyPressedEvent()
{
auto app = getApp();
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->keyPressed.connect([this, app](QKeyEvent *event) {
2018-10-21 13:43:02 +02:00
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
{
2018-06-06 18:57:22 +02:00
auto c = this->split_->getChannel();
2018-10-21 13:43:02 +02:00
if (c == nullptr)
2017-05-28 00:47:16 +02:00
return;
2018-06-06 18:57:22 +02:00
QString message = ui_.textEdit->toPlainText();
2017-08-12 12:24:28 +02:00
QString sendMessage = app->commands->execCommand(message, c, false);
sendMessage = sendMessage.replace('\n', ' ');
c->sendMessage(sendMessage);
// don't add duplicate messages and empty message to message history
2018-09-01 14:33:27 +02:00
if ((this->prevMsg_.isEmpty() ||
!this->prevMsg_.endsWith(message)) &&
!message.trimmed().isEmpty())
2018-10-21 14:44:59 +02:00
{
2018-06-06 18:57:22 +02:00
this->prevMsg_.append(message);
2018-10-21 14:44:59 +02:00
}
2017-08-12 12:24:28 +02:00
2017-05-28 00:47:16 +02:00
event->accept();
2018-10-21 13:43:02 +02:00
if (!(event->modifiers() == Qt::ControlModifier))
{
2018-06-06 18:57:22 +02:00
this->currMsg_ = QString();
this->ui_.textEdit->setText(QString());
}
2018-06-06 18:57:22 +02:00
this->prevIndex_ = this->prevMsg_.size();
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Up)
{
if ((event->modifiers() & Qt::ShiftModifier) != 0)
{
return;
}
2018-10-21 13:43:02 +02:00
if (event->modifiers() == Qt::AltModifier)
{
2018-06-06 18:57:22 +02:00
SplitContainer *page = this->split_->getContainer();
2018-10-21 13:43:02 +02:00
if (page != nullptr)
{
2018-05-25 14:57:17 +02:00
page->selectNextSplit(SplitContainer::Above);
}
2018-10-21 13:43:02 +02:00
}
else
{
if (this->prevMsg_.size() && this->prevIndex_)
{
if (this->prevIndex_ == (this->prevMsg_.size()))
{
2018-06-06 18:57:22 +02:00
this->currMsg_ = ui_.textEdit->toPlainText();
2018-04-08 14:33:45 +02:00
}
2018-06-06 18:57:22 +02:00
this->prevIndex_--;
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setText(
this->prevMsg_.at(this->prevIndex_));
2018-04-08 14:33:45 +02:00
2018-06-06 18:57:22 +02:00
QTextCursor cursor = this->ui_.textEdit->textCursor();
2018-04-08 14:33:45 +02:00
cursor.movePosition(QTextCursor::End);
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setTextCursor(cursor);
}
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Down)
{
if ((event->modifiers() & Qt::ShiftModifier) != 0)
{
return;
}
2018-10-21 13:43:02 +02:00
if (event->modifiers() == Qt::AltModifier)
{
2018-06-06 18:57:22 +02:00
SplitContainer *page = this->split_->getContainer();
2018-10-21 13:43:02 +02:00
if (page != nullptr)
{
2018-05-25 14:57:17 +02:00
page->selectNextSplit(SplitContainer::Below);
}
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-27 20:12:38 +02:00
// If user did not write anything before then just do nothing.
2018-10-21 13:43:02 +02:00
if (this->prevMsg_.isEmpty())
{
2018-08-27 20:12:38 +02:00
return;
}
bool cursorToEnd = true;
QString message = ui_.textEdit->toPlainText();
2018-06-06 18:57:22 +02:00
if (this->prevIndex_ != (this->prevMsg_.size() - 1) &&
2018-10-21 13:43:02 +02:00
this->prevIndex_ != this->prevMsg_.size())
{
2018-06-06 18:57:22 +02:00
this->prevIndex_++;
2018-08-06 21:17:03 +02:00
this->ui_.textEdit->setText(
this->prevMsg_.at(this->prevIndex_));
2018-10-21 13:43:02 +02:00
}
else
{
2018-06-06 18:57:22 +02:00
this->prevIndex_ = this->prevMsg_.size();
2018-10-21 13:43:02 +02:00
if (message == this->prevMsg_.at(this->prevIndex_ - 1))
{
2018-08-27 20:12:38 +02:00
// If user has just come from a message history
// Then simply get currMsg_.
this->ui_.textEdit->setText(this->currMsg_);
2018-10-21 13:43:02 +02:00
}
else if (message != this->currMsg_)
{
2018-09-01 14:33:27 +02:00
// If user are already in current message
2018-08-27 20:12:38 +02:00
// And type something new
// Then replace currMsg_ with new one.
this->currMsg_ = message;
}
2018-08-27 20:22:53 +02:00
// If user is already in current message
// Then don't touch cursos.
2018-09-01 14:33:27 +02:00
cursorToEnd =
(message == this->prevMsg_.at(this->prevIndex_ - 1));
}
2018-04-08 14:33:45 +02:00
2018-10-21 13:43:02 +02:00
if (cursorToEnd)
{
2018-08-27 20:12:38 +02:00
QTextCursor cursor = this->ui_.textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
this->ui_.textEdit->setTextCursor(cursor);
}
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Left)
{
if (event->modifiers() == Qt::AltModifier)
{
2018-06-06 18:57:22 +02:00
SplitContainer *page = this->split_->getContainer();
2018-10-21 13:43:02 +02:00
if (page != nullptr)
{
2018-05-25 14:57:17 +02:00
page->selectNextSplit(SplitContainer::Left);
}
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Right)
{
if (event->modifiers() == Qt::AltModifier)
{
2018-06-06 18:57:22 +02:00
SplitContainer *page = this->split_->getContainer();
2018-10-21 13:43:02 +02:00
if (page != nullptr)
{
2018-05-25 14:57:17 +02:00
page->selectNextSplit(SplitContainer::Right);
}
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Tab)
{
if (event->modifiers() == Qt::ControlModifier)
{
2018-08-06 21:17:03 +02:00
SplitContainer *page =
static_cast<SplitContainer *>(this->split_->parentWidget());
2018-08-06 21:17:03 +02:00
Notebook *notebook =
static_cast<Notebook *>(page->parentWidget());
2018-05-23 04:22:17 +02:00
notebook->selectNextTab();
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_Backtab)
{
if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier))
{
2018-08-06 21:17:03 +02:00
SplitContainer *page =
static_cast<SplitContainer *>(this->split_->parentWidget());
2018-08-06 21:17:03 +02:00
Notebook *notebook =
static_cast<Notebook *>(page->parentWidget());
2018-05-23 04:22:17 +02:00
notebook->selectPreviousTab();
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier)
{
if (this->split_->view_->hasSelection())
{
2018-08-02 14:23:27 +02:00
this->split_->copyToClipboard();
2017-09-21 02:20:02 +02:00
event->accept();
}
2018-10-21 13:43:02 +02:00
}
else if (event->key() == Qt::Key_E &&
event->modifiers() == Qt::ControlModifier)
{
2018-08-29 01:22:57 +02:00
this->openEmotePopup();
2017-01-29 13:23:22 +01:00
}
});
2017-01-22 12:46:35 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitInput::clearSelection()
2017-09-21 02:20:02 +02:00
{
2018-06-06 18:57:22 +02:00
QTextCursor c = this->ui_.textEdit->textCursor();
2017-09-21 02:20:02 +02:00
c.setPosition(c.position());
c.setPosition(c.position(), QTextCursor::KeepAnchor);
2018-06-06 18:57:22 +02:00
this->ui_.textEdit->setTextCursor(c);
2017-09-21 02:20:02 +02:00
}
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
}
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
2018-06-06 10:46:23 +02:00
if (text.startsWith("/r ", Qt::CaseInsensitive) &&
2018-06-06 18:57:22 +02:00
this->split_->getChannel()->isTwitchChannel()) //
2018-05-25 13:53:55 +02:00
{
QString lastUser = app->twitch.server->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();
static QRegularExpression spaceRegex("\\s\\s+");
text = text.replace(spaceRegex, " ");
2017-12-17 02:40:05 +01:00
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
}
2017-12-17 02:40:05 +01:00
QString labelText;
2018-10-21 13:43:02 +02:00
if (text.length() == 0 || getSettings()->showMessageLength)
{
2017-12-17 02:40:05 +01:00
labelText = "";
2018-10-21 13:43:02 +02:00
}
else
{
2017-12-17 02:40:05 +01:00
labelText = QString::number(text.length());
}
2018-06-06 18:57:22 +02:00
this->ui_.textEditLength->setText(labelText);
2017-01-29 13:23:22 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitInput::paintEvent(QPaintEvent *)
2017-01-01 02:30:42 +01:00
{
QPainter painter(this);
2018-10-21 13:43:02 +02:00
if (this->theme->isLightTheme())
{
2018-05-25 13:53:55 +02:00
int s = int(3 * this->getScale());
2018-08-09 16:20:09 +02:00
QRect rect = this->rect().marginsRemoved(QMargins(s - 1, s - 1, s, s));
2018-05-23 04:22:17 +02:00
2018-07-06 17:11:37 +02:00
painter.fillRect(rect, this->theme->splits.input.background);
2018-05-23 04:22:17 +02:00
painter.setPen(QColor("#ccc"));
painter.drawRect(rect);
2018-10-21 13:43:02 +02:00
}
else
{
2018-05-25 13:53:55 +02:00
int s = int(1 * this->getScale());
2018-08-09 16:20:09 +02:00
QRect rect = this->rect().marginsRemoved(QMargins(s - 1, s - 1, s, s));
2018-05-23 04:22:17 +02:00
2018-07-06 17:11:37 +02:00
painter.fillRect(rect, this->theme->splits.input.background);
2018-05-23 04:22:17 +02:00
painter.setPen(QColor("#333"));
painter.drawRect(rect);
2017-12-26 16:54:39 +01:00
}
2018-06-06 13:35:06 +02:00
// int offset = 2;
2018-08-06 21:17:03 +02:00
// painter.fillRect(offset, this->height() - offset, this->width() - 2 *
// offset, 1,
2018-06-06 13:35:06 +02:00
// getApp()->themes->splits.input.focusedLine);
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);
}
}
2017-11-12 17:21:50 +01:00
void SplitInput::mousePressEvent(QMouseEvent *)
2017-07-31 01:26:20 +02:00
{
2018-06-06 18:57:22 +02:00
this->split_->giveFocus(Qt::MouseFocusReason);
2017-07-31 01:26:20 +02:00
}
} // namespace chatterino