diff --git a/include/Utils.h b/include/Utils.h index 183ebbb..bbe46dd 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 dc08913..d856b9d 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 f898994..3e84051 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 381702e..dc597bc 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 663f719..9d575c0 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 e3987e7..17ee198 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;