From 5b5d35fd1f33555f6dacecacaf53a18517218b79 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 30 Jun 2018 15:13:15 +0300 Subject: [PATCH] Add basic support for replies (#292) --- include/ChatPage.h | 2 ++ include/TextInputWidget.h | 1 + include/timeline/TimelineItem.h | 3 +++ src/ChatPage.cc | 1 + src/TextInputWidget.cc | 11 +++++++++++ src/timeline/TimelineItem.cc | 22 ++++++++++++++++++++++ src/timeline/TimelineView.cc | 2 +- 7 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/ChatPage.h b/include/ChatPage.h index a4c6ccc5..234aabfd 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -76,6 +76,8 @@ signals: void connectionLost(); void connectionRestored(); + void messageReply(const QString &username, const QString &msg); + void notificationsRetrieved(const mtx::responses::Notifications &); void uploadFailed(const QString &msg); diff --git a/include/TextInputWidget.h b/include/TextInputWidget.h index af58c2c3..2dcb178f 100644 --- a/include/TextInputWidget.h +++ b/include/TextInputWidget.h @@ -144,6 +144,7 @@ public slots: void openFileSelection(); void hideUploadSpinner(); void focusLineEdit() { input_->setFocus(); } + void addReply(const QString &username, const QString &msg); private slots: void addSelectedEmoji(const QString &emoji); diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h index f055f217..180623f4 100644 --- a/include/timeline/TimelineItem.h +++ b/include/timeline/TimelineItem.h @@ -212,6 +212,8 @@ private: void init(); //! Add a context menu option to save the image of the timeline item. void addSaveImageAction(ImageItem *image); + //! Add the reply action in the context menu for widgets that support it. + void addReplyAction(); template void setupLocalWidgetLayout(Widget *widget, const QString &userid, bool withSender); @@ -240,6 +242,7 @@ private: QAction *showReadReceipts_; QAction *markAsRead_; QAction *redactMsg_; + QAction *replyMsg_; QHBoxLayout *topLayout_ = nullptr; QHBoxLayout *messageLayout_ = nullptr; diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 3d026ab3..df1ab172 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -605,6 +605,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) }); connect(this, &ChatPage::dropToLoginPageCb, this, &ChatPage::dropToLoginPage); + connect(this, &ChatPage::messageReply, text_input_, &TextInputWidget::addReply); instance_ = this; } diff --git a/src/TextInputWidget.cc b/src/TextInputWidget.cc index acb33fa7..7ee7fa16 100644 --- a/src/TextInputWidget.cc +++ b/src/TextInputWidget.cc @@ -614,3 +614,14 @@ TextInputWidget::paintEvent(QPaintEvent *) p.setPen(QPen(borderColor())); p.drawLine(QPointF(0, 0), QPointF(width(), 0)); } + +void +TextInputWidget::addReply(const QString &username, const QString &msg) +{ + input_->setText(QString("> %1: %2\n\n").arg(username).arg(msg)); + input_->setFocus(); + + auto cursor = input_->textCursor(); + cursor.movePosition(QTextCursor::End); + input_->setTextCursor(cursor); +} diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc index c104801d..bbe3ff34 100644 --- a/src/timeline/TimelineItem.cc +++ b/src/timeline/TimelineItem.cc @@ -125,6 +125,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty, , room_id_{room_id} { init(); + addReplyAction(); auto displayName = Cache::displayName(room_id_, userid); auto timestamp = QDateTime::currentDateTime(); @@ -290,6 +291,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent , room_id_{room_id} { init(); + addReplyAction(); event_id_ = QString::fromStdString(event.event_id); const auto sender = QString::fromStdString(event.sender); @@ -388,6 +391,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent , room_id_{room_id} { init(); + addReplyAction(); event_id_ = QString::fromStdString(event.event_id); const auto sender = QString::fromStdString(event.sender); @@ -610,6 +614,24 @@ TimelineItem::addSaveImageAction(ImageItem *image) } } +void +TimelineItem::addReplyAction() +{ + if (contextMenu_) { + auto replyAction = new QAction("Reply", this); + contextMenu_->addAction(replyAction); + + connect(replyAction, &QAction::triggered, this, [this]() { + if (!body_) + return; + + emit ChatPage::instance()->messageReply( + Cache::displayName(room_id_, descriptionMsg_.userid), + body_->toPlainText()); + }); + } +} + void TimelineItem::addAvatar() { diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc index 98ff0983..47e92a3b 100644 --- a/src/timeline/TimelineView.cc +++ b/src/timeline/TimelineView.cc @@ -143,7 +143,7 @@ TimelineView::fetchHistory() isPaginationInProgress_ = true; getMessages(); - paginationTimer_->start(5000); + paginationTimer_->start(2000); return; }