mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
Preview: fix image scaling for downloaded preview images
This commit is contained in:
parent
a12f01e617
commit
3b2154f45e
@ -39,28 +39,53 @@ void VPreviewManager::updateImageLinks(const QVector<VElementRegion> &p_imageReg
|
|||||||
previewImages(ts, p_imageRegions);
|
previewImages(ts, p_imageRegions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QPixmap scalePreviewImage(const QPixmap &p_img, int p_width, int p_height)
|
||||||
|
{
|
||||||
|
const Qt::TransformationMode tMode = Qt::SmoothTransformation;
|
||||||
|
qreal sf = VUtils::calculateScaleFactor();
|
||||||
|
if (p_width > 0) {
|
||||||
|
if (p_height > 0) {
|
||||||
|
return p_img.scaled(p_width * sf,
|
||||||
|
p_height * sf,
|
||||||
|
Qt::IgnoreAspectRatio,
|
||||||
|
tMode);
|
||||||
|
} else {
|
||||||
|
return p_img.scaledToWidth(p_width * sf, tMode);
|
||||||
|
}
|
||||||
|
} else if (p_height > 0) {
|
||||||
|
return p_img.scaledToHeight(p_height * sf, tMode);
|
||||||
|
} else {
|
||||||
|
if (sf < 1.1) {
|
||||||
|
return p_img;
|
||||||
|
} else {
|
||||||
|
return p_img.scaledToWidth(p_img.width() * sf, tMode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void VPreviewManager::imageDownloaded(const QByteArray &p_data, const QString &p_url)
|
void VPreviewManager::imageDownloaded(const QByteArray &p_data, const QString &p_url)
|
||||||
{
|
{
|
||||||
if (!m_previewEnabled) {
|
if (!m_previewEnabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = m_urlToName.find(p_url);
|
auto it = m_urlMap.find(p_url);
|
||||||
if (it == m_urlToName.end()) {
|
if (it == m_urlMap.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString name = it.value();
|
QSharedPointer<UrlImageInfo> info = it.value();
|
||||||
m_urlToName.erase(it);
|
m_urlMap.erase(it);
|
||||||
|
|
||||||
if (m_editor->containsImage(name) || name.isEmpty()) {
|
if (m_editor->containsImage(info->m_name) || info->m_name.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap image;
|
QPixmap image;
|
||||||
image.loadFromData(p_data);
|
image.loadFromData(p_data);
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
m_editor->addImage(name, image);
|
m_editor->addImage(info->m_name,
|
||||||
|
scalePreviewImage(image, info->m_width, info->m_height));
|
||||||
emit requestUpdateImageLinks();
|
emit requestUpdateImageLinks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,38 +250,24 @@ QString VPreviewManager::imageResourceName(const ImageLinkInfo &p_link)
|
|||||||
if (QFileInfo::exists(imgPath)) {
|
if (QFileInfo::exists(imgPath)) {
|
||||||
// Local file.
|
// Local file.
|
||||||
image = VUtils::pixmapFromFile(imgPath);
|
image = VUtils::pixmapFromFile(imgPath);
|
||||||
|
if (image.isNull()) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// URL. Try to download it.
|
// URL. Try to download it.
|
||||||
|
// qrc:// files will touch this path.
|
||||||
m_downloader->download(imgPath);
|
m_downloader->download(imgPath);
|
||||||
m_urlToName.insert(imgPath, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (image.isNull()) {
|
QSharedPointer<UrlImageInfo> info(new UrlImageInfo(name,
|
||||||
|
p_link.m_width,
|
||||||
|
p_link.m_height));
|
||||||
|
m_urlMap.insert(imgPath, info);
|
||||||
|
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize the image.
|
m_editor->addImage(name,
|
||||||
Qt::TransformationMode tMode = Qt::SmoothTransformation;
|
scalePreviewImage(image, p_link.m_width, p_link.m_height));
|
||||||
qreal sf = VUtils::calculateScaleFactor();
|
|
||||||
if (p_link.m_width > 0) {
|
|
||||||
if (p_link.m_height > 0) {
|
|
||||||
m_editor->addImage(name, image.scaled(p_link.m_width * sf,
|
|
||||||
p_link.m_height * sf,
|
|
||||||
Qt::IgnoreAspectRatio,
|
|
||||||
tMode));
|
|
||||||
} else {
|
|
||||||
m_editor->addImage(name, image.scaledToWidth(p_link.m_width * sf, tMode));
|
|
||||||
}
|
|
||||||
} else if (p_link.m_height > 0) {
|
|
||||||
m_editor->addImage(name, image.scaledToHeight(p_link.m_height * sf, tMode));
|
|
||||||
} else {
|
|
||||||
if (sf < 1.1) {
|
|
||||||
m_editor->addImage(name, image);
|
|
||||||
} else {
|
|
||||||
m_editor->addImage(name, image.scaledToWidth(image.width() * sf, tMode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,6 +172,19 @@ private:
|
|||||||
int m_height;
|
int m_height;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UrlImageInfo {
|
||||||
|
UrlImageInfo(const QString &p_name, int p_width, int p_height)
|
||||||
|
: m_name(p_name),
|
||||||
|
m_width(p_width),
|
||||||
|
m_height(p_height)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QString m_name;
|
||||||
|
int m_width;
|
||||||
|
int m_height;
|
||||||
|
};
|
||||||
|
|
||||||
// Start to preview images according to image links.
|
// Start to preview images according to image links.
|
||||||
void previewImages(TS p_timeStamp, const QVector<VElementRegion> &p_imageRegions);
|
void previewImages(TS p_timeStamp, const QVector<VElementRegion> &p_imageRegions);
|
||||||
|
|
||||||
@ -228,7 +241,7 @@ private:
|
|||||||
|
|
||||||
// Map from URL to name in the resource manager.
|
// Map from URL to name in the resource manager.
|
||||||
// Used for downloading images.
|
// Used for downloading images.
|
||||||
QHash<QString, QString> m_urlToName;
|
QHash<QString, QSharedPointer<UrlImageInfo>> m_urlMap;
|
||||||
|
|
||||||
// Timestamp per each preview source.
|
// Timestamp per each preview source.
|
||||||
TS m_timeStamps[(int)PreviewSource::MaxNumberOfSources];
|
TS m_timeStamps[(int)PreviewSource::MaxNumberOfSources];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user