Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to avoid auto-scrolling out of top dictionary #1114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions articleview.cc
Expand Up @@ -217,6 +217,14 @@ QString dictionaryIdFromScrollTo( QString const & scrollTo )
return scrollTo.mid( scrollToPrefixLength );
}

QString scrollToFromUrl( QUrl const & url )
{
QString value = Qt4x5::Url::queryItemValue( url, "scrollto" );
if( isScrollTo( value ) )
return value;
return QString();
}

QString searchStatusMessageNoMatches()
{
return ArticleView::tr( "Phrase not found" );
Expand Down Expand Up @@ -261,6 +269,7 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
goForwardAction( this ),
selectCurrentArticleAction( this ),
copyAsTextAction( this ),
jumpToTargetArticleAction( this ),
inspectAction( this ),
openSearchAction( openSearchAction_ ),
searchIsOpened( false ),
Expand Down Expand Up @@ -349,6 +358,12 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
connect( &copyAsTextAction, SIGNAL( triggered() ),
this, SLOT( copyAsText() ) );

jumpToTargetArticleAction.setShortcut( QKeySequence( "Alt+T" ) );
jumpToTargetArticleAction.setText( tr( "Jump to Target Article" ) );
ui.definition->addAction( &jumpToTargetArticleAction );
connect( &jumpToTargetArticleAction, SIGNAL( triggered() ),
this, SLOT( jumpToTargetArticle() ) );

inspectAction.setShortcut( QKeySequence( Qt::Key_F12 ) );
inspectAction.setText( tr( "Inspect" ) );
ui.definition->addAction( &inspectAction );
Expand Down Expand Up @@ -615,9 +630,10 @@ void ArticleView::loadFinished( bool )
}
}
else
if( cfg.preferences.autoScrollToTargetArticle )
{
QString const scrollTo = Qt4x5::Url::queryItemValue( url, "scrollto" );
if( isScrollTo( scrollTo ) )
QString const scrollTo = scrollToFromUrl( url );
if( !scrollTo.isEmpty() )
{
// There is no active article saved in history, but we have it as a parameter.
// setCurrentArticle will save it and scroll there.
Expand Down Expand Up @@ -1928,10 +1944,15 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
menu.addAction( ui.definition->pageAction( QWebPage::SelectAll ) );
}

QStringList const ids = getArticlesList();

QString const scrollTo = scrollToFromUrl( ui.definition->url() );
if( !scrollTo.isEmpty() && ids.contains( dictionaryIdFromScrollTo( scrollTo ) ) )
menu.addAction( &jumpToTargetArticleAction );

map< QAction *, QString > tableOfContents;

// Add table of contents
QStringList ids = getArticlesList();

if ( !menu.isEmpty() && ids.size() )
menu.addSeparator();
Expand Down Expand Up @@ -2093,6 +2114,15 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
#endif
}

void ArticleView::jumpToTargetArticle()
{
QString const scrollTo = scrollToFromUrl( ui.definition->url() );
// When there is no "scrollto" query item in the current definition's URL
// and this slot's action is triggered via the shortcut - do nothing.
if( !scrollTo.isEmpty() )
setCurrentArticle( scrollTo, true );
}

void ArticleView::resourceDownloadFinished()
{
if ( resourceDownloadRequests.empty() )
Expand Down
4 changes: 3 additions & 1 deletion articleview.hh
Expand Up @@ -37,7 +37,7 @@ class ArticleView: public QFrame

QAction pasteAction, articleUpAction, articleDownAction,
goBackAction, goForwardAction, selectCurrentArticleAction,
copyAsTextAction, inspectAction;
copyAsTextAction, jumpToTargetArticleAction, inspectAction;
QAction & openSearchAction;
bool searchIsOpened;
bool expandOptionalParts;
Expand Down Expand Up @@ -273,6 +273,8 @@ private slots:
void linkHovered( const QString & link, const QString & title, const QString & textContent );
void contextMenuRequested( QPoint const & );

void jumpToTargetArticle();

void resourceDownloadFinished();

/// We handle pasting by attempting to define the word in clipboard.
Expand Down
8 changes: 8 additions & 0 deletions config.cc
Expand Up @@ -221,6 +221,7 @@ Preferences::Preferences():
autoStart( false ),
doubleClickTranslates( true ),
selectWordBySingleClick( false ),
autoScrollToTargetArticle( true ),
escKeyHidesMainWindow( false ),
alwaysOnTop ( false ),
searchInDock ( false ),
Expand Down Expand Up @@ -871,6 +872,9 @@ Class load() THROW_SPEC( exError )
if ( !preferences.namedItem( "selectWordBySingleClick" ).isNull() )
c.preferences.selectWordBySingleClick = ( preferences.namedItem( "selectWordBySingleClick" ).toElement().text() == "1" );

if ( !preferences.namedItem( "autoScrollToTargetArticle" ).isNull() )
c.preferences.autoScrollToTargetArticle = ( preferences.namedItem( "autoScrollToTargetArticle" ).toElement().text() == "1" );

if ( !preferences.namedItem( "escKeyHidesMainWindow" ).isNull() )
c.preferences.escKeyHidesMainWindow = ( preferences.namedItem( "escKeyHidesMainWindow" ).toElement().text() == "1" );

Expand Down Expand Up @@ -1741,6 +1745,10 @@ void save( Class const & c ) THROW_SPEC( exError )
opt.appendChild( dd.createTextNode( c.preferences.selectWordBySingleClick ? "1":"0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "autoScrollToTargetArticle" );
opt.appendChild( dd.createTextNode( c.preferences.autoScrollToTargetArticle ? "1":"0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "escKeyHidesMainWindow" );
opt.appendChild( dd.createTextNode( c.preferences.escKeyHidesMainWindow ? "1":"0" ) );
preferences.appendChild( opt );
Expand Down
1 change: 1 addition & 0 deletions config.hh
Expand Up @@ -293,6 +293,7 @@ struct Preferences
bool autoStart;
bool doubleClickTranslates;
bool selectWordBySingleClick;
bool autoScrollToTargetArticle;
bool escKeyHidesMainWindow;
bool alwaysOnTop;

Expand Down
2 changes: 2 additions & 0 deletions preferences.cc
Expand Up @@ -169,6 +169,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
ui.cbAutostart->setChecked( p.autoStart );
ui.doubleClickTranslates->setChecked( p.doubleClickTranslates );
ui.selectBySingleClick->setChecked( p.selectWordBySingleClick);
ui.autoScrollToTargetArticle->setChecked( p.autoScrollToTargetArticle );
ui.escKeyHidesMainWindow->setChecked( p.escKeyHidesMainWindow );

ui.enableMainWindowHotkey->setChecked( p.enableMainWindowHotkey );
Expand Down Expand Up @@ -387,6 +388,7 @@ Config::Preferences Preferences::getPreferences()
p.autoStart = ui.cbAutostart->isChecked();
p.doubleClickTranslates = ui.doubleClickTranslates->isChecked();
p.selectWordBySingleClick = ui.selectBySingleClick->isChecked();
p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked();
p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked();

p.enableMainWindowHotkey = ui.enableMainWindowHotkey->isChecked();
Expand Down
17 changes: 17 additions & 0 deletions preferences.ui
Expand Up @@ -369,6 +369,23 @@ be the last ones.</string>
</item>
</layout>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="autoScrollToTargetArticle">
<property name="toolTip">
<string>Normally, clicking on a link, double-clicking on a word or looking up
selection in an article loads the translation and almost immediately
scrolls to the article from the same dictionary. With this option off,
however, the article from the topmost dictionary is shown.
Once the translation is loaded, pressing Alt+T scrolls to the target article.</string>
</property>
<property name="text">
<string>Automatically scroll to target article</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">
Expand Down