From 15342f3c53854201b301751b24eae08b74697c7d Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 8 Jan 2021 00:50:12 +0300 Subject: [PATCH] self nick in the chat fix, hovering message feature --- CMakeLists.txt | 2 +- ui/models/messagefeed.cpp | 47 +++++++++++++++++++++++++++++------- ui/models/messagefeed.h | 31 +++++++++++++++++++++++- ui/utils/feedview.cpp | 29 ++++++++++++++++------ ui/utils/feedview.h | 3 +++ ui/utils/messagedelegate.cpp | 47 +++++++++++++++++++++++++++--------- ui/utils/messagedelegate.h | 1 + 7 files changed, 130 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47599dc..f02df03 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.4) project(squawk) set(CMAKE_INCLUDE_CURRENT_DIR ON) diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 689d0d6..ae17c7b 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -29,7 +29,9 @@ const QHash 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(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; } diff --git a/ui/models/messagefeed.h b/ui/models/messagefeed.h index e8031ff..af4bd6a 100644 --- a/ui/models/messagefeed.h +++ b/ui/models/messagefeed.h @@ -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 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 diff --git a/ui/utils/feedview.cpp b/ui/utils/feedview.cpp index afa86a3..69c6ce9 100644 --- a/ui/utils/feedview.cpp +++ b/ui/utils/feedview.cpp @@ -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::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::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; diff --git a/ui/utils/feedview.h b/ui/utils/feedview.h index d084130..50f46e4 100644 --- a/ui/utils/feedview.h +++ b/ui/utils/feedview.h @@ -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); diff --git a/ui/utils/messagedelegate.cpp b/ui/utils/messagedelegate.cpp index 315cae0..089557b 100644 --- a/ui/utils/messagedelegate.cpp +++ b/ui/utils/messagedelegate.cpp @@ -16,8 +16,10 @@ * along with this program. If not, see . */ +#include #include #include + #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(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 // { diff --git a/ui/utils/messagedelegate.h b/ui/utils/messagedelegate.h index 0ed8463..4daa0a2 100644 --- a/ui/utils/messagedelegate.h +++ b/ui/utils/messagedelegate.h @@ -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;