Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Show Matrix ID tooltip when hovering over display name
Browse files Browse the repository at this point in the history
fixes #212
  • Loading branch information
mujx committed May 26, 2018
1 parent b371c15 commit 0fe81ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
43 changes: 40 additions & 3 deletions include/timeline/TimelineItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <QAbstractTextDocumentLayout>
#include <QApplication>
#include <QDateTime>
#include <QHBoxLayout>
#include <QLabel>
Expand Down Expand Up @@ -78,6 +79,42 @@ private slots:
void adjustHeight(const QSizeF &size) { setFixedHeight(size.height()); }
};

class UserProfileFilter : public QObject
{
Q_OBJECT

public:
explicit UserProfileFilter(const QString &user_id, QLabel *parent)
: QObject(parent)
, user_id_{user_id}
{}

signals:
void hoverOff();
void hoverOn();

protected:
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonRelease) {
// QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
// TODO: Open user profile
return true;
} else if (event->type() == QEvent::HoverLeave) {
emit hoverOff();
return true;
} else if (event->type() == QEvent::HoverEnter) {
emit hoverOn();
return true;
}

return QObject::eventFilter(obj, event);
}

private:
QString user_id_;
};

class TimelineItem : public QWidget
{
Q_OBJECT
Expand Down Expand Up @@ -182,7 +219,7 @@ class TimelineItem : public QWidget
void setupWidgetLayout(Widget *widget, const Event &event, bool withSender);

void generateBody(const QString &body);
void generateBody(const QString &userid, const QString &body);
void generateBody(const QString &user_id, const QString &displayname, const QString &body);
void generateTimestamp(const QDateTime &time);

void setupAvatarLayout(const QString &userName);
Expand Down Expand Up @@ -237,7 +274,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget, const QString &userid, bool
widgetLayout_->addStretch(1);

if (withSender) {
generateBody(displayName, "");
generateBody(userid, displayName, "");
setupAvatarLayout(displayName);

headerLayout_->addLayout(widgetLayout_);
Expand Down Expand Up @@ -283,7 +320,7 @@ TimelineItem::setupWidgetLayout(Widget *widget, const Event &event, bool withSen
widgetLayout_->addStretch(1);

if (withSender) {
generateBody(displayName, "");
generateBody(sender, displayName, "");
setupAvatarLayout(displayName);

headerLayout_->addLayout(widgetLayout_);
Expand Down
40 changes: 31 additions & 9 deletions src/timeline/TimelineItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
generateTimestamp(timestamp);

if (withSender) {
generateBody(displayName, body);
generateBody(userid, displayName, body);
setupAvatarLayout(displayName);

messageLayout_->addLayout(headerLayout_, 1);
Expand Down Expand Up @@ -292,7 +292,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
if (with_sender) {
auto displayName = Cache::displayName(room_id_, sender);

generateBody(displayName, body);
generateBody(sender, displayName, body);
setupAvatarLayout(displayName);

messageLayout_->addLayout(headerLayout_, 1);
Expand Down Expand Up @@ -339,7 +339,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
emoteMsg.replace("\n", "<br/>");

if (with_sender) {
generateBody(displayName, emoteMsg);
generateBody(sender, displayName, emoteMsg);
setupAvatarLayout(displayName);

messageLayout_->addLayout(headerLayout_, 1);
Expand Down Expand Up @@ -391,7 +391,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
body.replace("\n", "<br/>");

if (with_sender) {
generateBody(displayName, body);
generateBody(sender, displayName, body);
setupAvatarLayout(displayName);

messageLayout_->addLayout(headerLayout_, 1);
Expand Down Expand Up @@ -435,21 +435,43 @@ TimelineItem::generateBody(const QString &body)

// The username/timestamp is displayed along with the message body.
void
TimelineItem::generateBody(const QString &userid, const QString &body)
TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body)
{
auto sender = userid;
auto sender = displayname;

if (userid.startsWith("@")) {
if (displayname.startsWith("@")) {
// TODO: Fix this by using a UserId type.
if (userid.split(":")[0].split("@").size() > 1)
sender = userid.split(":")[0].split("@")[1];
if (displayname.split(":")[0].split("@").size() > 1)
sender = displayname.split(":")[0].split("@")[1];
}

QFontMetrics fm(usernameFont_);

userName_ = new QLabel(this);
userName_->setFont(usernameFont_);
userName_->setText(fm.elidedText(sender, Qt::ElideRight, 500));
userName_->setToolTip(user_id);
userName_->setToolTipDuration(1500);
userName_->setAttribute(Qt::WA_Hover);
userName_->setAlignment(Qt::AlignLeft);
userName_->setFixedWidth(QFontMetrics(userName_->font()).width(userName_->text()));

auto filter = new UserProfileFilter(user_id, userName_);
userName_->installEventFilter(filter);

connect(filter, &UserProfileFilter::hoverOn, this, [this]() {
QFont f = userName_->font();
f.setUnderline(true);
userName_->setCursor(Qt::PointingHandCursor);
userName_->setFont(f);
});

connect(filter, &UserProfileFilter::hoverOff, this, [this]() {
QFont f = userName_->font();
f.setUnderline(false);
userName_->setCursor(Qt::ArrowCursor);
userName_->setFont(f);
});

generateBody(body);
}
Expand Down

0 comments on commit 0fe81ec

Please sign in to comment.