plugin/cim: better sourceSize

This commit is contained in:
2 * r + 2 * t 2025-09-01 15:42:27 +10:00
parent d76c799dd2
commit 7dcb545ba0
3 changed files with 11 additions and 27 deletions

View file

@ -8,13 +8,8 @@ Image {
property alias path: manager.path property alias path: manager.path
property int sourceWidth
property int sourceHeight
asynchronous: true asynchronous: true
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
sourceSize.width: sourceWidth
sourceSize.height: sourceHeight
Connections { Connections {
target: QsWindow.window target: QsWindow.window

View file

@ -18,24 +18,15 @@ qreal CachingImageManager::effectiveScale() const {
return 1.0; return 1.0;
} }
int CachingImageManager::effectiveWidth() const { QSize CachingImageManager::effectiveSize() const {
if (!m_item) { if (!m_item) {
return 0; return QSize();
} }
int width = static_cast<int>(std::ceil(m_item->width() * effectiveScale())); const qreal scale = effectiveScale();
m_item->setProperty("sourceWidth", width); const QSize size = QSizeF(m_item->width() * scale, m_item->height() * scale).toSize();
return width; m_item->setProperty("sourceSize", size);
} return size;
int CachingImageManager::effectiveHeight() const {
if (!m_item) {
return 0;
}
int height = static_cast<int>(std::ceil(m_item->height() * effectiveScale()));
m_item->setProperty("sourceHeight", height);
return height;
} }
QQuickItem* CachingImageManager::item() const { QQuickItem* CachingImageManager::item() const {
@ -119,16 +110,15 @@ void CachingImageManager::updateSource(const QString& path) {
return; return;
} }
const int width = self->effectiveWidth(); const QSize size = self->effectiveSize();
const int height = self->effectiveHeight();
if (!self->m_item || !width || !height) { if (!self->m_item || !size.width() || !size.height()) {
return; return;
} }
const QString fillMode = self->m_item->property("fillMode").toString(); const QString fillMode = self->m_item->property("fillMode").toString();
const QString filename = QString("%1@%2x%3-%4.png") const QString filename = QString("%1@%2x%3-%4.png")
.arg(sha).arg(width).arg(height) .arg(sha).arg(size.width()).arg(size.height())
.arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch"); .arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch");
const QUrl cache = self->m_cacheDir.resolved(QUrl(filename)); const QUrl cache = self->m_cacheDir.resolved(QUrl(filename));
@ -149,7 +139,7 @@ void CachingImageManager::updateSource(const QString& path) {
self->m_item->setProperty("source", cache); self->m_item->setProperty("source", cache);
} else { } else {
self->m_item->setProperty("source", QUrl::fromLocalFile(path)); self->m_item->setProperty("source", QUrl::fromLocalFile(path));
self->createCache(path, cache.toLocalFile(), fillMode, QSize(width, height)); self->createCache(path, cache.toLocalFile(), fillMode, size);
} }
// Clear current running sha if same // Clear current running sha if same

View file

@ -52,8 +52,7 @@ private:
QMetaObject::Connection m_heightConn; QMetaObject::Connection m_heightConn;
[[nodiscard]] qreal effectiveScale() const; [[nodiscard]] qreal effectiveScale() const;
[[nodiscard]] int effectiveWidth() const; [[nodiscard]] QSize effectiveSize() const;
[[nodiscard]] int effectiveHeight() const;
void createCache(const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const; void createCache(const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const;
[[nodiscard]] QString sha256sum(const QString& path) const; [[nodiscard]] QString sha256sum(const QString& path) const;