fix: consider devicePixelRatio when scaling icons (#1751)

This commit is contained in:
shenleban tongying 2024-09-09 16:19:56 -04:00 committed by GitHub
parent aaaeb585b6
commit 5e62b1c567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 19 deletions

View file

@ -6,18 +6,15 @@
#include <cstdio> #include <cstdio>
#include "dictionary.hh" #include "dictionary.hh"
#include <QCryptographicHash>
// For needToRebuildIndex(), read below // For needToRebuildIndex(), read below
#include <QFileInfo> #include <QFileInfo>
#include <QDateTime> #include <QDateTime>
#include "config.hh" #include "config.hh"
#include <QDir> #include <QDir>
#include <QFileInfo>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QDateTime>
#include <QImage> #include <QImage>
#include <QPixmap>
#include <QPainter> #include <QPainter>
#include <QRegularExpression> #include <QRegularExpression>
#include "utils.hh" #include "utils.hh"
@ -238,6 +235,11 @@ void Class::loadIcon() noexcept
dictionaryIconLoaded = true; dictionaryIconLoaded = true;
} }
int Class::getOptimalIconSize()
{
return 64 * qGuiApp->devicePixelRatio();
}
bool Class::loadIconFromFile( QString const & _filename, bool isFullName ) bool Class::loadIconFromFile( QString const & _filename, bool isFullName )
{ {
QFileInfo info; QFileInfo info;
@ -266,19 +268,14 @@ bool Class::loadIconFromFile( QString const & _filename, bool isFullName )
} }
if ( info.isFile() ) { if ( info.isFile() ) {
QImage img( fileName ); auto iconSize = getOptimalIconSize();
QPixmap img( fileName );
if ( !img.isNull() ) { if ( !img.isNull() ) {
// Load successful // 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 ); auto result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );
dictionaryIcon = QIcon( QPixmap::fromImage( result ) ); dictionaryIcon = QIcon( result );
return !dictionaryIcon.isNull(); return !dictionaryIcon.isNull();
} }
@ -293,6 +290,8 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text )
QImage img( iconUrl ); QImage img( iconUrl );
if ( !img.isNull() ) { if ( !img.isNull() ) {
auto iconSize = getOptimalIconSize();
QImage result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation ); QImage result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );
QPainter painter( &result ); QPainter painter( &result );

View file

@ -12,6 +12,7 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QWaitCondition> #include <QWaitCondition>
#include <QGuiApplication>
#include "config.hh" #include "config.hh"
#include "ex.hh" #include "ex.hh"
@ -320,7 +321,7 @@ protected:
// By default set icon to empty // By default set icon to empty
virtual void loadIcon() noexcept; virtual void loadIcon() noexcept;
const int iconSize = 64; static int getOptimalIconSize();
// Load icon from filename directly if isFullName == true // Load icon from filename directly if isFullName == true
// else treat filename as name without extension // else treat filename as name without extension

View file

@ -19,12 +19,7 @@ DictionaryBar::DictionaryBar( QWidget * parent,
editDictionaryCommand( _editDictionaryCommand ), editDictionaryCommand( _editDictionaryCommand ),
maxDictionaryRefsInContextMenu( maxDictionaryRefsInContextMenu_ ) maxDictionaryRefsInContextMenu( maxDictionaryRefsInContextMenu_ )
{ {
normalIconSize = { this->iconSize().height(), this->iconSize().height() };
auto iconWidth = this->size().width();
auto iconHeight = this->size().height();
normalIconSize = { std::max( iconWidth, iconHeight ), std::max( iconWidth, iconHeight ) };
setObjectName( "dictionaryBar" ); setObjectName( "dictionaryBar" );