big image preview optimisations, preview positioning fix, memory leaks fix

This commit is contained in:
Blue 2021-05-17 23:32:44 +03:00
parent 721f6daa36
commit ddfaa63a24
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 43 additions and 20 deletions

View File

@ -128,12 +128,12 @@ Shared::Global::FileInfo Shared::Global::getFileInfo(const QString& path)
QSize size; QSize size;
if (big == "image") { if (big == "image") {
QMovie mov(path); QMovie mov(path);
if (mov.isValid()) { if (mov.isValid() && mov.frameCount() > 1) {
p = FileInfo::Preview::animation; p = FileInfo::Preview::animation;
} else { } else {
p = FileInfo::Preview::picture; p = FileInfo::Preview::picture;
} }
QImage img(path); QImageReader img(path);
size = img.size(); size = img.size();
// } else if (big == "video") { // } else if (big == "video") {
// p = FileInfo::Preview::movie; // p = FileInfo::Preview::movie;

View File

@ -289,6 +289,8 @@ void MessageDelegate::initializeFonts(const QFont& font)
bodyMetrics = QFontMetrics(bodyFont); bodyMetrics = QFontMetrics(bodyFont);
nickMetrics = QFontMetrics(nickFont); nickMetrics = QFontMetrics(nickFont);
dateMetrics = QFontMetrics(dateFont); dateMetrics = QFontMetrics(dateFont);
Preview::initializeFont(bodyFont);
} }
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)

View File

@ -22,7 +22,6 @@
constexpr int margin = 6; constexpr int margin = 6;
constexpr int maxAttachmentHeight = 500; constexpr int maxAttachmentHeight = 500;
bool Preview::fontInitialized = false;
QFont Preview::font; QFont Preview::font;
QFontMetrics Preview::metrics(Preview::font); QFontMetrics Preview::metrics(Preview::font);
@ -41,12 +40,6 @@ Preview::Preview(const QString& pPath, const QSize& pMaxSize, const QPoint& pos,
actualPreview(false), actualPreview(false),
right(pRight) right(pRight)
{ {
if (!fontInitialized) {
font.setBold(true);
font.setPixelSize(14);
metrics = QFontMetrics(font);
fontInitialized = true;
}
initializeElements(); initializeElements();
if (fileReachable) { if (fileReachable) {
@ -54,6 +47,13 @@ Preview::Preview(const QString& pPath, const QSize& pMaxSize, const QPoint& pos,
} }
} }
void Preview::initializeFont(const QFont& newFont)
{
font = newFont;
font.setBold(true);
metrics = QFontMetrics(font);
}
Preview::~Preview() Preview::~Preview()
{ {
clean(); clean();
@ -104,6 +104,9 @@ void Preview::actualize(const QString& newPath, const QSize& newSize, const QPoi
} }
} else if (maxSizeChanged) { } else if (maxSizeChanged) {
applyNewMaxSize(); applyNewMaxSize();
if (right) {
positionChanged = true;
}
} }
if (positionChanged || !actualPreview) { if (positionChanged || !actualPreview) {
positionElements(); positionElements();
@ -132,6 +135,9 @@ void Preview::setSize(const QSize& newSize)
} }
if (maxSizeChanged || !actualPreview) { if (maxSizeChanged || !actualPreview) {
applyNewMaxSize(); applyNewMaxSize();
if (right) {
positionElements();
}
} }
} }
} }
@ -140,13 +146,14 @@ void Preview::applyNewSize()
{ {
switch (info.preview) { switch (info.preview) {
case Shared::Global::FileInfo::Preview::picture: { case Shared::Global::FileInfo::Preview::picture: {
QPixmap img(path); QImageReader img(path);
if (img.isNull()) { if (!img.canRead()) {
delete widget;
fileReachable = false; fileReachable = false;
} else { } else {
img = img.scaled(actualSize, Qt::KeepAspectRatio); img.setScaledSize(actualSize);
widget->resize(actualSize); widget->resize(actualSize);
widget->setPixmap(img); widget->setPixmap(QPixmap::fromImage(img.read()));
} }
} }
break; break;
@ -172,7 +179,7 @@ void Preview::applyNewMaxSize()
default: { default: {
int labelWidth = maxSize.width() - actualSize.width() - margin; int labelWidth = maxSize.width() - actualSize.width() - margin;
QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth); QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth);
cachedLabelSize = metrics.size(0, elidedName); cachedLabelSize = metrics.boundingRect(elidedName).size();
label->setText(elidedName); label->setText(elidedName);
label->resize(cachedLabelSize); label->resize(cachedLabelSize);
} }
@ -225,20 +232,23 @@ void Preview::initializeElements()
{ {
switch (info.preview) { switch (info.preview) {
case Shared::Global::FileInfo::Preview::picture: { case Shared::Global::FileInfo::Preview::picture: {
QPixmap img(path); QImageReader img(path);
if (img.isNull()) { if (!img.canRead()) {
fileReachable = false; fileReachable = false;
} else { } else {
actualPreview = true; actualPreview = true;
img = img.scaled(actualSize, Qt::KeepAspectRatio); img.setScaledSize(actualSize);
widget = new QLabel(parent); widget = new QLabel(parent);
widget->setPixmap(img); widget->setPixmap(QPixmap::fromImage(img.read()));
widget->show(); widget->show();
} }
} }
break; break;
case Shared::Global::FileInfo::Preview::animation:{ case Shared::Global::FileInfo::Preview::animation:{
movie = new QMovie(path); movie = new QMovie(path);
QObject::connect(movie, &QMovie::error,
std::bind(&Preview::handleQMovieError, this, std::placeholders::_1)
);
if (!movie->isValid()) { if (!movie->isValid()) {
fileReachable = false; fileReachable = false;
delete movie; delete movie;
@ -262,7 +272,7 @@ void Preview::initializeElements()
label->setFont(font); label->setFont(font);
int labelWidth = maxSize.width() - actualSize.width() - margin; int labelWidth = maxSize.width() - actualSize.width() - margin;
QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth); QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth);
cachedLabelSize = metrics.size(0, elidedName); cachedLabelSize = metrics.boundingRect(elidedName).size();
label->setText(elidedName); label->setText(elidedName);
label->show(); label->show();
} }
@ -302,3 +312,12 @@ QSize Preview::constrainAttachSize(QSize src, QSize bounds)
return src; return src;
} }
void Preview::handleQMovieError(QImageReader::ImageReaderError error)
{
if (error == QImageReader::FileNotFoundError) {
fileReachable = false;
movie->deleteLater();
widget->deleteLater();
}
}

View File

@ -29,6 +29,7 @@
#include <QMovie> #include <QMovie>
#include <QFont> #include <QFont>
#include <QFontMetrics> #include <QFontMetrics>
#include <QImageReader>
#include <shared/global.h> #include <shared/global.h>
@ -47,9 +48,9 @@ public:
bool isFileReachable() const; bool isFileReachable() const;
QSize size() const; QSize size() const;
static void initializeFont(const QFont& newFont);
static QSize constrainAttachSize(QSize src, QSize bounds); static QSize constrainAttachSize(QSize src, QSize bounds);
static QSize calculateAttachSize(const QString& path, const QRect& bounds); static QSize calculateAttachSize(const QString& path, const QRect& bounds);
static bool fontInitialized;
static QFont font; static QFont font;
static QFontMetrics metrics; static QFontMetrics metrics;
@ -59,6 +60,7 @@ private:
void clean(); void clean();
void applyNewSize(); void applyNewSize();
void applyNewMaxSize(); void applyNewMaxSize();
void handleQMovieError(QImageReader::ImageReaderError error);
private: private:
Shared::Global::FileInfo info; Shared::Global::FileInfo info;