summary refs log tree commit diff
path: root/src/HistoryViewItem.cc
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-13 04:11:22 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-13 04:11:22 +0300
commit27f7142cd86a46d1b13d99a6b86c126892fa86ca (patch)
treef52d2ecf2ec2893cea67551b7c7fdde2fe39fe72 /src/HistoryViewItem.cc
parentDon't use icons as room avatars (diff)
downloadnheko-27f7142cd86a46d1b13d99a6b86c126892fa86ca.tar.xz
Initial implementation for local echo
Each HistoryView maintains a list of pending events. Each pending
message is validated from the homeserver with either the returned
EventId or the body of the message.

Currently there is no support to remove invalid messages.

Also some small refactoring:
    - ChatPage doesn't know about the message being sent. The message
      delivery is solely handled by HistoryViewManager.
    - Nick coloring function moved to HistoryViewManager.
Diffstat (limited to 'src/HistoryViewItem.cc')
-rw-r--r--src/HistoryViewItem.cc87
1 files changed, 73 insertions, 14 deletions
diff --git a/src/HistoryViewItem.cc b/src/HistoryViewItem.cc

index 8eb92372..83d3cf85 100644 --- a/src/HistoryViewItem.cc +++ b/src/HistoryViewItem.cc
@@ -20,35 +20,63 @@ #include "HistoryViewItem.h" -HistoryViewItem::HistoryViewItem(const Event &event, bool with_sender, const QString &color, QWidget *parent) +HistoryViewItem::HistoryViewItem(const QString &userid, const QString &color, const QString &body, QWidget *parent) : QWidget(parent) { - QString sender = ""; + generateTimestamp(QDateTime::currentDateTime()); + generateBody(userid, color, body); + setupLayout(); +} - if (with_sender) - sender = event.sender().split(":")[0].split("@")[1]; +HistoryViewItem::HistoryViewItem(const QString &body, QWidget *parent) + : QWidget(parent) +{ + generateTimestamp(QDateTime::currentDateTime()); + generateBody(body); + setupLayout(); +} - auto body = event.content().value("body").toString().trimmed(); +HistoryViewItem::HistoryViewItem(const Event &event, bool with_sender, const QString &color, QWidget *parent) + : QWidget(parent) +{ + auto body = event.content().value("body").toString().trimmed().toHtmlEscaped(); auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp()); - auto local_time = timestamp.toString("HH:mm"); + + generateTimestamp(timestamp); if (event.content().value("msgtype").toString() == "m.notice") body = "<i style=\"color: #565E5E\">" + body + "</i>"; - time_label_ = new QLabel(this); - time_label_->setWordWrap(true); - QString msg( + if (with_sender) + generateBody(event.sender(), color, body); + else + generateBody(body); + + setupLayout(); +} + +void HistoryViewItem::generateBody(const QString &body) +{ + content_label_ = new QLabel(this); + content_label_->setWordWrap(true); + content_label_->setAlignment(Qt::AlignTop); + content_label_->setStyleSheet("margin: 0;"); + QString content( "<html>" "<head/>" "<body>" - " <span style=\"font-size: 7pt; color: #5d6565;\">" + " <span style=\"font-size: 10pt; color: #B1AEA5;\">" " %1" " </span>" "</body>" "</html>"); - time_label_->setText(msg.arg(local_time)); - time_label_->setStyleSheet("margin-left: 7px; margin-right: 7px; margin-top: 0;"); - time_label_->setAlignment(Qt::AlignTop); + content_label_->setText(content.arg(body)); + content_label_->setTextInteractionFlags(Qt::TextSelectableByMouse); +} + +void HistoryViewItem::generateBody(const QString &userid, const QString &color, const QString &body) +{ + auto sender = userid.split(":")[0].split("@")[1]; content_label_ = new QLabel(this); content_label_->setWordWrap(true); @@ -68,10 +96,41 @@ HistoryViewItem::HistoryViewItem(const Event &event, bool with_sender, const QSt "</html>"); content_label_->setText(content.arg(color).arg(sender).arg(body)); content_label_->setTextInteractionFlags(Qt::TextSelectableByMouse); +} + +void HistoryViewItem::generateTimestamp(const QDateTime &time) +{ + auto local_time = time.toString("HH:mm"); + + time_label_ = new QLabel(this); + QString msg( + "<html>" + "<head/>" + "<body>" + " <span style=\"font-size: 7pt; color: #5d6565;\">" + " %1" + " </span>" + "</body>" + "</html>"); + time_label_->setText(msg.arg(local_time)); + time_label_->setStyleSheet("margin-left: 7px; margin-right: 7px; margin-top: 0;"); + time_label_->setAlignment(Qt::AlignTop); +} + +void HistoryViewItem::setupLayout() +{ + if (time_label_ == nullptr) { + qWarning() << "HistoryViewItem: Time label is not initialized"; + return; + } + + if (content_label_ == nullptr) { + qWarning() << "HistoryViewItem: Content label is not initialized"; + return; + } top_layout_ = new QHBoxLayout(); top_layout_->setMargin(0); - top_layout_->addWidget(time_label_); top_layout_->addWidget(content_label_, 1);