just proxying button event from feed view delegate to the feed model

This commit is contained in:
Blue 2021-02-06 14:02:42 +03:00
parent b3c6860e25
commit ebe5addfb5
6 changed files with 62 additions and 2 deletions

View File

@ -296,3 +296,13 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c
return att; 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;
}

View File

@ -55,6 +55,8 @@ public:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
void responseArchive(const std::list<Shared::Message> list, bool last); void responseArchive(const std::list<Shared::Message> list, bool last);
void downloadAttachment(const QString& messageId);
void uploadAttachment(const QString& messageId);
unsigned int unreadMessagesCount() const; unsigned int unreadMessagesCount() const;

View File

@ -24,6 +24,7 @@
#include <QDebug> #include <QDebug>
#include "messagedelegate.h" #include "messagedelegate.h"
#include "ui/models/messagefeed.h"
constexpr int maxMessageHeight = 10000; constexpr int maxMessageHeight = 10000;
constexpr int approximateSingleMessageHeight = 20; constexpr int approximateSingleMessageHeight = 20;
@ -33,6 +34,7 @@ FeedView::FeedView(QWidget* parent):
hints(), hints(),
vo(0), vo(0),
specialDelegate(false), specialDelegate(false),
specialModel(false),
clearWidgetsMode(false) clearWidgetsMode(false)
{ {
horizontalScrollBar()->setRange(0, 0); horizontalScrollBar()->setRange(0, 0);
@ -231,7 +233,7 @@ void FeedView::paintEvent(QPaintEvent* event)
QPoint cursor = vp->mapFromGlobal(QCursor::pos()); QPoint cursor = vp->mapFromGlobal(QCursor::pos());
if (clearWidgetsMode && specialDelegate) { if (clearWidgetsMode && specialDelegate) {
MessageDelegate* del = dynamic_cast<MessageDelegate*>(itemDelegate()); MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
del->beginClearWidgets(); del->beginClearWidgets();
} }
@ -242,7 +244,7 @@ void FeedView::paintEvent(QPaintEvent* event)
} }
if (clearWidgetsMode && specialDelegate) { if (clearWidgetsMode && specialDelegate) {
MessageDelegate* del = dynamic_cast<MessageDelegate*>(itemDelegate()); MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
del->endClearWidgets(); del->endClearWidgets();
clearWidgetsMode = false; clearWidgetsMode = false;
} }
@ -276,12 +278,43 @@ QFont FeedView::getFont() const
void FeedView::setItemDelegate(QAbstractItemDelegate* delegate) void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
{ {
if (specialDelegate) {
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
}
QAbstractItemView::setItemDelegate(delegate); QAbstractItemView::setItemDelegate(delegate);
MessageDelegate* del = dynamic_cast<MessageDelegate*>(delegate); MessageDelegate* del = dynamic_cast<MessageDelegate*>(delegate);
if (del) { if (del) {
specialDelegate = true; specialDelegate = true;
connect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
} else { } else {
specialDelegate = false; specialDelegate = false;
} }
} }
void FeedView::setModel(QAbstractItemModel* model)
{
QAbstractItemView::setModel(model);
Models::MessageFeed* feed = dynamic_cast<Models::MessageFeed*>(model);
if (feed) {
specialModel = true;
} else {
specialModel = false;
}
}
void FeedView::onMessageButtonPushed(const QString& messageId, bool download)
{
if (specialModel) {
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
if (download) {
feed->downloadAttachment(messageId);
} else {
feed->uploadAttachment(messageId);
}
}
}

View File

@ -43,6 +43,7 @@ public:
void setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags command) override; void setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags command) override;
QRegion visualRegionForSelection(const QItemSelection & selection) const override; QRegion visualRegionForSelection(const QItemSelection & selection) const override;
void setItemDelegate(QAbstractItemDelegate* delegate); void setItemDelegate(QAbstractItemDelegate* delegate);
void setModel(QAbstractItemModel * model) override;
QFont getFont() const; QFont getFont() const;
@ -51,6 +52,7 @@ public slots:
protected slots: protected slots:
void rowsInserted(const QModelIndex & parent, int start, int end) override; void rowsInserted(const QModelIndex & parent, int start, int end) override;
void verticalScrollbarValueChanged(int value) override; void verticalScrollbarValueChanged(int value) override;
void onMessageButtonPushed(const QString& messageId, bool download);
protected: protected:
int verticalOffset() const override; int verticalOffset() const override;
@ -71,6 +73,7 @@ private:
std::deque<Hint> hints; std::deque<Hint> hints;
int vo; int vo;
bool specialDelegate; bool specialDelegate;
bool specialModel;
bool clearWidgetsMode; bool clearWidgetsMode;
}; };

View File

@ -253,6 +253,7 @@ QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const
result->download = false; result->download = false;
} }
buttons->insert(std::make_pair(data.id, result)); buttons->insert(std::make_pair(data.id, result));
connect(result, &QPushButton::clicked, this, &MessageDelegate::onButtonPushed);
} }
return result; return result;
@ -285,6 +286,11 @@ void MessageDelegate::endClearWidgets()
} }
} }
void MessageDelegate::onButtonPushed() const
{
FeedButton* btn = static_cast<FeedButton*>(sender());
emit buttonPushed(btn->messageId, btn->download);
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const // void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// { // {

View File

@ -50,10 +50,16 @@ public:
void endClearWidgets(); void endClearWidgets();
void beginClearWidgets(); void beginClearWidgets();
signals:
void buttonPushed(const QString& messageId, bool download) const;
protected: protected:
void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const; void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const;
QPushButton* getButton(const Models::FeedItem& data) const; QPushButton* getButton(const Models::FeedItem& data) const;
protected slots:
void onButtonPushed() const;
private: private:
class FeedButton : public QPushButton { class FeedButton : public QPushButton {
public: public: