diff --git a/dictionary.cc b/dictionary.cc index ca6d2f93..5a20875f 100644 --- a/dictionary.cc +++ b/dictionary.cc @@ -516,5 +516,14 @@ QString generateRandomDictionaryId() QCryptographicHash::Md5 ).toHex() ); } - +QMap< std::string, sptr< Dictionary::Class > > +dictToMap( std::vector< sptr< Dictionary::Class > > const & dicts ) +{ + QMap< std::string, sptr< Dictionary::Class > > dictMap; + for( auto dict : dicts ) + { + dictMap.insert( dict.get()->getId(), dict ); + } + return dictMap; +} } diff --git a/dictionary.hh b/dictionary.hh index 3d18ac54..b21933e2 100644 --- a/dictionary.hh +++ b/dictionary.hh @@ -479,6 +479,9 @@ bool needToRebuildIndex( vector< string > const & dictionaryFiles, /// dictionaries. QString generateRandomDictionaryId(); +QMap< std::string, sptr< Dictionary::Class > > +dictToMap( std::vector< sptr< Dictionary::Class > > const & dicts ); + } #endif diff --git a/editdictionaries.cc b/editdictionaries.cc index b9f0b8fa..3d4fe595 100644 --- a/editdictionaries.cc +++ b/editdictionaries.cc @@ -11,17 +11,19 @@ using std::vector; EditDictionaries::EditDictionaries( QWidget * parent, Config::Class & cfg_, vector< sptr< Dictionary::Class > > & dictionaries_, + QMap > & dictMap_, Instances::Groups & groupInstances_, QNetworkAccessManager & dictNetMgr_ ): QDialog( parent, Qt::WindowSystemMenuHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint ), cfg( cfg_ ), dictionaries( dictionaries_ ), + dictMap(dictMap_), groupInstances( groupInstances_ ), dictNetMgr( dictNetMgr_ ), origCfg( cfg ), sources( this, cfg ), orderAndProps( new OrderAndProps( this, cfg.dictionaryOrder, cfg.inactiveDictionaries, - dictionaries ) ), + dictionaries, dictMap ) ), groups( new Groups( this, dictionaries, cfg.groups, orderAndProps->getCurrentDictionaryOrder() ) ), dictionariesChanged( false ), groupsChanged( false ), @@ -244,7 +246,7 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups ) if ( rebuildGroups ) { - orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries ); + orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries, dictMap ); ui.tabs->insertTab( 1, orderAndProps.get(), QIcon(":/icons/book.svg"), tr( "&Dictionaries" ) ); groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() ); diff --git a/editdictionaries.hh b/editdictionaries.hh index 14affb18..a6de3819 100644 --- a/editdictionaries.hh +++ b/editdictionaries.hh @@ -23,6 +23,7 @@ public: EditDictionaries( QWidget * parent, Config::Class & cfg, std::vector< sptr< Dictionary::Class > > & dictionaries, + QMap > & dictMap_, Instances::Groups & groupInstances, // We only clear those on rescan QNetworkAccessManager & dictNetMgr ); @@ -73,6 +74,7 @@ private: Config::Class & cfg; std::vector< sptr< Dictionary::Class > > & dictionaries; + QMap > & dictMap; Instances::Groups & groupInstances; QNetworkAccessManager & dictNetMgr; diff --git a/groups.cc b/groups.cc index d9514bd0..d36b9520 100644 --- a/groups.cc +++ b/groups.cc @@ -20,7 +20,7 @@ Groups::Groups( QWidget * parent, // Populate the dictionaries' list ui.dictionaries->setAsSource(); - ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, + ui.dictionaries->populate( Instances::Group( order, Dictionary::dictToMap(dicts), Config::Group() ).dictionaries, dicts ); ui.searchLine->applyTo( ui.dictionaries ); @@ -87,14 +87,13 @@ void Groups::editGroup( unsigned id ) void Groups::updateDictionaryOrder( Config::Group const & order ) { // Make sure it differs from what we have + auto dictMap = Dictionary::dictToMap( dicts ); + Instances::Group newOrder( order, dictMap, Config::Group() ); - Instances::Group newOrder( order, dicts, Config::Group() ); - - if ( ui.dictionaries->getCurrentDictionaries() != newOrder.dictionaries ) + if( ui.dictionaries->getCurrentDictionaries() != newOrder.dictionaries ) { // Repopulate - ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, - dicts ); + ui.dictionaries->populate( Instances::Group( order, dictMap, Config::Group() ).dictionaries, dicts ); } } diff --git a/groups_widgets.cc b/groups_widgets.cc index 851b6ba4..a91190ef 100644 --- a/groups_widgets.cc +++ b/groups_widgets.cc @@ -31,7 +31,8 @@ DictGroupWidget::DictGroupWidget( QWidget * parent, groupId( group.id ) { ui.setupUi( this ); - ui.dictionaries->populate( Instances::Group( group, dicts, Config::Group() ).dictionaries, dicts ); + auto dictMap = Dictionary::dictToMap(dicts); + ui.dictionaries->populate( Instances::Group( group, dictMap, Config::Group() ).dictionaries, dicts ); // Populate icons' list @@ -328,7 +329,7 @@ bool DictListModel::setData( QModelIndex const & index, const QVariant & value, g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) ); - Instances::Group i( g, *allDicts, Config::Group() ); + Instances::Group i( g, Dictionary::dictToMap(*allDicts), Config::Group() ); if ( i.dictionaries.size() == 1 ) { @@ -584,8 +585,7 @@ void DictGroupsWidget::populate( Config::Groups const & groups, { DictGroupWidget *gr = new DictGroupWidget( this, *allDicts, groups[ x ] ); addTab( gr, escapeAmps( groups[ x ].name ) ); - connect( gr, SIGNAL( showDictionaryInfo( QString const & ) ), - this, SIGNAL( showDictionaryInfo( QString const & ) ) ); +// connect( gr, &DictGroupWidget::showDictionaryInfo,this, &DictGroupsWidget::showDictionaryInfo ); connect( gr->getModel(), SIGNAL( contentChanged() ), this, SLOT( tabDataChanged() ) ); setCurrentIndex( x ); diff --git a/instances.cc b/instances.cc index fda59f22..30a12407 100644 --- a/instances.cc +++ b/instances.cc @@ -11,7 +11,7 @@ using std::set; using std::string; Group::Group( Config::Group const & cfgGroup, - vector< sptr< Dictionary::Class > > const & allDictionaries, + QMap > const & allDictionaries, Config::Group const & inactiveGroup ): id( cfgGroup.id ), name( cfgGroup.name ), @@ -22,70 +22,29 @@ Group::Group( Config::Group const & cfgGroup, if ( !cfgGroup.iconData.isEmpty() ) iconData = iconFromData( cfgGroup.iconData ); - vector< sptr< Dictionary::Class > > groupDicts; + QMap > groupDicts; for( unsigned x = 0; x < (unsigned)cfgGroup.dictionaries.size(); ++x ) { std::string id = cfgGroup.dictionaries[ x ].id.toStdString(); - bool added = false; - - for( unsigned y = allDictionaries.size(); y--; ) - if ( allDictionaries[ y ]->getId() == id ) - { - groupDicts.push_back( allDictionaries[ y ] ); - added = true; - break; - } - - if ( !added ) - { - // Try matching by name instead - QString qname = cfgGroup.dictionaries[ x ].name; - std::string name = qname.toUtf8().data(); - - if ( !qname.isEmpty() ) - { - - // To avoid duplicates in dictionaries list we don't add dictionary - // if it with such name was already added or presented in rest of list - - unsigned n; - for( n = 0; n < groupDicts.size(); n++ ) - if( groupDicts[ n ]->getName() == name ) - break; - if( n < groupDicts.size() ) - continue; - - for( n = x + 1; n < (unsigned)cfgGroup.dictionaries.size(); n++ ) - if( cfgGroup.dictionaries[ n ].name == qname ) - break; - if( n < (unsigned)cfgGroup.dictionaries.size() ) - continue; - - for( unsigned y = 0; y < allDictionaries.size(); ++y ) - if ( allDictionaries[ y ]->getName() == name ) - { - groupDicts.push_back( allDictionaries[ y ] ); - break; - } - } + if(allDictionaries.contains(id)){ + groupDicts.insert(id, allDictionaries[ id ] ); } } // Remove inactive dictionaries - - if( inactiveGroup.dictionaries.isEmpty() ) - dictionaries = groupDicts; - else + if( !inactiveGroup.dictionaries.isEmpty() ) { set< string > inactiveSet; for( int i = 0; i < inactiveGroup.dictionaries.size(); i++ ) - inactiveSet.insert( inactiveGroup.dictionaries[ i ].id.toStdString() ); - - for( unsigned i = 0; i < groupDicts.size(); i++ ) - if( inactiveSet.find( groupDicts[ i ]->getId() ) == inactiveSet.end() ) - dictionaries.push_back( groupDicts[ i ] ); + { + groupDicts.remove(inactiveGroup.dictionaries[ i ].id.toStdString()); + } + } + for(const auto & dict : groupDicts) + { + dictionaries.push_back(dict); } } diff --git a/instances.hh b/instances.hh index f4ac1d57..1ce40b18 100644 --- a/instances.hh +++ b/instances.hh @@ -29,7 +29,7 @@ struct Group /// Instantiates the given group from its configuration. If some dictionary /// wasn't found, it just skips it. Group( Config::Group const & cfgGroup, - vector< sptr< Dictionary::Class > > const & allDictionaries, + QMap > const & allDictionaries, Config::Group const & inactiveGroup ); /// Creates an empty group. diff --git a/mainwindow.cc b/mainwindow.cc index 13b72033..7b2dbd18 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -1345,6 +1345,9 @@ void MainWindow::makeDictionaries() loadDictionaries( this, isVisible(), cfg, dictionaries, dictNetMgr, false ); + //create map + dictMap = Dictionary::dictToMap(dictionaries); + for( unsigned x = 0; x < dictionaries.size(); x++ ) { dictionaries[ x ]->setFTSParameters( cfg.preferences.fts ); @@ -1390,11 +1393,11 @@ void MainWindow::updateGroupList() // Add dictionaryOrder first, as the 'All' group. { - Instances::Group g( cfg.dictionaryOrder, dictionaries, Config::Group() ); + Instances::Group g( cfg.dictionaryOrder, dictMap, Config::Group() ); // Add any missing entries to dictionary order Instances::complementDictionaryOrder( g, - Instances::Group( cfg.inactiveDictionaries, dictionaries, Config::Group() ), + Instances::Group( cfg.inactiveDictionaries, dictMap, Config::Group() ), dictionaries ); g.name = tr( "All" ); @@ -1405,7 +1408,7 @@ void MainWindow::updateGroupList() } for( int x = 0; x < cfg.groups.size(); ++x ) - groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictionaries, cfg.inactiveDictionaries ) ); + groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictMap, cfg.inactiveDictionaries ) ); // Update names for dictionaries that are present, so that they could be // found in case they got moved. @@ -2051,7 +2054,7 @@ void MainWindow::editDictionaries( unsigned editDictionaryGroup ) { // Limit existence of newCfg Config::Class newCfg = cfg; - EditDictionaries dicts( this, newCfg, dictionaries, groupInstances, dictNetMgr ); + EditDictionaries dicts( this, newCfg, dictionaries, dictMap, groupInstances, dictNetMgr ); connect( &dicts, SIGNAL( showDictionaryInfo( QString const & ) ), this, SLOT( showDictionaryInfo( QString const & ) ) ); diff --git a/mainwindow.hh b/mainwindow.hh index 3c012630..2c7c075b 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -151,6 +151,7 @@ private: History history; DictionaryBar dictionaryBar; vector< sptr< Dictionary::Class > > dictionaries; + QMap > dictMap; /// Here we store unmuted dictionaries when the dictionary bar is active vector< sptr< Dictionary::Class > > dictionariesUnmuted; Instances::Groups groupInstances; diff --git a/orderandprops.cc b/orderandprops.cc index 322e4f8d..863d596c 100644 --- a/orderandprops.cc +++ b/orderandprops.cc @@ -86,13 +86,14 @@ OrderAndProps::OrderAndProps( QWidget * parent, Config::Group const & dictionaryOrder, Config::Group const & inactiveDictionaries, std::vector< sptr< Dictionary::Class > > const & - allDictionaries ): + allDictionaries , + QMap > const & dictMap): QWidget( parent ) { ui.setupUi( this ); - Instances::Group order( dictionaryOrder, allDictionaries, Config::Group() ); - Instances::Group inactive( inactiveDictionaries, allDictionaries, Config::Group() ); + Instances::Group order( dictionaryOrder, dictMap, Config::Group() ); + Instances::Group inactive( inactiveDictionaries, dictMap, Config::Group() ); Instances::complementDictionaryOrder( order, inactive, allDictionaries ); diff --git a/orderandprops.hh b/orderandprops.hh index 79c49877..61be3ff2 100644 --- a/orderandprops.hh +++ b/orderandprops.hh @@ -16,7 +16,8 @@ public: OrderAndProps( QWidget * parent, Config::Group const & dictionaryOrder, Config::Group const & inactiveDictionaries, - std::vector< sptr< Dictionary::Class > > const & allDictionaries ); + std::vector< sptr< Dictionary::Class > > const & allDictionaries , + QMap > const & dictMap); Config::Group getCurrentDictionaryOrder() const; Config::Group getCurrentInactiveDictionaries() const;