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

@ -81,7 +81,8 @@ Shared::Global::Global():
}), }),
pluginSupport({ pluginSupport({
{"KWallet", false} {"KWallet", false}
}) }),
fileCache()
{ {
if (instance != 0) { if (instance != 0) {
throw 551; throw 551;
@ -90,6 +91,34 @@ Shared::Global::Global():
instance = this; instance = this;
} }
Shared::Global::FileInfo Shared::Global::getFileInfo(const QString& path)
{
std::map<QString, FileInfo>::const_iterator itr = instance->fileCache.find(path);
if (itr == instance->fileCache.end()) {
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
QStringList parts = type.name().split("/");
QString big = parts.front();
QFileInfo info(path);
FileInfo::Preview p = FileInfo::Preview::none;
QSize size;
if (big == "image") {
if (parts.back() == "gif") {
//TODO need to consider GIF as a movie
}
p = FileInfo::Preview::picture;
QImage img(path);
size = img.size();
}
itr = instance->fileCache.insert(std::make_pair(path, FileInfo({info.fileName(), size, type, p}))).first;
}
return itr->second;
}
Shared::Global * Shared::Global::getInstance() Shared::Global * Shared::Global::getInstance()
{ {
return instance; return instance;

View File

@ -29,6 +29,11 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QMimeType>
#include <QMimeDatabase>
#include <QFileInfo>
#include <QImage>
#include <QSize>
namespace Shared { namespace Shared {
@ -36,6 +41,19 @@ namespace Shared {
Q_DECLARE_TR_FUNCTIONS(Global) Q_DECLARE_TR_FUNCTIONS(Global)
public: public:
struct FileInfo {
enum class Preview {
none,
picture,
movie
};
QString name;
QSize size;
QMimeType mime;
Preview preview;
};
Global(); Global();
static Global* getInstance(); static Global* getInstance();
@ -64,6 +82,8 @@ namespace Shared {
static const std::set<QString> supportedImagesExts; static const std::set<QString> supportedImagesExts;
static FileInfo getFileInfo(const QString& path);
template<typename T> template<typename T>
static T fromInt(int src); static T fromInt(int src);
@ -87,6 +107,7 @@ namespace Shared {
static Global* instance; static Global* instance;
std::map<QString, bool> pluginSupport; std::map<QString, bool> pluginSupport;
std::map<QString, FileInfo> fileCache;
}; };
} }

View File

@ -407,7 +407,7 @@ QModelIndex Models::MessageFeed::modelIndexByTime(const QString& id, const QDate
--tItr; --tItr;
} }
if (found) { if (found || id == (*tItr)->getId()) {
int position = indexByTime.rank(tItr); int position = indexByTime.rank(tItr);
return createIndex(position, 0, *tItr); return createIndex(position, 0, *tItr);
} }

View File

@ -113,6 +113,13 @@ void FeedView::rowsInserted(const QModelIndex& parent, int start, int end)
QAbstractItemView::rowsInserted(parent, start, 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() void FeedView::updateGeometries()
{ {
qDebug() << "updateGeometries"; qDebug() << "updateGeometries";

View File

@ -52,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 dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles) override;
void onMessageButtonPushed(const QString& messageId, bool download); void onMessageButtonPushed(const QString& messageId, bool download);
protected: protected:

View File

@ -115,7 +115,6 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->setFont(nickFont); painter->setFont(nickFont);
painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect); painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect);
opt.rect.adjust(0, rect.height(), 0, 0); opt.rect.adjust(0, rect.height(), 0, 0);
painter->save(); painter->save();
switch (data.attach.state) { switch (data.attach.state) {
@ -131,6 +130,11 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
paintButton(getButton(data), painter, data.sentByMe, opt); paintButton(getButton(data), painter, data.sentByMe, opt);
break; break;
case Models::ready: case Models::ready:
clearHelperWidget(data);
paintPreview(data, painter, opt);
break;
case Models::errorDownload:
case Models::errorUpload:
break; break;
} }
painter->restore(); painter->restore();
@ -178,6 +182,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
messageSize.rheight() += buttonHeight; messageSize.rheight() += buttonHeight;
break; break;
case Models::ready: case Models::ready:
messageSize.rheight() += calculateAttachSize(attach.localPath, messageRect).height();
break; break;
case Models::errorDownload: case Models::errorDownload:
case Models::errorUpload: case Models::errorUpload:
@ -245,15 +250,41 @@ void MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentBy
{ {
QPoint start = option.rect.topLeft(); QPoint start = option.rect.topLeft();
QWidget* vp = static_cast<QWidget*>(painter->device()); //QWidget* vp = static_cast<QWidget*>(painter->device());
bar->setParent(vp);
bar->move(start); // if (bar->parent() != vp) {
// bar->setParent(vp);
// }
// bar->move(start);
bar->resize(option.rect.width(), barHeight); 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); 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 QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const
{ {
std::map<QString, FeedButton*>::const_iterator itr = buttons->find(data.id); 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 // void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// { // {

View File

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