Linkify URLs in room topics

closes #52
This commit is contained in:
Konstantinos Sideris 2017-08-26 15:36:10 +03:00
parent 177dd6a5b0
commit 6797cbc943
2 changed files with 105 additions and 90 deletions

View file

@ -32,66 +32,74 @@
#include "Menu.h" #include "Menu.h"
#include "RoomSettings.h" #include "RoomSettings.h"
static const QString URL_HTML = "<a href=\"\\1\" style=\"color: #333333\">\\1</a>";
static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
class TopRoomBar : public QWidget class TopRoomBar : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
TopRoomBar(QWidget *parent = 0); TopRoomBar(QWidget *parent = 0);
~TopRoomBar(); ~TopRoomBar();
inline void updateRoomAvatar(const QImage &avatar_image); inline void updateRoomAvatar(const QImage &avatar_image);
inline void updateRoomAvatar(const QIcon &icon); inline void updateRoomAvatar(const QIcon &icon);
inline void updateRoomName(const QString &name); inline void updateRoomName(const QString &name);
inline void updateRoomTopic(const QString &topic); inline void updateRoomTopic(QString topic);
void updateRoomAvatarFromName(const QString &name); void updateRoomAvatarFromName(const QString &name);
void setRoomSettings(QSharedPointer<RoomSettings> settings); void setRoomSettings(QSharedPointer<RoomSettings> settings);
void reset(); void reset();
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
private: private:
QHBoxLayout *top_layout_; QHBoxLayout *topLayout_;
QVBoxLayout *text_layout_; QVBoxLayout *textLayout_;
QLabel *name_label_; QLabel *nameLabel_;
QLabel *topic_label_; QLabel *topicLabel_;
QSharedPointer<RoomSettings> roomSettings_; QSharedPointer<RoomSettings> roomSettings_;
QMenu *menu_; QMenu *menu_;
QAction *toggleNotifications_; QAction *toggleNotifications_;
FlatButton *settingsBtn_; FlatButton *settingsBtn_;
Avatar *avatar_; Avatar *avatar_;
int buttonSize_; int buttonSize_;
}; };
inline void inline void
TopRoomBar::updateRoomAvatar(const QImage &avatar_image) TopRoomBar::updateRoomAvatar(const QImage &avatar_image)
{ {
avatar_->setImage(avatar_image); avatar_->setImage(avatar_image);
} }
inline void inline void
TopRoomBar::updateRoomAvatar(const QIcon &icon) TopRoomBar::updateRoomAvatar(const QIcon &icon)
{ {
avatar_->setIcon(icon); avatar_->setIcon(icon);
} }
inline void inline void
TopRoomBar::updateRoomName(const QString &name) TopRoomBar::updateRoomName(const QString &name)
{ {
QString elidedText = QFontMetrics(name_label_->font()).elidedText(name, Qt::ElideRight, width() * 0.8); QString elidedText =
name_label_->setText(elidedText); QFontMetrics(nameLabel_->font()).elidedText(name, Qt::ElideRight, width() * 0.8);
nameLabel_->setText(elidedText);
} }
inline void inline void
TopRoomBar::updateRoomTopic(const QString &topic) TopRoomBar::updateRoomTopic(QString topic)
{ {
QString elidedText = QFontMetrics(topic_label_->font()).elidedText(topic, Qt::ElideRight, width() * 0.8); topic.replace(URL_REGEX, URL_HTML);
topic_label_->setText(elidedText);
QString elidedText =
QFontMetrics(topicLabel_->font()).elidedText(topic, Qt::ElideRight, width() * 0.6);
topicLabel_->setText(topic);
} }

View file

@ -24,109 +24,116 @@ TopRoomBar::TopRoomBar(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, buttonSize_{ 32 } , buttonSize_{ 32 }
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMinimumSize(QSize(0, 65)); setMinimumSize(QSize(0, 65));
setStyleSheet("background-color: #f8fbfe; color: #171919;"); setStyleSheet("background-color: #f8fbfe; color: #171919;");
top_layout_ = new QHBoxLayout(); topLayout_ = new QHBoxLayout();
top_layout_->setSpacing(10); topLayout_->setSpacing(10);
top_layout_->setMargin(10); topLayout_->setMargin(10);
avatar_ = new Avatar(this); avatar_ = new Avatar(this);
avatar_->setLetter(QChar('?')); avatar_->setLetter(QChar('?'));
avatar_->setBackgroundColor(QColor("#d6dde3")); avatar_->setBackgroundColor(QColor("#d6dde3"));
avatar_->setTextColor(QColor("#555459")); avatar_->setTextColor(QColor("#555459"));
avatar_->setSize(35); avatar_->setSize(35);
text_layout_ = new QVBoxLayout(); textLayout_ = new QVBoxLayout();
text_layout_->setSpacing(0); textLayout_->setSpacing(0);
text_layout_->setContentsMargins(0, 0, 0, 0); textLayout_->setContentsMargins(0, 0, 0, 0);
QFont roomFont("Open Sans SemiBold"); QFont roomFont("Open Sans SemiBold");
roomFont.setPixelSize(conf::topRoomBar::fonts::roomName); roomFont.setPixelSize(conf::topRoomBar::fonts::roomName);
name_label_ = new QLabel(this); nameLabel_ = new QLabel(this);
name_label_->setFont(roomFont); nameLabel_->setFont(roomFont);
QFont descriptionFont("Open Sans"); QFont descriptionFont("Open Sans");
descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription); descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription);
topic_label_ = new QLabel(this); topicLabel_ = new QLabel(this);
topic_label_->setFont(descriptionFont); topicLabel_->setFont(descriptionFont);
topicLabel_->setTextFormat(Qt::RichText);
topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction);
topicLabel_->setOpenExternalLinks(true);
text_layout_->addWidget(name_label_); textLayout_->addWidget(nameLabel_);
text_layout_->addWidget(topic_label_); textLayout_->addWidget(topicLabel_);
settingsBtn_ = new FlatButton(this); settingsBtn_ = new FlatButton(this);
settingsBtn_->setForegroundColor(QColor("#acc7dc")); settingsBtn_->setForegroundColor(QColor("#acc7dc"));
settingsBtn_->setFixedSize(buttonSize_, buttonSize_); settingsBtn_->setFixedSize(buttonSize_, buttonSize_);
settingsBtn_->setCornerRadius(buttonSize_ / 2); settingsBtn_->setCornerRadius(buttonSize_ / 2);
QIcon settings_icon; QIcon settings_icon;
settings_icon.addFile(":/icons/icons/vertical-ellipsis.png", QSize(), QIcon::Normal, QIcon::Off); settings_icon.addFile(
settingsBtn_->setIcon(settings_icon); ":/icons/icons/vertical-ellipsis.png", QSize(), QIcon::Normal, QIcon::Off);
settingsBtn_->setIconSize(QSize(buttonSize_ / 2, buttonSize_ / 2)); settingsBtn_->setIcon(settings_icon);
settingsBtn_->setIconSize(QSize(buttonSize_ / 2, buttonSize_ / 2));
top_layout_->addWidget(avatar_); topLayout_->addWidget(avatar_);
top_layout_->addLayout(text_layout_); topLayout_->addLayout(textLayout_);
top_layout_->addStretch(1); topLayout_->addStretch(1);
top_layout_->addWidget(settingsBtn_); topLayout_->addWidget(settingsBtn_);
menu_ = new Menu(this); menu_ = new Menu(this);
toggleNotifications_ = new QAction(tr("Disable notifications"), this); toggleNotifications_ = new QAction(tr("Disable notifications"), this);
connect(toggleNotifications_, &QAction::triggered, this, [=]() { roomSettings_->toggleNotifications(); }); connect(toggleNotifications_, &QAction::triggered, this, [=]() {
roomSettings_->toggleNotifications();
});
menu_->addAction(toggleNotifications_); menu_->addAction(toggleNotifications_);
connect(settingsBtn_, &QPushButton::clicked, this, [=]() { connect(settingsBtn_, &QPushButton::clicked, this, [=]() {
if (roomSettings_->isNotificationsEnabled()) if (roomSettings_->isNotificationsEnabled())
toggleNotifications_->setText(tr("Disable notifications")); toggleNotifications_->setText(tr("Disable notifications"));
else else
toggleNotifications_->setText(tr("Enable notifications")); toggleNotifications_->setText(tr("Enable notifications"));
auto pos = mapToGlobal(settingsBtn_->pos()); auto pos = mapToGlobal(settingsBtn_->pos());
menu_->popup(QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_)); menu_->popup(
}); QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_));
});
setLayout(top_layout_); setLayout(topLayout_);
} }
void void
TopRoomBar::updateRoomAvatarFromName(const QString &name) TopRoomBar::updateRoomAvatarFromName(const QString &name)
{ {
QChar letter = '?'; QChar letter = '?';
if (name.size() > 0) if (name.size() > 0)
letter = name[0]; letter = name[0];
avatar_->setLetter(letter); avatar_->setLetter(letter);
} }
void void
TopRoomBar::reset() TopRoomBar::reset()
{ {
name_label_->setText(""); nameLabel_->setText("");
topic_label_->setText(""); topicLabel_->setText("");
avatar_->setLetter(QChar('?')); avatar_->setLetter(QChar('?'));
} }
void void
TopRoomBar::paintEvent(QPaintEvent *event) TopRoomBar::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
QStyleOption option; QStyleOption option;
option.initFrom(this); option.initFrom(this);
QPainter painter(this); QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this); style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
} }
void void
TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings) TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings)
{ {
roomSettings_ = settings; roomSettings_ = settings;
} }
TopRoomBar::~TopRoomBar() TopRoomBar::~TopRoomBar()