2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-05-18 18:01:50 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "orderandprops.hh"
|
|
|
|
#include "instances.hh"
|
2009-05-24 15:45:37 +00:00
|
|
|
#include "langcoder.hh"
|
2009-05-24 16:38:08 +00:00
|
|
|
#include "language.hh"
|
2011-09-09 12:05:28 +00:00
|
|
|
#include "fsencoding.hh"
|
2012-12-18 12:33:53 +00:00
|
|
|
#include <algorithm>
|
2012-12-19 12:59:38 +00:00
|
|
|
#include <QPair>
|
2012-12-18 12:33:53 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::sort;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool dictNameLessThan( sptr< Dictionary::Class > const & dict1,
|
|
|
|
sptr< Dictionary::Class > const & dict2 )
|
|
|
|
{
|
|
|
|
QString str1 = QString::fromUtf8( dict1->getName().c_str() );
|
|
|
|
QString str2 = QString::fromUtf8( dict2->getName().c_str() );
|
|
|
|
if( str1.isEmpty() && !str2.isEmpty() )
|
|
|
|
return false;
|
|
|
|
if( !str1.isEmpty() && str2.isEmpty() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return str1.compare( str2, Qt::CaseInsensitive ) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dictLessThan( sptr< Dictionary::Class > const & dict1,
|
|
|
|
sptr< Dictionary::Class > const & dict2 )
|
|
|
|
{
|
2012-12-19 12:59:38 +00:00
|
|
|
int idFrom1 = dict1->getLangFrom();
|
|
|
|
int idTo1 = dict1->getLangTo();
|
|
|
|
if( idFrom1 == 0)
|
|
|
|
{
|
|
|
|
QPair<quint32,quint32> 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<quint32,quint32> 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 );
|
2012-12-18 12:33:53 +00:00
|
|
|
if( str1.isEmpty() && !str2.isEmpty() )
|
|
|
|
return false;
|
|
|
|
if( !str1.isEmpty() && str2.isEmpty() )
|
|
|
|
return true;
|
|
|
|
int res = str1.compare( str2, Qt::CaseInsensitive );
|
|
|
|
if( res )
|
|
|
|
return res < 0;
|
|
|
|
|
2012-12-19 12:59:38 +00:00
|
|
|
str1 = LangCoder::decode( idTo1 );
|
|
|
|
str2 = LangCoder::decode( idTo2 );
|
2012-12-18 12:33:53 +00:00
|
|
|
if( str1.isEmpty() && !str2.isEmpty() )
|
|
|
|
return false;
|
|
|
|
if( !str1.isEmpty() && str2.isEmpty() )
|
|
|
|
return true;
|
|
|
|
res = str1.compare( str2, Qt::CaseInsensitive );
|
|
|
|
if( res )
|
|
|
|
return res < 0;
|
|
|
|
|
|
|
|
str1 = QString::fromUtf8( dict1->getName().c_str() );
|
|
|
|
str2 = QString::fromUtf8( dict2->getName().c_str() );
|
|
|
|
if( str1.isEmpty() && !str2.isEmpty() )
|
|
|
|
return false;
|
|
|
|
if( !str1.isEmpty() && str2.isEmpty() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return str1.compare( str2, Qt::CaseInsensitive ) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2009-05-18 18:01:50 +00:00
|
|
|
|
|
|
|
OrderAndProps::OrderAndProps( QWidget * parent,
|
|
|
|
Config::Group const & dictionaryOrder,
|
|
|
|
Config::Group const & inactiveDictionaries,
|
|
|
|
std::vector< sptr< Dictionary::Class > > const &
|
|
|
|
allDictionaries ):
|
|
|
|
QWidget( parent )
|
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
2009-05-23 14:19:57 +00:00
|
|
|
|
2009-05-24 15:45:37 +00:00
|
|
|
// For now we don't support arrows, so remove them until we get to that
|
|
|
|
delete ui.moveActiveUp;
|
|
|
|
delete ui.moveActiveDown;
|
|
|
|
|
|
|
|
delete ui.moveToActive;
|
|
|
|
delete ui.moveToInactive;
|
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
Instances::Group order( dictionaryOrder, allDictionaries );
|
|
|
|
Instances::Group inactive( inactiveDictionaries, allDictionaries );
|
|
|
|
|
|
|
|
Instances::complementDictionaryOrder( order, inactive, allDictionaries );
|
|
|
|
|
|
|
|
ui.dictionaryOrder->populate( order.dictionaries, allDictionaries );
|
|
|
|
ui.inactiveDictionaries->populate( inactive.dictionaries, allDictionaries );
|
2009-05-24 15:45:37 +00:00
|
|
|
|
|
|
|
disableDictionaryDescription();
|
2012-12-18 12:33:53 +00:00
|
|
|
|
|
|
|
ui.dictionaryOrder->setContextMenuPolicy( Qt::CustomContextMenu );
|
|
|
|
connect( ui.dictionaryOrder, SIGNAL( customContextMenuRequested( QPoint ) ),
|
|
|
|
this, SLOT( contextMenuRequested( QPoint ) ) );
|
2009-05-18 18:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Config::Group OrderAndProps::getCurrentDictionaryOrder() const
|
|
|
|
{
|
|
|
|
Instances::Group g( "" );
|
|
|
|
|
|
|
|
g.dictionaries = ui.dictionaryOrder->getCurrentDictionaries();
|
|
|
|
|
|
|
|
return g.makeConfigGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::Group OrderAndProps::getCurrentInactiveDictionaries() const
|
|
|
|
{
|
|
|
|
Instances::Group g( "" );
|
|
|
|
|
|
|
|
g.dictionaries = ui.inactiveDictionaries->getCurrentDictionaries();
|
|
|
|
|
|
|
|
return g.makeConfigGroup();
|
|
|
|
}
|
2009-05-24 15:45:37 +00:00
|
|
|
|
2010-09-25 17:48:18 +00:00
|
|
|
void OrderAndProps::on_dictionaryOrder_clicked( QModelIndex const & idx )
|
2009-05-24 15:45:37 +00:00
|
|
|
{
|
2010-09-25 17:48:18 +00:00
|
|
|
describeDictionary( ui.dictionaryOrder, idx );
|
2009-05-24 15:45:37 +00:00
|
|
|
}
|
|
|
|
|
2010-09-25 17:48:18 +00:00
|
|
|
void OrderAndProps::on_inactiveDictionaries_clicked( QModelIndex const & idx )
|
2009-05-24 15:45:37 +00:00
|
|
|
{
|
2010-09-25 17:48:18 +00:00
|
|
|
describeDictionary( ui.inactiveDictionaries, idx );
|
2009-05-24 15:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OrderAndProps::disableDictionaryDescription()
|
|
|
|
{
|
|
|
|
ui.dictionaryInformation->setEnabled( false );
|
|
|
|
|
|
|
|
ui.dictionaryName->clear();
|
|
|
|
ui.dictionaryTotalArticles->clear();
|
|
|
|
ui.dictionaryTotalWords->clear();
|
|
|
|
ui.dictionaryTranslatesFrom->clear();
|
|
|
|
ui.dictionaryTranslatesTo->clear();
|
|
|
|
ui.dictionaryFileList->clear();
|
2012-09-07 11:51:42 +00:00
|
|
|
|
|
|
|
ui.dictionaryDescription->clear();
|
|
|
|
ui.dictionaryDescription->setVisible( false );
|
|
|
|
ui.dictionaryDescriptionLabel->setVisible( false );
|
2009-05-24 15:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OrderAndProps::describeDictionary( DictListWidget * lst, QModelIndex const & idx )
|
|
|
|
{
|
|
|
|
if ( !idx.isValid() || (unsigned) idx.row() >= lst->getCurrentDictionaries().size() )
|
|
|
|
disableDictionaryDescription();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sptr< Dictionary::Class > dict = lst->getCurrentDictionaries()[ idx.row() ];
|
|
|
|
|
|
|
|
ui.dictionaryInformation->setEnabled( true );
|
|
|
|
|
|
|
|
ui.dictionaryName->setText( QString::fromUtf8( dict->getName().c_str() ) );
|
|
|
|
|
|
|
|
ui.dictionaryTotalArticles->setText( QString::number( dict->getArticleCount() ) );
|
|
|
|
ui.dictionaryTotalWords->setText( QString::number( dict->getWordCount() ) );
|
2012-09-24 12:51:51 +00:00
|
|
|
ui.dictionaryTranslatesFrom->setText( Language::localizedStringForId( dict->getLangFrom() ) );
|
|
|
|
ui.dictionaryTranslatesTo->setText( Language::localizedStringForId( dict->getLangTo() ) );
|
2009-05-24 15:45:37 +00:00
|
|
|
|
|
|
|
std::vector< std::string > const & filenames = dict->getDictionaryFilenames();
|
|
|
|
|
|
|
|
QString filenamesText;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < filenames.size(); x++ )
|
|
|
|
{
|
2011-09-09 12:05:28 +00:00
|
|
|
filenamesText += FsEncoding::decode( filenames[ x ].c_str() );
|
2009-05-24 15:45:37 +00:00
|
|
|
filenamesText += '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.dictionaryFileList->setPlainText( filenamesText );
|
2012-09-07 11:51:42 +00:00
|
|
|
|
|
|
|
QString const& descText = dict->getDescription();
|
|
|
|
if( !descText.isEmpty() && descText.compare( "NONE" ) != 0 )
|
|
|
|
{
|
|
|
|
ui.dictionaryDescription->setPlainText( descText );
|
|
|
|
ui.dictionaryDescription->setVisible( true );
|
|
|
|
ui.dictionaryDescriptionLabel->setVisible( true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui.dictionaryDescription->setVisible( false );
|
|
|
|
ui.dictionaryDescriptionLabel->setVisible( false );
|
|
|
|
}
|
2009-05-24 15:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 12:33:53 +00:00
|
|
|
|
|
|
|
void OrderAndProps::contextMenuRequested( const QPoint & pos )
|
|
|
|
{
|
|
|
|
QMenu menu( this );
|
|
|
|
QAction * sortNameAction = new QAction( tr( "Sort by name" ), &menu );
|
|
|
|
menu.addAction( sortNameAction );
|
|
|
|
QAction * sortLangAction = new QAction( tr( "Sort by languages" ), &menu );
|
|
|
|
menu.addAction( sortLangAction );
|
|
|
|
|
|
|
|
QAction * result = menu.exec( ui.dictionaryOrder->mapToGlobal( pos ) );
|
|
|
|
|
|
|
|
if( result == sortNameAction || result == sortLangAction )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > sortedDicts = ui.dictionaryOrder->getCurrentDictionaries();
|
|
|
|
if( result == sortNameAction )
|
|
|
|
sort( sortedDicts.begin(), sortedDicts.end(), dictNameLessThan );
|
|
|
|
else
|
|
|
|
sort( sortedDicts.begin(), sortedDicts.end(), dictLessThan );
|
|
|
|
ui.dictionaryOrder->populate( sortedDicts );
|
|
|
|
}
|
|
|
|
}
|