Remove nick colors

This commit is contained in:
Konstantinos Sideris 2017-08-26 11:33:26 +03:00
parent 18625d9d27
commit 43b1bdfe63
8 changed files with 727 additions and 763 deletions

View file

@ -1,10 +1,12 @@
---
Language: Cpp
AccessModifierOffset: -8
AlignAfterOpenBracket: true
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignEscapedNewlinesLeft: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
BasedOnStyle: Mozilla
@ -12,10 +14,10 @@ BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Linux
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 120
ContinuationIndentWidth: 8
ColumnLimit: 100
CompactNamespaces: false
IndentCaseLabels: false
IndentWidth: 8
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
UseTab: Always

1
.gitignore vendored
View file

@ -77,3 +77,4 @@ result
*.dmg
dist/MacOS/nheko.app/Contents/MacOS/nheko
.clang

View file

@ -40,19 +40,19 @@ class TimelineItem : public QWidget
public:
TimelineItem(const events::MessageEvent<msgs::Notice> &e,
bool with_sender,
const QString &color,
QWidget *parent = 0);
TimelineItem(const events::MessageEvent<msgs::Text> &e,
bool with_sender,
const QString &color,
QWidget *parent = 0);
// For local messages.
TimelineItem(const QString &userid, const QString &color, QString body, QWidget *parent = 0);
TimelineItem(const QString &userid, QString body, QWidget *parent = 0);
TimelineItem(QString body, QWidget *parent = 0);
TimelineItem(ImageItem *img, const events::MessageEvent<msgs::Image> &e, const QString &color, QWidget *parent);
TimelineItem(ImageItem *img, const events::MessageEvent<msgs::Image> &e, QWidget *parent);
TimelineItem(ImageItem *img,
const events::MessageEvent<msgs::Image> &e,
bool with_sender,
QWidget *parent);
void setUserAvatar(const QImage &pixmap);
inline DescInfo descriptionMessage() const;
@ -63,7 +63,7 @@ private:
void init();
void generateBody(const QString &body);
void generateBody(const QString &userid, const QString &color, const QString &body);
void generateBody(const QString &userid, const QString &body);
void generateTimestamp(const QDateTime &time);
QString descriptiveTime(const QDateTime &then);

View file

@ -67,16 +67,15 @@ public:
QSharedPointer<MatrixClient> client,
const QString &room_id,
QWidget *parent = 0);
TimelineView(QSharedPointer<MatrixClient> client, const QString &room_id, QWidget *parent = 0);
TimelineView(QSharedPointer<MatrixClient> client,
const QString &room_id,
QWidget *parent = 0);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Image> &e,
const QString &color,
bool with_sender);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Notice> &e,
const QString &color,
bool with_sender);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Text> &e,
const QString &color,
bool with_sender);
// Add new events at the end of the timeline.

View file

@ -43,10 +43,8 @@ public:
void clearAll();
static QString chooseRandomColor();
static QString getUserColor(const QString &userid);
static QString displayName(const QString &userid);
static QMap<QString, QString> NICK_COLORS;
static QMap<QString, QString> DISPLAY_NAMES;
signals:

View file

@ -69,7 +69,7 @@ TimelineItem::init()
/*
* For messages created locally. The avatar and the username are displayed.
*/
TimelineItem::TimelineItem(const QString &userid, const QString &color, QString body, QWidget *parent)
TimelineItem::TimelineItem(const QString &userid, QString body, QWidget *parent)
: QWidget(parent)
{
init();
@ -79,7 +79,7 @@ TimelineItem::TimelineItem(const QString &userid, const QString &color, QString
auto displayName = TimelineViewManager::displayName(userid);
generateTimestamp(QDateTime::currentDateTime());
generateBody(displayName, color, body);
generateBody(displayName, body);
setupAvatarLayout(displayName);
@ -116,7 +116,7 @@ TimelineItem::TimelineItem(QString body, QWidget *parent)
*/
TimelineItem::TimelineItem(ImageItem *image,
const events::MessageEvent<msgs::Image> &event,
const QString &color,
bool with_sender,
QWidget *parent)
: QWidget(parent)
{
@ -132,46 +132,23 @@ TimelineItem::TimelineItem(ImageItem *image,
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp())) };
generateTimestamp(timestamp);
generateBody(displayName, color, "");
setupAvatarLayout(displayName);
auto imageLayout = new QHBoxLayout();
imageLayout->addWidget(image);
imageLayout->addStretch(1);
mainLayout_->addLayout(headerLayout_);
mainLayout_->addLayout(imageLayout);
AvatarProvider::resolve(event.sender(), this);
}
/*
* Used to display images. Only the image is displayed.
*/
TimelineItem::TimelineItem(ImageItem *image, const events::MessageEvent<msgs::Image> &event, QWidget *parent)
: QWidget(parent)
{
init();
auto displayName = TimelineViewManager::displayName(event.sender());
QSettings settings;
descriptionMsg_ = { event.sender() == settings.value("auth/user_id") ? "You" : displayName,
event.sender(),
" sent an image",
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp())) };
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
generateTimestamp(timestamp);
setupSimpleLayout();
auto imageLayout = new QHBoxLayout();
imageLayout->setMargin(0);
imageLayout->addWidget(image);
imageLayout->addStretch(1);
if (with_sender) {
generateBody(displayName, "");
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
AvatarProvider::resolve(event.sender(), this);
} else {
setupSimpleLayout();
}
mainLayout_->addLayout(imageLayout);
}
@ -180,7 +157,6 @@ TimelineItem::TimelineItem(ImageItem *image, const events::MessageEvent<msgs::Im
*/
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event,
bool with_sender,
const QString &color,
QWidget *parent)
: QWidget(parent)
{
@ -201,7 +177,7 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event,
if (with_sender) {
auto displayName = TimelineViewManager::displayName(event.sender());
generateBody(displayName, color, body);
generateBody(displayName, body);
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
@ -220,7 +196,6 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event,
*/
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Text> &event,
bool with_sender,
const QString &color,
QWidget *parent)
: QWidget(parent)
{
@ -243,7 +218,7 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Text> &event,
if (with_sender) {
auto displayName = TimelineViewManager::displayName(event.sender());
generateBody(displayName, color, body);
generateBody(displayName, body);
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
@ -275,7 +250,7 @@ TimelineItem::generateBody(const QString &body)
// The username/timestamp is displayed along with the message body.
void
TimelineItem::generateBody(const QString &userid, const QString &color, const QString &body)
TimelineItem::generateBody(const QString &userid, const QString &body)
{
auto sender = userid;
@ -283,7 +258,7 @@ TimelineItem::generateBody(const QString &userid, const QString &color, const QS
if (userid.split(":")[0].split("@").size() > 1)
sender = userid.split(":")[0].split("@")[1];
QString userContent("<span style=\"color: %1\"> %2 </span>");
QString userContent("<span style=\"color: #171717\"> %1 </span>");
QString bodyContent("<span style=\"color: #171717;\"> %1 </span>");
QFont usernameFont = font_;
@ -291,7 +266,7 @@ TimelineItem::generateBody(const QString &userid, const QString &color, const QS
userName_ = new QLabel(this);
userName_->setFont(usernameFont);
userName_->setText(userContent.arg(color).arg(sender));
userName_->setText(userContent.arg(sender));
if (body.isEmpty())
return;
@ -332,7 +307,8 @@ TimelineItem::replaceEmoji(const QString &body)
// TODO: Be more precise here.
if (code > 9000)
fmtBody += QString("<span style=\"font-family: Emoji One; font-size: %1px\">")
fmtBody += QString("<span style=\"font-family: Emoji "
"One; font-size: %1px\">")
.arg(conf::emojiSize) +
QString(c) + "</span>";
else
@ -376,15 +352,18 @@ TimelineItem::setupSimpleLayout()
timestamp_->adjustSize();
// Align the end of the avatar bubble with the end of the timestamp for
// messages with and without avatar. Otherwise their bodies would not be aligned.
// messages with and without avatar. Otherwise their bodies would not be
// aligned.
int tsWidth = timestamp_->fontMetrics().width(plainText);
int offset = std::max(0, conf::timeline::avatarSize - tsWidth);
int defaultFontHeight = QFontMetrics(font_).ascent();
timestamp_->setAlignment(Qt::AlignTop);
timestamp_->setContentsMargins(offset + 1, defaultFontHeight - timestamp_->fontMetrics().ascent(), 0, 0);
topLayout_->setContentsMargins(conf::timeline::msgMargin, conf::timeline::msgMargin / 3, 0, 0);
timestamp_->setContentsMargins(
offset + 1, defaultFontHeight - timestamp_->fontMetrics().ascent(), 0, 0);
topLayout_->setContentsMargins(
conf::timeline::msgMargin, conf::timeline::msgMargin / 3, 0, 0);
}
void
@ -403,13 +382,12 @@ TimelineItem::descriptiveTime(const QDateTime &then)
auto days = then.daysTo(now);
if (days == 0) {
if (days == 0)
return then.toString("HH:mm");
} else if (days < 2) {
else if (days < 2)
return QString("Yesterday");
} else if (days < 365) {
else if (days < 365)
return then.toString("dd/MM");
}
return then.toString("dd/MM/yy");
}

View file

@ -49,7 +49,9 @@ TimelineView::TimelineView(const Timeline &timeline,
addEvents(timeline);
}
TimelineView::TimelineView(QSharedPointer<MatrixClient> client, const QString &room_id, QWidget *parent)
TimelineView::TimelineView(QSharedPointer<MatrixClient> client,
const QString &room_id,
QWidget *parent)
: QWidget(parent)
, room_id_{ room_id }
, client_{ client }
@ -118,7 +120,8 @@ TimelineView::scrollDown()
return;
}
// If the gap is small enough move the scroll bar down. e.g when a new message appears.
// If the gap is small enough move the scroll bar down. e.g when a new
// message appears.
if (max - current < SCROLL_BAR_GAP)
scroll_area_->verticalScrollBar()->setValue(max);
}
@ -134,14 +137,16 @@ TimelineView::sliderMoved(int position)
if (isTimelineFinished)
return;
// Prevent user from moving up when there is pagination in progress.
// Prevent user from moving up when there is pagination in
// progress.
// TODO: Keep a map of the event ids to filter out duplicates.
if (isPaginationInProgress_)
return;
isPaginationInProgress_ = true;
// FIXME: Maybe move this to TimelineViewManager to remove the extra calls?
// FIXME: Maybe move this to TimelineViewManager to remove the
// extra calls?
client_->messages(room_id_, prev_batch_token_);
}
}
@ -160,7 +165,8 @@ TimelineView::addBackwardsEvents(const QString &room_id, const RoomMessages &msg
isTimelineFinished = false;
QList<TimelineItem *> items;
// Parse in reverse order to determine where we should not show sender's name.
// Parse in reverse order to determine where we should not show sender's
// name.
auto it = msgs.chunk().constEnd();
while (it != msgs.chunk().constBegin()) {
--it;
@ -188,8 +194,8 @@ TimelineView::addBackwardsEvents(const QString &room_id, const RoomMessages &msg
if (!msgs.chunk().isEmpty() && scroll_layout_->count() > 1)
notifyForLastEvent();
// If this batch is the first being rendered (i.e the first and the last events
// originate from this batch), set the last sender.
// If this batch is the first being rendered (i.e the first and the last
// events originate from this batch), set the last sender.
if (lastSender_.isEmpty() && !items.isEmpty())
lastSender_ = items.constFirst()->descriptionMessage().userid;
}
@ -223,11 +229,10 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
}
auto with_sender = isSenderRendered(text.sender(), direction);
auto color = TimelineViewManager::getUserColor(text.sender());
updateLastSender(text.sender(), direction);
return createTimelineItem(text, color, with_sender);
return createTimelineItem(text, with_sender);
} else if (msg_type == events::MessageEventType::Notice) {
events::MessageEvent<msgs::Notice> notice;
@ -245,11 +250,10 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
eventIds_[notice.eventId()] = true;
auto with_sender = isSenderRendered(notice.sender(), direction);
auto color = TimelineViewManager::getUserColor(notice.sender());
updateLastSender(notice.sender(), direction);
return createTimelineItem(notice, color, with_sender);
return createTimelineItem(notice, with_sender);
} else if (msg_type == events::MessageEventType::Image) {
events::MessageEvent<msgs::Image> img;
@ -266,11 +270,10 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
eventIds_[img.eventId()] = true;
auto with_sender = isSenderRendered(img.sender(), direction);
auto color = TimelineViewManager::getUserColor(img.sender());
updateLastSender(img.sender(), direction);
return createTimelineItem(img, color, with_sender);
return createTimelineItem(img, with_sender);
} else if (msg_type == events::MessageEventType::Unknown) {
qWarning() << "Unknown message type" << event;
return nullptr;
@ -344,9 +347,15 @@ TimelineView::init()
paginationTimer_ = new QTimer(this);
connect(paginationTimer_, &QTimer::timeout, this, &TimelineView::fetchHistory);
connect(client_.data(), &MatrixClient::messagesRetrieved, this, &TimelineView::addBackwardsEvents);
connect(client_.data(),
&MatrixClient::messagesRetrieved,
this,
&TimelineView::addBackwardsEvents);
connect(scroll_area_->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sliderMoved(int)));
connect(scroll_area_->verticalScrollBar(),
SIGNAL(valueChanged(int)),
this,
SLOT(sliderMoved(int)));
connect(scroll_area_->verticalScrollBar(),
SIGNAL(rangeChanged(int, int)),
this,
@ -372,32 +381,25 @@ TimelineView::isSenderRendered(const QString &user_id, TimelineDirection directi
}
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Image> &event, const QString &color, bool with_sender)
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Image> &event, bool with_sender)
{
auto image = new ImageItem(client_, event);
auto item = new TimelineItem(image, event, with_sender, scroll_widget_);
if (with_sender) {
auto item = new TimelineItem(image, event, color, scroll_widget_);
return item;
}
auto item = new TimelineItem(image, event, scroll_widget_);
return item;
}
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Notice> &event,
const QString &color,
bool with_sender)
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Notice> &event, bool with_sender)
{
TimelineItem *item = new TimelineItem(event, with_sender, color, scroll_widget_);
TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_);
return item;
}
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Text> &event, const QString &color, bool with_sender)
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Text> &event, bool with_sender)
{
TimelineItem *item = new TimelineItem(event, with_sender, color, scroll_widget_);
TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_);
return item;
}
@ -422,7 +424,8 @@ TimelineView::updatePendingMessage(int txn_id, QString event_id)
}
bool
TimelineView::isPendingMessage(const events::MessageEvent<msgs::Text> &e, const QString &local_userid)
TimelineView::isPendingMessage(const events::MessageEvent<msgs::Text> &e,
const QString &local_userid)
{
if (e.sender() != local_userid)
return false;
@ -455,12 +458,11 @@ TimelineView::addUserTextMessage(const QString &body, int txn_id)
auto user_id = settings.value("auth/user_id").toString();
auto with_sender = lastSender_ != user_id;
auto color = TimelineViewManager::getUserColor(user_id);
TimelineItem *view_item;
if (with_sender)
view_item = new TimelineItem(user_id, color, body, scroll_widget_);
view_item = new TimelineItem(user_id, body, scroll_widget_);
else
view_item = new TimelineItem(body, scroll_widget_);

View file

@ -66,8 +66,6 @@ TimelineViewManager::sendTextMessage(const QString &msg)
void
TimelineViewManager::clearAll()
{
NICK_COLORS.clear();
for (auto view : views_)
removeWidget(view.data());
@ -155,7 +153,6 @@ TimelineViewManager::setHistoryView(const QString &room_id)
view->scrollDown();
}
QMap<QString, QString> TimelineViewManager::NICK_COLORS;
QMap<QString, QString> TimelineViewManager::DISPLAY_NAMES;
QString
@ -216,19 +213,6 @@ TimelineViewManager::chooseRandomColor()
return color.name();
}
QString
TimelineViewManager::getUserColor(const QString &userid)
{
auto color = NICK_COLORS.value(userid);
if (color.isEmpty()) {
color = chooseRandomColor();
NICK_COLORS.insert(userid, color);
}
return color;
}
QString
TimelineViewManager::displayName(const QString &userid)
{