2017-11-12 17:21:50 +01:00
|
|
|
#include "widgets/helper/resizingtextedit.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "common.hpp"
|
2018-03-24 12:13:09 +01:00
|
|
|
#include "util/completionmodel.hpp"
|
2017-07-09 00:09:02 +02:00
|
|
|
|
|
|
|
ResizingTextEdit::ResizingTextEdit()
|
|
|
|
{
|
|
|
|
auto sizePolicy = this->sizePolicy();
|
|
|
|
sizePolicy.setHeightForWidth(true);
|
|
|
|
sizePolicy.setVerticalPolicy(QSizePolicy::Preferred);
|
|
|
|
this->setSizePolicy(sizePolicy);
|
2017-08-12 12:20:51 +02:00
|
|
|
this->setAcceptRichText(false);
|
2017-07-09 00:09:02 +02:00
|
|
|
|
|
|
|
QObject::connect(this, &QTextEdit::textChanged, this, &QWidget::updateGeometry);
|
2017-07-09 16:33:08 +02:00
|
|
|
|
|
|
|
this->setFocusPolicy(Qt::ClickFocus);
|
2017-07-09 00:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize ResizingTextEdit::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(this->width(), this->heightForWidth(this->width()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResizingTextEdit::hasHeightForWidth() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResizingTextEdit::heightForWidth(int) const
|
|
|
|
{
|
|
|
|
auto margins = this->contentsMargins();
|
|
|
|
|
|
|
|
return margins.top() + document()->size().height() + margins.bottom() + 5;
|
|
|
|
}
|
|
|
|
|
2017-08-01 00:10:02 +02:00
|
|
|
QString ResizingTextEdit::textUnderCursor(bool *hadSpace) const
|
2017-07-09 00:09:02 +02:00
|
|
|
{
|
2017-08-01 00:10:02 +02:00
|
|
|
auto currentText = this->toPlainText();
|
|
|
|
|
|
|
|
QTextCursor tc = this->textCursor();
|
|
|
|
|
|
|
|
auto textUpToCursor = currentText.left(tc.selectionStart());
|
|
|
|
|
|
|
|
auto words = textUpToCursor.splitRef(' ');
|
|
|
|
if (words.size() == 0) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
QString lastWord;
|
|
|
|
for (auto it = words.crbegin(); it != words.crend(); ++it) {
|
|
|
|
auto word = *it;
|
|
|
|
|
|
|
|
if (first && word.isEmpty()) {
|
|
|
|
first = false;
|
|
|
|
if (hadSpace != nullptr) {
|
|
|
|
*hadSpace = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastWord = word.toString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastWord.isEmpty()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastWord;
|
2017-07-09 00:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResizingTextEdit::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
event->ignore();
|
|
|
|
|
2018-04-03 02:55:32 +02:00
|
|
|
this->keyPressed.invoke(event);
|
2017-07-09 00:09:02 +02:00
|
|
|
|
2018-04-10 01:54:30 +02:00
|
|
|
bool doComplete = (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) &&
|
|
|
|
(event->modifiers() & Qt::ControlModifier) == Qt::NoModifier;
|
2018-02-05 15:11:50 +01:00
|
|
|
|
|
|
|
if (doComplete) {
|
|
|
|
// check if there is a completer
|
2018-02-05 21:20:38 +01:00
|
|
|
if (!this->completer) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2017-07-09 00:09:02 +02:00
|
|
|
QString currentCompletionPrefix = this->textUnderCursor();
|
2017-07-09 16:33:08 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
// check if there is something to complete
|
2017-07-09 00:09:02 +02:00
|
|
|
if (!currentCompletionPrefix.size()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-09 16:33:08 +02:00
|
|
|
|
2017-12-17 03:06:39 +01:00
|
|
|
auto *completionModel =
|
2018-03-24 12:13:09 +01:00
|
|
|
static_cast<chatterino::CompletionModel *>(this->completer->model());
|
2017-12-17 03:06:39 +01:00
|
|
|
|
2018-03-05 22:49:19 +01:00
|
|
|
if (!this->completionInProgress) {
|
|
|
|
// First type pressing tab after modifying a message, we refresh our completion model
|
2018-03-30 13:45:27 +02:00
|
|
|
this->completer->setModel(completionModel);
|
2017-12-17 21:05:25 +01:00
|
|
|
completionModel->refresh();
|
2018-03-05 22:49:19 +01:00
|
|
|
this->completionInProgress = true;
|
2017-07-09 00:09:02 +02:00
|
|
|
this->completer->setCompletionPrefix(currentCompletionPrefix);
|
|
|
|
this->completer->complete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scrolling through selections
|
2018-04-10 01:54:30 +02:00
|
|
|
if (event->key() == Qt::Key_Tab) {
|
|
|
|
if (!this->completer->setCurrentRow(this->completer->currentRow() + 1)) {
|
|
|
|
// wrap over and start again
|
|
|
|
this->completer->setCurrentRow(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!this->completer->setCurrentRow(this->completer->currentRow() - 1)) {
|
|
|
|
// wrap over and start again
|
|
|
|
this->completer->setCurrentRow(this->completer->completionCount() - 1);
|
|
|
|
}
|
2017-07-09 00:09:02 +02:00
|
|
|
}
|
2017-07-09 16:33:08 +02:00
|
|
|
|
2017-07-09 00:09:02 +02:00
|
|
|
this->completer->complete();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2017-07-09 00:09:02 +02:00
|
|
|
// (hemirt)
|
|
|
|
// this resets the selection in the completion list, it should probably only trigger on actual
|
|
|
|
// chat input (space, character) and not on every key input (pressing alt for example)
|
2018-04-10 01:54:30 +02:00
|
|
|
// (fourtf)
|
|
|
|
// fixed for shift+tab, there might be a better solution but nobody is gonna bother anyways
|
|
|
|
if (event->key() != Qt::Key_Shift && event->key() != Qt::Key_Control &&
|
|
|
|
event->key() != Qt::Key_Alt && event->key() != Qt::Key_Super_L &&
|
|
|
|
event->key() != Qt::Key_Super_R) {
|
|
|
|
this->completionInProgress = false;
|
|
|
|
}
|
2017-07-09 00:09:02 +02:00
|
|
|
|
|
|
|
if (!event->isAccepted()) {
|
|
|
|
QTextEdit::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 14:57:17 +02:00
|
|
|
void ResizingTextEdit::focusInEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
QTextEdit::focusInEvent(event);
|
|
|
|
|
|
|
|
if (event->gotFocus()) {
|
|
|
|
this->focused.invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:09:02 +02:00
|
|
|
void ResizingTextEdit::setCompleter(QCompleter *c)
|
|
|
|
{
|
|
|
|
if (this->completer) {
|
2018-05-25 14:57:17 +02:00
|
|
|
QObject::disconnect(this->completer, nullptr, this, nullptr);
|
2017-07-09 00:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->completer = c;
|
|
|
|
|
|
|
|
if (!this->completer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->completer->setWidget(this);
|
|
|
|
this->completer->setCompletionMode(QCompleter::InlineCompletion);
|
|
|
|
this->completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
QObject::connect(completer,
|
|
|
|
static_cast<void (QCompleter::*)(const QString &)>(&QCompleter::highlighted),
|
|
|
|
this, &ResizingTextEdit::insertCompletion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizingTextEdit::insertCompletion(const QString &completion)
|
|
|
|
{
|
|
|
|
if (this->completer->widget() != this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-01 00:10:02 +02:00
|
|
|
bool hadSpace = false;
|
|
|
|
auto prefix = this->textUnderCursor(&hadSpace);
|
|
|
|
|
|
|
|
int prefixSize = prefix.size();
|
|
|
|
|
|
|
|
if (hadSpace) {
|
|
|
|
++prefixSize;
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:09:02 +02:00
|
|
|
QTextCursor tc = this->textCursor();
|
2017-08-01 00:10:02 +02:00
|
|
|
tc.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, prefixSize);
|
2017-07-09 00:09:02 +02:00
|
|
|
tc.insertText(completion);
|
|
|
|
this->setTextCursor(tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
QCompleter *ResizingTextEdit::getCompleter() const
|
|
|
|
{
|
|
|
|
return this->completer;
|
|
|
|
}
|