2021-05-15 22:07:49 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "preview.h"
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
#include <QFontDatabase>
|
2021-05-15 22:07:49 +00:00
|
|
|
|
|
|
|
constexpr int margin = 6;
|
|
|
|
constexpr int maxAttachmentHeight = 500;
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
QFont getFont () {
|
|
|
|
QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
|
|
|
|
font.setBold(true);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont Preview::font = getFont();
|
2021-05-15 22:07:49 +00:00
|
|
|
QFontMetrics Preview::metrics(Preview::font);
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
Preview::Preview(const QString& pPath, const QSize& pMaxSize, const QPoint& pos, QWidget* pParent):
|
2021-05-15 22:07:49 +00:00
|
|
|
info(Shared::Global::getFileInfo(pPath)),
|
|
|
|
maxSize(pMaxSize),
|
|
|
|
actualSize(constrainAttachSize(info.size, maxSize)),
|
|
|
|
cachedLabelSize(0, 0),
|
|
|
|
position(pos),
|
|
|
|
widget(0),
|
|
|
|
label(0),
|
|
|
|
parent(pParent),
|
|
|
|
movie(0),
|
|
|
|
fileReachable(true),
|
2022-01-09 14:32:23 +00:00
|
|
|
actualPreview(false)
|
2021-05-15 22:07:49 +00:00
|
|
|
{
|
|
|
|
initializeElements();
|
|
|
|
if (fileReachable) {
|
|
|
|
positionElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Preview::~Preview()
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::clean()
|
|
|
|
{
|
|
|
|
if (fileReachable) {
|
|
|
|
if (info.preview == Shared::Global::FileInfo::Preview::animation) {
|
|
|
|
delete movie;
|
|
|
|
}
|
|
|
|
delete widget;
|
|
|
|
if (!actualPreview) {
|
|
|
|
delete label;
|
|
|
|
} else {
|
|
|
|
actualPreview = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileReachable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::actualize(const QString& newPath, const QSize& newSize, const QPoint& newPoint)
|
|
|
|
{
|
|
|
|
bool positionChanged = false;
|
|
|
|
bool sizeChanged = false;
|
|
|
|
bool maxSizeChanged = false;
|
|
|
|
|
|
|
|
if (maxSize != newSize) {
|
|
|
|
maxSize = newSize;
|
|
|
|
maxSizeChanged = true;
|
|
|
|
QSize ns = constrainAttachSize(info.size, maxSize);
|
|
|
|
if (actualSize != ns) {
|
|
|
|
sizeChanged = true;
|
|
|
|
actualSize = ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (position != newPoint) {
|
|
|
|
position = newPoint;
|
|
|
|
positionChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setPath(newPath) && fileReachable) {
|
|
|
|
if (sizeChanged) {
|
|
|
|
applyNewSize();
|
|
|
|
if (maxSizeChanged && !actualPreview) {
|
|
|
|
applyNewMaxSize();
|
|
|
|
}
|
|
|
|
} else if (maxSizeChanged) {
|
|
|
|
applyNewMaxSize();
|
|
|
|
}
|
|
|
|
if (positionChanged || !actualPreview) {
|
|
|
|
positionElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::setSize(const QSize& newSize)
|
|
|
|
{
|
|
|
|
bool sizeChanged = false;
|
|
|
|
bool maxSizeChanged = false;
|
|
|
|
|
|
|
|
if (maxSize != newSize) {
|
|
|
|
maxSize = newSize;
|
|
|
|
maxSizeChanged = true;
|
|
|
|
QSize ns = constrainAttachSize(info.size, maxSize);
|
|
|
|
if (actualSize != ns) {
|
|
|
|
sizeChanged = true;
|
|
|
|
actualSize = ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileReachable) {
|
|
|
|
if (sizeChanged) {
|
|
|
|
applyNewSize();
|
|
|
|
}
|
|
|
|
if (maxSizeChanged || !actualPreview) {
|
|
|
|
applyNewMaxSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::applyNewSize()
|
|
|
|
{
|
|
|
|
switch (info.preview) {
|
|
|
|
case Shared::Global::FileInfo::Preview::picture: {
|
2022-01-09 14:32:23 +00:00
|
|
|
QImageReader img(info.path);
|
2021-05-17 20:32:44 +00:00
|
|
|
if (!img.canRead()) {
|
|
|
|
delete widget;
|
2021-05-15 22:07:49 +00:00
|
|
|
fileReachable = false;
|
|
|
|
} else {
|
2021-05-17 20:32:44 +00:00
|
|
|
img.setScaledSize(actualSize);
|
2021-05-15 22:07:49 +00:00
|
|
|
widget->resize(actualSize);
|
2021-05-17 20:32:44 +00:00
|
|
|
widget->setPixmap(QPixmap::fromImage(img.read()));
|
2021-05-15 22:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Shared::Global::FileInfo::Preview::animation:{
|
|
|
|
movie->setScaledSize(actualSize);
|
|
|
|
widget->resize(actualSize);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
QIcon icon = QIcon::fromTheme(info.mime.iconName());
|
2021-10-06 14:45:10 +00:00
|
|
|
if (icon.isNull()) {
|
|
|
|
icon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
|
|
|
}
|
2021-05-15 22:07:49 +00:00
|
|
|
widget->setPixmap(icon.pixmap(actualSize));
|
|
|
|
widget->resize(actualSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::applyNewMaxSize()
|
|
|
|
{
|
|
|
|
switch (info.preview) {
|
|
|
|
case Shared::Global::FileInfo::Preview::picture:
|
|
|
|
case Shared::Global::FileInfo::Preview::animation:
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
int labelWidth = maxSize.width() - actualSize.width() - margin;
|
|
|
|
QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth);
|
2021-05-17 20:32:44 +00:00
|
|
|
cachedLabelSize = metrics.boundingRect(elidedName).size();
|
2021-05-15 22:07:49 +00:00
|
|
|
label->setText(elidedName);
|
|
|
|
label->resize(cachedLabelSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize Preview::size() const
|
|
|
|
{
|
|
|
|
if (actualPreview) {
|
|
|
|
return actualSize;
|
|
|
|
} else {
|
|
|
|
return QSize(actualSize.width() + margin + cachedLabelSize.width(), actualSize.height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Preview::isFileReachable() const
|
|
|
|
{
|
|
|
|
return fileReachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::setPosition(const QPoint& newPoint)
|
|
|
|
{
|
|
|
|
if (position != newPoint) {
|
|
|
|
position = newPoint;
|
|
|
|
if (fileReachable) {
|
|
|
|
positionElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Preview::setPath(const QString& newPath)
|
|
|
|
{
|
2022-01-09 14:32:23 +00:00
|
|
|
if (info.path != newPath) {
|
|
|
|
info = Shared::Global::getFileInfo(newPath);
|
2021-05-15 22:07:49 +00:00
|
|
|
actualSize = constrainAttachSize(info.size, maxSize);
|
|
|
|
clean();
|
|
|
|
initializeElements();
|
|
|
|
if (fileReachable) {
|
|
|
|
positionElements();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::initializeElements()
|
|
|
|
{
|
|
|
|
switch (info.preview) {
|
|
|
|
case Shared::Global::FileInfo::Preview::picture: {
|
2022-01-09 14:32:23 +00:00
|
|
|
QImageReader img(info.path);
|
2021-05-17 20:32:44 +00:00
|
|
|
if (!img.canRead()) {
|
2021-05-15 22:07:49 +00:00
|
|
|
fileReachable = false;
|
|
|
|
} else {
|
|
|
|
actualPreview = true;
|
2021-05-17 20:32:44 +00:00
|
|
|
img.setScaledSize(actualSize);
|
2021-05-15 22:07:49 +00:00
|
|
|
widget = new QLabel(parent);
|
2021-05-17 20:32:44 +00:00
|
|
|
widget->setPixmap(QPixmap::fromImage(img.read()));
|
2021-05-15 22:07:49 +00:00
|
|
|
widget->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Shared::Global::FileInfo::Preview::animation:{
|
2022-01-09 14:32:23 +00:00
|
|
|
movie = new QMovie(info.path);
|
2021-05-17 20:32:44 +00:00
|
|
|
QObject::connect(movie, &QMovie::error,
|
|
|
|
std::bind(&Preview::handleQMovieError, this, std::placeholders::_1)
|
|
|
|
);
|
2021-05-15 22:07:49 +00:00
|
|
|
if (!movie->isValid()) {
|
|
|
|
fileReachable = false;
|
|
|
|
delete movie;
|
|
|
|
} else {
|
|
|
|
actualPreview = true;
|
|
|
|
movie->setScaledSize(actualSize);
|
|
|
|
widget = new QLabel(parent);
|
|
|
|
widget->setMovie(movie);
|
|
|
|
movie->start();
|
|
|
|
widget->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
QIcon icon = QIcon::fromTheme(info.mime.iconName());
|
2021-10-06 14:45:10 +00:00
|
|
|
if (icon.isNull()) {
|
|
|
|
icon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
widget = new QLabel(parent);
|
|
|
|
widget->setPixmap(icon.pixmap(actualSize));
|
|
|
|
widget->show();
|
|
|
|
|
|
|
|
label = new QLabel(parent);
|
|
|
|
label->setFont(font);
|
|
|
|
int labelWidth = maxSize.width() - actualSize.width() - margin;
|
|
|
|
QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, labelWidth);
|
2021-05-17 20:32:44 +00:00
|
|
|
cachedLabelSize = metrics.boundingRect(elidedName).size();
|
2021-05-15 22:07:49 +00:00
|
|
|
label->setText(elidedName);
|
|
|
|
label->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preview::positionElements()
|
|
|
|
{
|
|
|
|
int start = position.x();
|
|
|
|
widget->move(start, position.y());
|
|
|
|
if (!actualPreview) {
|
|
|
|
int x = start + actualSize.width() + margin;
|
|
|
|
int y = position.y() + (actualSize.height() - cachedLabelSize.height()) / 2;
|
|
|
|
label->move(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
bool Preview::canVisualize(const Shared::Global::FileInfo& info)
|
|
|
|
{
|
|
|
|
switch (info.preview) {
|
|
|
|
case Shared::Global::FileInfo::Preview::picture: {
|
|
|
|
QImageReader img(info.path);
|
|
|
|
return img.canRead();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Shared::Global::FileInfo::Preview::animation:{
|
|
|
|
QMovie movie(info.path);
|
|
|
|
return movie.isValid();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
QSize Preview::calculateAttachSize(const QString& path, const QRect& bounds)
|
|
|
|
{
|
|
|
|
Shared::Global::FileInfo info = Shared::Global::getFileInfo(path);
|
2022-01-09 14:32:23 +00:00
|
|
|
QSize constrained = constrainAttachSize(info.size, bounds.size());
|
|
|
|
if (!canVisualize(info)) {
|
|
|
|
int maxLabelWidth = bounds.width() - info.size.width() - margin;
|
|
|
|
QString elidedName = metrics.elidedText(info.name, Qt::ElideMiddle, maxLabelWidth);
|
|
|
|
int labelWidth = metrics.boundingRect(elidedName).size().width();
|
|
|
|
constrained.rwidth() += margin + labelWidth;
|
|
|
|
}
|
|
|
|
return constrained;
|
2021-05-15 22:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize Preview::constrainAttachSize(QSize src, QSize bounds)
|
|
|
|
{
|
|
|
|
if (bounds.height() > maxAttachmentHeight) {
|
|
|
|
bounds.setHeight(maxAttachmentHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src.width() > bounds.width() || src.height() > bounds.height()) {
|
|
|
|
src.scale(bounds, Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
2021-05-17 20:32:44 +00:00
|
|
|
|
|
|
|
void Preview::handleQMovieError(QImageReader::ImageReaderError error)
|
|
|
|
{
|
|
|
|
if (error == QImageReader::FileNotFoundError) {
|
|
|
|
fileReachable = false;
|
|
|
|
movie->deleteLater();
|
|
|
|
widget->deleteLater();
|
|
|
|
}
|
|
|
|
}
|