diff --git a/groups_widgets.cc b/groups_widgets.cc index 67d00815..be22432b 100644 --- a/groups_widgets.cc +++ b/groups_widgets.cc @@ -16,6 +16,7 @@ #include #include #include +#include using std::vector; @@ -597,20 +598,48 @@ void DictGroupsWidget::addAutoGroups() return; QMap< QString, QVector< sptr > > dictMap; + QMap< QString, QVector< sptr > > morphoMap; + + // Put active dictionaries into lists for ( unsigned i = 0; i < activeDicts->size(); i++ ) { sptr dict = activeDicts->at( i ); - QString lfrom = LangCoder::intToCode2( dict->getLangFrom() ); - QString lto = LangCoder::intToCode2( dict->getLangTo() ); - QString name("Unassigned"); - if ( !lfrom.isEmpty() && !lto.isEmpty() ) + int idFrom = dict->getLangFrom(); + int idTo = dict->getLangTo(); + if( idFrom == 0) { + // Attempt to find language pair in dictionary name + + QPair ids = LangCoder::findIdsForName( QString::fromUtf8( dict->getName().c_str() ) ); + idFrom = ids.first; + idTo = ids.second; + } + + QString name("Unassigned"); + if ( idFrom != 0 && idTo != 0 ) + { + QString lfrom = LangCoder::intToCode2( idFrom ); + QString lto = LangCoder::intToCode2( idTo ); lfrom[ 0 ] = lfrom[ 0 ].toTitleCase(); lto[ 0 ] = lto[ 0 ].toTitleCase(); name = lfrom + " - " + lto; } + else if( !dict->getDictionaryFilenames().empty() ) + { + // Handle special case - morphology dictionaries + + QString fileName = QFileInfo( FsEncoding::decode( dict->getDictionaryFilenames()[ 0 ].c_str() ) ).fileName(); + if( fileName.endsWith( ".aff", Qt::CaseInsensitive ) ) + { + QString code = fileName.left( 2 ).toLower(); + QVector > vd = morphoMap[ code ]; + vd.append( dict ); + morphoMap[ code ] = vd; + continue; + } + } QVector > vd = dictMap[ name ]; vd.append( dict ); @@ -618,6 +647,23 @@ void DictGroupsWidget::addAutoGroups() } QStringList groupList = dictMap.uniqueKeys(); + QStringList morphoList = morphoMap.uniqueKeys(); + + // Insert morphology dictionaries into corresponding lists + + for( QStringList::ConstIterator ln = morphoList.begin(); ln != morphoList.end(); ++ln ) + { + for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr ) + if( ln->compare( gr->left( 2 ), Qt::CaseInsensitive ) == 0 ) + { + QVector > vdg = dictMap[ *gr ]; + vdg += morphoMap[ *ln ]; + dictMap[ *gr ] = vdg; + } + } + + // Make groups + for( QStringList::ConstIterator gr = groupList.begin(); gr != groupList.end(); ++gr ) { int n; diff --git a/langcoder.cc b/langcoder.cc index 13ff4576..e1e30cba 100644 --- a/langcoder.cc +++ b/langcoder.cc @@ -133,19 +133,14 @@ quint32 LangCoder::guessId( const QString & lang ) return code2toInt( lstr.left(2).toAscii().data() ); } -QPair LangCoder::findIdsForFilename( QString const & name ) +QPair LangCoder::findIdsForName( QString const & name ) { - QString nameFolded = "|" + QFileInfo( name ).fileName().toCaseFolded() + "|"; - -// qDebug() << nameFolded; - + QString nameFolded = "|" + name.toCaseFolded() + "|"; QRegExp reg( "[^a-z]([a-z]{2,3})-([a-z]{2,3})[^a-z]" ); reg.setMinimal(true); int off = 0; + while ( reg.indexIn( nameFolded, off ) >= 0 ) { -// qDebug() << reg.cap(1); -// qDebug() << reg.cap(2); - quint32 from = guessId( reg.cap(1) ); quint32 to = guessId( reg.cap(2) ); if (from && to) @@ -157,6 +152,11 @@ QPair LangCoder::findIdsForFilename( QString const & name ) return QPair(0, 0); } +QPair LangCoder::findIdsForFilename( QString const & name ) +{ + return findIdsForName( QFileInfo( name ).fileName() ); +} + /* LangStruct& LangCoder::CodeToLangStruct(const QString &code) { diff --git a/langcoder.hh b/langcoder.hh index 1df25bf2..cabfa778 100644 --- a/langcoder.hh +++ b/langcoder.hh @@ -235,6 +235,7 @@ public: static quint32 findIdForLanguageCode3( const char * ); + static QPair findIdsForName( QString const & ); static QPair findIdsForFilename( QString const & ); static quint32 guessId( const QString & lang ); diff --git a/orderandprops.cc b/orderandprops.cc index 29f3799f..313cace7 100644 --- a/orderandprops.cc +++ b/orderandprops.cc @@ -7,6 +7,7 @@ #include "language.hh" #include "fsencoding.hh" #include +#include using std::vector; using std::sort; @@ -29,8 +30,26 @@ bool dictNameLessThan( sptr< Dictionary::Class > const & dict1, bool dictLessThan( sptr< Dictionary::Class > const & dict1, sptr< Dictionary::Class > const & dict2 ) { - QString str1 = LangCoder::decode( dict1->getLangFrom() ); - QString str2 = LangCoder::decode( dict2->getLangFrom() ); + int idFrom1 = dict1->getLangFrom(); + int idTo1 = dict1->getLangTo(); + if( idFrom1 == 0) + { + QPair ids = LangCoder::findIdsForName( QString::fromUtf8( dict1->getName().c_str() ) ); + idFrom1 = ids.first; + idTo1 = ids.second; + } + + int idFrom2 = dict2->getLangFrom(); + int idTo2 = dict2->getLangTo(); + if( idFrom2 == 0) + { + QPair ids = LangCoder::findIdsForName( QString::fromUtf8( dict2->getName().c_str() ) ); + idFrom2 = ids.first; + idTo2 = ids.second; + } + + QString str1 = LangCoder::decode( idFrom1 ); + QString str2 = LangCoder::decode( idFrom2 ); if( str1.isEmpty() && !str2.isEmpty() ) return false; if( !str1.isEmpty() && str2.isEmpty() ) @@ -39,8 +58,8 @@ bool dictLessThan( sptr< Dictionary::Class > const & dict1, if( res ) return res < 0; - str1 = LangCoder::decode( dict1->getLangTo() ); - str2 = LangCoder::decode( dict2->getLangTo() ); + str1 = LangCoder::decode( idTo1 ); + str2 = LangCoder::decode( idTo2 ); if( str1.isEmpty() && !str2.isEmpty() ) return false; if( !str1.isEmpty() && str2.isEmpty() )