self nick in the chat fix, hovering message feature

This commit is contained in:
Blue 2021-01-08 00:50:12 +03:00
parent 270a32db9e
commit 15342f3c53
7 changed files with 130 additions and 30 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.4)
project(squawk)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

View File

@ -29,7 +29,9 @@ const QHash<int, QByteArray> Models::MessageFeed::roles = {
{DeliveryState, "deliveryState"},
{Correction, "correction"},
{SentByMe,"sentByMe"},
{Avatar, "avatar"}
{Avatar, "avatar"},
{Attach, "attach"},
{Bulk, "bulk"}
};
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
@ -94,15 +96,11 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
answer = msg->getBody();
break;
case Sender:
if (rosterItem->isRoom()) {
if (sentByMe(*msg)) {
answer = rosterItem->getDisplayedName();
} else {
answer = msg->getFromResource();
}
if (sentByMe(*msg)) {
answer = rosterItem->getAccountName();
} else {
if (sentByMe(*msg)) {
answer = rosterItem->getAccountName();
if (rosterItem->isRoom()) {
answer = msg->getFromResource();
} else {
answer = rosterItem->getDisplayedName();
}
@ -139,7 +137,38 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
answer = path;
}
}
case Attach:
break;
case Bulk: {
FeedItem item;
item.sentByMe = sentByMe(*msg);
item.date = msg->getTime();
item.state = msg->getState();
item.correction = msg->getEdited();
item.text = msg->getBody();
item.avatar.clear();
if (item.sentByMe) {
item.sender = rosterItem->getAccountName();
item.avatar = rosterItem->getAccountAvatarPath();
} else {
if (rosterItem->isRoom()) {
item.sender = msg->getFromResource();
const Room* room = static_cast<const Room*>(rosterItem);
item.avatar = room->getParticipantIconPath(msg->getFromResource());
} else {
item.sender = rosterItem->getDisplayedName();
if (rosterItem->getAvatarState() != Shared::Avatar::empty) {
item.avatar = rosterItem->getAvatarPath();
}
}
}
if (item.avatar.size() == 0) {
item.avatar = Shared::iconPath("user", true);
}
answer.setValue(item);
}
default:
break;
}

View File

@ -72,7 +72,17 @@ public:
DeliveryState,
Correction,
SentByMe,
Avatar
Avatar,
Attach,
Bulk
};
enum Attachment {
none,
remote,
downloading,
uploading,
ready
};
private:
enum SyncState {
@ -80,6 +90,12 @@ private:
syncing,
complete
};
struct Attach {
Attachment state;
qreal progress;
QString localPath;
};
//tags
struct id {};
struct time {};
@ -118,6 +134,19 @@ private:
static const QHash<int, QByteArray> roles;
};
struct FeedItem {
QString text;
QString sender;
QString avatar;
bool sentByMe;
bool correction;
QDateTime date;
Shared::Message::State state;
MessageFeed::Attachment attach;
};
};
Q_DECLARE_METATYPE(Models::FeedItem);
#endif // MESSAGEFEED_H

View File

@ -33,6 +33,9 @@ FeedView::FeedView(QWidget* parent):
{
horizontalScrollBar()->setRange(0, 0);
verticalScrollBar()->setSingleStep(approximateSingleMessageHeight);
setMouseTracking(true);
setSelectionBehavior(SelectItems);
// viewport()->setAttribute(Qt::WA_Hover, true);
}
FeedView::~FeedView()
@ -41,14 +44,12 @@ FeedView::~FeedView()
QModelIndex FeedView::indexAt(const QPoint& point) const
{
int32_t totalHeight = viewport()->height() + vo;
if (point.y() <= totalHeight) { //if it's bigger - someone wants to know the index below the feed beginning, it's invalid
uint32_t y = totalHeight - point.y();
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
if (y > hints[i].offset) {
return model()->index(i - 1, 0, rootIndex());
}
int32_t vh = viewport()->height();
uint32_t y = vh - point.y() + vo;
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
if (hints[i].offset >= y) {
return model()->index(i - 1, 0, rootIndex());
}
}
@ -219,9 +220,11 @@ void FeedView::paintEvent(QPaintEvent* event)
QPainter painter(vp);
QStyleOptionViewItem option = viewOptions();
option.features = QStyleOptionViewItem::WrapText;
QPoint cursor = vp->mapFromGlobal(QCursor::pos());
for (const QModelIndex& index : toRener) {
option.rect = visualRect(index);
option.state.setFlag(QStyle::State_MouseOver, option.rect.contains(cursor));
itemDelegate(index)->paint(&painter, option, index);
}
}
@ -233,6 +236,16 @@ void FeedView::verticalScrollbarValueChanged(int value)
QAbstractItemView::verticalScrollbarValueChanged(vo);
}
void FeedView::mouseMoveEvent(QMouseEvent* event)
{
if (!isVisible()) {
return;
}
QAbstractItemView::mouseMoveEvent(event);
}
QFont FeedView::getFont() const
{
return viewOptions().font;

View File

@ -45,6 +45,8 @@ public:
QFont getFont() const;
public slots:
protected slots:
void rowsInserted(const QModelIndex & parent, int start, int end) override;
void verticalScrollbarValueChanged(int value) override;
@ -54,6 +56,7 @@ protected:
int horizontalOffset() const override;
void paintEvent(QPaintEvent * event) override;
void updateGeometries() override;
void mouseMoveEvent(QMouseEvent * event) override;
private:
bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight);

View File

@ -16,8 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QPainter>
#include <QApplication>
#include "messagedelegate.h"
#include "ui/models/messagefeed.h"
@ -41,16 +43,21 @@ 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();
QVariant vi = index.data(Models::MessageFeed::Bulk);
if (!vi.isValid()) {
return;
}
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QIcon icon(index.data(Models::MessageFeed::Avatar).toString());
if (sentByMe) {
if (option.state & QStyle::State_MouseOver) {
painter->fillRect(option.rect, option.palette.brush(QPalette::Inactive, QPalette::Highlight));
}
QIcon icon(data.avatar);
if (data.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));
@ -58,7 +65,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
QStyleOptionViewItem opt = option;
QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2);
if (!sentByMe) {
if (!data.sentByMe) {
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0);
} else {
@ -66,27 +73,39 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
}
opt.rect = messageRect;
QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, data.text).size();
messageSize.rheight() += nickMetrics.lineSpacing();
messageSize.rheight() += dateMetrics.height();
if (messageSize.width() < opt.rect.width()) {
QSize senderSize = nickMetrics.boundingRect(messageRect, 0, data.sender).size();
if (senderSize.width() > messageSize.width()) {
messageSize.setWidth(senderSize.width());
}
} else {
messageSize.setWidth(opt.rect.width());
}
QRect rect;
painter->setFont(nickFont);
painter->drawText(opt.rect, opt.displayAlignment, index.data(Models::MessageFeed::Sender).toString(), &rect);
painter->drawText(opt.rect, opt.displayAlignment, data.sender, &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);
painter->drawText(opt.rect, opt.displayAlignment | Qt::TextWordWrap, data.text, &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->drawText(opt.rect, opt.displayAlignment, data.date.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);
QRect messageRect = option.rect.adjusted(0, margin / 2, -(avatarHeight + 3 * margin), -margin / 2);
QStyleOptionViewItem opt = option;
opt.rect = messageRect;
QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString()).size();
@ -123,6 +142,12 @@ void MessageDelegate::initializeFonts(const QFont& font)
dateMetrics = QFontMetrics(dateFont);
}
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{
//qDebug() << event->type();
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// {

View File

@ -37,6 +37,7 @@ public:
//void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void initializeFonts(const QFont& font);
bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override;
private:
QFont bodyFont;