[sendreplydialog] Submit on enter, enter newline on shift+enter

This enables better keyboard navigation and is consistent with virtually all messaging apps

CCBUG: 441742
(cherry picked from commit 695e36755b)
This commit is contained in:
Nicolas Fella 2021-09-22 15:45:58 +02:00
parent f6b81d4bf9
commit 1cf0eaba39
3 changed files with 43 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#include <QLineEdit>
#include <QBoxLayout>
#include <QStandardPaths>
#include <QKeyEvent>
#include <KLocalizedString>
@ -27,6 +28,9 @@ SendReplyDialog::SendReplyDialog(const QString& originalMessage, const QString&
auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok);
button->setText(i18n("Send"));
auto textEdit = m_ui->replyEdit;
connect(textEdit, &SendReplyTextEdit::send, this, &SendReplyDialog::sendButtonClicked);
connect(this, &QDialog::accepted, this, &SendReplyDialog::sendButtonClicked);
setWindowTitle(topicName);
setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect")));
@ -46,3 +50,22 @@ QSize SendReplyDialog::sizeHint() const
{
return QSize(512, 64);
}
SendReplyTextEdit::SendReplyTextEdit(QWidget *parent)
: QTextEdit(parent)
{
}
void SendReplyTextEdit::keyPressEvent(QKeyEvent* event)
{
// Send reply on enter, except when shift + enter is pressed, then insert newline
const int key = event->key();
if (key == Qt::Key_Return || key == Qt::Key_Enter) {
if ((key == Qt::Key_Enter && (event->modifiers() == Qt::KeypadModifier)) || !event->modifiers()) {
Q_EMIT send();
event->accept();
return;
}
}
QTextEdit::keyPressEvent(event);
}

View file

@ -9,6 +9,7 @@
#include <QDialog>
#include <QSize>
#include <QTextEdit>
namespace Ui { class SendReplyDialog; }
@ -33,4 +34,15 @@ private:
const QScopedPointer<Ui::SendReplyDialog> m_ui;
};
class SendReplyTextEdit : public QTextEdit
{
Q_OBJECT
public:
SendReplyTextEdit(QWidget* parent);
void keyPressEvent(QKeyEvent* event) override;
Q_SIGNAL void send();
};
#endif

View file

@ -25,7 +25,7 @@
</widget>
</item>
<item>
<widget class="QTextEdit" name="replyEdit"/>
<widget class="SendReplyTextEdit" name="replyEdit"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
@ -71,4 +71,11 @@
</hints>
</connection>
</connections>
<customwidgets>
<customwidget>
<class>SendReplyTextEdit</class>
<extends>QTextEdit</extends>
<header>sendreplydialog.h</header>
</customwidget>
</customwidgets>
</ui>