From e40041978e7b42e17bc5264def255f71090160b4 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Mon, 21 Mar 2022 14:06:39 +0200 Subject: [PATCH] QmlDesigner: Fix font preview tooltip image The preview tooltip was recently reduced in size, but font previews were still being rendered the old size and scaled down, degrading image quality. The new default size is bit too small to render the sample text of fonts nicely, so added an option to set unscaled image to the tooltip, allowing us to use twice as wide preview images for fonts. Fixes: QDS-6486 Change-Id: Ieaabfbea11e47509de7cd6aed93464d8595ea541 Reviewed-by: Samuel Ghinet Reviewed-by: Thomas Hartmann Reviewed-by: Mahmoud Badri Reviewed-by: Qt CI Bot --- .../assetslibrary/assetslibrarymodel.cpp | 5 +-- .../assetslibrary/assetslibrarymodel.h | 5 +-- .../assetslibrary/assetslibrarywidget.cpp | 6 ++- .../previewtooltip/previewimagetooltip.cpp | 14 +++++-- .../previewtooltip/previewimagetooltip.h | 2 +- .../previewtooltip/previewtooltipbackend.cpp | 39 +++++++++++++------ .../previewtooltip/previewtooltipbackend.h | 5 +++ .../imagecache/imagecachefontcollector.cpp | 2 +- 8 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp index 9fe3524537d..6dd1504f95d 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp @@ -253,11 +253,8 @@ const QStringList &AssetsLibraryModel::supportedTexture3DSuffixes() return retList; } -AssetsLibraryModel::AssetsLibraryModel(SynchronousImageCache &fontImageCache, - Utils::FileSystemWatcher *fileSystemWatcher, - QObject *parent) +AssetsLibraryModel::AssetsLibraryModel(Utils::FileSystemWatcher *fileSystemWatcher, QObject *parent) : QAbstractListModel(parent) - , m_fontImageCache(fontImageCache) , m_fileSystemWatcher(fileSystemWatcher) { // add role names diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h index 2b87359e07d..e964766f952 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h @@ -47,9 +47,7 @@ class AssetsLibraryModel : public QAbstractListModel Q_PROPERTY(bool isEmpty READ isEmpty WRITE setIsEmpty NOTIFY isEmptyChanged) public: - AssetsLibraryModel(QmlDesigner::SynchronousImageCache &fontImageCache, - Utils::FileSystemWatcher *fileSystemWatcher, - QObject *parent = nullptr); + AssetsLibraryModel(Utils::FileSystemWatcher *fileSystemWatcher, QObject *parent = nullptr); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; @@ -97,7 +95,6 @@ private: void setIsEmpty(bool empty); - SynchronousImageCache &m_fontImageCache; QHash> m_iconCache; QString m_searchText; diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp index 34be8ab46d9..1b6c4f64fb9 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp @@ -117,7 +117,7 @@ AssetsLibraryWidget::AssetsLibraryWidget(AsynchronousImageCache &imageCache, , m_fontImageCache(synchronousFontImageCache) , m_assetsIconProvider(new AssetsLibraryIconProvider(synchronousFontImageCache)) , m_fileSystemWatcher(new Utils::FileSystemWatcher(this)) - , m_assetsModel(new AssetsLibraryModel(synchronousFontImageCache, m_fileSystemWatcher, this)) + , m_assetsModel(new AssetsLibraryModel(m_fileSystemWatcher, this)) , m_assetsWidget(new QQuickWidget(this)) , m_imageCache{imageCache} { @@ -130,11 +130,13 @@ AssetsLibraryWidget::AssetsLibraryWidget(AsynchronousImageCache &imageCache, m_assetsWidget->installEventFilter(this); m_fontPreviewTooltipBackend = std::make_unique(asynchronousFontImageCache); + // We want font images to have custom size, so don't scale them in the tooltip + m_fontPreviewTooltipBackend->setScaleImage(false); // Note: Though the text specified here appears in UI, it shouldn't be translated, as it's // a commonly used sentence to preview the font glyphs in latin fonts. // For fonts that do not have latin glyphs, the font family name will have to suffice for preview. m_fontPreviewTooltipBackend->setAuxiliaryData( - ImageCache::FontCollectorSizeAuxiliaryData{QSize{300, 300}, + ImageCache::FontCollectorSizeAuxiliaryData{QSize{300, 150}, Theme::getColor(Theme::DStextColor).name(), QStringLiteral("The quick brown fox jumps\n" "over the lazy dog\n" diff --git a/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.cpp b/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.cpp index 048e33f9aa5..3b70d79fd11 100644 --- a/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.cpp +++ b/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.cpp @@ -62,10 +62,16 @@ void PreviewImageTooltip::setInfo(const QString &info) m_ui->infoLabel->setText(info); } -void PreviewImageTooltip::setImage(const QImage &image) +void PreviewImageTooltip::setImage(const QImage &image, bool scale) { - m_ui->imageLabel->setPixmap(QPixmap::fromImage({image}).scaled(m_ui->imageLabel->width(), - m_ui->imageLabel->height(), - Qt::KeepAspectRatio)); + QPixmap pm = QPixmap::fromImage({image}); + if (scale) { + m_ui->imageLabel->setPixmap(pm.scaled(m_ui->imageLabel->width(), + m_ui->imageLabel->height(), + Qt::KeepAspectRatio)); + } else { + m_ui->imageLabel->setPixmap(pm); + } } + } diff --git a/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.h b/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.h index 09ca27fa2e3..2bebb316a0c 100644 --- a/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.h +++ b/src/plugins/qmldesigner/components/previewtooltip/previewimagetooltip.h @@ -46,7 +46,7 @@ public: void setName(const QString &name); void setPath(const QString &path); void setInfo(const QString &info); - void setImage(const QImage &pixmap); + void setImage(const QImage &image, bool scale = true); private: std::unique_ptr m_ui; diff --git a/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.cpp b/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.cpp index 475e4ac17d0..17a3b227bdf 100644 --- a/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.cpp +++ b/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.cpp @@ -55,10 +55,10 @@ void PreviewTooltipBackend::showTooltip() m_cache.requestImage( m_path, - [tooltip = QPointer(m_tooltip.get())](const QImage &image) { - QMetaObject::invokeMethod(tooltip, [tooltip, image] { + [tooltip = QPointer(m_tooltip.get()), scaleImage = m_scaleImage](const QImage &image) { + QMetaObject::invokeMethod(tooltip, [tooltip, image, scaleImage] { if (tooltip) { - tooltip->setImage(image); + tooltip->setImage(image, scaleImage); tooltip->show(); } }); @@ -126,9 +126,10 @@ QString PreviewTooltipBackend::name() const void PreviewTooltipBackend::setName(const QString &name) { - m_name = name; - if (m_name != name) + if (m_name != name) { + m_name = name; emit nameChanged(); + } } QString PreviewTooltipBackend::path() const @@ -138,9 +139,10 @@ QString PreviewTooltipBackend::path() const void PreviewTooltipBackend::setPath(const QString &path) { - m_path = path; - if (m_path != path) + if (m_path != path) { + m_path = path; emit pathChanged(); + } } QString PreviewTooltipBackend::info() const @@ -150,9 +152,10 @@ QString PreviewTooltipBackend::info() const void PreviewTooltipBackend::setInfo(const QString &info) { - m_info = info; - if (m_info != info) + if (m_info != info) { + m_info = info; emit infoChanged(); + } } QString PreviewTooltipBackend::extraId() const @@ -163,9 +166,23 @@ QString PreviewTooltipBackend::extraId() const // Sets the imageCache extraId hint. Valid content depends on image cache collector used. void PreviewTooltipBackend::setExtraId(const QString &extraId) { - m_extraId = extraId; - if (m_extraId != extraId) + if (m_extraId != extraId) { + m_extraId = extraId; emit extraIdChanged(); + } +} + +bool PreviewTooltipBackend::scaleImage() const +{ + return m_scaleImage; +} + +void PreviewTooltipBackend::setScaleImage(bool scale) +{ + if (m_scaleImage != scale) { + m_scaleImage = scale; + emit scaleImageChanged(); + } } } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.h b/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.h index 168ae72495e..bc7255a9797 100644 --- a/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.h +++ b/src/plugins/qmldesigner/components/previewtooltip/previewtooltipbackend.h @@ -45,6 +45,7 @@ class PreviewTooltipBackend : public QObject Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged) Q_PROPERTY(QString info READ info WRITE setInfo NOTIFY infoChanged) Q_PROPERTY(QString extraId READ extraId WRITE setExtraId NOTIFY extraIdChanged) + Q_PROPERTY(bool scaleImage READ scaleImage WRITE setScaleImage NOTIFY scaleImageChanged) public: PreviewTooltipBackend(AsynchronousImageCache &cache); @@ -62,6 +63,8 @@ public: void setInfo(const QString &info); QString extraId() const; void setExtraId(const QString &extraId); + bool scaleImage() const; + void setScaleImage(bool scale); bool isVisible() const; @@ -75,12 +78,14 @@ signals: void pathChanged(); void infoChanged(); void extraIdChanged(); + void scaleImageChanged(); private: QString m_name; QString m_path; QString m_info; QString m_extraId; + bool m_scaleImage = true; std::unique_ptr m_tooltip; ImageCache::AuxiliaryData m_auxiliaryData; AsynchronousImageCache &m_cache; diff --git a/src/plugins/qmldesigner/designercore/imagecache/imagecachefontcollector.cpp b/src/plugins/qmldesigner/designercore/imagecache/imagecachefontcollector.cpp index d74b28b0e38..3069600fd01 100644 --- a/src/plugins/qmldesigner/designercore/imagecache/imagecachefontcollector.cpp +++ b/src/plugins/qmldesigner/designercore/imagecache/imagecachefontcollector.cpp @@ -131,7 +131,7 @@ void ImageCacheFontCollector::start(Utils::SmallStringView name, auto &&auxiliaryData = Utils::get(auxiliaryDataValue); QColor textColor = auxiliaryData.colorName; QSize size = auxiliaryData.size; - QString text = font.family() + "\n\n" + auxiliaryData.text; + QString text = font.family() + "\n" + auxiliaryData.text; QImage image = createFontImage(text, textColor, font, size);