+ Language pair is now extracted from the Bgl data

+ Bgl icons are now saved and used
This commit is contained in:
Konstantin Isakov 2009-05-07 10:59:58 +00:00
parent 765ba9d597
commit 7fd9d0f6c3
3 changed files with 70 additions and 20 deletions

View file

@ -44,7 +44,7 @@ namespace
enum enum
{ {
Signature = 0x584c4742, // BGLX on little-endian, XLGB on big-endian Signature = 0x584c4742, // BGLX on little-endian, XLGB on big-endian
CurrentFormatVersion = 14 + BtreeIndexing::FormatVersion CurrentFormatVersion = 15 + BtreeIndexing::FormatVersion
}; };
struct IdxHeader struct IdxHeader
@ -67,6 +67,8 @@ namespace
uint32_t resourcesCount; // Number of resources stored uint32_t resourcesCount; // Number of resources stored
uint32_t langFrom; // Source language uint32_t langFrom; // Source language
uint32_t langTo; // Target language uint32_t langTo; // Target language
uint32_t iconAddress; // Address of the icon in the chunks' storage
uint32_t iconSize; // Size of the icon in the chunks' storage, 0 = no icon
} }
#ifndef _MSC_VER #ifndef _MSC_VER
__attribute__((packed)) __attribute__((packed))
@ -205,6 +207,8 @@ namespace
IdxHeader idxHeader; IdxHeader idxHeader;
string dictionaryName; string dictionaryName;
ChunkedStorage::Reader chunks; ChunkedStorage::Reader chunks;
QIcon dictionaryIcon;
bool dictionaryIconLoaded;
public: public:
@ -223,8 +227,7 @@ namespace
virtual unsigned long getWordCount() throw() virtual unsigned long getWordCount() throw()
{ return idxHeader.wordCount; } { return idxHeader.wordCount; }
virtual QIcon getIcon() throw() virtual QIcon getIcon() throw();
{ return QIcon(":/icons/icon32_bgl.png"); }
inline virtual quint32 getLangFrom() const inline virtual quint32 getLangFrom() const
{ return idxHeader.langFrom; } { return idxHeader.langFrom; }
@ -261,7 +264,8 @@ namespace
BtreeDictionary( id, vector< string >( 1, dictionaryFile ) ), BtreeDictionary( id, vector< string >( 1, dictionaryFile ) ),
idx( indexFile, "rb" ), idx( indexFile, "rb" ),
idxHeader( idx.read< IdxHeader >() ), idxHeader( idx.read< IdxHeader >() ),
chunks( idx, idxHeader.chunksOffset ) chunks( idx, idxHeader.chunksOffset ),
dictionaryIconLoaded( !idxHeader.iconSize )
{ {
idx.seek( sizeof( idxHeader ) ); idx.seek( sizeof( idxHeader ) );
@ -282,6 +286,49 @@ namespace
idx, idxMutex ); idx, idxMutex );
} }
QIcon BglDictionary::getIcon() throw()
{
if ( !dictionaryIconLoaded )
{
// Try loading icon now
vector< char > chunk;
Mutex::Lock _( idxMutex );
char * iconData = chunks.getBlock( idxHeader.iconAddress, chunk );
QImage img;
if (img.loadFromData( ( unsigned char *) iconData, idxHeader.iconSize ) )
{
// Load successful
// 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.drawImage( QPoint( img.width() == max ? 0 : ( max - img.width() ) / 2,
img.height() == max ? 0 : ( max - img.height() ) / 2 ),
img );
painter.end();
dictionaryIcon = QIcon( QPixmap::fromImage( result ) );
}
dictionaryIconLoaded = true;
}
if ( !dictionaryIcon.isNull() )
return dictionaryIcon;
else
return QIcon(":/icons/icon32_bgl.png");
}
void BglDictionary::loadArticle( uint32_t offset, string & headword, void BglDictionary::loadArticle( uint32_t offset, string & headword,
string & displayedHeadword, string & displayedHeadword,
@ -1000,6 +1047,14 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
b.setResourcePrefix( string( "bres://" ) + dictId + "/" ); b.setResourcePrefix( string( "bres://" ) + dictId + "/" );
// Save icon if there's one
if ( size_t sz = b.getIcon().size() )
{
idxHeader.iconAddress = chunks.startNewBlock();
chunks.addToBlock( &b.getIcon().front(), sz );
idxHeader.iconSize = sz;
}
for( ; ; ) for( ; ; )
{ {
bgl_entry e = b.readEntry( &resourceHandler ); bgl_entry e = b.readEntry( &resourceHandler );
@ -1061,21 +1116,8 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
idxHeader.foldingVersion = Folding::Version; idxHeader.foldingVersion = Folding::Version;
idxHeader.articleCount = articleCount; idxHeader.articleCount = articleCount;
idxHeader.wordCount = wordCount; idxHeader.wordCount = wordCount;
idxHeader.langFrom = LangCoder::findIdForLanguage( Utf8::decode( b.sourceLang() ) );
// read languages idxHeader.langTo = LangCoder::findIdForLanguage( Utf8::decode( b.targetLang() ) );
QPair<quint32,quint32> langs =
LangCoder::findIdsForFilename( QString::fromStdString( *i ) );
// if no languages found, try dictionary's name
if ( langs.first == 0 || langs.second == 0 )
{
langs =
LangCoder::findIdsForFilename( QString::fromStdString( b.title() ) );
}
idxHeader.langFrom = langs.first;
idxHeader.langTo = langs.second;
idx.rewind(); idx.rewind();

View file

@ -230,6 +230,10 @@ bool Babylon::read(std::string &source_charset, std::string &target_charset)
} }
m_description = headword; m_description = headword;
break; break;
case 11:
icon.resize( block.length - 2 );
memcpy( &icon.front(), &(block.data[ 2 ]), icon.size() );
break;
case 26: case 26:
type = (unsigned int)block.data[2]; type = (unsigned int)block.data[2];
if( type > 64 ) type -= 65; if( type > 64 ) type -= 65;

View file

@ -188,9 +188,12 @@ public:
inline std::string filename() const { return m_filename; }; inline std::string filename() const { return m_filename; };
std::vector< char > const & getIcon() const
{ return icon; }
enum enum
{ {
ParserVersion = 4 ParserVersion = 5
}; };
private: private:
@ -211,6 +214,7 @@ private:
std::string m_defaultCharset; std::string m_defaultCharset;
std::string m_sourceCharset; std::string m_sourceCharset;
std::string m_targetCharset; std::string m_targetCharset;
std::vector< char > icon;
std::string m_resourcePrefix; std::string m_resourcePrefix;