Extract duplicated "gdfrom-" string manipulation into functions

Such helper functions facilitate understanding of the code and simplify
implementing new features.
This commit is contained in:
Igor Kushnir 2021-06-29 11:59:16 +03:00
parent bc7fe1a3a8
commit f919685797
5 changed files with 61 additions and 30 deletions

View file

@ -183,6 +183,30 @@ static QVariant evaluateJavaScriptVariableSafe( QWebFrame * frame, const QString
.arg( variable ) ); .arg( variable ) );
} }
namespace {
char const * const scrollToPrefix = "gdfrom-";
bool isScrollTo( QString const & id )
{
return id.startsWith( scrollToPrefix );
}
QString dictionaryIdFromScrollTo( QString const & scrollTo )
{
Q_ASSERT( isScrollTo( scrollTo ) );
const int scrollToPrefixLength = 7;
return scrollTo.mid( scrollToPrefixLength );
}
} // unnamed namespace
QString ArticleView::scrollToFromDictionaryId( QString const & dictionaryId )
{
Q_ASSERT( !isScrollTo( dictionaryId ) );
return scrollToPrefix + dictionaryId;
}
ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
AudioPlayerPtr const & audioPlayer_, AudioPlayerPtr const & audioPlayer_,
std::vector< sptr< Dictionary::Class > > const & allDictionaries_, std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
@ -550,11 +574,14 @@ void ArticleView::loadFinished( bool )
} }
} }
else else
if ( Qt4x5::Url::queryItemValue( url, "scrollto" ).startsWith( "gdfrom-" ) )
{ {
// There is no active article saved in history, but we have it as a parameter. QString const scrollTo = Qt4x5::Url::queryItemValue( url, "scrollto" );
// setCurrentArticle will save it and scroll there. if( isScrollTo( scrollTo ) )
setCurrentArticle( Qt4x5::Url::queryItemValue( url, "scrollto" ), true ); {
// There is no active article saved in history, but we have it as a parameter.
// setCurrentArticle will save it and scroll there.
setCurrentArticle( scrollTo, true );
}
} }
@ -675,10 +702,10 @@ QStringList ArticleView::getArticlesList()
QString ArticleView::getActiveArticleId() QString ArticleView::getActiveArticleId()
{ {
QString currentArticle = getCurrentArticle(); QString currentArticle = getCurrentArticle();
if ( !currentArticle.startsWith( "gdfrom-" ) ) if ( !isScrollTo( currentArticle ) )
return QString(); // Incorrect id return QString(); // Incorrect id
return currentArticle.mid( 7 ); return dictionaryIdFromScrollTo( currentArticle );
} }
QString ArticleView::getCurrentArticle() QString ArticleView::getCurrentArticle()
@ -693,7 +720,7 @@ QString ArticleView::getCurrentArticle()
void ArticleView::jumpToDictionary( QString const & id, bool force ) void ArticleView::jumpToDictionary( QString const & id, bool force )
{ {
QString targetArticle = "gdfrom-" + id; QString targetArticle = scrollToFromDictionaryId( id );
// jump only if neceessary, or when forced // jump only if neceessary, or when forced
if ( force || targetArticle != getCurrentArticle() ) if ( force || targetArticle != getCurrentArticle() )
@ -704,13 +731,14 @@ void ArticleView::jumpToDictionary( QString const & id, bool force )
void ArticleView::setCurrentArticle( QString const & id, bool moveToIt ) void ArticleView::setCurrentArticle( QString const & id, bool moveToIt )
{ {
if ( !id.startsWith( "gdfrom-" ) ) if ( !isScrollTo( id ) )
return; // Incorrect id return; // Incorrect id
if ( !ui.definition->isVisible() ) if ( !ui.definition->isVisible() )
return; // No action on background page, scrollIntoView there don't work return; // No action on background page, scrollIntoView there don't work
if ( getArticlesList().contains( id.mid( 7 ) ) ) QString const dictionaryId = dictionaryIdFromScrollTo( id );
if ( getArticlesList().contains( dictionaryId ) )
{ {
if ( moveToIt ) if ( moveToIt )
ui.definition->page()->mainFrame()->evaluateJavaScript( QString( "document.getElementById('%1').scrollIntoView(true);" ).arg( id ) ); ui.definition->page()->mainFrame()->evaluateJavaScript( QString( "document.getElementById('%1').scrollIntoView(true);" ).arg( id ) );
@ -721,7 +749,7 @@ void ArticleView::setCurrentArticle( QString const & id, bool moveToIt )
ui.definition->history()->currentItem().setUserData( userData ); ui.definition->history()->currentItem().setUserData( userData );
ui.definition->page()->mainFrame()->evaluateJavaScript( ui.definition->page()->mainFrame()->evaluateJavaScript(
QString( "gdMakeArticleActive( '%1' );" ).arg( id.mid( 7 ) ) ); QString( "gdMakeArticleActive( '%1' );" ).arg( dictionaryId ) );
} }
} }
@ -737,7 +765,8 @@ bool ArticleView::isFramedArticle( QString const & ca )
return false; return false;
return ui.definition->page()->mainFrame()-> return ui.definition->page()->mainFrame()->
evaluateJavaScript( QString( "!!document.getElementById('gdexpandframe-%1');" ).arg( ca.mid( 7 ) ) ).toBool(); evaluateJavaScript( QString( "!!document.getElementById('gdexpandframe-%1');" )
.arg( dictionaryIdFromScrollTo( ca ) ) ).toBool();
} }
bool ArticleView::isExternalLink( QUrl const & url ) bool ArticleView::isExternalLink( QUrl const & url )
@ -765,8 +794,7 @@ void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts )
if ( result.type() == QVariant::String ) if ( result.type() == QVariant::String )
{ {
// Looks this way // Looks this way
contexts[ dictionaryIdFromScrollTo( ca ) ] = QString::fromLatin1( url.toEncoded() );
contexts[ ca.mid( 7 ) ] = QString::fromLatin1( url.toEncoded() );
QUrl target; QUrl target;
@ -798,7 +826,7 @@ void ArticleView::updateCurrentArticleFromCurrentFrame( QWebFrame * frame )
if ( frameName.startsWith( "gdexpandframe-" ) ) if ( frameName.startsWith( "gdexpandframe-" ) )
{ {
QString newCurrent = "gdfrom-" + frameName.mid( 14 ); QString newCurrent = scrollToFromDictionaryId( frameName.mid( 14 ) );
if ( getCurrentArticle() != newCurrent ) if ( getCurrentArticle() != newCurrent )
setCurrentArticle( newCurrent, false ); setCurrentArticle( newCurrent, false );
@ -1190,7 +1218,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref,
{ {
if( dictName.compare( QString::fromUtf8( allDictionaries[ i ]->getName().c_str() ) ) == 0 ) if( dictName.compare( QString::fromUtf8( allDictionaries[ i ]->getName().c_str() ) ) == 0 )
{ {
newScrollTo = QString( "gdfrom-" ) + QString::fromUtf8( allDictionaries[ i ]->getId().c_str() ); newScrollTo = scrollToFromDictionaryId( QString::fromUtf8( allDictionaries[ i ]->getId().c_str() ) );
break; break;
} }
} }
@ -2004,7 +2032,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
QString id = tableOfContents[ result ]; QString id = tableOfContents[ result ];
if ( id.size() ) if ( id.size() )
setCurrentArticle( "gdfrom-" + id, true ); setCurrentArticle( scrollToFromDictionaryId( id ), true );
} }
} }
#if 0 #if 0
@ -2124,7 +2152,7 @@ void ArticleView::moveOneArticleUp()
{ {
QStringList lst = getArticlesList(); QStringList lst = getArticlesList();
int idx = lst.indexOf( current.mid( 7 ) ); int idx = lst.indexOf( dictionaryIdFromScrollTo( current ) );
if ( idx != -1 ) if ( idx != -1 )
{ {
@ -2133,7 +2161,7 @@ void ArticleView::moveOneArticleUp()
if ( idx < 0 ) if ( idx < 0 )
idx = lst.size() - 1; idx = lst.size() - 1;
setCurrentArticle( "gdfrom-" + lst[ idx ], true ); setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
} }
} }
} }
@ -2146,13 +2174,13 @@ void ArticleView::moveOneArticleDown()
{ {
QStringList lst = getArticlesList(); QStringList lst = getArticlesList();
int idx = lst.indexOf( current.mid( 7 ) ); int idx = lst.indexOf( dictionaryIdFromScrollTo( current ) );
if ( idx != -1 ) if ( idx != -1 )
{ {
idx = ( idx + 1 ) % lst.size(); idx = ( idx + 1 ) % lst.size();
setCurrentArticle( "gdfrom-" + lst[ idx ], true ); setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
} }
} }
} }
@ -2230,10 +2258,10 @@ void ArticleView::on_highlightAllButton_clicked()
void ArticleView::onJsActiveArticleChanged(QString const & id) void ArticleView::onJsActiveArticleChanged(QString const & id)
{ {
if ( !id.startsWith( "gdfrom-" ) ) if ( !isScrollTo( id ) )
return; // Incorrect id return; // Incorrect id
emit activeArticleChanged( this, id.mid( 7 ) ); emit activeArticleChanged( this, dictionaryIdFromScrollTo( id ) );
} }
void ArticleView::doubleClicked( QPoint pos ) void ArticleView::doubleClicked( QPoint pos )

View file

@ -90,6 +90,9 @@ public:
typedef QMap< QString, QString > Contexts; typedef QMap< QString, QString > Contexts;
/// Returns "gdfrom-" + dictionaryId.
static QString scrollToFromDictionaryId( QString const & dictionaryId );
/// Shows the definition of the given word with the given group. /// Shows the definition of the given word with the given group.
/// scrollTo can be optionally set to a "gdfrom-xxxx" identifier to position /// scrollTo can be optionally set to a "gdfrom-xxxx" identifier to position
/// the page to that article on load. /// the page to that article on load.

View file

@ -2330,7 +2330,7 @@ void MainWindow::translateInputFinished( bool checkModifiers )
} }
void MainWindow::respondToTranslationRequest( Config::InputPhrase const & phrase, void MainWindow::respondToTranslationRequest( Config::InputPhrase const & phrase,
bool checkModifiers, QString const & dictID ) bool checkModifiers, QString const & scrollTo )
{ {
if ( phrase.isValid() ) if ( phrase.isValid() )
{ {
@ -2338,7 +2338,7 @@ void MainWindow::respondToTranslationRequest( Config::InputPhrase const & phrase
if ( checkModifiers && ( mods & (Qt::ControlModifier | Qt::ShiftModifier) ) ) if ( checkModifiers && ( mods & (Qt::ControlModifier | Qt::ShiftModifier) ) )
addNewTab(); addNewTab();
showTranslationFor( phrase, 0, dictID ); showTranslationFor( phrase, 0, scrollTo );
if ( cfg.preferences.searchInDock ) if ( cfg.preferences.searchInDock )
{ {
@ -2779,7 +2779,7 @@ void MainWindow::showHistoryItem( QString const & word )
void MainWindow::showTranslationFor( Config::InputPhrase const & phrase, void MainWindow::showTranslationFor( Config::InputPhrase const & phrase,
unsigned inGroup, unsigned inGroup,
QString const & dictID ) QString const & scrollTo )
{ {
ArticleView *view = getCurrentArticleView(); ArticleView *view = getCurrentArticleView();
@ -2789,7 +2789,7 @@ void MainWindow::showTranslationFor( Config::InputPhrase const & phrase,
( groupInstances.empty() ? 0 : ( groupInstances.empty() ? 0 :
groupInstances[ groupList->currentIndex() ].id ); groupInstances[ groupList->currentIndex() ].id );
view->showDefinition( phrase, group, dictID ); view->showDefinition( phrase, group, scrollTo );
updatePronounceAvailability(); updatePronounceAvailability();
updateFoundInDictsList(); updateFoundInDictsList();
@ -3906,7 +3906,7 @@ void MainWindow::headwordReceived( const QString & word, const QString & ID )
toggleMainWindow( true ); toggleMainWindow( true );
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( word ), NoPopupChange ); setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( word ), NoPopupChange );
respondToTranslationRequest( Config::InputPhrase::fromPhrase( word ), respondToTranslationRequest( Config::InputPhrase::fromPhrase( word ),
false, "gdfrom-" + ID ); false, ArticleView::scrollToFromDictionaryId( ID ) );
} }
void MainWindow::updateFavoritesMenu() void MainWindow::updateFavoritesMenu()

View file

@ -260,7 +260,7 @@ private:
QString unescapeTabHeader( QString const & header ); QString unescapeTabHeader( QString const & header );
void respondToTranslationRequest( Config::InputPhrase const & phrase, void respondToTranslationRequest( Config::InputPhrase const & phrase,
bool checkModifiers, QString const & dictID = QString() ); bool checkModifiers, QString const & scrollTo = QString() );
void updateSuggestionList(); void updateSuggestionList();
void updateSuggestionList( QString const & text ); void updateSuggestionList( QString const & text );
@ -406,7 +406,7 @@ private slots:
void mutedDictionariesChanged(); void mutedDictionariesChanged();
void showTranslationFor( Config::InputPhrase const &, unsigned inGroup = 0, void showTranslationFor( Config::InputPhrase const &, unsigned inGroup = 0,
QString const & dictID = QString() ); QString const & scrollTo = QString() );
void showTranslationFor( QString const & ); void showTranslationFor( QString const & );
void showTranslationFor( QString const &, QStringList const & dictIDs, void showTranslationFor( QString const &, QStringList const & dictIDs,

View file

@ -109,7 +109,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
for( QStringList::iterator i = availHelps.begin(); i != availHelps.end(); ++i ) for( QStringList::iterator i = availHelps.begin(); i != availHelps.end(); ++i )
{ {
QString loc = i->mid( 7, i->length() - 11 ); QString loc = i->mid( 7, i->length() - 11 ); // e.g. *i == "gdhelp_en.qch" => loc == "en"
QString lang = loc.mid( 0, 2 ); QString lang = loc.mid( 0, 2 );
QString reg; QString reg;
if(loc.length() >= 5 ) if(loc.length() >= 5 )