File indexing completed on 2024-04-28 08:49:09

0001 /**
0002  * SPDX-FileCopyrightText: 2015 Albert Vaca <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "sendreplydialog.h"
0008 
0009 #include <QBoxLayout>
0010 #include <QKeyEvent>
0011 #include <QLineEdit>
0012 #include <QPushButton>
0013 #include <QStandardPaths>
0014 #include <QTextEdit>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include "ui_sendreplydialog.h"
0019 
0020 SendReplyDialog::SendReplyDialog(const QString &originalMessage, const QString &replyId, const QString &topicName, QWidget *parent)
0021     : QDialog(parent)
0022     , m_replyId(replyId)
0023     , m_ui(new Ui::SendReplyDialog)
0024 {
0025     m_ui->setupUi(this);
0026     m_ui->textView->setText(topicName + QStringLiteral(": \n") + originalMessage);
0027 
0028     auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok);
0029     button->setText(i18n("Send"));
0030 
0031     const auto sendButtonClicked = [this]() {
0032         Q_EMIT sendReply(m_replyId, m_ui->replyEdit->toPlainText());
0033         close();
0034     };
0035     auto textEdit = m_ui->replyEdit;
0036     connect(textEdit, &SendReplyTextEdit::send, this, sendButtonClicked);
0037 
0038     connect(this, &QDialog::accepted, this, sendButtonClicked);
0039     setWindowTitle(topicName);
0040     setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect")));
0041     setAttribute(Qt::WA_DeleteOnClose);
0042     m_ui->replyEdit->setFocus();
0043 }
0044 
0045 SendReplyDialog::~SendReplyDialog() = default;
0046 
0047 QSize SendReplyDialog::sizeHint() const
0048 {
0049     return QSize(512, 64);
0050 }
0051 
0052 SendReplyTextEdit::SendReplyTextEdit(QWidget *parent)
0053     : QTextEdit(parent)
0054 {
0055 }
0056 
0057 void SendReplyTextEdit::keyPressEvent(QKeyEvent *event)
0058 {
0059     // Send reply on enter, except when shift + enter is pressed, then insert newline
0060     const int key = event->key();
0061     if (key == Qt::Key_Return || key == Qt::Key_Enter) {
0062         if ((key == Qt::Key_Enter && (event->modifiers() == Qt::KeypadModifier)) || !event->modifiers()) {
0063             Q_EMIT send();
0064             event->accept();
0065             return;
0066         }
0067     }
0068     QTextEdit::keyPressEvent(event);
0069 }
0070 
0071 #include "moc_sendreplydialog.cpp"