feature: High-quality dictionary icons in toolbar (#1750)

* better procedure of image scaling and size choosing
* allow user to set toolbar icon size via qt style sheet

---------

Co-authored-by: shenleban tongying <shenlebantongying@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Konstantin 2024-09-09 14:48:33 -04:00 committed by GitHub
parent 34e9ad5654
commit aaaeb585b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 42 additions and 39 deletions

View file

@ -271,27 +271,13 @@ bool Class::loadIconFromFile( QString const & _filename, bool isFullName )
if ( !img.isNull() ) {
// Load successful
//some icon is very large ,will crash the application.
img = img.scaledToWidth( 64 );
// 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
// Transform it to be square
int max = img.width() > img.height() ? img.width() : img.height();
QImage result( max, max, QImage::Format_ARGB32 );
result.fill( 0 ); // Black transparent
QPainter painter( &result );
painter.setRenderHint( QPainter::RenderHint::Antialiasing );
painter.drawImage( QPoint( img.width() == max ? 0 : ( max - img.width() ) / 2,
img.height() == max ? 0 : ( max - img.height() ) / 2 ),
img );
painter.end();
auto result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );
dictionaryIcon = QIcon( QPixmap::fromImage( result ) );
return !dictionaryIcon.isNull();
@ -307,18 +293,10 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text )
QImage img( iconUrl );
if ( !img.isNull() ) {
int iconSize = 64;
//some icon is very large ,will crash the application.
img = img.scaledToWidth( iconSize );
QImage result( iconSize, iconSize, QImage::Format_ARGB32 );
result.fill( 0 ); // Black transparent
int max = img.width() > img.height() ? img.width() : img.height();
QImage result = img.scaled( { iconSize, iconSize }, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );
QPainter painter( &result );
painter.setRenderHint( QPainter::RenderHint::Antialiasing );
painter.drawImage( QPoint( img.width() == max ? 0 : ( max - img.width() ) / 2,
img.height() == max ? 0 : ( max - img.height() ) / 2 ),
img );
painter.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing );
painter.setCompositionMode( QPainter::CompositionMode_SourceAtop );
QFont font = painter.font();

View file

@ -316,11 +316,12 @@ protected:
bool synonymSearchEnabled;
string dictionaryName;
std::optional< bool > metadata_enable_fts = std::nullopt;
// Load user icon if it exist
// By default set icon to empty
virtual void loadIcon() noexcept;
const int iconSize = 64;
// Load icon from filename directly if isFullName == true
// else treat filename as name without extension
bool loadIconFromFile( QString const & filename, bool isFullName = false );

View file

@ -4,6 +4,7 @@
#include <QMenu>
#include <QContextMenuEvent>
#include <QProcess>
#include <QStyle>
using std::vector;
@ -18,6 +19,13 @@ 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 ) };
setObjectName( "dictionaryBar" );
maxDictionaryRefsAction =
@ -72,14 +80,24 @@ void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > > const &
dictActions.append( action );
}
setDictionaryIconSize( 21 );
setUpdatesEnabled( true );
}
void DictionaryBar::setDictionaryIconSize( int extent )
void DictionaryBar::setDictionaryIconSize( IconSize size )
{
setIconSize( QSize( extent, extent ) );
switch ( size ) {
case IconSize::Small: {
auto smallSize = QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize );
setIconSize( { smallSize, smallSize } );
break;
}
case IconSize::Normal: {
setIconSize( normalIconSize );
break;
}
}
}
void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
@ -87,6 +105,7 @@ void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
showContextMenu( event );
}
void DictionaryBar::showContextMenu( QContextMenuEvent * event, bool extended )
{
QMenu menu( this );

View file

@ -34,7 +34,14 @@ public:
{
return mutedDictionaries;
}
void setDictionaryIconSize( int extent );
enum class IconSize {
Small,
Normal,
// TODO: implement something to have an Large option
};
void setDictionaryIconSize( IconSize size );
signals:
@ -69,6 +76,8 @@ private:
QList< QAction * > dictActions;
QAction * maxDictionaryRefsAction;
QSize normalIconSize; // cache icon size set by stylesheet provided by user
protected:
void contextMenuEvent( QContextMenuEvent * event );

View file

@ -1655,11 +1655,8 @@ void MainWindow::updateDictionaryBar()
dictionaryBar.setDictionaries( grp->dictionaries );
int extent = useSmallIconsInToolbarsAction.isChecked() ?
QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize ) :
QApplication::style()->pixelMetric( QStyle::PM_ToolBarIconSize );
dictionaryBar.setDictionaryIconSize( extent );
dictionaryBar.setDictionaryIconSize( useSmallIconsInToolbarsAction.isChecked() ? DictionaryBar::IconSize::Small :
DictionaryBar::IconSize::Normal );
}
}

View file

@ -1110,9 +1110,8 @@ void ScanPopup::on_goForwardButton_clicked() const
void ScanPopup::setDictionaryIconSize()
{
int extent = cfg.usingSmallIconsInToolbars ? QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize ) :
QApplication::style()->pixelMetric( QStyle::PM_ToolBarIconSize );
dictionaryBar.setDictionaryIconSize( extent );
dictionaryBar.setDictionaryIconSize( cfg.usingSmallIconsInToolbars ? DictionaryBar::IconSize::Small :
DictionaryBar::IconSize::Normal );
}
void ScanPopup::setGroupByName( QString const & name ) const