refactor: simplify MainWindow::showDictionaryHeadwords

* merge two methods into one
This commit is contained in:
shenleban tongying 2023-04-12 15:52:35 -04:00 committed by xiaoyifang
parent 2f4db6c34f
commit 86f1aeceb6
7 changed files with 39 additions and 54 deletions

View file

@ -177,10 +177,15 @@ void DictionaryBar::showContextMenu( QContextMenuEvent * event, bool extended )
return; return;
} }
if( result && result == headwordsAction ) if ( result && result == headwordsAction ) {
{ std::string id = dictAction->data().toString().toStdString();
QString id = dictAction->data().toString(); // TODO: use `Dictionary::class*` instead of `QString id` at action->setData to remove all similar `for` loops
emit showDictionaryHeadwords( id ); for ( const auto & dict : allDictionaries ) {
if ( id == dict->getId() ) {
emit showDictionaryHeadwords( dict.get() );
break;
}
}
return; return;
} }

View file

@ -40,7 +40,7 @@ signals:
void showDictionaryInfo( QString const & id ); void showDictionaryInfo( QString const & id );
/// Signal for show dictionary headwords command from context menu /// Signal for show dictionary headwords command from context menu
void showDictionaryHeadwords( QString const & id ); void showDictionaryHeadwords( Dictionary::Class * dict );
/// Signal for open dictionary folder from context menu /// Signal for open dictionary folder from context menu
void openDictionaryFolder( QString const & id ); void openDictionaryFolder( QString const & id );

View file

@ -62,7 +62,7 @@ signals:
void showDictionaryInfo( QString const & dictId ); void showDictionaryInfo( QString const & dictId );
void showDictionaryHeadwords( QString const & dictId ); void showDictionaryHeadwords( Dictionary::Class * dict );
private: private:

View file

@ -544,10 +544,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
connect( &dictionaryBar, &DictionaryBar::showDictionaryInfo, this, &MainWindow::showDictionaryInfo ); connect( &dictionaryBar, &DictionaryBar::showDictionaryInfo, this, &MainWindow::showDictionaryInfo );
connect( &dictionaryBar, connect( &dictionaryBar,&DictionaryBar::showDictionaryHeadwords,this,&MainWindow::showDictionaryHeadwords);
SIGNAL( showDictionaryHeadwords( QString const & ) ),
this,
SLOT( showDictionaryHeadwords( QString const & ) ) );
connect( &dictionaryBar, &DictionaryBar::openDictionaryFolder, this, &MainWindow::openDictionaryFolder ); connect( &dictionaryBar, &DictionaryBar::openDictionaryFolder, this, &MainWindow::openDictionaryFolder );
@ -2083,10 +2080,7 @@ void MainWindow::editDictionaries( unsigned editDictionaryGroup )
connect( &dicts, &EditDictionaries::showDictionaryInfo, this, &MainWindow::showDictionaryInfo ); connect( &dicts, &EditDictionaries::showDictionaryInfo, this, &MainWindow::showDictionaryInfo );
connect( &dicts, connect( &dicts, &EditDictionaries::showDictionaryHeadwords,this, &MainWindow::showDictionaryHeadwords);
SIGNAL( showDictionaryHeadwords( QString const & ) ),
this,
SLOT( showDictionaryHeadwords( QString const & ) ) );
if ( editDictionaryGroup != Instances::Group::NoGroupId ) if ( editDictionaryGroup != Instances::Group::NoGroupId )
dicts.editGroup( editDictionaryGroup ); dicts.editGroup( editDictionaryGroup );
@ -4199,7 +4193,7 @@ void MainWindow::showDictionaryInfo( const QString & id )
} }
else if( result == DictInfo::SHOW_HEADWORDS ) else if( result == DictInfo::SHOW_HEADWORDS )
{ {
showDictionaryHeadwords( owner, dictionaries[x].get() ); showDictionaryHeadwords( dictionaries[x].get() );
} }
break; break;
@ -4207,49 +4201,37 @@ void MainWindow::showDictionaryInfo( const QString & id )
} }
} }
void MainWindow::showDictionaryHeadwords( const QString & id ) void MainWindow::showDictionaryHeadwords( Dictionary::Class * dict )
{ {
QWidget * owner = 0;
if( sender()->objectName().compare( "EditDictionaries" ) == 0 ) QWidget * owner = qobject_cast< QWidget * >( sender() );
owner = qobject_cast< QWidget * >( sender() );
if( owner == 0 ) // DictHeadwords internally check parent== mainwindow to know why it is requested.
owner = this; // If the DictHeadwords is requested by Edit->Dictionaries->ShowHeadWords, (owner = "EditDictionaries")
// it will be a modal dialog. When click a word, that word will NOT be queried.
// In all other cases, just set owner = mainwindow(this),
for( unsigned x = 0; x < dictionaries.size(); x++ ) if ( owner->objectName() == "EditDictionaries" ) {
{
if( dictionaries[ x ]->getId() == id.toUtf8().data() )
{
showDictionaryHeadwords( owner, dictionaries[ x ].get() );
break;
}
}
}
void MainWindow::showDictionaryHeadwords( QWidget * owner, Dictionary::Class * dict )
{
if( owner && owner != this )
{
DictHeadwords headwords( owner, cfg, dict ); DictHeadwords headwords( owner, cfg, dict );
headwords.exec(); headwords.exec();
return;
} }
else {
if( headwordsDlg == 0 ) if ( headwordsDlg == nullptr ) {
{
headwordsDlg = new DictHeadwords( this, cfg, dict ); headwordsDlg = new DictHeadwords( this, cfg, dict );
addGlobalActionsToDialog( headwordsDlg ); addGlobalActionsToDialog( headwordsDlg );
addGroupComboBoxActionsToDialog( headwordsDlg, groupList ); addGroupComboBoxActionsToDialog( headwordsDlg, groupList );
connect( headwordsDlg, &DictHeadwords::headwordSelected, this, &MainWindow::headwordReceived ); connect( headwordsDlg, &DictHeadwords::headwordSelected, this, &MainWindow::headwordReceived );
connect( headwordsDlg, &DictHeadwords::closeDialog, this, &MainWindow::closeHeadwordsDialog, Qt::QueuedConnection ); connect( headwordsDlg, &DictHeadwords::closeDialog,
this, &MainWindow::closeHeadwordsDialog, Qt::QueuedConnection );
} }
else else{
headwordsDlg->setup( dict ); headwordsDlg->setup( dict );
}
headwordsDlg->show(); headwordsDlg->show();
}
} }
void MainWindow::closeHeadwordsDialog() void MainWindow::closeHeadwordsDialog()
{ {
if( headwordsDlg ) if( headwordsDlg )
@ -4376,7 +4358,7 @@ void MainWindow::foundDictsContextMenuRequested( const QPoint &pos )
{ {
if ( scanPopup ) if ( scanPopup )
scanPopup->blockSignals( true ); scanPopup->blockSignals( true );
showDictionaryHeadwords( this, pDict ); showDictionaryHeadwords( pDict );
if ( scanPopup ) if ( scanPopup )
scanPopup->blockSignals( false ); scanPopup->blockSignals( false );
} }

View file

@ -252,8 +252,6 @@ private:
void fillWordListFromHistory(); void fillWordListFromHistory();
void showDictionaryHeadwords( QWidget * owner, Dictionary::Class * dict );
QString unescapeTabHeader( QString const & header ); QString unescapeTabHeader( QString const & header );
void respondToTranslationRequest( Config::InputPhrase const & phrase, void respondToTranslationRequest( Config::InputPhrase const & phrase,
@ -294,7 +292,7 @@ private slots:
void showDictionaryInfo( QString const & id ); void showDictionaryInfo( QString const & id );
void showDictionaryHeadwords( QString const & id ); void showDictionaryHeadwords( Dictionary::Class * dict );
void openDictionaryFolder( QString const & id ); void openDictionaryFolder( QString const & id );

View file

@ -279,7 +279,7 @@ void OrderAndProps::contextMenuRequested( const QPoint & pos )
if( result && result == showHeadwordsAction ) if( result && result == showHeadwordsAction )
{ {
emit showDictionaryHeadwords( QString::fromUtf8( dict->getId().c_str() ) ); emit showDictionaryHeadwords( dict.get() );
} }
} }

View file

@ -38,7 +38,7 @@ private:
void describeDictionary( DictListWidget *, QModelIndex const & ); void describeDictionary( DictListWidget *, QModelIndex const & );
signals: signals:
void showDictionaryHeadwords( QString const & dictId ); void showDictionaryHeadwords( Dictionary::Class * dict );
}; };
#endif #endif