From feda879a3020ed8bb460b9989d2e9c9a8707cc23 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Sun, 18 Jun 2023 05:04:50 -0400 Subject: [PATCH] feat: load and display ZIM dictionary icon * Original GD never tries to load zim format's icon * This leverage the capability provided by libzim --- src/dict/zim.cc | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/dict/zim.cc b/src/dict/zim.cc index d41edfa5..afc0a50e 100644 --- a/src/dict/zim.cc +++ b/src/dict/zim.cc @@ -41,6 +41,8 @@ #include #include #include + #include + namespace Zim { using std::string; @@ -275,17 +277,31 @@ void ZimDictionary::loadIcon() noexcept if ( dictionaryIconLoaded ) return; + // Try to load Original GD's user provided icon QString fileName = QDir::fromNativeSeparators( getDictionaryFilenames()[ 0 ].c_str() ); - // Remove the extension fileName.chop( 3 ); - - if( !loadIconFromFile( fileName ) ) - { - // Load failed -- use default icons - dictionaryNativeIcon = dictionaryIcon = QIcon(":/icons/icon32_zim.png"); + if ( loadIconFromFile( fileName ) ) { + dictionaryIconLoaded = true; + return; } + // Try to load zim's illustration, which is usually 48x48 png + try { + auto illustration = df.getIllustrationItem( 48 ).getData(); + QImage img = QImage::fromData( reinterpret_cast< const uchar * >( illustration.data() ), illustration.size() ); + dictionaryNativeIcon = dictionaryIcon = QIcon( QPixmap::fromImage( img ) ); + + dictionaryIconLoaded = true; + return; + } + catch ( zim::EntryNotFound & e ) { + gdDebug( "ZIM icon not loaded for: %s", dictionaryName.c_str() ); + } + + // Fallback to default icon + dictionaryNativeIcon = dictionaryIcon = QIcon( ":/icons/icon32_zim.png" ); + dictionaryIconLoaded = true; }