/* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #include "sources.hh" #include #include #include #include "gddebug.hh" #ifdef MAKE_CHINESE_CONVERSION_SUPPORT #include "chineseconversion.hh" #endif Sources::Sources( QWidget * parent, Config::Class const & cfg): QWidget( parent ), #ifdef MAKE_CHINESE_CONVERSION_SUPPORT chineseConversion( new ChineseConversion( this, cfg.transliteration.chinese ) ), #endif #if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC ) textToSpeechSource( NULL ), #endif itemDelegate( new QItemDelegate( this ) ), itemEditorFactory( new QItemEditorFactory() ), mediawikisModel( this, cfg.mediawikis ), webSitesModel( this, cfg.webSites ), dictServersModel( this, cfg.dictServers ), programsModel( this, cfg.programs ), pathsModel( this, cfg.paths ), soundDirsModel( this, cfg.soundDirs ), hunspellDictsModel( this, cfg.hunspell ) { ui.setupUi( this ); Config::Hunspell const & hunspell = cfg.hunspell; Config::Transliteration const & trs = cfg.transliteration; Config::Forvo const & forvo = cfg.forvo; // TODO: will programTypeEditorCreator and itemEditorFactory be destroyed by // anyone? QItemEditorCreatorBase * programTypeEditorCreator = new QStandardItemEditorCreator< ProgramTypeEditor >(); itemEditorFactory->registerEditor( QVariant::Int, programTypeEditorCreator ); itemDelegate->setItemEditorFactory( itemEditorFactory ); ui.mediaWikis->setTabKeyNavigation( true ); ui.mediaWikis->setModel( &mediawikisModel ); ui.mediaWikis->resizeColumnToContents( 0 ); ui.mediaWikis->resizeColumnToContents( 1 ); ui.mediaWikis->resizeColumnToContents( 2 ); ui.mediaWikis->resizeColumnToContents( 3 ); ui.webSites->setTabKeyNavigation( true ); ui.webSites->setModel( &webSitesModel ); ui.webSites->resizeColumnToContents( 0 ); ui.webSites->resizeColumnToContents( 1 ); ui.webSites->resizeColumnToContents( 2 ); ui.webSites->resizeColumnToContents( 3 ); ui.webSites->resizeColumnToContents( 4 ); ui.dictServers->setTabKeyNavigation( true ); ui.dictServers->setModel( &dictServersModel ); ui.dictServers->resizeColumnToContents( 0 ); ui.dictServers->resizeColumnToContents( 1 ); ui.dictServers->resizeColumnToContents( 2 ); ui.dictServers->resizeColumnToContents( 3 ); ui.dictServers->resizeColumnToContents( 4 ); ui.dictServers->resizeColumnToContents( 5 ); ui.programs->setTabKeyNavigation( true ); ui.programs->setModel( &programsModel ); ui.programs->resizeColumnToContents( 0 ); // Make sure this thing will be large enough ui.programs->setColumnWidth( 1, QFontMetrics( QFont() ).width( ProgramTypeEditor::getNameForType( Config::Program::PrefixMatch ) ) + 16 ); ui.programs->resizeColumnToContents( 2 ); ui.programs->resizeColumnToContents( 3 ); ui.programs->resizeColumnToContents( 4 ); ui.programs->setItemDelegate( itemDelegate ); ui.paths->setTabKeyNavigation( true ); ui.paths->setModel( &pathsModel ); fitPathsColumns(); ui.soundDirs->setTabKeyNavigation( true ); ui.soundDirs->setModel( &soundDirsModel ); fitSoundDirsColumns(); ui.hunspellPath->setText( hunspell.dictionariesPath ); ui.hunspellDictionaries->setTabKeyNavigation( true ); ui.hunspellDictionaries->setModel( &hunspellDictsModel ); fitHunspellDictsColumns(); ui.enableRussianTransliteration->setChecked( trs.enableRussianTransliteration ); ui.enableGermanTransliteration->setChecked( trs.enableGermanTransliteration ); ui.enableGreekTransliteration->setChecked( trs.enableGreekTransliteration ); ui.enableBelarusianTransliteration->setChecked( trs.enableBelarusianTransliteration ); #ifdef MAKE_CHINESE_CONVERSION_SUPPORT ui.transliterationLayout->addWidget(chineseConversion); ui.transliterationLayout->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding)); #endif ui.enableRomaji->setChecked( trs.romaji.enable ); ui.enableHepburn->setChecked( trs.romaji.enableHepburn ); ui.enableNihonShiki->setChecked( trs.romaji.enableNihonShiki ); ui.enableKunreiShiki->setChecked( trs.romaji.enableKunreiShiki ); ui.enableHiragana->setChecked( trs.romaji.enableHiragana ); ui.enableKatakana->setChecked( trs.romaji.enableKatakana ); ui.forvoEnabled->setChecked( forvo.enable ); ui.forvoApiKey->setText( forvo.apiKey ); ui.forvoLanguageCodes->setText( forvo.languageCodes ); // Text to speech #if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC ) textToSpeechSource = new TextToSpeechSource( this, cfg.voiceEngines ); ui.tabWidget->addTab( textToSpeechSource, QIcon(":/icons/playsound_color.svg"), tr( "Text to Speech" ) ); #endif if ( Config::isPortableVersion() ) { // Paths ui.paths->setEnabled( false ); ui.addPath->setEnabled( false ); ui.removePath->setEnabled( false ); // Sound dirs { QStandardItemModel * model = new QStandardItemModel( this ); model->setHorizontalHeaderLabels( QStringList() << " " ); model->invisibleRootItem()->appendRow( new QStandardItem( tr( "(not available in portable version)" ) ) ); ui.soundDirs->setModel( model ); ui.soundDirs->setEnabled( false ); ui.addSoundDir->setEnabled( false ); ui.removeSoundDir->setEnabled( false ); } // Morpho ui.hunspellPath->setEnabled( false ); ui.changeHunspellPath->setEnabled( false ); } } void Sources::fitPathsColumns() { ui.paths->resizeColumnToContents( 0 ); ui.paths->resizeColumnToContents( 1 ); } void Sources::fitSoundDirsColumns() { ui.soundDirs->resizeColumnToContents( 0 ); ui.soundDirs->resizeColumnToContents( 1 ); ui.soundDirs->resizeColumnToContents( 2 ); } void Sources::fitHunspellDictsColumns() { ui.hunspellDictionaries->resizeColumnToContents( 0 ); ui.hunspellDictionaries->resizeColumnToContents( 1 ); } void Sources::on_addPath_clicked() { QString dir = QFileDialog::getExistingDirectory( this, tr( "Choose a directory" ) ); if ( !dir.isEmpty() ) { pathsModel.addNewPath( dir ); fitPathsColumns(); } } void Sources::on_removePath_clicked() { QModelIndex current = ui.paths->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove directory %1 from the list?" ).arg( pathsModel.getCurrentPaths()[ current.row() ].path ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) { pathsModel.removePath( current.row() ); fitPathsColumns(); } } void Sources::on_addSoundDir_clicked() { QString dir = QFileDialog::getExistingDirectory( this, tr( "Choose a directory" ) ); if ( !dir.isEmpty() ) { soundDirsModel.addNewSoundDir( dir, QDir( dir ).dirName() ); fitSoundDirsColumns(); } } void Sources::on_removeSoundDir_clicked() { QModelIndex current = ui.soundDirs->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove directory %1 from the list?" ).arg( soundDirsModel.getCurrentSoundDirs()[ current.row() ].path ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) { soundDirsModel.removeSoundDir( current.row() ); fitSoundDirsColumns(); } } void Sources::on_changeHunspellPath_clicked() { QString dir = QFileDialog::getExistingDirectory( this, tr( "Choose a directory" ) ); if ( !dir.isEmpty() ) { ui.hunspellPath->setText( dir ); hunspellDictsModel.changePath( dir ); fitHunspellDictsColumns(); } } void Sources::on_addMediaWiki_clicked() { mediawikisModel.addNewWiki(); QModelIndex result = mediawikisModel.index( mediawikisModel.rowCount( QModelIndex() ) - 1, 1, QModelIndex() ); ui.mediaWikis->scrollTo( result ); //ui.mediaWikis->setCurrentIndex( result ); ui.mediaWikis->edit( result ); } void Sources::on_removeMediaWiki_clicked() { QModelIndex current = ui.mediaWikis->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove site %1 from the list?" ).arg( mediawikisModel.getCurrentWikis()[ current.row() ].name ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) mediawikisModel.removeWiki( current.row() ); } void Sources::on_addWebSite_clicked() { webSitesModel.addNewSite(); QModelIndex result = webSitesModel.index( webSitesModel.rowCount( QModelIndex() ) - 1, 1, QModelIndex() ); ui.webSites->scrollTo( result ); ui.webSites->edit( result ); } void Sources::on_removeWebSite_clicked() { QModelIndex current = ui.webSites->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove site %1 from the list?" ).arg( webSitesModel.getCurrentWebSites()[ current.row() ].name ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) webSitesModel.removeSite( current.row() ); } void Sources::on_addDictServer_clicked() { dictServersModel.addNewServer(); QModelIndex result = dictServersModel.index( dictServersModel.rowCount( QModelIndex() ) - 1, 1, QModelIndex() ); ui.dictServers->scrollTo( result ); ui.dictServers->edit( result ); } void Sources::on_removeDictServer_clicked() { QModelIndex current = ui.dictServers->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove site %1 from the list?" ).arg( dictServersModel.getCurrentDictServers()[ current.row() ].name ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) dictServersModel.removeServer( current.row() ); } void Sources::on_addProgram_clicked() { programsModel.addNewProgram(); QModelIndex result = programsModel.index( programsModel.rowCount( QModelIndex() ) - 1, 1, QModelIndex() ); ui.programs->scrollTo( result ); ui.programs->edit( result ); } void Sources::on_removeProgram_clicked() { QModelIndex current = ui.programs->currentIndex(); if ( current.isValid() && QMessageBox::question( this, tr( "Confirm removal" ), tr( "Remove program %1 from the list?" ).arg( programsModel.getCurrentPrograms()[ current.row() ].name ), QMessageBox::Ok, QMessageBox::Cancel ) == QMessageBox::Ok ) programsModel.removeProgram( current.row() ); } Config::VoiceEngines Sources::getVoiceEngines() const { #if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC ) if ( !textToSpeechSource ) return Config::VoiceEngines(); return textToSpeechSource->getVoiceEnginesModel().getCurrentVoiceEngines(); #else return Config::VoiceEngines(); #endif } Config::Hunspell Sources::getHunspell() const { Config::Hunspell h; h.dictionariesPath = ui.hunspellPath->text(); h.enabledDictionaries = hunspellDictsModel.getEnabledDictionaries(); return h; } Config::Transliteration Sources::getTransliteration() const { Config::Transliteration tr; tr.enableRussianTransliteration = ui.enableRussianTransliteration->isChecked(); tr.enableGermanTransliteration = ui.enableGermanTransliteration->isChecked(); tr.enableGreekTransliteration = ui.enableGreekTransliteration->isChecked(); tr.enableBelarusianTransliteration = ui.enableBelarusianTransliteration->isChecked(); #ifdef MAKE_CHINESE_CONVERSION_SUPPORT chineseConversion->getConfig( tr.chinese ); #endif tr.romaji.enable = ui.enableRomaji->isChecked(); tr.romaji.enableHepburn = ui.enableHepburn->isChecked(); tr.romaji.enableNihonShiki = ui.enableNihonShiki->isChecked(); tr.romaji.enableKunreiShiki = ui.enableKunreiShiki->isChecked(); tr.romaji.enableHiragana = ui.enableHiragana->isChecked(); tr.romaji.enableKatakana = ui.enableKatakana->isChecked(); return tr; } Config::Forvo Sources::getForvo() const { Config::Forvo forvo; forvo.enable = ui.forvoEnabled->isChecked(); forvo.apiKey = ui.forvoApiKey->text(); forvo.languageCodes = ui.forvoLanguageCodes->text(); return forvo; } ////////// MediaWikisModel MediaWikisModel::MediaWikisModel( QWidget * parent, Config::MediaWikis const & mediawikis_ ): QAbstractItemModel( parent ), mediawikis( mediawikis_ ) { } void MediaWikisModel::removeWiki( int index ) { beginRemoveRows( QModelIndex(), index, index ); mediawikis.erase( mediawikis.begin() + index ); endRemoveRows(); } void MediaWikisModel::addNewWiki() { Config::MediaWiki w; w.enabled = false; w.id = Dictionary::generateRandomDictionaryId(); w.url = "http://"; beginInsertRows( QModelIndex(), mediawikis.size(), mediawikis.size() ); mediawikis.push_back( w ); endInsertRows(); } QModelIndex MediaWikisModel::index( int row, int column, QModelIndex const & /*parent*/ ) const { return createIndex( row, column ); } QModelIndex MediaWikisModel::parent( QModelIndex const & /*parent*/ ) const { return QModelIndex(); } Qt::ItemFlags MediaWikisModel::flags( QModelIndex const & index ) const { Qt::ItemFlags result = QAbstractItemModel::flags( index ); if ( index.isValid() ) { if ( !index.column() ) result |= Qt::ItemIsUserCheckable; else result |= Qt::ItemIsEditable; } return result; } int MediaWikisModel::rowCount( QModelIndex const & parent ) const { if ( parent.isValid() ) return 0; else return mediawikis.size(); } int MediaWikisModel::columnCount( QModelIndex const & parent ) const { if ( parent.isValid() ) return 0; else return 4; } QVariant MediaWikisModel::headerData( int section, Qt::Orientation /*orientation*/, int role ) const { if ( role == Qt::DisplayRole ) switch( section ) { case 0: return tr( "Enabled" ); case 1: return tr( "Name" ); case 2: return tr( "Address" ); case 3: return tr( "Icon" ); default: return QVariant(); } return QVariant(); } QVariant MediaWikisModel::data( QModelIndex const & index, int role ) const { if ( index.row() >= mediawikis.size() ) return QVariant(); if ( role == Qt::DisplayRole || role == Qt::EditRole ) { switch( index.column() ) { case 1: return mediawikis[ index.row() ].name; case 2: return mediawikis[ index.row() ].url; case 3: return mediawikis[ index.row() ].icon; default: return QVariant(); } } if ( role == Qt::CheckStateRole && !index.column() ) return mediawikis[ index.row() ].enabled ? Qt::Checked : Qt::Unchecked; return QVariant(); } bool MediaWikisModel::setData( QModelIndex const & index, const QVariant & value, int role ) { if ( index.row() >= mediawikis.size() ) return false; if ( role == Qt::CheckStateRole && !index.column() ) { //GD_DPRINTF( "type = %d\n", (int)value.type() ); //GD_DPRINTF( "value = %d\n", (int)value.toInt() ); // XXX it seems to be always passing Int( 2 ) as a value, so we just toggle mediawikis[ index.row() ].enabled = !mediawikis[ index.row() ].enabled; dataChanged( index, index ); return true; } if ( role == Qt::DisplayRole || role == Qt::EditRole ) switch( index.column() ) { case 1: mediawikis[ index.row() ].name = value.toString(); dataChanged( index, index ); return true; case 2: mediawikis[ index.row() ].url = value.toString(); dataChanged( index, index ); return true; case 3: mediawikis[ index.row() ].icon = value.toString(); dataChanged( index, index ); return true; default: return false; } return false; } ////////// WebSitesModel WebSitesModel::WebSitesModel( QWidget * parent, Config::WebSites const & webSites_ ): QAbstractItemModel( parent ), webSites( webSites_ ) { } void WebSitesModel::removeSite( int index ) { beginRemoveRows( QModelIndex(), index, index ); webSites.erase( webSites.begin() + index ); endRemoveRows(); } void WebSitesModel::addNewSite() { Config::WebSite w; w.enabled = false; w.id = Dictionary::generateRandomDictionaryId(); w.url = "http://"; w.inside_iframe = true; beginInsertRows( QModelIndex(), webSites.size(), webSites.size() ); webSites.push_back( w ); endInsertRows(); } QModelIndex WebSitesModel::index( int row, int column, QModelIndex const & /*parent*/ ) const { return createIndex( row, column ); } QModelIndex WebSitesModel::parent( QModelIndex const & /*parent*/ ) const { return QModelIndex(); } Qt::ItemFlags WebSitesModel::flags( QModelIndex const & index ) const { Qt::ItemFlags result = QAbstractItemModel::flags( index ); if ( index.isValid() ) { if ( index.column() <= 1 ) result |= Qt::ItemIsUserCheckable; else result |= Qt::ItemIsEditable; } return result; } int WebSitesModel::rowCount( QModelIndex const & parent ) const { if ( parent.isValid() ) return 0; else return webSites.size(); } int WebSitesModel::columnCount( QModelIndex const & parent ) const { if ( parent.isValid() ) return 0; else return 5; } QVariant WebSitesModel::headerData( int section, Qt::Orientation /*orientation*/, int role ) const { if( role == Qt::ToolTipRole ) { if( section == 1 ) return tr( "Insert article as link inside