opt: support UP/Down arrow key in headword UI (#1358)

* opt:refactor the code

* opt: headwords dialog response to the Up/Down Key

* opt: when the headword come from headword dialog ,not focus

* [autofix.ci] apply automated fixes

---------

Co-authored-by: YiFang Xiao <yifang.xiao@noreply.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2024-01-22 17:04:30 +08:00 committed by GitHub
parent 2877e1bb0b
commit c15cbbf607
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 53 additions and 29 deletions

View file

@ -4,6 +4,7 @@
#include "instances.hh" #include "instances.hh"
#include <set> #include <set>
#include <QBuffer> #include <QBuffer>
#include <utility>
namespace Instances { namespace Instances {
@ -27,7 +28,7 @@ Group::Group( Config::Group const & cfgGroup,
auto dictMap = Dictionary::dictToMap( allDictionaries ); auto dictMap = Dictionary::dictToMap( allDictionaries );
for ( auto const & dict : cfgGroup.dictionaries ) { for ( auto const & dict : cfgGroup.dictionaries ) {
std::string dictId = dict.id.toStdString(); std::string const dictId = dict.id.toStdString();
if ( dictMap.contains( dictId ) ) { if ( dictMap.contains( dictId ) ) {
groupDicts.insert( dictId, dictMap[ dictId ] ); groupDicts.insert( dictId, dictMap[ dictId ] );
@ -37,9 +38,8 @@ Group::Group( Config::Group const & cfgGroup,
// Remove inactive dictionaries // Remove inactive dictionaries
if ( !inactiveGroup.dictionaries.isEmpty() ) { if ( !inactiveGroup.dictionaries.isEmpty() ) {
set< string, std::less<> > inactiveSet;
for ( auto const & dict : inactiveGroup.dictionaries ) { for ( auto const & dict : inactiveGroup.dictionaries ) {
string dictId = dict.id.toStdString(); string const dictId = dict.id.toStdString();
groupDicts.remove( dictId ); groupDicts.remove( dictId );
dictOrderList.removeOne( dictId ); dictOrderList.removeOne( dictId );
} }
@ -51,15 +51,15 @@ Group::Group( Config::Group const & cfgGroup,
} }
} }
Group::Group( QString const & name_ ): Group::Group( QString name_ ):
id( 0 ), id( 0 ),
name( name_ ) name( std::move( name_ ) )
{ {
} }
Group::Group( unsigned id_, QString const & name_ ): Group::Group( unsigned id_, QString name_ ):
id( id_ ), id( id_ ),
name( name_ ) name( std::move( name_ ) )
{ {
} }
@ -102,9 +102,9 @@ void Group::checkMutedDictionaries( Config::MutedDictionaries * mutedDictionarie
Config::MutedDictionaries temp; Config::MutedDictionaries temp;
for ( auto const & dict : dictionaries ) { for ( auto const & dict : dictionaries ) {
QString id = QString::fromStdString( dict->getId() ); auto dictId = QString::fromStdString( dict->getId() );
if ( mutedDictionaries->contains( id ) ) if ( mutedDictionaries->contains( dictId ) )
temp.insert( id ); temp.insert( dictId );
} }
*mutedDictionaries = temp; *mutedDictionaries = temp;
} }
@ -139,9 +139,9 @@ void complementDictionaryOrder( Group & group,
for ( unsigned x = inactiveDictionaries.dictionaries.size(); x--; ) for ( unsigned x = inactiveDictionaries.dictionaries.size(); x--; )
presentIds.insert( inactiveDictionaries.dictionaries[ x ]->getId() ); presentIds.insert( inactiveDictionaries.dictionaries[ x ]->getId() );
for ( unsigned x = 0; x < dicts.size(); ++x ) { for ( const auto & dict : dicts ) {
if ( presentIds.find( dicts[ x ]->getId() ) == presentIds.end() ) if ( presentIds.find( dict->getId() ) == presentIds.end() )
group.dictionaries.push_back( dicts[ x ] ); group.dictionaries.push_back( dict );
} }
} }
@ -149,7 +149,7 @@ void updateNames( Config::Group & group, vector< sptr< Dictionary::Class > > con
{ {
for ( unsigned x = group.dictionaries.size(); x--; ) { for ( unsigned x = group.dictionaries.size(); x--; ) {
std::string id = group.dictionaries[ x ].id.toStdString(); std::string const id = group.dictionaries[ x ].id.toStdString();
for ( unsigned y = allDictionaries.size(); y--; ) for ( unsigned y = allDictionaries.size(); y--; )
if ( allDictionaries[ y ]->getId() == id ) { if ( allDictionaries[ y ]->getId() == id ) {
@ -161,8 +161,8 @@ void updateNames( Config::Group & group, vector< sptr< Dictionary::Class > > con
void updateNames( Config::Groups & groups, vector< sptr< Dictionary::Class > > const & allDictionaries ) void updateNames( Config::Groups & groups, vector< sptr< Dictionary::Class > > const & allDictionaries )
{ {
for ( int x = 0; x < groups.size(); ++x ) for ( auto & group : groups )
updateNames( groups[ x ], allDictionaries ); updateNames( group, allDictionaries );
} }
void updateNames( Config::Class & cfg, vector< sptr< Dictionary::Class > > const & allDictionaries ) void updateNames( Config::Class & cfg, vector< sptr< Dictionary::Class > > const & allDictionaries )

View file

@ -33,9 +33,9 @@ struct Group
Config::Group const & inactiveGroup ); Config::Group const & inactiveGroup );
/// Creates an empty group. /// Creates an empty group.
explicit Group( QString const & name_ ); explicit Group( QString name_ );
Group( unsigned id, QString const & name_ ); Group( unsigned id, QString name_ );
/// Makes the configuration group from the current contents. /// Makes the configuration group from the current contents.
Config::Group makeConfigGroup(); Config::Group makeConfigGroup();

View file

@ -168,11 +168,28 @@ void DictHeadwords::savePos()
bool DictHeadwords::eventFilter( QObject * obj, QEvent * ev ) bool DictHeadwords::eventFilter( QObject * obj, QEvent * ev )
{ {
if ( obj == ui.headersListView && ev->type() == QEvent::KeyPress ) { if ( obj == ui.headersListView && ev->type() == QEvent::KeyPress ) {
QKeyEvent * kev = static_cast< QKeyEvent * >( ev ); auto * kev = dynamic_cast< QKeyEvent * >( ev );
if ( kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter ) { if ( kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter ) {
itemClicked( ui.headersListView->currentIndex() ); itemClicked( ui.headersListView->currentIndex() );
return true; return true;
} }
else if ( kev->key() == Qt::Key_Up ) {
auto index = ui.headersListView->currentIndex();
if ( index.row() == 0 )
return true;
auto preIndex = ui.headersListView->model()->index( index.row() - 1, index.column() );
ui.headersListView->setCurrentIndex( preIndex );
return true;
}
else if ( kev->key() == Qt::Key_Down ) {
auto index = ui.headersListView->currentIndex();
//last row.
if ( index.row() == ui.headersListView->model()->rowCount() - 1 )
return true;
auto preIndex = ui.headersListView->model()->index( index.row() + 1, index.column() );
ui.headersListView->setCurrentIndex( preIndex );
return true;
}
} }
return QDialog::eventFilter( obj, ev ); return QDialog::eventFilter( obj, ev );
} }
@ -278,12 +295,11 @@ void DictHeadwords::showHeadwordsNumber()
.arg( QString::number( model->totalCount() ), QString::number( proxy->rowCount() ) ) ); .arg( QString::number( model->totalCount() ), QString::number( proxy->rowCount() ) ) );
} }
// TODO , the ui and the code mixed together , this is not the right way to do this. need future refactor
void DictHeadwords::loadAllSortedWords( QProgressDialog & progress ) void DictHeadwords::loadAllSortedWords( QProgressDialog & progress )
{ {
const int headwordsNumber = model->totalCount(); const int headwordsNumber = model->totalCount();
QMutexLocker _( &mutex ); QMutexLocker const _( &mutex );
if ( sortedWords.isEmpty() ) { if ( sortedWords.isEmpty() ) {
QSet< QString > allHeadwords; QSet< QString > allHeadwords;
@ -330,7 +346,7 @@ void DictHeadwords::saveHeadersToFile()
exportPath = QDir::homePath(); exportPath = QDir::homePath();
} }
QString fileName = QFileDialog::getSaveFileName( this, QString const fileName = QFileDialog::getSaveFileName( this,
tr( "Save headwords to file" ), tr( "Save headwords to file" ),
exportPath, exportPath,
tr( "Text files (*.txt);;All files (*.*)" ) ); tr( "Text files (*.txt);;All files (*.*)" ) );

View file

@ -2422,7 +2422,10 @@ void MainWindow::translateInputFinished( bool checkModifiers )
respondToTranslationRequest( word, checkModifiers ); respondToTranslationRequest( word, checkModifiers );
} }
void MainWindow::respondToTranslationRequest( QString const & word, bool checkModifiers, QString const & scrollTo ) void MainWindow::respondToTranslationRequest( QString const & word,
bool checkModifiers,
QString const & scrollTo,
bool focus )
{ {
if ( !word.isEmpty() ) { if ( !word.isEmpty() ) {
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers(); Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
@ -2436,8 +2439,10 @@ void MainWindow::respondToTranslationRequest( QString const & word, bool checkMo
activateWindow(); activateWindow();
} }
if ( focus ) {
focusArticleView(); focusArticleView();
} }
}
} }
void MainWindow::setInputLineText( QString text, WildcardPolicy wildcardPolicy, TranslateBoxPopup popupAction ) void MainWindow::setInputLineText( QString text, WildcardPolicy wildcardPolicy, TranslateBoxPopup popupAction )
@ -3618,7 +3623,7 @@ void MainWindow::headwordReceived( const QString & word, const QString & ID )
{ {
toggleMainWindow( true ); toggleMainWindow( true );
setInputLineText( word, WildcardPolicy::EscapeWildcards, NoPopupChange ); setInputLineText( word, WildcardPolicy::EscapeWildcards, NoPopupChange );
respondToTranslationRequest( word, false, ArticleView::scrollToFromDictionaryId( ID ) ); respondToTranslationRequest( word, false, ArticleView::scrollToFromDictionaryId( ID ), false );
} }
void MainWindow::updateFavoritesMenu() void MainWindow::updateFavoritesMenu()

View file

@ -249,7 +249,10 @@ private:
QString unescapeTabHeader( QString const & header ); QString unescapeTabHeader( QString const & header );
void respondToTranslationRequest( QString const & word, bool checkModifiers, QString const & scrollTo = QString() ); void respondToTranslationRequest( QString const & word,
bool checkModifiers,
QString const & scrollTo = QString(),
bool focus = true );
void updateSuggestionList(); void updateSuggestionList();
void updateSuggestionList( QString const & text ); void updateSuggestionList( QString const & text );