diff --git a/src/widgets/room/autotests/messagelineedittest.cpp b/src/widgets/room/autotests/messagelineedittest.cpp index 80bae44e..2e154ee3 100644 --- a/src/widgets/room/autotests/messagelineedittest.cpp +++ b/src/widgets/room/autotests/messagelineedittest.cpp @@ -1,34 +1,35 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagelineedittest.h" #include "room/messagelineedit.h" #include QTEST_MAIN(MessageLineEditTest) MessageLineEditTest::MessageLineEditTest(QObject *parent) : QObject(parent) { } void MessageLineEditTest::shouldHaveDefautValues() { MessageLineEdit w; - QVERIFY(w.isClearButtonEnabled()); + w.insert(QStringLiteral("test")); + QCOMPARE(w.text(), QStringLiteral("test")); } diff --git a/src/widgets/room/messagelineedit.cpp b/src/widgets/room/messagelineedit.cpp index 89bea500..5bdf973b 100644 --- a/src/widgets/room/messagelineedit.cpp +++ b/src/widgets/room/messagelineedit.cpp @@ -1,83 +1,107 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagelineedit.h" #include "rocketchataccount.h" #include "model/inputcompletermodel.h" #include "common/completionlistview.h" #include "ruqola.h" #include #include #include #include #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) #include #include #endif MessageLineEdit::MessageLineEdit(QWidget *parent) - : CompletionLineEdit(parent) + : CompletionTextEdit(parent) { - connect(this, &MessageLineEdit::returnPressed, this, [this]() { - Q_EMIT sendMessage(text()); - clear(); - }); - connect(this, &QLineEdit::textChanged, this, &MessageLineEdit::slotTextChanged); + connect(this, &QTextEdit::textChanged, this, &MessageLineEdit::slotTextChanged); setCompletionModel(Ruqola::self()->rocketChatAccount()->inputCompleterModel()); connect(this, &MessageLineEdit::complete, this, &MessageLineEdit::slotComplete); } MessageLineEdit::~MessageLineEdit() { } +void MessageLineEdit::insert(const QString &text) +{ + textCursor().insertText(text); +} + +QString MessageLineEdit::text() const +{ + return toPlainText(); +} + void MessageLineEdit::keyPressEvent(QKeyEvent *e) { + const int key = e->key(); + if (key == Qt::Key_Return) { + if (!e->modifiers()) { + Q_EMIT sendMessage(text()); + clear(); + } else { + textCursor().insertBlock(); + ensureCursorVisible(); + } + e->accept(); + return; + } else if (key == Qt::Key_Up || key == Qt::Key_Down) { + if (document()->blockCount() > 1) { + CompletionTextEdit::keyPressEvent(e); + return; + } + } + e->ignore(); // Check if the listview or room widget want to handle the key (e.g Esc, PageUp) Q_EMIT keyPressed(e); if (e->isAccepted()) { return; } - CompletionLineEdit::keyPressEvent(e); + CompletionTextEdit::keyPressEvent(e); } -void MessageLineEdit::slotTextChanged(const QString &text) +void MessageLineEdit::slotTextChanged() { auto *rcAccount = Ruqola::self()->rocketChatAccount(); - rcAccount->setInputTextChanged(text, cursorPosition()); - Q_EMIT textEditing(text.isEmpty()); + rcAccount->setInputTextChanged(text(), textCursor().position()); + Q_EMIT textEditing(document()->isEmpty()); } void MessageLineEdit::slotComplete(const QModelIndex &index) { const QString completerName = index.data(InputCompleterModel::CompleterName).toString(); auto *rcAccount = Ruqola::self()->rocketChatAccount(); - const QString newText = rcAccount->replaceWord(completerName + QLatin1Char(' '), text(), cursorPosition()); + const QString newText = rcAccount->replaceWord(completerName + QLatin1Char(' '), text(), textCursor().position()); mCompletionListView->hide(); - disconnect(this, &QLineEdit::textChanged, this, &MessageLineEdit::slotTextChanged); - setText(newText); - connect(this, &QLineEdit::textChanged, this, &MessageLineEdit::slotTextChanged); + disconnect(this, &QTextEdit::textChanged, this, &MessageLineEdit::slotTextChanged); + setPlainText(newText); + connect(this, &QTextEdit::textChanged, this, &MessageLineEdit::slotTextChanged); } diff --git a/src/widgets/room/messagelineedit.h b/src/widgets/room/messagelineedit.h index aced9701..5a273ba9 100644 --- a/src/widgets/room/messagelineedit.h +++ b/src/widgets/room/messagelineedit.h @@ -1,53 +1,58 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef MESSAGELINEEDIT_H #define MESSAGELINEEDIT_H -#include - -#include "common/completionlineedit.h" +#include "common/completiontextedit.h" #include "libruqolawidgets_private_export.h" class QListView; -class LIBRUQOLAWIDGETS_TESTS_EXPORT MessageLineEdit : public CompletionLineEdit +/** + * @brief The MessageLineEdit class is the widget used for typing messages to be sent. + * @todo rename to MessageTextEdit + */ +class LIBRUQOLAWIDGETS_TESTS_EXPORT MessageLineEdit : public CompletionTextEdit { Q_OBJECT public: explicit MessageLineEdit(QWidget *parent = nullptr); ~MessageLineEdit(); + void insert(const QString &text); + QString text() const; + Q_SIGNALS: void sendMessage(const QString &str); void keyPressed(QKeyEvent *ev); void textEditing(bool clearNotification); protected: void keyPressEvent(QKeyEvent *e) override; private: - void slotTextChanged(const QString &text); + void slotTextChanged(); void slotCompletionAvailable(); void slotComplete(const QModelIndex &index); }; #endif // MESSAGELINEEDIT_H diff --git a/src/widgets/room/messagelinewidget.cpp b/src/widgets/room/messagelinewidget.cpp index dca1dee4..ccccedd0 100644 --- a/src/widgets/room/messagelinewidget.cpp +++ b/src/widgets/room/messagelinewidget.cpp @@ -1,137 +1,137 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagelineedit.h" #include "messagelinewidget.h" #include "misc/emoticonmenuwidget.h" #include "dialogs/uploadfiledialog.h" #include #include #include #include #include MessageLineWidget::MessageLineWidget(QWidget *parent) : QWidget(parent) { auto *mainLayout = new QHBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainLayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setSpacing(0); mSendFile = new QToolButton(this); mSendFile->setObjectName(QStringLiteral("mSendFile")); mainLayout->addWidget(mSendFile); mSendFile->setIcon(QIcon::fromTheme(QStringLiteral("document-send-symbolic"))); connect(mSendFile, &QToolButton::clicked, this, &MessageLineWidget::slotSendFile); - mMessageLineEdit = new MessageLineEdit(this); - mMessageLineEdit->setObjectName(QStringLiteral("mMessageLineEdit")); - mainLayout->addWidget(mMessageLineEdit); - connect(mMessageLineEdit, &MessageLineEdit::sendMessage, this, &MessageLineWidget::slotSendMessage); - connect(mMessageLineEdit, &MessageLineEdit::textEditing, this, &MessageLineWidget::textEditing); + mMessageTextEdit = new MessageLineEdit(this); + mMessageTextEdit->setObjectName(QStringLiteral("mMessageLineEdit")); + mainLayout->addWidget(mMessageTextEdit); + connect(mMessageTextEdit, &MessageLineEdit::sendMessage, this, &MessageLineWidget::slotSendMessage); + connect(mMessageTextEdit, &MessageLineEdit::textEditing, this, &MessageLineWidget::textEditing); mEmoticonButton = new QToolButton(this); mEmoticonButton->setObjectName(QStringLiteral("mEmoticonButton")); mEmoticonButton->setIcon(QIcon::fromTheme(QStringLiteral("face-smile"))); mEmoticonButton->setPopupMode(QToolButton::InstantPopup); mainLayout->addWidget(mEmoticonButton); mSendMessageButton = new QToolButton(this); mSendMessageButton->setObjectName(QStringLiteral("mSendMessageButton")); mSendMessageButton->setIcon(QIcon::fromTheme(QStringLiteral("mail-sent"))); mainLayout->addWidget(mSendMessageButton); connect(mSendMessageButton, &QToolButton::clicked, this, [this]() { - slotSendMessage(mMessageLineEdit->text()); - mMessageLineEdit->clear(); + slotSendMessage(mMessageTextEdit->text()); + mMessageTextEdit->clear(); }); auto *emoticonMenu = new QMenu(this); auto *action = new QWidgetAction(emoticonMenu); mEmoticonMenuWidget = new EmoticonMenuWidget(this); action->setDefaultWidget(mEmoticonMenuWidget); emoticonMenu->addAction(action); mEmoticonButton->setMenu(emoticonMenu); - connect(mEmoticonMenuWidget, &EmoticonMenuWidget::insertEmoticons, mMessageLineEdit, &MessageLineEdit::insert); + connect(mEmoticonMenuWidget, &EmoticonMenuWidget::insertEmoticons, mMessageTextEdit, &MessageLineEdit::insert); - setFocusProxy(mMessageLineEdit); + setFocusProxy(mMessageTextEdit); } MessageLineWidget::~MessageLineWidget() { } void MessageLineWidget::slotSendMessage(const QString &msg) { if (!msg.isEmpty()) { Q_EMIT sendMessage(msg); } } void MessageLineWidget::setCurrentRocketChatAccount(RocketChatAccount *account) { mEmoticonMenuWidget->setCurrentRocketChatAccount(account); } void MessageLineWidget::setText(const QString &text) { - mMessageLineEdit->setText(text); + mMessageTextEdit->setText(text); } QString MessageLineWidget::text() const { - return mMessageLineEdit->text(); + return mMessageTextEdit->text(); } MessageLineEdit *MessageLineWidget::messageLineEdit() const { - return mMessageLineEdit; + return mMessageTextEdit; } void MessageLineWidget::slotSendFile() { QPointer dlg = new UploadFileDialog(this); if (dlg->exec()) { const UploadFileDialog::UploadFileInfo result = dlg->fileInfo(); Q_EMIT sendFile(result); } delete dlg; } MessageLineWidget::EditingMode MessageLineWidget::mode() const { return mMode; } void MessageLineWidget::setMode(const EditingMode &mode) { if (mMode != mode) { mMode = mode; //Cache icon ? switch (mMode) { case EditingMode::NewMessage: mSendMessageButton->setIcon(QIcon::fromTheme(QStringLiteral("mail-sent"))); break; case EditingMode::EditMessage: mSendMessageButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-symbolic"))); break; } } } diff --git a/src/widgets/room/messagelinewidget.h b/src/widgets/room/messagelinewidget.h index 941d27ab..6236239e 100644 --- a/src/widgets/room/messagelinewidget.h +++ b/src/widgets/room/messagelinewidget.h @@ -1,71 +1,71 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef MESSAGELINEWIDGET_H #define MESSAGELINEWIDGET_H #include #include "dialogs/uploadfiledialog.h" #include "libruqolawidgets_private_export.h" class MessageLineEdit; class QToolButton; class EmoticonMenuWidget; class RocketChatAccount; class LIBRUQOLAWIDGETS_TESTS_EXPORT MessageLineWidget : public QWidget { Q_OBJECT public: enum class EditingMode { EditMessage, NewMessage, }; explicit MessageLineWidget(QWidget *parent = nullptr); ~MessageLineWidget(); void setCurrentRocketChatAccount(RocketChatAccount *account); void setText(const QString &text); Q_REQUIRED_RESULT QString text() const; MessageLineEdit *messageLineEdit() const; Q_REQUIRED_RESULT EditingMode mode() const; void setMode(const EditingMode &mode); Q_SIGNALS: void sendMessage(const QString &str); void sendFile(const UploadFileDialog::UploadFileInfo &result); void textEditing(bool clearNotification); private: void slotSendMessage(const QString &msg); void slotSendFile(); EditingMode mMode = EditingMode::NewMessage; - MessageLineEdit *mMessageLineEdit = nullptr; + MessageLineEdit *mMessageTextEdit = nullptr; QToolButton *mSendFile = nullptr; QToolButton *mEmoticonButton = nullptr; QToolButton *mSendMessageButton = nullptr; EmoticonMenuWidget *mEmoticonMenuWidget = nullptr; }; #endif // MESSAGELINEWIDGET_H