Skip to content

Commit

Permalink
opt: support UP/Down arrow key in headword UI (#1358)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jan 22, 2024
1 parent 2877e1b commit c15cbbf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/instances.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "instances.hh"
#include <set>
#include <QBuffer>
#include <utility>

namespace Instances {

Expand All @@ -27,7 +28,7 @@ Group::Group( Config::Group const & cfgGroup,
auto dictMap = Dictionary::dictToMap( allDictionaries );

for ( auto const & dict : cfgGroup.dictionaries ) {
std::string dictId = dict.id.toStdString();
std::string const dictId = dict.id.toStdString();

if ( dictMap.contains( dictId ) ) {
groupDicts.insert( dictId, dictMap[ dictId ] );
Expand All @@ -37,9 +38,8 @@ Group::Group( Config::Group const & cfgGroup,

// Remove inactive dictionaries
if ( !inactiveGroup.dictionaries.isEmpty() ) {
set< string, std::less<> > inactiveSet;
for ( auto const & dict : inactiveGroup.dictionaries ) {
string dictId = dict.id.toStdString();
string const dictId = dict.id.toStdString();
groupDicts.remove( dictId );
dictOrderList.removeOne( dictId );
}
Expand All @@ -51,15 +51,15 @@ Group::Group( Config::Group const & cfgGroup,
}
}

Group::Group( QString const & name_ ):
Group::Group( QString name_ ):
id( 0 ),
name( name_ )
name( std::move( name_ ) )
{
}

Group::Group( unsigned id_, QString const & name_ ):
Group::Group( unsigned id_, QString name_ ):
id( id_ ),
name( name_ )
name( std::move( name_ ) )
{
}

Expand Down Expand Up @@ -102,9 +102,9 @@ void Group::checkMutedDictionaries( Config::MutedDictionaries * mutedDictionarie
Config::MutedDictionaries temp;

for ( auto const & dict : dictionaries ) {
QString id = QString::fromStdString( dict->getId() );
if ( mutedDictionaries->contains( id ) )
temp.insert( id );
auto dictId = QString::fromStdString( dict->getId() );
if ( mutedDictionaries->contains( dictId ) )
temp.insert( dictId );
}
*mutedDictionaries = temp;
}
Expand Down Expand Up @@ -139,17 +139,17 @@ void complementDictionaryOrder( Group & group,
for ( unsigned x = inactiveDictionaries.dictionaries.size(); x--; )
presentIds.insert( inactiveDictionaries.dictionaries[ x ]->getId() );

for ( unsigned x = 0; x < dicts.size(); ++x ) {
if ( presentIds.find( dicts[ x ]->getId() ) == presentIds.end() )
group.dictionaries.push_back( dicts[ x ] );
for ( const auto & dict : dicts ) {
if ( presentIds.find( dict->getId() ) == presentIds.end() )
group.dictionaries.push_back( dict );
}
}

void updateNames( Config::Group & group, vector< sptr< Dictionary::Class > > const & allDictionaries )
{

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--; )
if ( allDictionaries[ y ]->getId() == id ) {
Expand All @@ -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 )
{
for ( int x = 0; x < groups.size(); ++x )
updateNames( groups[ x ], allDictionaries );
for ( auto & group : groups )
updateNames( group, allDictionaries );
}

void updateNames( Config::Class & cfg, vector< sptr< Dictionary::Class > > const & allDictionaries )
Expand Down
4 changes: 2 additions & 2 deletions src/instances.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ struct Group
Config::Group const & inactiveGroup );

/// 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.
Config::Group makeConfigGroup();
Expand Down
30 changes: 23 additions & 7 deletions src/ui/dictheadwords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,28 @@ void DictHeadwords::savePos()
bool DictHeadwords::eventFilter( QObject * obj, QEvent * ev )
{
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 ) {
itemClicked( ui.headersListView->currentIndex() );
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 );
}
Expand Down Expand Up @@ -278,12 +295,11 @@ void DictHeadwords::showHeadwordsNumber()
.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 )
{
const int headwordsNumber = model->totalCount();

QMutexLocker _( &mutex );
QMutexLocker const _( &mutex );
if ( sortedWords.isEmpty() ) {
QSet< QString > allHeadwords;

Expand Down Expand Up @@ -330,10 +346,10 @@ void DictHeadwords::saveHeadersToFile()
exportPath = QDir::homePath();
}

QString fileName = QFileDialog::getSaveFileName( this,
tr( "Save headwords to file" ),
exportPath,
tr( "Text files (*.txt);;All files (*.*)" ) );
QString const fileName = QFileDialog::getSaveFileName( this,
tr( "Save headwords to file" ),
exportPath,
tr( "Text files (*.txt);;All files (*.*)" ) );
if ( fileName.size() == 0 )
return;

Expand Down
11 changes: 8 additions & 3 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,10 @@ void MainWindow::translateInputFinished( bool 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() ) {
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
Expand All @@ -2436,7 +2439,9 @@ void MainWindow::respondToTranslationRequest( QString const & word, bool checkMo
activateWindow();
}

focusArticleView();
if ( focus ) {
focusArticleView();
}
}
}

Expand Down Expand Up @@ -3618,7 +3623,7 @@ void MainWindow::headwordReceived( const QString & word, const QString & ID )
{
toggleMainWindow( true );
setInputLineText( word, WildcardPolicy::EscapeWildcards, NoPopupChange );
respondToTranslationRequest( word, false, ArticleView::scrollToFromDictionaryId( ID ) );
respondToTranslationRequest( word, false, ArticleView::scrollToFromDictionaryId( ID ), false );
}

void MainWindow::updateFavoritesMenu()
Expand Down
5 changes: 4 additions & 1 deletion src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ private:

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( QString const & text );
Expand Down

0 comments on commit c15cbbf

Please sign in to comment.