Handle surrogate pairs in avatars

remotes/origin/HEAD
Konstantinos Sideris 7 years ago
parent 8beef5e61f
commit 5b09c8e652
  1. 5
      include/Utils.h
  2. 4
      include/ui/Avatar.h
  3. 4
      src/RoomInfoListItem.cc
  4. 12
      src/TopRoomBar.cc
  5. 9
      src/Utils.cc
  6. 4
      src/ui/Avatar.cc

@ -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);
}

@ -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_;

@ -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();

@ -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();

@ -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;
}

@ -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;

Loading…
Cancel
Save