Handle surrogate pairs in avatars

This commit is contained in:
Konstantinos Sideris 2018-01-12 10:21:53 +02:00
parent 8beef5e61f
commit 5b09c8e652
6 changed files with 25 additions and 13 deletions

View file

@ -17,4 +17,9 @@ descriptiveTime(const QDateTime &then);
//! in the RoomList. //! in the RoomList.
DescInfo DescInfo
getMessageDescription(const TimelineEvent &event, const QString &localUser); 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);
} }

View file

@ -21,7 +21,7 @@ public:
void setBackgroundColor(const QColor &color); void setBackgroundColor(const QColor &color);
void setIcon(const QIcon &icon); void setIcon(const QIcon &icon);
void setImage(const QImage &image); void setImage(const QImage &image);
void setLetter(const QChar &letter); void setLetter(const QString &letter);
void setSize(int size); void setSize(int size);
void setTextColor(const QColor &color); void setTextColor(const QColor &color);
@ -38,7 +38,7 @@ private:
void init(); void init();
ui::AvatarType type_; ui::AvatarType type_;
QChar letter_; QString letter_;
QColor background_color_; QColor background_color_;
QColor text_color_; QColor text_color_;
QIcon icon_; QIcon icon_;

View file

@ -28,6 +28,7 @@
#include "RoomInfoListItem.h" #include "RoomInfoListItem.h"
#include "RoomSettings.h" #include "RoomSettings.h"
#include "Theme.h" #include "Theme.h"
#include "Utils.h"
constexpr int Padding = 7; constexpr int Padding = 7;
constexpr int IconSize = 48; constexpr int IconSize = 48;
@ -244,7 +245,8 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
p.setFont(font); p.setFont(font);
p.setPen(QColor("#333")); p.setPen(QColor("#333"));
p.setBrush(Qt::NoBrush); 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 { } else {
p.save(); p.save();

View file

@ -27,6 +27,7 @@
#include "OverlayModal.h" #include "OverlayModal.h"
#include "RoomSettings.h" #include "RoomSettings.h"
#include "TopRoomBar.h" #include "TopRoomBar.h"
#include "Utils.h"
TopRoomBar::TopRoomBar(QWidget *parent) TopRoomBar::TopRoomBar(QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -40,7 +41,7 @@ TopRoomBar::TopRoomBar(QWidget *parent)
topLayout_->setMargin(10); topLayout_->setMargin(10);
avatar_ = new Avatar(this); avatar_ = new Avatar(this);
avatar_->setLetter(QChar('?')); avatar_->setLetter("");
avatar_->setSize(35); avatar_->setSize(35);
textLayout_ = new QVBoxLayout(); textLayout_ = new QVBoxLayout();
@ -169,12 +170,7 @@ TopRoomBar::closeLeaveRoomDialog(bool leaving)
void void
TopRoomBar::updateRoomAvatarFromName(const QString &name) TopRoomBar::updateRoomAvatarFromName(const QString &name)
{ {
QChar letter = '?'; avatar_->setLetter(utils::firstChar(name));
if (name.size() > 0)
letter = name[0];
avatar_->setLetter(letter);
update(); update();
} }
@ -183,7 +179,7 @@ TopRoomBar::reset()
{ {
nameLabel_->setText(""); nameLabel_->setText("");
topicLabel_->setText(""); topicLabel_->setText("");
avatar_->setLetter(QChar('?')); avatar_->setLetter("");
roomName_.clear(); roomName_.clear();
roomTopic_.clear(); roomTopic_.clear();

View file

@ -119,3 +119,12 @@ utils::getMessageDescription(const TimelineEvent &event, const QString &localUse
return DescInfo{}; return DescInfo{};
} }
QString
utils::firstChar(const QString &input)
{
if (!input.isEmpty())
return QString::fromUcs4(&input.toUcs4().at(0), 1);
return input;
}

View file

@ -7,7 +7,7 @@ Avatar::Avatar(QWidget *parent)
{ {
size_ = ui::AvatarSize; size_ = ui::AvatarSize;
type_ = ui::AvatarType::Letter; type_ = ui::AvatarType::Letter;
letter_ = QChar('A'); letter_ = "A";
QFont _font(font()); QFont _font(font());
_font.setPointSizeF(ui::FontSize); _font.setPointSizeF(ui::FontSize);
@ -79,7 +79,7 @@ Avatar::setSize(int size)
} }
void void
Avatar::setLetter(const QChar &letter) Avatar::setLetter(const QString &letter)
{ {
letter_ = letter; letter_ = letter;
type_ = ui::AvatarType::Letter; type_ = ui::AvatarType::Letter;