From 5e62b1c56721f443a0005017cb10e9eac09f0eb4 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Mon, 9 Sep 2024 16:19:56 -0400 Subject: [PATCH] fix: consider devicePixelRatio when scaling icons (#1751) --- src/dict/dictionary.cc | 23 +++++++++++------------ src/dict/dictionary.hh | 3 ++- src/ui/dictionarybar.cc | 7 +------ 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/dict/dictionary.cc b/src/dict/dictionary.cc index 112e71ea..1cd646f8 100644 --- a/src/dict/dictionary.cc +++ b/src/dict/dictionary.cc @@ -6,18 +6,15 @@ #include #include "dictionary.hh" -#include - // For needToRebuildIndex(), read below #include #include #include "config.hh" #include -#include #include -#include #include +#include #include #include #include "utils.hh" @@ -238,6 +235,11 @@ void Class::loadIcon() noexcept dictionaryIconLoaded = true; } +int Class::getOptimalIconSize() +{ + return 64 * qGuiApp->devicePixelRatio(); +} + bool Class::loadIconFromFile( QString const & _filename, bool isFullName ) { QFileInfo info; @@ -266,19 +268,14 @@ bool Class::loadIconFromFile( QString const & _filename, bool isFullName ) } if ( info.isFile() ) { - QImage img( fileName ); + auto iconSize = getOptimalIconSize(); + QPixmap img( fileName ); if ( !img.isNull() ) { // Load successful - - // Apply the color key -#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) - img.setAlphaChannel( img.createMaskFromColor( QColor( 192, 192, 192 ).rgb(), Qt::MaskOutColor ) ); -#endif - auto result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation ); - dictionaryIcon = QIcon( QPixmap::fromImage( result ) ); + dictionaryIcon = QIcon( result ); return !dictionaryIcon.isNull(); } @@ -293,6 +290,8 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text ) QImage img( iconUrl ); if ( !img.isNull() ) { + auto iconSize = getOptimalIconSize(); + QImage result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation ); QPainter painter( &result ); diff --git a/src/dict/dictionary.hh b/src/dict/dictionary.hh index e6d7b121..5b3b915a 100644 --- a/src/dict/dictionary.hh +++ b/src/dict/dictionary.hh @@ -12,6 +12,7 @@ #include #include #include +#include #include "config.hh" #include "ex.hh" @@ -320,7 +321,7 @@ protected: // By default set icon to empty virtual void loadIcon() noexcept; - const int iconSize = 64; + static int getOptimalIconSize(); // Load icon from filename directly if isFullName == true // else treat filename as name without extension diff --git a/src/ui/dictionarybar.cc b/src/ui/dictionarybar.cc index d3a3cc01..f10722a7 100644 --- a/src/ui/dictionarybar.cc +++ b/src/ui/dictionarybar.cc @@ -19,12 +19,7 @@ DictionaryBar::DictionaryBar( QWidget * parent, editDictionaryCommand( _editDictionaryCommand ), maxDictionaryRefsInContextMenu( maxDictionaryRefsInContextMenu_ ) { - - auto iconWidth = this->size().width(); - auto iconHeight = this->size().height(); - - normalIconSize = { std::max( iconWidth, iconHeight ), std::max( iconWidth, iconHeight ) }; - + normalIconSize = { this->iconSize().height(), this->iconSize().height() }; setObjectName( "dictionaryBar" );