2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
2018-01-10 10:52:59 +03:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QClipboard>
|
2017-09-10 12:58:00 +03:00
|
|
|
#include <QFileDialog>
|
2018-01-10 10:52:59 +03:00
|
|
|
#include <QMimeData>
|
2017-12-01 18:33:49 +03:00
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QMimeType>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOption>
|
2018-03-25 21:50:45 +03:00
|
|
|
#include <QtConcurrent>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "ChatPage.h"
|
2020-01-31 08:12:02 +03:00
|
|
|
#include "Logging.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
#include "TextInputWidget.h"
|
2018-03-25 00:16:15 +03:00
|
|
|
#include "Utils.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "ui/FlatButton.h"
|
|
|
|
#include "ui/LoadingIndicator.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-09-19 23:38:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
#include "emoji/MacHelper.h"
|
|
|
|
#endif
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
static constexpr size_t INPUT_HISTORY_SIZE = 127;
|
2018-01-25 08:57:19 +03:00
|
|
|
static constexpr int MAX_TEXTINPUT_HEIGHT = 120;
|
2018-09-29 14:05:59 +03:00
|
|
|
static constexpr int ButtonHeight = 22;
|
2017-11-06 00:01:21 +03:00
|
|
|
|
2017-04-23 21:31:08 +03:00
|
|
|
FilteredTextEdit::FilteredTextEdit(QWidget *parent)
|
2017-11-06 01:25:47 +03:00
|
|
|
: QTextEdit{parent}
|
|
|
|
, history_index_{0}
|
2019-06-12 04:04:30 +03:00
|
|
|
, suggestionsPopup_{parent}
|
2018-01-10 10:52:59 +03:00
|
|
|
, previewDialog_{parent}
|
2017-04-23 21:31:08 +03:00
|
|
|
{
|
2018-01-25 08:57:19 +03:00
|
|
|
setFrameStyle(QFrame::NoFrame);
|
2017-11-06 00:01:21 +03:00
|
|
|
connect(document()->documentLayout(),
|
|
|
|
&QAbstractTextDocumentLayout::documentSizeChanged,
|
|
|
|
this,
|
|
|
|
&FilteredTextEdit::updateGeometry);
|
2018-01-25 08:57:19 +03:00
|
|
|
connect(document()->documentLayout(),
|
|
|
|
&QAbstractTextDocumentLayout::documentSizeChanged,
|
|
|
|
this,
|
2018-02-20 18:09:11 +03:00
|
|
|
[this]() { emit heightChanged(document()->size().toSize().height()); });
|
2017-11-06 00:01:21 +03:00
|
|
|
working_history_.push_back("");
|
|
|
|
connect(this, &QTextEdit::textChanged, this, &FilteredTextEdit::textChanged);
|
2017-09-03 11:43:45 +03:00
|
|
|
setAcceptRichText(false);
|
2017-10-31 21:11:49 +03:00
|
|
|
|
|
|
|
typingTimer_ = new QTimer(this);
|
|
|
|
typingTimer_->setInterval(1000);
|
|
|
|
typingTimer_->setSingleShot(true);
|
|
|
|
|
|
|
|
connect(typingTimer_, &QTimer::timeout, this, &FilteredTextEdit::stopTyping);
|
2018-01-10 10:52:59 +03:00
|
|
|
connect(&previewDialog_,
|
2018-02-18 23:52:31 +03:00
|
|
|
&dialogs::PreviewUploadOverlay::confirmUpload,
|
2018-01-10 10:52:59 +03:00
|
|
|
this,
|
2018-02-18 23:52:31 +03:00
|
|
|
&FilteredTextEdit::uploadData);
|
2018-01-10 10:52:59 +03:00
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
connect(this, &FilteredTextEdit::resultsRetrieved, this, &FilteredTextEdit::showResults);
|
2019-06-12 04:04:30 +03:00
|
|
|
connect(
|
|
|
|
&suggestionsPopup_, &SuggestionsPopup::itemSelected, this, [this](const QString &text) {
|
|
|
|
suggestionsPopup_.hide();
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2019-06-12 04:04:30 +03:00
|
|
|
auto cursor = textCursor();
|
|
|
|
const int end = cursor.position();
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2019-06-12 04:04:30 +03:00
|
|
|
cursor.setPosition(atTriggerPosition_, QTextCursor::MoveAnchor);
|
|
|
|
cursor.setPosition(end, QTextCursor::KeepAnchor);
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
cursor.insertText(text);
|
|
|
|
});
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-04-10 11:47:23 +03:00
|
|
|
// For cycling through the suggestions by hitting tab.
|
|
|
|
connect(this,
|
2018-04-14 14:12:36 +03:00
|
|
|
&FilteredTextEdit::selectNextSuggestion,
|
2019-06-12 04:04:30 +03:00
|
|
|
&suggestionsPopup_,
|
2018-04-14 14:12:36 +03:00
|
|
|
&SuggestionsPopup::selectNextSuggestion);
|
|
|
|
connect(this,
|
|
|
|
&FilteredTextEdit::selectPreviousSuggestion,
|
2019-06-12 04:04:30 +03:00
|
|
|
&suggestionsPopup_,
|
2018-04-14 14:12:36 +03:00
|
|
|
&SuggestionsPopup::selectPreviousSuggestion);
|
2018-04-27 01:57:46 +03:00
|
|
|
connect(this, &FilteredTextEdit::selectHoveredSuggestion, this, [this]() {
|
2019-06-12 04:04:30 +03:00
|
|
|
suggestionsPopup_.selectHoveredSuggestion<UserItem>();
|
2018-04-27 01:57:46 +03:00
|
|
|
});
|
2018-04-10 11:47:23 +03:00
|
|
|
|
2018-01-10 10:52:59 +03:00
|
|
|
previewDialog_.hide();
|
2017-04-23 21:31:08 +03:00
|
|
|
}
|
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
void
|
2020-01-31 18:08:30 +03:00
|
|
|
FilteredTextEdit::showResults(const std::vector<SearchResult> &results)
|
2018-03-25 00:16:15 +03:00
|
|
|
{
|
|
|
|
QPoint pos;
|
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
if (isAnchorValid()) {
|
2018-03-25 00:16:15 +03:00
|
|
|
auto cursor = textCursor();
|
|
|
|
cursor.setPosition(atTriggerPosition_);
|
|
|
|
pos = viewport()->mapToGlobal(cursorRect(cursor).topLeft());
|
|
|
|
} else {
|
|
|
|
auto rect = cursorRect();
|
|
|
|
pos = viewport()->mapToGlobal(rect.topLeft());
|
|
|
|
}
|
|
|
|
|
2019-06-12 04:04:30 +03:00
|
|
|
suggestionsPopup_.addUsers(results);
|
|
|
|
suggestionsPopup_.move(pos.x(), pos.y() - suggestionsPopup_.height() - 10);
|
|
|
|
suggestionsPopup_.show();
|
2018-03-25 00:16:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
FilteredTextEdit::keyPressEvent(QKeyEvent *event)
|
2017-04-23 21:31:08 +03:00
|
|
|
{
|
2017-11-05 01:19:00 +03:00
|
|
|
const bool isModifier = (event->modifiers() != Qt::NoModifier);
|
|
|
|
|
2018-09-19 23:38:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
if (event->modifiers() == (Qt::ControlModifier | Qt::MetaModifier) &&
|
|
|
|
event->key() == Qt::Key_Space)
|
|
|
|
MacHelper::showEmojiWindow();
|
|
|
|
#endif
|
|
|
|
|
2017-11-05 01:19:00 +03:00
|
|
|
if (!isModifier) {
|
|
|
|
if (!typingTimer_->isActive())
|
|
|
|
emit startedTyping();
|
2017-10-31 21:11:49 +03:00
|
|
|
|
2017-11-05 01:19:00 +03:00
|
|
|
typingTimer_->start();
|
|
|
|
}
|
2017-10-31 21:11:49 +03:00
|
|
|
|
2018-03-25 00:16:15 +03:00
|
|
|
// calculate the new query
|
2018-07-29 21:58:18 +03:00
|
|
|
if (textCursor().position() < atTriggerPosition_ || !isAnchorValid()) {
|
2018-03-25 00:16:15 +03:00
|
|
|
resetAnchor();
|
|
|
|
closeSuggestions();
|
|
|
|
}
|
|
|
|
|
2019-06-12 04:04:30 +03:00
|
|
|
if (suggestionsPopup_.isVisible()) {
|
2018-03-25 00:16:15 +03:00
|
|
|
switch (event->key()) {
|
2018-04-14 14:12:36 +03:00
|
|
|
case Qt::Key_Down:
|
2018-04-10 11:47:23 +03:00
|
|
|
case Qt::Key_Tab:
|
2018-04-14 14:12:36 +03:00
|
|
|
emit selectNextSuggestion();
|
2018-04-10 11:47:23 +03:00
|
|
|
return;
|
2018-03-25 00:16:15 +03:00
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
2018-04-10 11:47:23 +03:00
|
|
|
emit selectHoveredSuggestion();
|
|
|
|
return;
|
2018-03-25 00:16:15 +03:00
|
|
|
case Qt::Key_Escape:
|
|
|
|
closeSuggestions();
|
2018-04-14 14:12:36 +03:00
|
|
|
return;
|
|
|
|
case Qt::Key_Up:
|
|
|
|
case Qt::Key_Backtab: {
|
|
|
|
emit selectPreviousSuggestion();
|
|
|
|
return;
|
2018-03-25 00:16:15 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
switch (event->key()) {
|
2018-03-25 00:16:15 +03:00
|
|
|
case Qt::Key_At:
|
|
|
|
atTriggerPosition_ = textCursor().position();
|
2018-07-29 21:58:18 +03:00
|
|
|
anchorType_ = AnchorType::Sigil;
|
2018-03-25 00:16:15 +03:00
|
|
|
|
|
|
|
QTextEdit::keyPressEvent(event);
|
|
|
|
break;
|
2018-07-29 21:58:18 +03:00
|
|
|
case Qt::Key_Tab: {
|
|
|
|
auto cursor = textCursor();
|
|
|
|
const int initialPos = cursor.position();
|
|
|
|
|
|
|
|
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
|
|
|
auto word = cursor.selectedText();
|
|
|
|
|
|
|
|
const int startOfWord = cursor.position();
|
|
|
|
|
|
|
|
// There is a word to complete.
|
|
|
|
if (initialPos != startOfWord) {
|
|
|
|
atTriggerPosition_ = startOfWord;
|
|
|
|
anchorType_ = AnchorType::Tab;
|
|
|
|
|
|
|
|
emit showSuggestions(word);
|
|
|
|
} else {
|
|
|
|
QTextEdit::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-11-06 00:01:21 +03:00
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
if (!(event->modifiers() & Qt::ShiftModifier)) {
|
|
|
|
stopTyping();
|
|
|
|
submit();
|
|
|
|
} else {
|
|
|
|
QTextEdit::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::Key_Up: {
|
|
|
|
auto initial_cursor = textCursor();
|
|
|
|
QTextEdit::keyPressEvent(event);
|
2018-01-16 21:51:46 +03:00
|
|
|
|
|
|
|
if (textCursor() == initial_cursor && textCursor().atStart() &&
|
2017-11-06 00:01:21 +03:00
|
|
|
history_index_ + 1 < working_history_.size()) {
|
|
|
|
++history_index_;
|
|
|
|
setPlainText(working_history_[history_index_]);
|
|
|
|
moveCursor(QTextCursor::End);
|
2018-01-17 11:16:54 +03:00
|
|
|
} else if (textCursor() == initial_cursor) {
|
|
|
|
// Move to the start of the text if there aren't any lines to move up to.
|
2018-01-16 21:51:46 +03:00
|
|
|
initial_cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
|
|
|
|
setTextCursor(initial_cursor);
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::Key_Down: {
|
|
|
|
auto initial_cursor = textCursor();
|
2017-09-03 11:43:45 +03:00
|
|
|
QTextEdit::keyPressEvent(event);
|
2018-01-16 21:51:46 +03:00
|
|
|
|
|
|
|
if (textCursor() == initial_cursor && textCursor().atEnd() && history_index_ > 0) {
|
2017-11-06 00:01:21 +03:00
|
|
|
--history_index_;
|
|
|
|
setPlainText(working_history_[history_index_]);
|
|
|
|
moveCursor(QTextCursor::End);
|
2018-01-17 11:16:54 +03:00
|
|
|
} else if (textCursor() == initial_cursor) {
|
|
|
|
// Move to the end of the text if there aren't any lines to move down to.
|
2018-01-16 21:51:46 +03:00
|
|
|
initial_cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
|
|
|
|
setTextCursor(initial_cursor);
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
QTextEdit::keyPressEvent(event);
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
if (isModifier)
|
|
|
|
return;
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
if (textCursor().position() == 0) {
|
2018-07-15 21:26:48 +03:00
|
|
|
resetAnchor();
|
2018-03-25 00:16:15 +03:00
|
|
|
closeSuggestions();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
// Check if the current word should be autocompleted.
|
|
|
|
auto cursor = textCursor();
|
|
|
|
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
|
|
|
auto word = cursor.selectedText();
|
2018-03-25 00:16:15 +03:00
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
if (hasAnchor(cursor.position(), anchorType_) && isAnchorValid()) {
|
|
|
|
if (word.isEmpty()) {
|
2018-03-25 00:16:15 +03:00
|
|
|
closeSuggestions();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-29 21:58:18 +03:00
|
|
|
emit showSuggestions(word);
|
2018-03-25 00:16:15 +03:00
|
|
|
} else {
|
2018-07-15 21:26:48 +03:00
|
|
|
resetAnchor();
|
2018-03-25 00:16:15 +03:00
|
|
|
closeSuggestions();
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
break;
|
2017-10-31 21:11:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 10:52:59 +03:00
|
|
|
bool
|
|
|
|
FilteredTextEdit::canInsertFromMimeData(const QMimeData *source) const
|
|
|
|
{
|
|
|
|
return (source->hasImage() || QTextEdit::canInsertFromMimeData(source));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredTextEdit::insertFromMimeData(const QMimeData *source)
|
|
|
|
{
|
2020-05-18 15:02:54 +03:00
|
|
|
qInfo() << "Got mime formats: \n" << source->formats();
|
2018-02-18 23:52:31 +03:00
|
|
|
const auto formats = source->formats().filter("/");
|
|
|
|
const auto image = formats.filter("image/", Qt::CaseInsensitive);
|
|
|
|
const auto audio = formats.filter("audio/", Qt::CaseInsensitive);
|
|
|
|
const auto video = formats.filter("video/", Qt::CaseInsensitive);
|
|
|
|
|
2019-07-22 03:38:44 +03:00
|
|
|
if (source->hasImage()) {
|
|
|
|
QImage img = qvariant_cast<QImage>(source->imageData());
|
|
|
|
previewDialog_.setPreview(img, image.front());
|
2018-02-18 23:52:31 +03:00
|
|
|
} else if (!audio.empty()) {
|
|
|
|
showPreview(source, audio);
|
|
|
|
} else if (!video.empty()) {
|
|
|
|
showPreview(source, video);
|
|
|
|
} else if (source->hasUrls()) {
|
|
|
|
// Generic file path for any platform.
|
|
|
|
QString path;
|
|
|
|
for (auto &&u : source->urls()) {
|
|
|
|
if (u.isLocalFile()) {
|
|
|
|
path = u.toLocalFile();
|
|
|
|
break;
|
|
|
|
}
|
2018-01-10 10:52:59 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 23:52:31 +03:00
|
|
|
if (!path.isEmpty() && QFileInfo{path}.exists()) {
|
|
|
|
previewDialog_.setPreview(path);
|
|
|
|
} else {
|
|
|
|
qWarning()
|
|
|
|
<< "Clipboard does not contain any valid file paths:" << source->urls();
|
|
|
|
}
|
|
|
|
} else if (source->hasFormat("x-special/gnome-copied-files")) {
|
2018-01-10 10:52:59 +03:00
|
|
|
// Special case for X11 users. See "Notes for X11 Users" in source.
|
|
|
|
// Source: http://doc.qt.io/qt-5/qclipboard.html
|
2018-02-18 23:52:31 +03:00
|
|
|
|
|
|
|
// This MIME type returns a string with multiple lines separated by '\n'. The first
|
|
|
|
// line is the command to perform with the clipboard (not useful to us). The
|
|
|
|
// following lines are the file URIs.
|
|
|
|
//
|
|
|
|
// Source: the nautilus source code in file 'src/nautilus-clipboard.c' in function
|
|
|
|
// nautilus_clipboard_get_uri_list_from_selection_data()
|
|
|
|
// https://github.com/GNOME/nautilus/blob/master/src/nautilus-clipboard.c
|
|
|
|
|
|
|
|
auto data = source->data("x-special/gnome-copied-files").split('\n');
|
|
|
|
if (data.size() < 2) {
|
|
|
|
qWarning() << "MIME format is malformed, cannot perform paste.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path;
|
|
|
|
for (int i = 1; i < data.size(); ++i) {
|
|
|
|
QUrl url{data[i]};
|
|
|
|
if (url.isLocalFile()) {
|
|
|
|
path = url.toLocalFile();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
previewDialog_.setPreview(path);
|
|
|
|
} else {
|
|
|
|
qWarning() << "Clipboard does not contain any valid file paths:" << data;
|
|
|
|
}
|
2018-01-10 10:52:59 +03:00
|
|
|
} else {
|
|
|
|
QTextEdit::insertFromMimeData(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 21:11:49 +03:00
|
|
|
void
|
|
|
|
FilteredTextEdit::stopTyping()
|
|
|
|
{
|
|
|
|
typingTimer_->stop();
|
|
|
|
emit stoppedTyping();
|
2017-04-23 21:31:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
QSize
|
|
|
|
FilteredTextEdit::sizeHint() const
|
|
|
|
{
|
|
|
|
ensurePolished();
|
|
|
|
auto margins = viewportMargins();
|
|
|
|
margins += document()->documentMargin();
|
|
|
|
QSize size = document()->size().toSize();
|
|
|
|
size.rwidth() += margins.left() + margins.right();
|
|
|
|
size.rheight() += margins.top() + margins.bottom();
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
|
|
|
FilteredTextEdit::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
ensurePolished();
|
|
|
|
auto margins = viewportMargins();
|
|
|
|
margins += document()->documentMargin();
|
|
|
|
margins += contentsMargins();
|
|
|
|
QSize size(fontMetrics().averageCharWidth() * 10,
|
|
|
|
fontMetrics().lineSpacing() + margins.top() + margins.bottom());
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredTextEdit::submit()
|
|
|
|
{
|
2017-11-16 17:33:52 +03:00
|
|
|
if (toPlainText().trimmed().isEmpty())
|
|
|
|
return;
|
2017-11-16 15:42:13 +03:00
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
if (true_history_.size() == INPUT_HISTORY_SIZE)
|
|
|
|
true_history_.pop_back();
|
|
|
|
true_history_.push_front(toPlainText());
|
|
|
|
working_history_ = true_history_;
|
|
|
|
working_history_.push_front("");
|
|
|
|
history_index_ = 0;
|
|
|
|
|
|
|
|
QString text = toPlainText();
|
2018-01-10 10:52:59 +03:00
|
|
|
|
2017-11-06 00:01:21 +03:00
|
|
|
if (text.startsWith('/')) {
|
|
|
|
int command_end = text.indexOf(' ');
|
|
|
|
if (command_end == -1)
|
|
|
|
command_end = text.size();
|
|
|
|
auto name = text.mid(1, command_end - 1);
|
|
|
|
auto args = text.mid(command_end + 1);
|
|
|
|
if (name.isEmpty() || name == "/") {
|
2020-04-13 17:22:30 +03:00
|
|
|
message(args);
|
2017-11-06 00:01:21 +03:00
|
|
|
} else {
|
|
|
|
command(name, args);
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 17:22:30 +03:00
|
|
|
message(std::move(text));
|
2017-11-06 00:01:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredTextEdit::textChanged()
|
|
|
|
{
|
|
|
|
working_history_[history_index_] = toPlainText();
|
|
|
|
}
|
|
|
|
|
2018-01-10 10:52:59 +03:00
|
|
|
void
|
2019-12-05 17:31:53 +03:00
|
|
|
FilteredTextEdit::uploadData(const QByteArray data,
|
|
|
|
const QString &mediaType,
|
|
|
|
const QString &filename)
|
2018-01-10 10:52:59 +03:00
|
|
|
{
|
|
|
|
QSharedPointer<QBuffer> buffer{new QBuffer{this}};
|
2018-02-18 23:52:31 +03:00
|
|
|
buffer->setData(data);
|
|
|
|
|
|
|
|
emit startedUpload();
|
|
|
|
|
2020-04-13 17:22:30 +03:00
|
|
|
emit media(buffer, mediaType, filename);
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredTextEdit::showPreview(const QMimeData *source, const QStringList &formats)
|
|
|
|
{
|
|
|
|
// Retrieve data as MIME type.
|
|
|
|
auto const &mime = formats.first();
|
|
|
|
QByteArray data = source->data(mime);
|
|
|
|
previewDialog_.setPreview(data, mime);
|
2018-01-10 10:52:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
TextInputWidget::TextInputWidget(QWidget *parent)
|
2018-01-25 08:57:19 +03:00
|
|
|
: QWidget(parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-10-06 18:07:02 +03:00
|
|
|
QFont f;
|
|
|
|
f.setPointSizeF(f.pointSizeF());
|
|
|
|
const int fontHeight = QFontMetrics(f).height();
|
2019-03-03 21:34:57 +03:00
|
|
|
const int contentHeight = static_cast<int>(fontHeight * 2.5);
|
|
|
|
const int InputHeight = static_cast<int>(fontHeight * 1.5);
|
2018-10-06 18:07:02 +03:00
|
|
|
|
|
|
|
setFixedHeight(contentHeight);
|
2017-09-03 11:43:45 +03:00
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
topLayout_ = new QHBoxLayout();
|
2017-10-04 11:33:34 +03:00
|
|
|
topLayout_->setSpacing(0);
|
2019-03-03 18:41:46 +03:00
|
|
|
topLayout_->setContentsMargins(13, 1, 13, 0);
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2020-08-14 02:03:27 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2020-07-11 02:19:48 +03:00
|
|
|
callBtn_ = new FlatButton(this);
|
2020-07-23 04:15:45 +03:00
|
|
|
changeCallButtonState(WebRTCSession::State::DISCONNECTED);
|
2020-07-11 02:19:48 +03:00
|
|
|
connect(&WebRTCSession::instance(),
|
2020-07-23 04:15:45 +03:00
|
|
|
&WebRTCSession::stateChanged,
|
2020-07-11 02:19:48 +03:00
|
|
|
this,
|
|
|
|
&TextInputWidget::changeCallButtonState);
|
2020-08-14 02:03:27 +03:00
|
|
|
#endif
|
2020-07-11 02:19:48 +03:00
|
|
|
|
2017-09-03 11:43:45 +03:00
|
|
|
QIcon send_file_icon;
|
2017-10-15 22:08:51 +03:00
|
|
|
send_file_icon.addFile(":/icons/icons/ui/paper-clip-outline.png");
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
sendFileBtn_ = new FlatButton(this);
|
2018-07-25 23:28:37 +03:00
|
|
|
sendFileBtn_->setToolTip(tr("Send a file"));
|
2017-09-10 12:58:00 +03:00
|
|
|
sendFileBtn_->setIcon(send_file_icon);
|
2018-03-18 18:08:13 +03:00
|
|
|
sendFileBtn_->setIconSize(QSize(ButtonHeight, ButtonHeight));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
spinner_ = new LoadingIndicator(this);
|
2018-03-18 18:08:13 +03:00
|
|
|
spinner_->setFixedHeight(InputHeight);
|
|
|
|
spinner_->setFixedWidth(InputHeight);
|
2017-11-16 17:33:52 +03:00
|
|
|
spinner_->setObjectName("FileUploadSpinner");
|
2017-09-10 12:58:00 +03:00
|
|
|
spinner_->hide();
|
2017-09-03 11:43:45 +03:00
|
|
|
|
|
|
|
input_ = new FilteredTextEdit(this);
|
2018-03-18 18:08:13 +03:00
|
|
|
input_->setFixedHeight(InputHeight);
|
2018-01-25 08:57:19 +03:00
|
|
|
input_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2017-09-03 11:43:45 +03:00
|
|
|
input_->setPlaceholderText(tr("Write a message..."));
|
2018-01-25 08:57:19 +03:00
|
|
|
|
2018-10-06 18:07:02 +03:00
|
|
|
connect(input_,
|
|
|
|
&FilteredTextEdit::heightChanged,
|
|
|
|
this,
|
|
|
|
[this, InputHeight, contentHeight](int height) {
|
|
|
|
int widgetHeight =
|
|
|
|
std::min(MAX_TEXTINPUT_HEIGHT, std::max(height, contentHeight));
|
2019-03-03 18:41:46 +03:00
|
|
|
int textInputHeight =
|
|
|
|
std::min(widgetHeight - 1, std::max(height, InputHeight));
|
2018-01-25 08:57:19 +03:00
|
|
|
|
2018-10-06 18:07:02 +03:00
|
|
|
setFixedHeight(widgetHeight);
|
|
|
|
input_->setFixedHeight(textInputHeight);
|
2018-08-09 18:20:17 +03:00
|
|
|
|
2018-10-06 18:07:02 +03:00
|
|
|
emit heightChanged(widgetHeight);
|
|
|
|
});
|
2018-03-25 00:16:15 +03:00
|
|
|
connect(input_, &FilteredTextEdit::showSuggestions, this, [this](const QString &q) {
|
2019-12-15 04:56:04 +03:00
|
|
|
if (q.isEmpty())
|
2018-03-25 00:16:15 +03:00
|
|
|
return;
|
|
|
|
|
2018-03-25 21:50:45 +03:00
|
|
|
QtConcurrent::run([this, q = q.toLower().toStdString()]() {
|
2018-04-21 16:34:50 +03:00
|
|
|
try {
|
2019-12-15 04:56:04 +03:00
|
|
|
emit input_->resultsRetrieved(cache::searchUsers(
|
2018-04-21 16:34:50 +03:00
|
|
|
ChatPage::instance()->currentRoom().toStdString(), q));
|
|
|
|
} catch (const lmdb::error &e) {
|
2019-12-23 07:22:03 +03:00
|
|
|
nhlog::db()->error("Suggestion retrieval failed: {}", e.what());
|
2018-03-25 00:16:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
sendMessageBtn_ = new FlatButton(this);
|
2018-07-25 23:28:37 +03:00
|
|
|
sendMessageBtn_->setToolTip(tr("Send a message"));
|
2017-09-03 11:43:45 +03:00
|
|
|
|
|
|
|
QIcon send_message_icon;
|
2017-10-15 22:08:51 +03:00
|
|
|
send_message_icon.addFile(":/icons/icons/ui/cursor.png");
|
2017-09-10 12:58:00 +03:00
|
|
|
sendMessageBtn_->setIcon(send_message_icon);
|
2018-03-18 18:08:13 +03:00
|
|
|
sendMessageBtn_->setIconSize(QSize(ButtonHeight, ButtonHeight));
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2019-01-26 21:17:08 +03:00
|
|
|
emojiBtn_ = new emoji::PickButton(this);
|
|
|
|
emojiBtn_->setToolTip(tr("Emoji"));
|
|
|
|
|
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
// macOS has a native emoji picker.
|
|
|
|
emojiBtn_->hide();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QIcon emoji_icon;
|
|
|
|
emoji_icon.addFile(":/icons/icons/ui/smile.png");
|
|
|
|
emojiBtn_->setIcon(emoji_icon);
|
|
|
|
emojiBtn_->setIconSize(QSize(ButtonHeight, ButtonHeight));
|
|
|
|
|
2020-08-14 02:03:27 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2020-07-11 02:19:48 +03:00
|
|
|
topLayout_->addWidget(callBtn_);
|
2020-08-14 02:03:27 +03:00
|
|
|
#endif
|
2017-09-10 12:58:00 +03:00
|
|
|
topLayout_->addWidget(sendFileBtn_);
|
|
|
|
topLayout_->addWidget(input_);
|
2019-01-26 21:17:08 +03:00
|
|
|
topLayout_->addWidget(emojiBtn_);
|
2017-09-10 12:58:00 +03:00
|
|
|
topLayout_->addWidget(sendMessageBtn_);
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
setLayout(topLayout_);
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2020-08-14 02:03:27 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2020-07-11 02:19:48 +03:00
|
|
|
connect(callBtn_, &FlatButton::clicked, this, &TextInputWidget::callButtonPress);
|
2020-08-14 02:03:27 +03:00
|
|
|
#endif
|
2017-11-06 00:01:21 +03:00
|
|
|
connect(sendMessageBtn_, &FlatButton::clicked, input_, &FilteredTextEdit::submit);
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(sendFileBtn_, SIGNAL(clicked()), this, SLOT(openFileSelection()));
|
2017-11-06 00:01:21 +03:00
|
|
|
connect(input_, &FilteredTextEdit::message, this, &TextInputWidget::sendTextMessage);
|
|
|
|
connect(input_, &FilteredTextEdit::command, this, &TextInputWidget::command);
|
2019-12-05 17:31:53 +03:00
|
|
|
connect(input_, &FilteredTextEdit::media, this, &TextInputWidget::uploadMedia);
|
2019-01-26 21:17:08 +03:00
|
|
|
connect(emojiBtn_,
|
|
|
|
SIGNAL(emojiSelected(const QString &)),
|
|
|
|
this,
|
|
|
|
SLOT(addSelectedEmoji(const QString &)));
|
|
|
|
|
2017-10-31 21:11:49 +03:00
|
|
|
connect(input_, &FilteredTextEdit::startedTyping, this, &TextInputWidget::startedTyping);
|
|
|
|
|
|
|
|
connect(input_, &FilteredTextEdit::stoppedTyping, this, &TextInputWidget::stoppedTyping);
|
2018-02-18 23:52:31 +03:00
|
|
|
|
|
|
|
connect(
|
|
|
|
input_, &FilteredTextEdit::startedUpload, this, &TextInputWidget::showUploadSpinner);
|
2017-04-23 21:31:08 +03:00
|
|
|
}
|
|
|
|
|
2019-01-26 21:17:08 +03:00
|
|
|
void
|
|
|
|
TextInputWidget::addSelectedEmoji(const QString &emoji)
|
|
|
|
{
|
|
|
|
QTextCursor cursor = input_->textCursor();
|
|
|
|
|
|
|
|
QTextCharFormat charfmt;
|
|
|
|
input_->setCurrentCharFormat(charfmt);
|
|
|
|
|
|
|
|
input_->insertPlainText(emoji);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
|
|
|
|
input_->setCurrentCharFormat(charfmt);
|
|
|
|
|
|
|
|
input_->show();
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-11-06 00:01:21 +03:00
|
|
|
TextInputWidget::command(QString command, QString args)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-11-06 00:01:21 +03:00
|
|
|
if (command == "me") {
|
2020-04-13 17:22:30 +03:00
|
|
|
sendEmoteMessage(args);
|
2017-11-06 00:01:21 +03:00
|
|
|
} else if (command == "join") {
|
|
|
|
sendJoinRoomRequest(args);
|
2020-01-29 02:30:53 +03:00
|
|
|
} else if (command == "invite") {
|
|
|
|
sendInviteRoomRequest(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
|
|
|
} else if (command == "kick") {
|
|
|
|
sendKickRoomRequest(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
|
|
|
} else if (command == "ban") {
|
|
|
|
sendBanRoomRequest(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
|
|
|
} else if (command == "unban") {
|
|
|
|
sendUnbanRoomRequest(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
2020-05-18 15:02:14 +03:00
|
|
|
} else if (command == "roomnick") {
|
|
|
|
changeRoomNick(args);
|
2018-01-16 23:34:31 +03:00
|
|
|
} else if (command == "shrug") {
|
2020-04-13 17:22:30 +03:00
|
|
|
sendTextMessage("¯\\_(ツ)_/¯");
|
2018-01-16 23:34:31 +03:00
|
|
|
} else if (command == "fliptable") {
|
2020-04-13 17:22:30 +03:00
|
|
|
sendTextMessage("(╯°□°)╯︵ ┻━┻");
|
2020-02-01 23:30:10 +03:00
|
|
|
} else if (command == "unfliptable") {
|
2020-04-13 17:22:30 +03:00
|
|
|
sendTextMessage(" ┯━┯╭( º _ º╭)");
|
2020-02-01 23:30:10 +03:00
|
|
|
} else if (command == "sovietflip") {
|
2020-04-13 17:22:30 +03:00
|
|
|
sendTextMessage("ノ┬─┬ノ ︵ ( \\o°o)\\");
|
2017-09-03 11:43:45 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
void
|
|
|
|
TextInputWidget::openFileSelection()
|
|
|
|
{
|
2020-02-15 04:35:26 +03:00
|
|
|
const QString homeFolder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
|
2017-12-01 18:33:49 +03:00
|
|
|
const auto fileName =
|
2020-02-15 04:35:26 +03:00
|
|
|
QFileDialog::getOpenFileName(this, tr("Select a file"), homeFolder, tr("All Files (*)"));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2017-12-01 18:33:49 +03:00
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType mime = db.mimeTypeForFile(fileName, QMimeDatabase::MatchContent);
|
|
|
|
|
|
|
|
const auto format = mime.name().split("/")[0];
|
2017-11-30 00:39:35 +03:00
|
|
|
|
2018-01-10 10:52:59 +03:00
|
|
|
QSharedPointer<QFile> file{new QFile{fileName, this}};
|
2019-12-05 17:31:53 +03:00
|
|
|
|
2020-04-13 17:22:30 +03:00
|
|
|
emit uploadMedia(file, format, QFileInfo(fileName).fileName());
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
showUploadSpinner();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextInputWidget::showUploadSpinner()
|
|
|
|
{
|
|
|
|
topLayout_->removeWidget(sendFileBtn_);
|
|
|
|
sendFileBtn_->hide();
|
|
|
|
|
|
|
|
topLayout_->insertWidget(0, spinner_);
|
|
|
|
spinner_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextInputWidget::hideUploadSpinner()
|
|
|
|
{
|
|
|
|
topLayout_->removeWidget(spinner_);
|
|
|
|
topLayout_->insertWidget(0, sendFileBtn_);
|
|
|
|
sendFileBtn_->show();
|
|
|
|
spinner_->stop();
|
|
|
|
}
|
|
|
|
|
2017-10-31 21:11:49 +03:00
|
|
|
void
|
|
|
|
TextInputWidget::stopTyping()
|
|
|
|
{
|
|
|
|
input_->stopTyping();
|
|
|
|
}
|
2017-11-03 09:54:17 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
TextInputWidget::focusInEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
input_->setFocus(event->reason());
|
|
|
|
}
|
2017-11-16 17:33:52 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
TextInputWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
2018-03-18 18:08:13 +03:00
|
|
|
|
2017-11-16 17:33:52 +03:00
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|
2020-07-11 02:19:48 +03:00
|
|
|
|
|
|
|
void
|
2020-07-23 04:15:45 +03:00
|
|
|
TextInputWidget::changeCallButtonState(WebRTCSession::State state)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
|
|
|
QIcon icon;
|
2020-07-26 17:59:50 +03:00
|
|
|
if (state == WebRTCSession::State::ICEFAILED ||
|
|
|
|
state == WebRTCSession::State::DISCONNECTED) {
|
2020-07-11 02:19:48 +03:00
|
|
|
callBtn_->setToolTip(tr("Place a call"));
|
2020-07-23 04:15:45 +03:00
|
|
|
icon.addFile(":/icons/icons/ui/place-call.png");
|
|
|
|
} else {
|
|
|
|
callBtn_->setToolTip(tr("Hang up"));
|
|
|
|
icon.addFile(":/icons/icons/ui/end-call.png");
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
callBtn_->setIcon(icon);
|
2020-07-23 04:15:45 +03:00
|
|
|
callBtn_->setIconSize(QSize(ButtonHeight * 1.1, ButtonHeight * 1.1));
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|