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 <samuel.ghinet@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Miikka Heikkinen
2022-03-21 14:06:39 +02:00
parent bd607909a5
commit e40041978e
8 changed files with 51 additions and 27 deletions

View File

@@ -253,11 +253,8 @@ const QStringList &AssetsLibraryModel::supportedTexture3DSuffixes()
return retList; return retList;
} }
AssetsLibraryModel::AssetsLibraryModel(SynchronousImageCache &fontImageCache, AssetsLibraryModel::AssetsLibraryModel(Utils::FileSystemWatcher *fileSystemWatcher, QObject *parent)
Utils::FileSystemWatcher *fileSystemWatcher,
QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, m_fontImageCache(fontImageCache)
, m_fileSystemWatcher(fileSystemWatcher) , m_fileSystemWatcher(fileSystemWatcher)
{ {
// add role names // add role names

View File

@@ -47,9 +47,7 @@ class AssetsLibraryModel : public QAbstractListModel
Q_PROPERTY(bool isEmpty READ isEmpty WRITE setIsEmpty NOTIFY isEmptyChanged) Q_PROPERTY(bool isEmpty READ isEmpty WRITE setIsEmpty NOTIFY isEmptyChanged)
public: public:
AssetsLibraryModel(QmlDesigner::SynchronousImageCache &fontImageCache, AssetsLibraryModel(Utils::FileSystemWatcher *fileSystemWatcher, QObject *parent = nullptr);
Utils::FileSystemWatcher *fileSystemWatcher,
QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
@@ -97,7 +95,6 @@ private:
void setIsEmpty(bool empty); void setIsEmpty(bool empty);
SynchronousImageCache &m_fontImageCache;
QHash<QString, QPair<QDateTime, QIcon>> m_iconCache; QHash<QString, QPair<QDateTime, QIcon>> m_iconCache;
QString m_searchText; QString m_searchText;

View File

@@ -117,7 +117,7 @@ AssetsLibraryWidget::AssetsLibraryWidget(AsynchronousImageCache &imageCache,
, m_fontImageCache(synchronousFontImageCache) , m_fontImageCache(synchronousFontImageCache)
, m_assetsIconProvider(new AssetsLibraryIconProvider(synchronousFontImageCache)) , m_assetsIconProvider(new AssetsLibraryIconProvider(synchronousFontImageCache))
, m_fileSystemWatcher(new Utils::FileSystemWatcher(this)) , 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_assetsWidget(new QQuickWidget(this))
, m_imageCache{imageCache} , m_imageCache{imageCache}
{ {
@@ -130,11 +130,13 @@ AssetsLibraryWidget::AssetsLibraryWidget(AsynchronousImageCache &imageCache,
m_assetsWidget->installEventFilter(this); m_assetsWidget->installEventFilter(this);
m_fontPreviewTooltipBackend = std::make_unique<PreviewTooltipBackend>(asynchronousFontImageCache); m_fontPreviewTooltipBackend = std::make_unique<PreviewTooltipBackend>(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 // 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. // 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. // For fonts that do not have latin glyphs, the font family name will have to suffice for preview.
m_fontPreviewTooltipBackend->setAuxiliaryData( m_fontPreviewTooltipBackend->setAuxiliaryData(
ImageCache::FontCollectorSizeAuxiliaryData{QSize{300, 300}, ImageCache::FontCollectorSizeAuxiliaryData{QSize{300, 150},
Theme::getColor(Theme::DStextColor).name(), Theme::getColor(Theme::DStextColor).name(),
QStringLiteral("The quick brown fox jumps\n" QStringLiteral("The quick brown fox jumps\n"
"over the lazy dog\n" "over the lazy dog\n"

View File

@@ -62,10 +62,16 @@ void PreviewImageTooltip::setInfo(const QString &info)
m_ui->infoLabel->setText(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(), QPixmap pm = QPixmap::fromImage({image});
if (scale) {
m_ui->imageLabel->setPixmap(pm.scaled(m_ui->imageLabel->width(),
m_ui->imageLabel->height(), m_ui->imageLabel->height(),
Qt::KeepAspectRatio)); Qt::KeepAspectRatio));
} else {
m_ui->imageLabel->setPixmap(pm);
}
} }
} }

View File

@@ -46,7 +46,7 @@ public:
void setName(const QString &name); void setName(const QString &name);
void setPath(const QString &path); void setPath(const QString &path);
void setInfo(const QString &info); void setInfo(const QString &info);
void setImage(const QImage &pixmap); void setImage(const QImage &image, bool scale = true);
private: private:
std::unique_ptr<Ui::PreviewImageTooltip> m_ui; std::unique_ptr<Ui::PreviewImageTooltip> m_ui;

View File

@@ -55,10 +55,10 @@ void PreviewTooltipBackend::showTooltip()
m_cache.requestImage( m_cache.requestImage(
m_path, m_path,
[tooltip = QPointer<PreviewImageTooltip>(m_tooltip.get())](const QImage &image) { [tooltip = QPointer<PreviewImageTooltip>(m_tooltip.get()), scaleImage = m_scaleImage](const QImage &image) {
QMetaObject::invokeMethod(tooltip, [tooltip, image] { QMetaObject::invokeMethod(tooltip, [tooltip, image, scaleImage] {
if (tooltip) { if (tooltip) {
tooltip->setImage(image); tooltip->setImage(image, scaleImage);
tooltip->show(); tooltip->show();
} }
}); });
@@ -126,9 +126,10 @@ QString PreviewTooltipBackend::name() const
void PreviewTooltipBackend::setName(const QString &name) void PreviewTooltipBackend::setName(const QString &name)
{ {
if (m_name != name) {
m_name = name; m_name = name;
if (m_name != name)
emit nameChanged(); emit nameChanged();
}
} }
QString PreviewTooltipBackend::path() const QString PreviewTooltipBackend::path() const
@@ -138,9 +139,10 @@ QString PreviewTooltipBackend::path() const
void PreviewTooltipBackend::setPath(const QString &path) void PreviewTooltipBackend::setPath(const QString &path)
{ {
if (m_path != path) {
m_path = path; m_path = path;
if (m_path != path)
emit pathChanged(); emit pathChanged();
}
} }
QString PreviewTooltipBackend::info() const QString PreviewTooltipBackend::info() const
@@ -150,9 +152,10 @@ QString PreviewTooltipBackend::info() const
void PreviewTooltipBackend::setInfo(const QString &info) void PreviewTooltipBackend::setInfo(const QString &info)
{ {
if (m_info != info) {
m_info = info; m_info = info;
if (m_info != info)
emit infoChanged(); emit infoChanged();
}
} }
QString PreviewTooltipBackend::extraId() const 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. // Sets the imageCache extraId hint. Valid content depends on image cache collector used.
void PreviewTooltipBackend::setExtraId(const QString &extraId) void PreviewTooltipBackend::setExtraId(const QString &extraId)
{ {
if (m_extraId != extraId) {
m_extraId = extraId; m_extraId = extraId;
if (m_extraId != extraId)
emit extraIdChanged(); 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 } // namespace QmlDesigner

View File

@@ -45,6 +45,7 @@ class PreviewTooltipBackend : public QObject
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged) Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
Q_PROPERTY(QString info READ info WRITE setInfo NOTIFY infoChanged) Q_PROPERTY(QString info READ info WRITE setInfo NOTIFY infoChanged)
Q_PROPERTY(QString extraId READ extraId WRITE setExtraId NOTIFY extraIdChanged) Q_PROPERTY(QString extraId READ extraId WRITE setExtraId NOTIFY extraIdChanged)
Q_PROPERTY(bool scaleImage READ scaleImage WRITE setScaleImage NOTIFY scaleImageChanged)
public: public:
PreviewTooltipBackend(AsynchronousImageCache &cache); PreviewTooltipBackend(AsynchronousImageCache &cache);
@@ -62,6 +63,8 @@ public:
void setInfo(const QString &info); void setInfo(const QString &info);
QString extraId() const; QString extraId() const;
void setExtraId(const QString &extraId); void setExtraId(const QString &extraId);
bool scaleImage() const;
void setScaleImage(bool scale);
bool isVisible() const; bool isVisible() const;
@@ -75,12 +78,14 @@ signals:
void pathChanged(); void pathChanged();
void infoChanged(); void infoChanged();
void extraIdChanged(); void extraIdChanged();
void scaleImageChanged();
private: private:
QString m_name; QString m_name;
QString m_path; QString m_path;
QString m_info; QString m_info;
QString m_extraId; QString m_extraId;
bool m_scaleImage = true;
std::unique_ptr<PreviewImageTooltip> m_tooltip; std::unique_ptr<PreviewImageTooltip> m_tooltip;
ImageCache::AuxiliaryData m_auxiliaryData; ImageCache::AuxiliaryData m_auxiliaryData;
AsynchronousImageCache &m_cache; AsynchronousImageCache &m_cache;

View File

@@ -131,7 +131,7 @@ void ImageCacheFontCollector::start(Utils::SmallStringView name,
auto &&auxiliaryData = Utils::get<ImageCache::FontCollectorSizeAuxiliaryData>(auxiliaryDataValue); auto &&auxiliaryData = Utils::get<ImageCache::FontCollectorSizeAuxiliaryData>(auxiliaryDataValue);
QColor textColor = auxiliaryData.colorName; QColor textColor = auxiliaryData.colorName;
QSize size = auxiliaryData.size; 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); QImage image = createFontImage(text, textColor, font, size);