2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
#include <QContextMenuEvent>
|
2017-06-05 02:14:05 +03:00
|
|
|
#include <QFontDatabase>
|
2018-01-03 19:05:49 +03:00
|
|
|
#include <QMenu>
|
2017-07-01 12:46:58 +03:00
|
|
|
#include <QTextEdit>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "Avatar.h"
|
2018-01-03 19:05:49 +03:00
|
|
|
#include "ChatPage.h"
|
2017-07-15 17:11:46 +03:00
|
|
|
#include "Config.h"
|
2017-11-30 14:53:28 +03:00
|
|
|
|
|
|
|
#include "timeline/TimelineItem.h"
|
2017-12-01 16:39:50 +03:00
|
|
|
#include "timeline/widgets/AudioItem.h"
|
2017-11-30 14:53:28 +03:00
|
|
|
#include "timeline/widgets/FileItem.h"
|
|
|
|
#include "timeline/widgets/ImageItem.h"
|
2017-12-01 19:28:26 +03:00
|
|
|
#include "timeline/widgets/VideoItem.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::init()
|
2017-06-05 02:14:05 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
userAvatar_ = nullptr;
|
|
|
|
timestamp_ = nullptr;
|
|
|
|
userName_ = nullptr;
|
|
|
|
body_ = nullptr;
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
font_.setPixelSize(conf::fontSize);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
QFontMetrics fm(font_);
|
2017-07-01 14:03:00 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
receiptsMenu_ = new QMenu(this);
|
|
|
|
showReadReceipts_ = new QAction("Read receipts", this);
|
|
|
|
receiptsMenu_->addAction(showReadReceipts_);
|
|
|
|
connect(showReadReceipts_, &QAction::triggered, this, [=]() {
|
|
|
|
if (!event_id_.isEmpty())
|
|
|
|
ChatPage::instance()->showReadReceipts(event_id_);
|
|
|
|
});
|
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
topLayout_ = new QHBoxLayout(this);
|
|
|
|
mainLayout_ = new QVBoxLayout;
|
|
|
|
messageLayout_ = new QHBoxLayout;
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
topLayout_->setContentsMargins(conf::timeline::msgMargin, conf::timeline::msgMargin, 0, 0);
|
|
|
|
topLayout_->setSpacing(0);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
topLayout_->addLayout(mainLayout_, 1);
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
mainLayout_->setContentsMargins(conf::timeline::headerLeftMargin, 0, 0, 0);
|
|
|
|
mainLayout_->setSpacing(0);
|
2017-06-05 02:14:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
/*
|
2017-09-03 11:43:45 +03:00
|
|
|
* For messages created locally.
|
2017-07-01 12:46:58 +03:00
|
|
|
*/
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineItem::TimelineItem(mtx::events::MessageType ty,
|
2017-09-03 11:43:45 +03:00
|
|
|
const QString &userid,
|
|
|
|
QString body,
|
|
|
|
bool withSender,
|
|
|
|
QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
init();
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
auto displayName = TimelineViewManager::displayName(userid);
|
2017-09-03 11:43:45 +03:00
|
|
|
auto timestamp = QDateTime::currentDateTime();
|
2017-05-11 01:28:06 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (ty == mtx::events::MessageType::Emote) {
|
2017-09-03 11:43:45 +03:00
|
|
|
body = QString("* %1 %2").arg(displayName).arg(body);
|
2018-01-05 01:27:32 +03:00
|
|
|
descriptionMsg_ = {"", userid, body, utils::descriptiveTime(timestamp), timestamp};
|
2017-09-03 11:43:45 +03:00
|
|
|
} else {
|
2018-01-05 01:27:32 +03:00
|
|
|
descriptionMsg_ = {
|
|
|
|
"You: ", userid, body, utils::descriptiveTime(timestamp), timestamp};
|
2017-09-03 11:43:45 +03:00
|
|
|
}
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-09-22 22:12:36 +03:00
|
|
|
body = body.toHtmlEscaped();
|
2017-12-24 14:13:07 +03:00
|
|
|
body.replace(conf::strings::url_regex, conf::strings::url_html);
|
2017-11-06 03:23:19 +03:00
|
|
|
body.replace("\n", "<br/>");
|
2017-09-03 11:43:45 +03:00
|
|
|
generateTimestamp(timestamp);
|
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->setContentsMargins(0, 0, 20, 4);
|
|
|
|
messageLayout_->setSpacing(20);
|
|
|
|
|
2017-09-03 11:43:45 +03:00
|
|
|
if (withSender) {
|
|
|
|
generateBody(displayName, body);
|
|
|
|
setupAvatarLayout(displayName);
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addLayout(headerLayout_, 1);
|
2017-09-03 11:43:45 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
AvatarProvider::resolve(userid, [=](const QImage &img) { setUserAvatar(img); });
|
2017-09-03 11:43:45 +03:00
|
|
|
} else {
|
|
|
|
generateBody(body);
|
|
|
|
setupSimpleLayout();
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addWidget(body_, 1);
|
2017-09-03 11:43:45 +03:00
|
|
|
}
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addWidget(timestamp_);
|
|
|
|
mainLayout_->addLayout(messageLayout_);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
TimelineItem::TimelineItem(ImageItem *image,
|
|
|
|
const QString &userid,
|
|
|
|
bool withSender,
|
|
|
|
QWidget *parent)
|
2017-11-06 00:04:55 +03:00
|
|
|
: QWidget{parent}
|
2017-09-10 12:58:00 +03:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
2017-11-30 00:39:35 +03:00
|
|
|
setupLocalWidgetLayout<ImageItem>(image, userid, "sent an image", withSender);
|
|
|
|
}
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2017-11-30 00:39:35 +03:00
|
|
|
TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
{
|
|
|
|
init();
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2017-11-30 00:39:35 +03:00
|
|
|
setupLocalWidgetLayout<FileItem>(file, userid, "sent a file", withSender);
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:39:50 +03:00
|
|
|
TimelineItem::TimelineItem(AudioItem *audio,
|
|
|
|
const QString &userid,
|
|
|
|
bool withSender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
setupLocalWidgetLayout<AudioItem>(audio, userid, "sent an audio clip", withSender);
|
|
|
|
}
|
|
|
|
|
2017-12-01 19:28:26 +03:00
|
|
|
TimelineItem::TimelineItem(VideoItem *video,
|
|
|
|
const QString &userid,
|
|
|
|
bool withSender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
setupLocalWidgetLayout<VideoItem>(video, userid, "sent a video clip", withSender);
|
|
|
|
}
|
|
|
|
|
2017-07-01 12:46:58 +03:00
|
|
|
TimelineItem::TimelineItem(ImageItem *image,
|
2017-12-04 19:41:19 +03:00
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::Image> &event,
|
2017-08-26 11:33:26 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2017-04-28 14:56:45 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
|
2017-12-01 16:39:50 +03:00
|
|
|
image, event, " sent an image", with_sender);
|
2017-04-28 14:56:45 +03:00
|
|
|
}
|
|
|
|
|
2017-11-28 03:01:37 +03:00
|
|
|
TimelineItem::TimelineItem(FileItem *file,
|
2017-12-04 19:41:19 +03:00
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::File> &event,
|
2017-11-28 03:01:37 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::File>, FileItem>(
|
2017-12-01 16:39:50 +03:00
|
|
|
file, event, " sent a file", with_sender);
|
|
|
|
}
|
2017-11-28 03:01:37 +03:00
|
|
|
|
2017-12-01 16:39:50 +03:00
|
|
|
TimelineItem::TimelineItem(AudioItem *audio,
|
2017-12-04 19:41:19 +03:00
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::Audio> &event,
|
2017-12-01 16:39:50 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Audio>, AudioItem>(
|
2017-12-01 16:39:50 +03:00
|
|
|
audio, event, " sent an audio clip", with_sender);
|
2017-11-28 03:01:37 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 19:28:26 +03:00
|
|
|
TimelineItem::TimelineItem(VideoItem *video,
|
2017-12-04 19:41:19 +03:00
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::Video> &event,
|
2017-12-01 19:28:26 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Video>, VideoItem>(
|
2017-12-01 19:28:26 +03:00
|
|
|
video, event, " sent a video clip", with_sender);
|
|
|
|
}
|
|
|
|
|
2017-07-01 12:46:58 +03:00
|
|
|
/*
|
|
|
|
* Used to display remote notice messages.
|
|
|
|
*/
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice> &event,
|
2017-08-26 11:33:26 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
init();
|
2017-11-24 01:10:58 +03:00
|
|
|
|
2017-12-26 00:02:33 +03:00
|
|
|
event_id_ = QString::fromStdString(event.event_id);
|
|
|
|
const auto sender = QString::fromStdString(event.sender);
|
|
|
|
const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
|
|
|
|
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
|
2017-11-24 01:10:58 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
descriptionMsg_ = {TimelineViewManager::displayName(sender),
|
|
|
|
sender,
|
2017-11-06 00:04:55 +03:00
|
|
|
" sent a notification",
|
2018-01-05 01:27:32 +03:00
|
|
|
utils::descriptiveTime(timestamp),
|
2017-12-26 00:02:33 +03:00
|
|
|
timestamp};
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
generateTimestamp(timestamp);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-12-24 14:13:07 +03:00
|
|
|
body.replace(conf::strings::url_regex, conf::strings::url_html);
|
2017-11-06 03:23:19 +03:00
|
|
|
body.replace("\n", "<br/>");
|
2017-11-16 17:33:52 +03:00
|
|
|
body = "<i>" + body + "</i>";
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->setContentsMargins(0, 0, 20, 4);
|
|
|
|
messageLayout_->setSpacing(20);
|
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
if (with_sender) {
|
2017-12-04 19:41:19 +03:00
|
|
|
auto displayName = TimelineViewManager::displayName(sender);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
generateBody(displayName, body);
|
|
|
|
setupAvatarLayout(displayName);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addLayout(headerLayout_, 1);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
AvatarProvider::resolve(sender, [=](const QImage &img) { setUserAvatar(img); });
|
2017-08-26 11:33:26 +03:00
|
|
|
} else {
|
|
|
|
generateBody(body);
|
|
|
|
setupSimpleLayout();
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addWidget(body_, 1);
|
2017-08-26 11:33:26 +03:00
|
|
|
}
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addWidget(timestamp_);
|
|
|
|
mainLayout_->addLayout(messageLayout_);
|
2017-05-07 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2017-09-02 16:47:59 +03:00
|
|
|
/*
|
|
|
|
* Used to display remote emote messages.
|
|
|
|
*/
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote> &event,
|
2017-09-02 16:47:59 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
event_id_ = QString::fromStdString(event.event_id);
|
|
|
|
const auto sender = QString::fromStdString(event.sender);
|
2017-11-24 01:10:58 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
auto body = QString::fromStdString(event.content.body).trimmed();
|
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
|
|
|
|
auto displayName = TimelineViewManager::displayName(sender);
|
2017-09-02 16:47:59 +03:00
|
|
|
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body);
|
|
|
|
|
2018-01-05 01:27:32 +03:00
|
|
|
descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp};
|
2017-09-02 16:47:59 +03:00
|
|
|
|
|
|
|
generateTimestamp(timestamp);
|
2017-09-22 22:12:36 +03:00
|
|
|
emoteMsg = emoteMsg.toHtmlEscaped();
|
2017-12-24 14:13:07 +03:00
|
|
|
emoteMsg.replace(conf::strings::url_regex, conf::strings::url_html);
|
2017-11-06 03:23:19 +03:00
|
|
|
emoteMsg.replace("\n", "<br/>");
|
2017-09-02 16:47:59 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->setContentsMargins(0, 0, 20, 4);
|
|
|
|
messageLayout_->setSpacing(20);
|
|
|
|
|
2017-09-02 16:47:59 +03:00
|
|
|
if (with_sender) {
|
|
|
|
generateBody(displayName, emoteMsg);
|
|
|
|
setupAvatarLayout(displayName);
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addLayout(headerLayout_, 1);
|
2017-09-02 16:47:59 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
AvatarProvider::resolve(sender, [=](const QImage &img) { setUserAvatar(img); });
|
2017-09-02 16:47:59 +03:00
|
|
|
} else {
|
|
|
|
generateBody(emoteMsg);
|
|
|
|
setupSimpleLayout();
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addWidget(body_, 1);
|
2017-09-02 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addWidget(timestamp_);
|
|
|
|
mainLayout_->addLayout(messageLayout_);
|
2017-09-02 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-01 12:46:58 +03:00
|
|
|
/*
|
|
|
|
* Used to display remote text messages.
|
|
|
|
*/
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text> &event,
|
2017-08-26 11:33:26 +03:00
|
|
|
bool with_sender,
|
|
|
|
QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2017-05-07 17:15:38 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
init();
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
event_id_ = QString::fromStdString(event.event_id);
|
|
|
|
const auto sender = QString::fromStdString(event.sender);
|
2017-11-24 01:10:58 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
auto body = QString::fromStdString(event.content.body).trimmed();
|
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
|
|
|
|
auto displayName = TimelineViewManager::displayName(sender);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
QSettings settings;
|
2017-12-04 19:41:19 +03:00
|
|
|
descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
|
|
|
|
sender,
|
2017-11-06 00:04:55 +03:00
|
|
|
QString(": %1").arg(body),
|
2018-01-05 01:27:32 +03:00
|
|
|
utils::descriptiveTime(timestamp),
|
2017-12-26 00:02:33 +03:00
|
|
|
timestamp};
|
2017-08-06 18:53:31 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
generateTimestamp(timestamp);
|
2017-04-12 00:52:56 +03:00
|
|
|
|
2017-09-22 22:12:36 +03:00
|
|
|
body = body.toHtmlEscaped();
|
2017-12-24 14:13:07 +03:00
|
|
|
body.replace(conf::strings::url_regex, conf::strings::url_html);
|
2017-11-06 03:23:19 +03:00
|
|
|
body.replace("\n", "<br/>");
|
2017-05-11 01:28:06 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->setContentsMargins(0, 0, 20, 4);
|
|
|
|
messageLayout_->setSpacing(20);
|
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
if (with_sender) {
|
|
|
|
generateBody(displayName, body);
|
|
|
|
setupAvatarLayout(displayName);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addLayout(headerLayout_, 1);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
AvatarProvider::resolve(sender, [=](const QImage &img) { setUserAvatar(img); });
|
2017-08-26 11:33:26 +03:00
|
|
|
} else {
|
|
|
|
generateBody(body);
|
|
|
|
setupSimpleLayout();
|
2018-01-14 13:29:54 +03:00
|
|
|
|
|
|
|
messageLayout_->addWidget(body_, 1);
|
2017-08-26 11:33:26 +03:00
|
|
|
}
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
messageLayout_->addWidget(timestamp_);
|
|
|
|
mainLayout_->addLayout(messageLayout_);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2018-01-14 13:54:17 +03:00
|
|
|
void
|
|
|
|
TimelineItem::markReceived()
|
|
|
|
{
|
|
|
|
auto checkmark = new QLabel("✓", this);
|
|
|
|
checkmark->setStyleSheet(QString("font-size: %1px;").arg(conf::timeline::fonts::timestamp));
|
|
|
|
checkmark->setAlignment(Qt::AlignTop);
|
|
|
|
|
|
|
|
messageLayout_->insertWidget(1, checkmark);
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:14:05 +03:00
|
|
|
// Only the body is displayed.
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::generateBody(const QString &body)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-11-16 17:33:52 +03:00
|
|
|
QString content("<span> %1 </span>");
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
body_ = new QLabel(this);
|
|
|
|
body_->setFont(font_);
|
|
|
|
body_->setWordWrap(true);
|
|
|
|
body_->setText(content.arg(replaceEmoji(body)));
|
|
|
|
body_->setMargin(0);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
|
|
|
|
body_->setOpenExternalLinks(true);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-06-05 02:14:05 +03:00
|
|
|
// The username/timestamp is displayed along with the message body.
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-08-26 11:33:26 +03:00
|
|
|
TimelineItem::generateBody(const QString &userid, const QString &body)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
auto sender = userid;
|
2017-05-08 19:44:01 +03:00
|
|
|
|
2017-09-28 22:26:24 +03:00
|
|
|
if (userid.startsWith("@")) {
|
|
|
|
// TODO: Fix this by using a UserId type.
|
|
|
|
if (userid.split(":")[0].split("@").size() > 1)
|
|
|
|
sender = userid.split(":")[0].split("@")[1];
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
QFont usernameFont = font_;
|
2018-01-08 12:27:37 +03:00
|
|
|
usernameFont.setWeight(60);
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2017-12-23 15:06:59 +03:00
|
|
|
QFontMetrics fm(usernameFont);
|
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
userName_ = new QLabel(this);
|
|
|
|
userName_->setFont(usernameFont);
|
2017-12-23 15:06:59 +03:00
|
|
|
userName_->setText(fm.elidedText(sender, Qt::ElideRight, 500));
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
if (body.isEmpty())
|
|
|
|
return;
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
body_ = new QLabel(this);
|
|
|
|
body_->setFont(font_);
|
|
|
|
body_->setWordWrap(true);
|
2017-11-25 15:01:01 +03:00
|
|
|
body_->setText(QString("<span> %1 </span>").arg(replaceEmoji(body)));
|
2017-08-26 11:33:26 +03:00
|
|
|
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
|
|
|
|
body_->setOpenExternalLinks(true);
|
|
|
|
body_->setMargin(0);
|
2017-06-05 02:14:05 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::generateTimestamp(const QDateTime &time)
|
2017-06-05 02:14:05 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
QFont timestampFont;
|
|
|
|
timestampFont.setPixelSize(conf::timeline::fonts::timestamp);
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
QFontMetrics fm(timestampFont);
|
|
|
|
int topMargin = QFontMetrics(font_).ascent() - fm.ascent();
|
2017-07-01 12:46:58 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
timestamp_ = new QLabel(this);
|
2018-01-14 13:29:54 +03:00
|
|
|
timestamp_->setAlignment(Qt::AlignTop);
|
2017-08-26 11:33:26 +03:00
|
|
|
timestamp_->setFont(timestampFont);
|
2018-01-14 13:29:54 +03:00
|
|
|
timestamp_->setText(
|
|
|
|
QString("<span style=\"color: #999\"> %1 </span>").arg(time.toString("HH:mm")));
|
2017-08-26 11:33:26 +03:00
|
|
|
timestamp_->setContentsMargins(0, topMargin, 0, 0);
|
2017-11-16 17:33:52 +03:00
|
|
|
timestamp_->setStyleSheet(
|
|
|
|
QString("font-size: %1px;").arg(conf::timeline::fonts::timestamp));
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QString
|
|
|
|
TimelineItem::replaceEmoji(const QString &body)
|
2017-04-19 19:38:39 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
QString fmtBody = "";
|
|
|
|
|
2017-10-02 20:52:21 +03:00
|
|
|
QVector<uint> utf32_string = body.toUcs4();
|
2017-08-26 11:33:26 +03:00
|
|
|
|
2017-10-02 20:52:21 +03:00
|
|
|
for (auto &code : utf32_string) {
|
2017-08-26 11:33:26 +03:00
|
|
|
// TODO: Be more precise here.
|
|
|
|
if (code > 9000)
|
|
|
|
fmtBody += QString("<span style=\"font-family: Emoji "
|
|
|
|
"One; font-size: %1px\">")
|
|
|
|
.arg(conf::emojiSize) +
|
2017-10-02 20:52:21 +03:00
|
|
|
QString::fromUcs4(&code, 1) + "</span>";
|
2017-08-26 11:33:26 +03:00
|
|
|
else
|
2017-10-02 20:52:21 +03:00
|
|
|
fmtBody += QString::fromUcs4(&code, 1);
|
2017-08-26 11:33:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmtBody;
|
2017-04-19 19:38:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::setupAvatarLayout(const QString &userName)
|
2017-06-05 02:14:05 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
topLayout_->setContentsMargins(conf::timeline::msgMargin, conf::timeline::msgMargin, 0, 0);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
userAvatar_ = new Avatar(this);
|
|
|
|
userAvatar_->setLetter(QChar(userName[0]).toUpper());
|
|
|
|
userAvatar_->setSize(conf::timeline::avatarSize);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
// TODO: The provided user name should be a UserId class
|
|
|
|
if (userName[0] == '@' && userName.size() > 1)
|
|
|
|
userAvatar_->setLetter(QChar(userName[1]).toUpper());
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
sideLayout_ = new QVBoxLayout;
|
|
|
|
sideLayout_->setMargin(0);
|
|
|
|
sideLayout_->setSpacing(0);
|
2017-08-26 11:33:26 +03:00
|
|
|
sideLayout_->addWidget(userAvatar_);
|
|
|
|
sideLayout_->addStretch(1);
|
2018-01-14 13:29:54 +03:00
|
|
|
topLayout_->insertLayout(0, sideLayout_);
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2018-01-14 13:29:54 +03:00
|
|
|
headerLayout_ = new QVBoxLayout;
|
2017-12-12 10:31:00 +03:00
|
|
|
headerLayout_->setMargin(0);
|
2018-01-14 13:29:54 +03:00
|
|
|
headerLayout_->setSpacing(0);
|
2017-12-12 10:31:00 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
headerLayout_->addWidget(userName_);
|
2018-01-14 13:29:54 +03:00
|
|
|
headerLayout_->addWidget(body_);
|
2017-06-05 02:14:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::setupSimpleLayout()
|
2017-06-05 02:14:05 +03:00
|
|
|
{
|
2018-01-14 13:29:54 +03:00
|
|
|
topLayout_->setContentsMargins(conf::timeline::avatarSize + conf::timeline::msgMargin + 1,
|
|
|
|
conf::timeline::msgMargin / 3,
|
|
|
|
0,
|
|
|
|
0);
|
2017-06-05 02:14:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineItem::setUserAvatar(const QImage &avatar)
|
2017-06-05 02:14:05 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
if (userAvatar_ == nullptr)
|
|
|
|
return;
|
2017-06-05 02:14:05 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
userAvatar_->setImage(avatar);
|
2017-06-05 02:14:05 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:11:33 +03:00
|
|
|
TimelineItem::~TimelineItem() {}
|
2017-11-16 17:33:52 +03:00
|
|
|
|
2018-01-03 19:05:49 +03:00
|
|
|
void
|
|
|
|
TimelineItem::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
if (receiptsMenu_)
|
|
|
|
receiptsMenu_->exec(event->globalPos());
|
|
|
|
}
|
|
|
|
|
2017-11-16 17:33:52 +03:00
|
|
|
void
|
|
|
|
TimelineItem::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|