From 5b09c8e652756f8b631fc3e5b6d72a3df3e4faa3 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Fri, 12 Jan 2018 10:21:53 +0200 Subject: [PATCH] Handle surrogate pairs in avatars --- include/Utils.h | 5 +++++ include/ui/Avatar.h | 4 ++-- src/RoomInfoListItem.cc | 4 +++- src/TopRoomBar.cc | 12 ++++-------- src/Utils.cc | 9 +++++++++ src/ui/Avatar.cc | 4 ++-- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/include/Utils.h b/include/Utils.h index 183ebbbe..bbe46dd8 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -17,4 +17,9 @@ descriptiveTime(const QDateTime &then); //! in the RoomList. DescInfo getMessageDescription(const TimelineEvent &event, const QString &localUser); + +//! Get the first character of a string, taking into account that +//! surrogate pairs might be in use. +QString +firstChar(const QString &input); } diff --git a/include/ui/Avatar.h b/include/ui/Avatar.h index dc089139..d856b9d8 100644 --- a/include/ui/Avatar.h +++ b/include/ui/Avatar.h @@ -21,7 +21,7 @@ public: void setBackgroundColor(const QColor &color); void setIcon(const QIcon &icon); void setImage(const QImage &image); - void setLetter(const QChar &letter); + void setLetter(const QString &letter); void setSize(int size); void setTextColor(const QColor &color); @@ -38,7 +38,7 @@ private: void init(); ui::AvatarType type_; - QChar letter_; + QString letter_; QColor background_color_; QColor text_color_; QIcon icon_; diff --git a/src/RoomInfoListItem.cc b/src/RoomInfoListItem.cc index f8989948..3e84051e 100644 --- a/src/RoomInfoListItem.cc +++ b/src/RoomInfoListItem.cc @@ -28,6 +28,7 @@ #include "RoomInfoListItem.h" #include "RoomSettings.h" #include "Theme.h" +#include "Utils.h" constexpr int Padding = 7; constexpr int IconSize = 48; @@ -244,7 +245,8 @@ RoomInfoListItem::paintEvent(QPaintEvent *event) p.setFont(font); p.setPen(QColor("#333")); p.setBrush(Qt::NoBrush); - p.drawText(avatarRegion.translated(0, -1), Qt::AlignCenter, QChar(roomName()[0])); + p.drawText( + avatarRegion.translated(0, -1), Qt::AlignCenter, utils::firstChar(roomName())); } else { p.save(); diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc index 381702e0..dc597bc9 100644 --- a/src/TopRoomBar.cc +++ b/src/TopRoomBar.cc @@ -27,6 +27,7 @@ #include "OverlayModal.h" #include "RoomSettings.h" #include "TopRoomBar.h" +#include "Utils.h" TopRoomBar::TopRoomBar(QWidget *parent) : QWidget(parent) @@ -40,7 +41,7 @@ TopRoomBar::TopRoomBar(QWidget *parent) topLayout_->setMargin(10); avatar_ = new Avatar(this); - avatar_->setLetter(QChar('?')); + avatar_->setLetter(""); avatar_->setSize(35); textLayout_ = new QVBoxLayout(); @@ -169,12 +170,7 @@ TopRoomBar::closeLeaveRoomDialog(bool leaving) void TopRoomBar::updateRoomAvatarFromName(const QString &name) { - QChar letter = '?'; - - if (name.size() > 0) - letter = name[0]; - - avatar_->setLetter(letter); + avatar_->setLetter(utils::firstChar(name)); update(); } @@ -183,7 +179,7 @@ TopRoomBar::reset() { nameLabel_->setText(""); topicLabel_->setText(""); - avatar_->setLetter(QChar('?')); + avatar_->setLetter(""); roomName_.clear(); roomTopic_.clear(); diff --git a/src/Utils.cc b/src/Utils.cc index 663f7196..9d575c09 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -119,3 +119,12 @@ utils::getMessageDescription(const TimelineEvent &event, const QString &localUse return DescInfo{}; } + +QString +utils::firstChar(const QString &input) +{ + if (!input.isEmpty()) + return QString::fromUcs4(&input.toUcs4().at(0), 1); + + return input; +} diff --git a/src/ui/Avatar.cc b/src/ui/Avatar.cc index e3987e7a..17ee198e 100644 --- a/src/ui/Avatar.cc +++ b/src/ui/Avatar.cc @@ -7,7 +7,7 @@ Avatar::Avatar(QWidget *parent) { size_ = ui::AvatarSize; type_ = ui::AvatarType::Letter; - letter_ = QChar('A'); + letter_ = "A"; QFont _font(font()); _font.setPointSizeF(ui::FontSize); @@ -79,7 +79,7 @@ Avatar::setSize(int size) } void -Avatar::setLetter(const QChar &letter) +Avatar::setLetter(const QString &letter) { letter_ = letter; type_ = ui::AvatarType::Letter;