1
0
forked from blue/squawk

Some basic message painting

This commit is contained in:
Blue 2020-08-21 00:32:30 +03:00
parent e1eea2f3a2
commit e0ef1ef797
16 changed files with 296 additions and 44 deletions

View File

@ -40,6 +40,7 @@ set(squawkUI_SRC
utils/comboboxdelegate.cpp
utils/dropshadoweffect.cpp
utils/feedview.cpp
utils/messagedelegate.cpp
)
# Tell CMake to create the helloworld executable

View File

@ -231,7 +231,7 @@ void Models::Account::toOfflineState()
Item::toOfflineState();
}
QString Models::Account::getAvatarPath()
QString Models::Account::getAvatarPath() const
{
return avatarPath;
}

View File

@ -57,7 +57,7 @@ namespace Models {
QString getError() const;
void setAvatarPath(const QString& path);
QString getAvatarPath();
QString getAvatarPath() const;
void setAvailability(Shared::Availability p_avail);
void setAvailability(unsigned int p_avail);

View File

@ -27,7 +27,7 @@ Models::Element::Element(Type p_type, const Models::Account* acc, const QString&
avatarPath(),
avatarState(Shared::Avatar::empty),
account(acc),
feed(new MessageFeed())
feed(new MessageFeed(this))
{
connect(feed, &MessageFeed::requestArchive, this, &Element::requestArchive);
@ -149,3 +149,8 @@ void Models::Element::responseArchive(const std::list<Shared::Message> list)
{
feed->responseArchive(list);
}
bool Models::Element::isRoom() const
{
return type != contact;
}

View File

@ -42,6 +42,7 @@ public:
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
unsigned int getMessagesCount() const;
void responseArchive(const std::list<Shared::Message> list);
bool isRoom() const;
signals:
void requestArchive(const QString& before);

View File

@ -283,6 +283,15 @@ Shared::ConnectionState Models::Item::getAccountConnectionState() const
return acc->getState();
}
QString Models::Item::getAccountAvatarPath() const
{
const Account* acc = getParentAccount();
if (acc == nullptr) {
return "";
}
return acc->getAvatarPath();
}
QString Models::Item::getDisplayedName() const
{
return name;

View File

@ -80,6 +80,7 @@ class Item : public QObject{
QString getAccountName() const;
QString getAccountJid() const;
QString getAccountResource() const;
QString getAccountAvatarPath() const;
Shared::ConnectionState getAccountConnectionState() const;
Shared::Availability getAccountAvailability() const;

View File

@ -17,35 +17,39 @@
*/
#include "messagefeed.h"
#include "element.h"
#include "room.h"
#include <QDebug>
const QHash<int, QByteArray> MessageFeed::roles = {
const QHash<int, QByteArray> Models::MessageFeed::roles = {
{Text, "text"},
{Sender, "sender"},
{Date, "date"},
{DeliveryState, "deliveryState"},
{Correction, "correction"},
{SentByMe,"sentByMe"}
{SentByMe,"sentByMe"},
{Avatar, "avatar"}
};
MessageFeed::MessageFeed(QObject* parent):
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
QAbstractListModel(parent),
storage(),
indexById(storage.get<id>()),
indexByTime(storage.get<time>()),
rosterItem(ri),
syncState(incomplete)
{
}
MessageFeed::~MessageFeed()
Models::MessageFeed::~MessageFeed()
{
for (Shared::Message* message : storage) {
delete message;
}
}
void MessageFeed::addMessage(const Shared::Message& msg)
void Models::MessageFeed::addMessage(const Shared::Message& msg)
{
QString id = msg.getId();
StorageById::const_iterator itr = indexById.find(id);
@ -67,15 +71,15 @@ void MessageFeed::addMessage(const Shared::Message& msg)
endInsertRows();
}
void MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
void Models::MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
{
}
void MessageFeed::removeMessage(const QString& id)
void Models::MessageFeed::removeMessage(const QString& id)
{
}
QVariant MessageFeed::data(const QModelIndex& index, int role) const
QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
{
int i = index.row();
QVariant answer;
@ -90,7 +94,19 @@ QVariant MessageFeed::data(const QModelIndex& index, int role) const
answer = msg->getBody();
break;
case Sender:
answer = msg->getFrom();
if (rosterItem->isRoom()) {
if (sentByMe(*msg)) {
answer = rosterItem->getDisplayedName();
} else {
answer = msg->getFromResource();
}
} else {
if (sentByMe(*msg)) {
answer = rosterItem->getAccountName();
} else {
answer = rosterItem->getDisplayedName();
}
}
break;
case Date:
answer = msg->getTime();
@ -102,51 +118,55 @@ QVariant MessageFeed::data(const QModelIndex& index, int role) const
answer = msg->getEdited();
break;
case SentByMe:
answer = msg->getOutgoing();
answer = sentByMe(*msg);
break;
case Avatar: {
QString path;
if (sentByMe(*msg)) {
path = rosterItem->getAccountAvatarPath();
} else if (!rosterItem->isRoom()) {
if (rosterItem->getAvatarState() != Shared::Avatar::empty) {
path = rosterItem->getAvatarPath();
}
} else {
const Room* room = static_cast<const Room*>(rosterItem);
path = room->getParticipantIconPath(msg->getFromResource());
}
if (path.size() == 0) {
answer = Shared::iconPath("user", true);
} else {
answer = path;
}
}
break;
default:
break;
}
} else {
switch (role) {
case Qt::DisplayRole:
case Text:
answer = "loading...";
break;
default:
answer = "";
break;
}
}
return answer;
}
int MessageFeed::rowCount(const QModelIndex& parent) const
int Models::MessageFeed::rowCount(const QModelIndex& parent) const
{
int count = storage.size();
if (syncState == syncing) {
count++;
}
return count;
return storage.size();
}
unsigned int MessageFeed::unreadMessagesCount() const
unsigned int Models::MessageFeed::unreadMessagesCount() const
{
return storage.size(); //let's say they are all new for now =)
}
bool MessageFeed::canFetchMore(const QModelIndex& parent) const
bool Models::MessageFeed::canFetchMore(const QModelIndex& parent) const
{
return syncState == incomplete;
}
void MessageFeed::fetchMore(const QModelIndex& parent)
void Models::MessageFeed::fetchMore(const QModelIndex& parent)
{
if (syncState == incomplete) {
beginInsertRows(QModelIndex(), storage.size(), storage.size());
syncState = syncing;
endInsertRows();
emit requestStateChange(true);
if (storage.size() == 0) {
emit requestArchive("");
@ -156,13 +176,11 @@ void MessageFeed::fetchMore(const QModelIndex& parent)
}
}
void MessageFeed::responseArchive(const std::list<Shared::Message> list)
void Models::MessageFeed::responseArchive(const std::list<Shared::Message> list)
{
Storage::size_type size = storage.size();
if (syncState == syncing) {
beginRemoveRows(QModelIndex(), size, size);
syncState = incomplete;
endRemoveRows();
emit requestStateChange(false);
}
beginInsertRows(QModelIndex(), size, size + list.size() - 1);
@ -173,7 +191,17 @@ void MessageFeed::responseArchive(const std::list<Shared::Message> list)
endInsertRows();
}
QHash<int, QByteArray> MessageFeed::roleNames() const
QHash<int, QByteArray> Models::MessageFeed::roleNames() const
{
return roles;
}
bool Models::MessageFeed::sentByMe(const Shared::Message& msg) const
{
if (rosterItem->isRoom()) {
const Room* room = static_cast<const Room*>(rosterItem);
return room->getNick().toLower() == msg.getFromResource().toLower();
} else {
return msg.getOutgoing();
}
}

View File

@ -29,13 +29,17 @@
#include <boost/multi_index/mem_fun.hpp>
#include <shared/message.h>
#include <shared/icons.h>
namespace Models {
class Element;
class MessageFeed : public QAbstractListModel
{
Q_OBJECT
public:
MessageFeed(QObject *parent = nullptr);
MessageFeed(const Element* rosterItem, QObject *parent = nullptr);
~MessageFeed();
void addMessage(const Shared::Message& msg);
@ -55,6 +59,10 @@ public:
signals:
void requestArchive(const QString& before);
void requestStateChange(bool requesting);
protected:
bool sentByMe(const Shared::Message& msg) const;
public:
enum MessageRoles {
@ -63,7 +71,8 @@ public:
Date,
DeliveryState,
Correction,
SentByMe
SentByMe,
Avatar
};
private:
enum SyncState {
@ -104,10 +113,11 @@ private:
StorageById& indexById;
StorageByTime& indexByTime;
const Element* rosterItem;
SyncState syncState;
static const QHash<int, QByteArray> roles;
};
};
#endif // MESSAGEFEED_H

View File

@ -310,7 +310,12 @@ QString Models::Room::getParticipantIconPath(const QString& name) const
{
std::map<QString, Models::Participant*>::const_iterator itr = participants.find(name);
if (itr == participants.end()) {
return "";
std::map<QString, QString>::const_iterator eitr = exParticipantAvatars.find(name);
if (eitr != exParticipantAvatars.end()) {
return eitr->second;
} else {
return "";
}
}
return itr->second->getAvatarPath();

View File

@ -232,3 +232,8 @@ void FeedView::verticalScrollbarValueChanged(int value)
QAbstractItemView::verticalScrollbarValueChanged(vo);
}
QFont FeedView::getFont() const
{
return viewOptions().font;
}

View File

@ -43,6 +43,8 @@ public:
void setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags command) override;
QRegion visualRegionForSelection(const QItemSelection & selection) const override;
QFont getFont() const;
protected slots:
void rowsInserted(const QModelIndex & parent, int start, int end) override;
void verticalScrollbarValueChanged(int value) override;

View File

@ -0,0 +1,130 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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/>.
*/
#include <QPainter>
#include <QApplication>
#include "messagedelegate.h"
#include "ui/models/messagefeed.h"
constexpr int avatarHeight = 50;
constexpr int margin = 6;
MessageDelegate::MessageDelegate(QObject* parent):
QStyledItemDelegate(parent),
bodyFont(),
nickFont(),
dateFont(),
bodyMetrics(bodyFont),
nickMetrics(nickFont),
dateMetrics(dateFont)
{
}
MessageDelegate::~MessageDelegate()
{
}
void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
bool sentByMe = false;
QVariant sbm = index.data(Models::MessageFeed::SentByMe);
if (sbm.isValid()) {
sentByMe = sbm.toBool();
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QIcon icon(index.data(Models::MessageFeed::Avatar).toString());
if (sentByMe) {
painter->drawPixmap(option.rect.width() - avatarHeight - margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
} else {
painter->drawPixmap(margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
}
QStyleOptionViewItem opt = option;
QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2);
if (!sentByMe) {
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0);
} else {
opt.displayAlignment = Qt::AlignRight | Qt::AlignTop;
}
opt.rect = messageRect;
QRect rect;
painter->setFont(nickFont);
painter->drawText(opt.rect, opt.displayAlignment, index.data(Models::MessageFeed::Sender).toString(), &rect);
opt.rect.adjust(0, rect.height(), 0, 0);
painter->setFont(bodyFont);
painter->drawText(opt.rect, opt.displayAlignment | Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString(), &rect);
opt.rect.adjust(0, rect.height(), 0, 0);
painter->setFont(dateFont);
QColor q = painter->pen().color();
q.setAlpha(180);
painter->setPen(q);
painter->drawText(opt.rect, opt.displayAlignment, index.data(Models::MessageFeed::Date).toDateTime().toLocalTime().toString(), &rect);
painter->restore();
}
QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QRect messageRect = option.rect.adjusted(0, margin / 2, -(avatarHeight + 3 * margin), -margin);
QStyleOptionViewItem opt = option;
opt.rect = messageRect;
QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString()).size();
messageSize.rheight() += nickMetrics.lineSpacing();
messageSize.rheight() += dateMetrics.height();
if (messageSize.height() < avatarHeight) {
messageSize.setHeight(avatarHeight);
}
messageSize.rheight() += margin;
return messageSize;
}
void MessageDelegate::initializeFonts(const QFont& font)
{
bodyFont = font;
nickFont = font;
dateFont = font;
nickFont.setBold(true);
dateFont.setItalic(true);
float dps = dateFont.pointSizeF();
if (dps != -1) {
dateFont.setPointSizeF(dps * 0.7);
} else {
dateFont.setPointSize(dateFont.pointSize() - 2);
}
bodyMetrics = QFontMetrics(bodyFont);
nickMetrics = QFontMetrics(nickFont);
dateMetrics = QFontMetrics(dateFont);
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// {
//
// }

View File

@ -0,0 +1,50 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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/>.
*/
#ifndef MESSAGEDELEGATE_H
#define MESSAGEDELEGATE_H
#include <QStyledItemDelegate>
#include <QFont>
#include <QFontMetrics>
#include "shared/icons.h"
class MessageDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MessageDelegate(QObject *parent = nullptr);
~MessageDelegate();
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
//void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void initializeFonts(const QFont& font);
private:
QFont bodyFont;
QFont nickFont;
QFont dateFont;
QFontMetrics bodyMetrics;
QFontMetrics nickMetrics;
QFontMetrics dateMetrics;
};
#endif // MESSAGEDELEGATE_H

View File

@ -46,6 +46,7 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
overlay(new QWidget()),
filesToAttach(),
feed(new FeedView()),
delegate(new MessageDelegate()),
scroll(down),
manualSliderChange(false),
requestingHistory(false),
@ -54,6 +55,8 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
{
m_ui->setupUi(this);
feed->setItemDelegate(delegate);
delegate->initializeFonts(feed->getFont());
feed->setModel(el->feed);
m_ui->widget->layout()->addWidget(feed);

View File

@ -34,6 +34,7 @@
#include "ui/utils/flowlayout.h"
#include "ui/utils/badge.h"
#include "ui/utils/feedview.h"
#include "ui/utils/messagedelegate.h"
#include "shared/icons.h"
#include "shared/utils.h"
@ -147,6 +148,7 @@ protected:
QWidget* overlay;
W::Order<Badge*, Badge::Comparator> filesToAttach;
FeedView* feed;
MessageDelegate* delegate;
Scroll scroll;
bool manualSliderChange;
bool requestingHistory;