From ebe5addfb5438ef3a3a73b95b04ac42b3ded2eec Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 6 Feb 2021 14:02:42 +0300 Subject: [PATCH] just proxying button event from feed view delegate to the feed model --- ui/models/messagefeed.cpp | 10 ++++++++++ ui/models/messagefeed.h | 2 ++ ui/utils/feedview.cpp | 37 ++++++++++++++++++++++++++++++++++-- ui/utils/feedview.h | 3 +++ ui/utils/messagedelegate.cpp | 6 ++++++ ui/utils/messagedelegate.h | 6 ++++++ 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 78c216f..7d167cd 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -296,3 +296,13 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c return att; } + +void Models::MessageFeed::downloadAttachment(const QString& messageId) +{ + qDebug() << "request to download attachment of the message" << messageId; +} + +void Models::MessageFeed::uploadAttachment(const QString& messageId) +{ + qDebug() << "request to upload attachment of the message" << messageId; +} diff --git a/ui/models/messagefeed.h b/ui/models/messagefeed.h index bc403c1..9a58c45 100644 --- a/ui/models/messagefeed.h +++ b/ui/models/messagefeed.h @@ -55,6 +55,8 @@ public: QHash roleNames() const override; void responseArchive(const std::list list, bool last); + void downloadAttachment(const QString& messageId); + void uploadAttachment(const QString& messageId); unsigned int unreadMessagesCount() const; diff --git a/ui/utils/feedview.cpp b/ui/utils/feedview.cpp index 21f2956..15f6fb3 100644 --- a/ui/utils/feedview.cpp +++ b/ui/utils/feedview.cpp @@ -24,6 +24,7 @@ #include #include "messagedelegate.h" +#include "ui/models/messagefeed.h" constexpr int maxMessageHeight = 10000; constexpr int approximateSingleMessageHeight = 20; @@ -33,6 +34,7 @@ FeedView::FeedView(QWidget* parent): hints(), vo(0), specialDelegate(false), + specialModel(false), clearWidgetsMode(false) { horizontalScrollBar()->setRange(0, 0); @@ -231,7 +233,7 @@ void FeedView::paintEvent(QPaintEvent* event) QPoint cursor = vp->mapFromGlobal(QCursor::pos()); if (clearWidgetsMode && specialDelegate) { - MessageDelegate* del = dynamic_cast(itemDelegate()); + MessageDelegate* del = static_cast(itemDelegate()); del->beginClearWidgets(); } @@ -242,7 +244,7 @@ void FeedView::paintEvent(QPaintEvent* event) } if (clearWidgetsMode && specialDelegate) { - MessageDelegate* del = dynamic_cast(itemDelegate()); + MessageDelegate* del = static_cast(itemDelegate()); del->endClearWidgets(); clearWidgetsMode = false; } @@ -276,12 +278,43 @@ QFont FeedView::getFont() const void FeedView::setItemDelegate(QAbstractItemDelegate* delegate) { + if (specialDelegate) { + MessageDelegate* del = static_cast(itemDelegate()); + disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed); + } + QAbstractItemView::setItemDelegate(delegate); MessageDelegate* del = dynamic_cast(delegate); if (del) { specialDelegate = true; + connect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed); } else { specialDelegate = false; } } + +void FeedView::setModel(QAbstractItemModel* model) +{ + QAbstractItemView::setModel(model); + + Models::MessageFeed* feed = dynamic_cast(model); + if (feed) { + specialModel = true; + } else { + specialModel = false; + } +} + +void FeedView::onMessageButtonPushed(const QString& messageId, bool download) +{ + if (specialModel) { + Models::MessageFeed* feed = static_cast(model()); + + if (download) { + feed->downloadAttachment(messageId); + } else { + feed->uploadAttachment(messageId); + } + } +} diff --git a/ui/utils/feedview.h b/ui/utils/feedview.h index 0256a4d..6d16ea3 100644 --- a/ui/utils/feedview.h +++ b/ui/utils/feedview.h @@ -43,6 +43,7 @@ public: void setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags command) override; QRegion visualRegionForSelection(const QItemSelection & selection) const override; void setItemDelegate(QAbstractItemDelegate* delegate); + void setModel(QAbstractItemModel * model) override; QFont getFont() const; @@ -51,6 +52,7 @@ public slots: protected slots: void rowsInserted(const QModelIndex & parent, int start, int end) override; void verticalScrollbarValueChanged(int value) override; + void onMessageButtonPushed(const QString& messageId, bool download); protected: int verticalOffset() const override; @@ -71,6 +73,7 @@ private: std::deque hints; int vo; bool specialDelegate; + bool specialModel; bool clearWidgetsMode; }; diff --git a/ui/utils/messagedelegate.cpp b/ui/utils/messagedelegate.cpp index 5aebebe..038b0af 100644 --- a/ui/utils/messagedelegate.cpp +++ b/ui/utils/messagedelegate.cpp @@ -253,6 +253,7 @@ QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const result->download = false; } buttons->insert(std::make_pair(data.id, result)); + connect(result, &QPushButton::clicked, this, &MessageDelegate::onButtonPushed); } return result; @@ -285,6 +286,11 @@ void MessageDelegate::endClearWidgets() } } +void MessageDelegate::onButtonPushed() const +{ + FeedButton* btn = static_cast(sender()); + emit buttonPushed(btn->messageId, btn->download); +} // void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const // { diff --git a/ui/utils/messagedelegate.h b/ui/utils/messagedelegate.h index b71163c..69ffb84 100644 --- a/ui/utils/messagedelegate.h +++ b/ui/utils/messagedelegate.h @@ -50,10 +50,16 @@ public: void endClearWidgets(); void beginClearWidgets(); +signals: + void buttonPushed(const QString& messageId, bool download) const; + protected: void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const; QPushButton* getButton(const Models::FeedItem& data) const; +protected slots: + void onButtonPushed() const; + private: class FeedButton : public QPushButton { public: