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;
}
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;
void responseArchive(const std::list<Shared::Message> list, bool last);
void downloadAttachment(const QString& messageId);
void uploadAttachment(const QString& messageId);
unsigned int unreadMessagesCount() const;

View File

@ -24,6 +24,7 @@
#include <QDebug>
#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<MessageDelegate*>(itemDelegate());
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
del->beginClearWidgets();
}
@ -242,7 +244,7 @@ void FeedView::paintEvent(QPaintEvent* event)
}
if (clearWidgetsMode && specialDelegate) {
MessageDelegate* del = dynamic_cast<MessageDelegate*>(itemDelegate());
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
del->endClearWidgets();
clearWidgetsMode = false;
}
@ -276,12 +278,43 @@ QFont FeedView::getFont() const
void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
{
if (specialDelegate) {
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
}
QAbstractItemView::setItemDelegate(delegate);
MessageDelegate* del = dynamic_cast<MessageDelegate*>(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<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;
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<Hint> hints;
int vo;
bool specialDelegate;
bool specialModel;
bool clearWidgetsMode;
};

View File

@ -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<FeedButton*>(sender());
emit buttonPushed(btn->messageId, btn->download);
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// {

View File

@ -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: