debug and actual first way to display pictures in messageFeed

This commit is contained in:
Blue 2021-04-21 00:56:47 +03:00
parent 48e498be25
commit 0e937199b0
7 changed files with 118 additions and 8 deletions

View file

@ -113,6 +113,13 @@ void FeedView::rowsInserted(const QModelIndex& parent, int start, int end)
QAbstractItemView::rowsInserted(parent, start, end);
}
void FeedView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
{
//TODO make optimisations! There are some roles but not all that change geometry!
updateGeometries();
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
}
void FeedView::updateGeometries()
{
qDebug() << "updateGeometries";

View file

@ -52,6 +52,7 @@ public slots:
protected slots:
void rowsInserted(const QModelIndex & parent, int start, int end) override;
void verticalScrollbarValueChanged(int value) override;
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles) override;
void onMessageButtonPushed(const QString& messageId, bool download);
protected:

View file

@ -115,7 +115,6 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->setFont(nickFont);
painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect);
opt.rect.adjust(0, rect.height(), 0, 0);
painter->save();
switch (data.attach.state) {
@ -131,6 +130,11 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
paintButton(getButton(data), painter, data.sentByMe, opt);
break;
case Models::ready:
clearHelperWidget(data);
paintPreview(data, painter, opt);
break;
case Models::errorDownload:
case Models::errorUpload:
break;
}
painter->restore();
@ -178,6 +182,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
messageSize.rheight() += buttonHeight;
break;
case Models::ready:
messageSize.rheight() += calculateAttachSize(attach.localPath, messageRect).height();
break;
case Models::errorDownload:
case Models::errorUpload:
@ -245,15 +250,41 @@ void MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentBy
{
QPoint start = option.rect.topLeft();
QWidget* vp = static_cast<QWidget*>(painter->device());
bar->setParent(vp);
bar->move(start);
//QWidget* vp = static_cast<QWidget*>(painter->device());
// if (bar->parent() != vp) {
// bar->setParent(vp);
// }
// bar->move(start);
bar->resize(option.rect.width(), barHeight);
bar->show();
// bar->show();
painter->translate(start);
bar->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
option.rect.adjust(0, barHeight, 0, 0);
}
void MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const
{
Shared::Global::FileInfo info = Shared::Global::getFileInfo(data.attach.localPath);
if (info.preview == Shared::Global::FileInfo::Preview::picture) {
QSize size = constrainAttachSize(info.size, option.rect.size());
QPoint start;
if (data.sentByMe) {
start = {option.rect.width() - size.width(), option.rect.top()};
start.rx() += margin;
} else {
start = option.rect.topLeft();
}
QImage img(data.attach.localPath);
painter->drawImage(QRect(start, size), img);
option.rect.adjust(0, size.height(), 0, 0);
}
}
QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const
{
std::map<QString, FeedButton*>::const_iterator itr = buttons->find(data.id);
@ -376,6 +407,24 @@ void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const
}
}
QSize MessageDelegate::calculateAttachSize(const QString& path, const QRect& bounds) const
{
Shared::Global::FileInfo info = Shared::Global::getFileInfo(path);
return constrainAttachSize(info.size, bounds.size());
}
QSize MessageDelegate::constrainAttachSize(QSize src, QSize bounds) const
{
bounds.setHeight(500);
if (src.width() > bounds.width() || src.height() > bounds.height()) {
src.scale(bounds, Qt::KeepAspectRatio);
}
return src;
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// {

View file

@ -30,6 +30,7 @@
#include <QProgressBar>
#include "shared/icons.h"
#include "shared/global.h"
namespace Models {
struct FeedItem;
@ -57,9 +58,12 @@ signals:
protected:
void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const;
void paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const;
void paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const;
QPushButton* getButton(const Models::FeedItem& data) const;
QProgressBar* getBar(const Models::FeedItem& data) const;
void clearHelperWidget(const Models::FeedItem& data) const;
QSize calculateAttachSize(const QString& path, const QRect& bounds) const;
QSize constrainAttachSize(QSize src, QSize bounds) const;
protected slots:
void onButtonPushed() const;
@ -86,7 +90,6 @@ private:
std::set<QString>* idsToKeep;
bool clearingWidgets;
};
#endif // MESSAGEDELEGATE_H