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;
}
if( result && result == headwordsAction )
{
QString id = dictAction->data().toString();
emit showDictionaryHeadwords( id );
if ( result && result == headwordsAction ) {
std::string id = dictAction->data().toString().toStdString();
// TODO: use `Dictionary::class*` instead of `QString id` at action->setData to remove all similar `for` loops
for ( const auto & dict : allDictionaries ) {
if ( id == dict->getId() ) {
emit showDictionaryHeadwords( dict.get() );
break;
}
}
return;
}

View file

@ -40,7 +40,7 @@ signals:
void showDictionaryInfo( QString const & id );
/// 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
void openDictionaryFolder( QString const & id );

View file

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

View file

@ -544,10 +544,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
connect( &dictionaryBar, &DictionaryBar::showDictionaryInfo, this, &MainWindow::showDictionaryInfo );
connect( &dictionaryBar,
SIGNAL( showDictionaryHeadwords( QString const & ) ),
this,
SLOT( showDictionaryHeadwords( QString const & ) ) );
connect( &dictionaryBar,&DictionaryBar::showDictionaryHeadwords,this,&MainWindow::showDictionaryHeadwords);
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,
SIGNAL( showDictionaryHeadwords( QString const & ) ),
this,
SLOT( showDictionaryHeadwords( QString const & ) ) );
connect( &dicts, &EditDictionaries::showDictionaryHeadwords,this, &MainWindow::showDictionaryHeadwords);
if ( editDictionaryGroup != Instances::Group::NoGroupId )
dicts.editGroup( editDictionaryGroup );
@ -4199,7 +4193,7 @@ void MainWindow::showDictionaryInfo( const QString & id )
}
else if( result == DictInfo::SHOW_HEADWORDS )
{
showDictionaryHeadwords( owner, dictionaries[x].get() );
showDictionaryHeadwords( dictionaries[x].get() );
}
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 )
owner = qobject_cast< QWidget * >( sender() );
QWidget * owner = qobject_cast< QWidget * >( sender() );
if( owner == 0 )
owner = this;
// DictHeadwords internally check parent== mainwindow to know why it is requested.
// 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( dictionaries[ x ]->getId() == id.toUtf8().data() )
{
showDictionaryHeadwords( owner, dictionaries[ x ].get() );
break;
}
}
}
void MainWindow::showDictionaryHeadwords( QWidget * owner, Dictionary::Class * dict )
{
if( owner && owner != this )
{
if ( owner->objectName() == "EditDictionaries" ) {
DictHeadwords headwords( owner, cfg, dict );
headwords.exec();
return;
}
if( headwordsDlg == 0 )
{
else {
if ( headwordsDlg == nullptr ) {
headwordsDlg = new DictHeadwords( this, cfg, dict );
addGlobalActionsToDialog( headwordsDlg );
addGroupComboBoxActionsToDialog( headwordsDlg, groupList );
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->show();
}
}
void MainWindow::closeHeadwordsDialog()
{
if( headwordsDlg )
@ -4376,7 +4358,7 @@ void MainWindow::foundDictsContextMenuRequested( const QPoint &pos )
{
if ( scanPopup )
scanPopup->blockSignals( true );
showDictionaryHeadwords( this, pDict );
showDictionaryHeadwords( pDict );
if ( scanPopup )
scanPopup->blockSignals( false );
}

View file

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

View file

@ -279,7 +279,7 @@ void OrderAndProps::contextMenuRequested( const QPoint & pos )
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 & );
signals:
void showDictionaryHeadwords( QString const & dictId );
void showDictionaryHeadwords( Dictionary::Class * dict );
};
#endif