2018-01-03 19:05:49 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "dialogs/ReadReceipts.h"
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
#include "AvatarProvider.h"
|
2018-04-21 16:34:50 +03:00
|
|
|
#include "Cache.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "ChatPage.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "ui/Avatar.h"
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
using namespace dialogs;
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
ReceiptItem::ReceiptItem(QWidget *parent,
|
|
|
|
const QString &user_id,
|
|
|
|
uint64_t timestamp,
|
|
|
|
const QString &room_id)
|
2018-01-03 19:05:49 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
topLayout_ = new QHBoxLayout(this);
|
|
|
|
topLayout_->setMargin(0);
|
|
|
|
|
|
|
|
textLayout_ = new QVBoxLayout;
|
|
|
|
textLayout_->setMargin(0);
|
2018-07-22 18:03:12 +03:00
|
|
|
textLayout_->setSpacing(conf::modals::TEXT_SPACING);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2018-07-22 18:03:12 +03:00
|
|
|
QFont nameFont;
|
|
|
|
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.1);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
auto displayName = Cache::displayName(room_id, user_id);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
avatar_ = new Avatar(this);
|
2018-07-22 18:03:12 +03:00
|
|
|
avatar_->setSize(44);
|
2018-01-16 22:02:29 +03:00
|
|
|
avatar_->setLetter(utils::firstChar(displayName));
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
// If it's a matrix id we use the second letter.
|
|
|
|
if (displayName.size() > 1 && displayName.at(0) == '@')
|
|
|
|
avatar_->setLetter(QChar(displayName.at(1)));
|
|
|
|
|
|
|
|
userName_ = new QLabel(displayName, this);
|
2018-07-22 18:03:12 +03:00
|
|
|
userName_->setFont(nameFont);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
timestamp_ = new QLabel(dateFormat(QDateTime::fromMSecsSinceEpoch(timestamp)), this);
|
|
|
|
|
|
|
|
textLayout_->addWidget(userName_);
|
|
|
|
textLayout_->addWidget(timestamp_);
|
|
|
|
|
|
|
|
topLayout_->addWidget(avatar_);
|
|
|
|
topLayout_->addLayout(textLayout_, 1);
|
|
|
|
|
2018-04-21 16:34:50 +03:00
|
|
|
AvatarProvider::resolve(ChatPage::instance()->currentRoom(),
|
|
|
|
user_id,
|
|
|
|
this,
|
|
|
|
[this](const QImage &img) { avatar_->setImage(img); });
|
2018-01-03 19:05:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
ReceiptItem::dateFormat(const QDateTime &then) const
|
|
|
|
{
|
|
|
|
auto now = QDateTime::currentDateTime();
|
|
|
|
auto days = then.daysTo(now);
|
|
|
|
|
|
|
|
if (days == 0)
|
|
|
|
return QString("Today %1").arg(then.toString("HH:mm"));
|
|
|
|
else if (days < 2)
|
2018-01-04 11:52:49 +03:00
|
|
|
return QString("Yesterday %1").arg(then.toString("HH:mm"));
|
2018-01-03 19:05:49 +03:00
|
|
|
else if (days < 365)
|
|
|
|
return then.toString("dd/MM HH:mm");
|
|
|
|
|
|
|
|
return then.toString("dd/MM/yy");
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadReceipts::ReadReceipts(QWidget *parent)
|
|
|
|
: QFrame(parent)
|
|
|
|
{
|
|
|
|
auto layout = new QVBoxLayout(this);
|
2018-07-22 18:03:12 +03:00
|
|
|
layout->setSpacing(conf::modals::WIDGET_SPACING);
|
|
|
|
layout->setMargin(conf::modals::WIDGET_MARGIN);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
userList_ = new QListWidget;
|
|
|
|
userList_->setFrameStyle(QFrame::NoFrame);
|
|
|
|
userList_->setSelectionMode(QAbstractItemView::NoSelection);
|
|
|
|
userList_->setAttribute(Qt::WA_MacShowFocusRect, 0);
|
2018-07-22 18:03:12 +03:00
|
|
|
userList_->setSpacing(conf::modals::TEXT_SPACING);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2018-08-11 17:55:44 +03:00
|
|
|
QFont doubleFont;
|
|
|
|
doubleFont.setPointSizeF(doubleFont.pointSizeF() * 2);
|
|
|
|
|
|
|
|
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
|
|
|
setMinimumHeight(userList_->sizeHint().height() * 2);
|
|
|
|
setMinimumWidth(std::max(userList_->sizeHint().width() + 4 * conf::modals::WIDGET_MARGIN,
|
|
|
|
QFontMetrics(doubleFont).averageCharWidth() * 30 -
|
|
|
|
2 * conf::modals::WIDGET_MARGIN));
|
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
QFont font;
|
2018-07-22 18:03:12 +03:00
|
|
|
font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
|
|
|
topLabel_ = new QLabel(tr("Read receipts"), this);
|
|
|
|
topLabel_->setAlignment(Qt::AlignCenter);
|
|
|
|
topLabel_->setFont(font);
|
|
|
|
|
|
|
|
layout->addWidget(topLabel_);
|
|
|
|
layout->addWidget(userList_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-15 00:33:12 +03:00
|
|
|
ReadReceipts::addUsers(const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &receipts)
|
2018-01-03 19:05:49 +03:00
|
|
|
{
|
|
|
|
// We want to remove any previous items that have been set.
|
|
|
|
userList_->clear();
|
|
|
|
|
2018-02-28 13:12:07 +03:00
|
|
|
for (const auto &receipt : receipts) {
|
2018-04-21 16:34:50 +03:00
|
|
|
auto user = new ReceiptItem(this,
|
|
|
|
QString::fromStdString(receipt.second),
|
|
|
|
receipt.first,
|
|
|
|
ChatPage::instance()->currentRoom());
|
2018-01-03 19:05:49 +03:00
|
|
|
auto item = new QListWidgetItem(userList_);
|
|
|
|
|
|
|
|
item->setSizeHint(user->minimumSizeHint());
|
|
|
|
item->setFlags(Qt::NoItemFlags);
|
|
|
|
item->setTextAlignment(Qt::AlignCenter);
|
|
|
|
|
|
|
|
userList_->setItemWidget(item, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadReceipts::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|