2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "scanpopup.hh"
|
2021-06-30 15:54:35 +00:00
|
|
|
#include "folding.hh"
|
2009-02-01 00:08:08 +00:00
|
|
|
#include <QCursor>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QBitmap>
|
|
|
|
#include <QMenu>
|
2009-02-06 13:05:25 +00:00
|
|
|
#include <QMouseEvent>
|
2009-02-08 16:35:30 +00:00
|
|
|
#include <QDesktopWidget>
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2014-02-05 13:41:49 +00:00
|
|
|
#include "gestures.hh"
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2014-05-12 13:53:13 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2013-04-10 13:31:44 +00:00
|
|
|
#include "macmouseover.hh"
|
|
|
|
#define MouseOver MacMouseOver
|
|
|
|
#else
|
|
|
|
#include "mouseover.hh"
|
|
|
|
#endif
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
using std::wstring;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-01-28 12:19:11 +00:00
|
|
|
/// We use different window flags under Windows and X11 due to slight differences
|
|
|
|
/// in their behavior on those platforms.
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
static const Qt::WindowFlags defaultUnpinnedWindowFlags =
|
2013-01-28 12:19:11 +00:00
|
|
|
|
2014-10-03 11:30:49 +00:00
|
|
|
#if defined (Q_OS_WIN) || ( defined (Q_OS_MAC) && QT_VERSION < QT_VERSION_CHECK( 5, 3, 0 ) )
|
2013-01-28 12:19:11 +00:00
|
|
|
Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
|
|
|
|
#else
|
|
|
|
Qt::Popup
|
|
|
|
#endif
|
|
|
|
;
|
2010-01-02 12:51:53 +00:00
|
|
|
|
2019-05-11 10:55:52 +00:00
|
|
|
static const Qt::WindowFlags pinnedWindowFlags =
|
|
|
|
#ifdef HAVE_X11
|
|
|
|
/// With the Qt::Dialog flag, scan popup is always on top of the main window
|
|
|
|
/// on Linux/X11 with Qt 4, Qt 5 since version 5.12.1 (QTBUG-74309).
|
|
|
|
/// Qt::Window allows to use the scan popup and the main window independently.
|
|
|
|
Qt::Window
|
|
|
|
#else
|
|
|
|
Qt::Dialog
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2018-04-17 17:52:39 +00:00
|
|
|
#ifdef HAVE_X11
|
2018-04-16 12:23:22 +00:00
|
|
|
static bool ownsClipboardMode( QClipboard::Mode mode )
|
|
|
|
{
|
|
|
|
const QClipboard & clipboard = *QApplication::clipboard();
|
|
|
|
switch( mode )
|
|
|
|
{
|
|
|
|
case QClipboard::Clipboard:
|
|
|
|
return clipboard.ownsClipboard();
|
|
|
|
case QClipboard::Selection:
|
|
|
|
return clipboard.ownsSelection();
|
|
|
|
case QClipboard::FindBuffer:
|
|
|
|
return clipboard.ownsFindBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
gdWarning( "Unknown clipboard mode: %d\n", static_cast< int >( mode ) );
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-17 17:52:39 +00:00
|
|
|
#endif
|
2018-04-16 12:23:22 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
ScanPopup::ScanPopup( QWidget * parent,
|
2009-02-06 17:04:11 +00:00
|
|
|
Config::Class & cfg_,
|
2017-06-05 13:15:38 +00:00
|
|
|
ArticleNetworkAccessManager & articleNetMgr,
|
2018-03-21 17:49:34 +00:00
|
|
|
AudioPlayerPtr const & audioPlayer_,
|
2009-02-01 00:08:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
|
2009-10-21 19:37:07 +00:00
|
|
|
Instances::Groups const & groups_,
|
|
|
|
History & history_ ):
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow( parent ),
|
2009-02-05 20:55:00 +00:00
|
|
|
cfg( cfg_ ),
|
2009-02-08 20:20:02 +00:00
|
|
|
isScanningEnabled( false ),
|
2009-02-01 00:08:08 +00:00
|
|
|
allDictionaries( allDictionaries_ ),
|
|
|
|
groups( groups_ ),
|
2009-10-21 19:37:07 +00:00
|
|
|
history( history_ ),
|
2009-05-16 11:14:43 +00:00
|
|
|
escapeAction( this ),
|
2012-09-16 10:19:47 +00:00
|
|
|
switchExpandModeAction( this ),
|
2013-01-25 15:42:44 +00:00
|
|
|
focusTranslateLineAction( this ),
|
2014-04-16 16:18:28 +00:00
|
|
|
openSearchAction( this ),
|
2009-02-06 13:05:25 +00:00
|
|
|
wordFinder( this ),
|
2013-06-11 18:31:01 +00:00
|
|
|
dictionaryBar( this, configEvents, cfg.editDictionaryCommandLine, cfg.preferences.maxDictionaryRefsInContextMenu ),
|
2010-01-02 23:26:09 +00:00
|
|
|
mouseEnteredOnce( false ),
|
|
|
|
mouseIntercepted( false ),
|
2017-10-23 14:21:43 +00:00
|
|
|
hideTimer( this ),
|
|
|
|
starIcon( ":/icons/star.png" ),
|
|
|
|
blueStarIcon( ":/icons/star_blue.png" )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
openSearchAction.setShortcut( QKeySequence( "Ctrl+F" ) );
|
|
|
|
|
2014-03-20 13:44:46 +00:00
|
|
|
if( layoutDirection() == Qt::RightToLeft )
|
|
|
|
{
|
|
|
|
// Adjust button icons for Right-To-Left layout
|
|
|
|
ui.goBackButton->setIcon( QIcon( ":/icons/next.png" ) );
|
|
|
|
ui.goForwardButton->setIcon( QIcon( ":/icons/previous.png" ) );
|
|
|
|
}
|
|
|
|
|
2011-07-14 08:17:59 +00:00
|
|
|
mainStatusBar = new MainStatusBar( this );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
ui.queryError->hide();
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
definition = new ArticleView( ui.outerFrame, articleNetMgr, audioPlayer_,
|
|
|
|
allDictionaries, groups, true, cfg,
|
2014-04-16 16:18:28 +00:00
|
|
|
openSearchAction,
|
2012-09-26 13:13:47 +00:00
|
|
|
dictionaryBar.toggleViewAction()
|
|
|
|
);
|
2009-04-30 20:20:05 +00:00
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
connect( this, SIGNAL(switchExpandMode() ),
|
|
|
|
definition, SLOT( switchExpandOptionalParts() ) );
|
|
|
|
connect( this, SIGNAL(setViewExpandMode( bool ) ),
|
|
|
|
definition, SLOT( receiveExpandOptionalParts( bool ) ) );
|
|
|
|
connect( definition, SIGNAL( setExpandMode( bool ) ),
|
|
|
|
this, SIGNAL( setExpandMode( bool ) ) );
|
2012-09-16 10:30:14 +00:00
|
|
|
connect( definition, SIGNAL( forceAddWordToHistory( QString ) ),
|
|
|
|
this, SIGNAL( forceAddWordToHistory( QString ) ) );
|
2012-09-25 13:13:35 +00:00
|
|
|
connect( this, SIGNAL( closeMenu() ),
|
|
|
|
definition, SIGNAL( closePopupMenu() ) );
|
2012-11-12 13:52:54 +00:00
|
|
|
connect( definition, SIGNAL( sendWordToHistory( QString ) ),
|
|
|
|
this, SIGNAL( sendWordToHistory( QString ) ) );
|
2013-01-25 15:42:44 +00:00
|
|
|
connect( definition, SIGNAL( typingEvent( QString const & ) ),
|
|
|
|
this, SLOT( typingEvent( QString const & ) ) );
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2017-07-14 13:02:21 +00:00
|
|
|
wordListDefaultFont = ui.translateBox->wordList()->font();
|
|
|
|
translateLineDefaultFont = ui.translateBox->font();
|
2019-01-26 19:11:27 +00:00
|
|
|
groupListDefaultFont = ui.groupList->font();
|
2017-07-14 13:02:21 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
ui.mainLayout->addWidget( definition );
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
ui.translateBox->wordList()->attachFinder( &wordFinder );
|
2013-01-27 22:12:00 +00:00
|
|
|
ui.translateBox->wordList()->setFocusPolicy(Qt::ClickFocus);
|
2013-01-25 15:42:44 +00:00
|
|
|
ui.translateBox->translateLine()->installEventFilter( this );
|
|
|
|
|
|
|
|
connect( ui.translateBox->translateLine(), SIGNAL( textChanged( QString const & ) ),
|
|
|
|
this, SLOT( translateInputChanged( QString const & ) ) );
|
|
|
|
|
|
|
|
connect( ui.translateBox->translateLine(), SIGNAL( returnPressed() ),
|
|
|
|
this, SLOT( translateInputFinished() ) );
|
|
|
|
|
|
|
|
connect( ui.translateBox->wordList(), SIGNAL( itemClicked( QListWidgetItem * ) ),
|
|
|
|
this, SLOT( wordListItemActivated( QListWidgetItem * ) ) );
|
|
|
|
|
|
|
|
connect( ui.translateBox->wordList(), SIGNAL( itemDoubleClicked ( QListWidgetItem * ) ),
|
|
|
|
this, SLOT( wordListItemActivated( QListWidgetItem * ) ) );
|
|
|
|
|
|
|
|
connect( ui.translateBox->wordList(), SIGNAL( statusBarMessage( QString const &, int, QPixmap const & ) ),
|
|
|
|
this, SLOT( showStatusBarMessage( QString const &, int, QPixmap const & ) ) );
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
ui.pronounceButton->hide();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
ui.groupList->fill( groups );
|
2009-04-10 12:48:40 +00:00
|
|
|
ui.groupList->setCurrentGroup( cfg.lastPopupGroupId );
|
2009-02-06 17:04:11 +00:00
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
dictionaryBar.setFloatable( false );
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
Instances::Group const * igrp = groups.findGroup( cfg.lastPopupGroupId );
|
|
|
|
if( cfg.lastPopupGroupId == Instances::Group::AllGroupId )
|
|
|
|
{
|
|
|
|
if( igrp )
|
|
|
|
igrp->checkMutedDictionaries( &cfg.popupMutedDictionaries );
|
|
|
|
dictionaryBar.setMutedDictionaries( &cfg.popupMutedDictionaries );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Config::Group * grp = cfg.getGroup( cfg.lastPopupGroupId );
|
|
|
|
if( igrp && grp )
|
|
|
|
igrp->checkMutedDictionaries( &grp->popupMutedDictionaries );
|
|
|
|
dictionaryBar.setMutedDictionaries( grp ? &grp->popupMutedDictionaries : 0 );
|
|
|
|
}
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
addToolBar( Qt::RightToolBarArea, &dictionaryBar );
|
|
|
|
|
2010-05-08 14:01:59 +00:00
|
|
|
connect( &dictionaryBar, SIGNAL(editGroupRequested()),
|
|
|
|
this, SLOT(editGroupRequested()) );
|
2012-09-25 13:13:35 +00:00
|
|
|
connect( this, SIGNAL( closeMenu() ),
|
|
|
|
&dictionaryBar, SIGNAL( closePopupMenu() ) );
|
|
|
|
connect( &dictionaryBar, SIGNAL( showDictionaryInfo( QString const & ) ),
|
|
|
|
this, SIGNAL( showDictionaryInfo( QString const & ) ) );
|
2013-06-09 13:31:57 +00:00
|
|
|
connect( &dictionaryBar, SIGNAL( openDictionaryFolder( QString const & ) ),
|
|
|
|
this, SIGNAL( openDictionaryFolder( QString const & ) ) );
|
2010-05-08 14:01:59 +00:00
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
if ( cfg.popupWindowGeometry.size() )
|
|
|
|
restoreGeometry( cfg.popupWindowGeometry );
|
|
|
|
|
|
|
|
if ( cfg.popupWindowState.size() )
|
|
|
|
restoreState( cfg.popupWindowState, 1 );
|
|
|
|
|
2017-07-13 15:00:19 +00:00
|
|
|
ui.onTopButton->setChecked( cfg.popupWindowAlwaysOnTop );
|
|
|
|
ui.onTopButton->setVisible( cfg.pinPopupWindow );
|
|
|
|
connect( ui.onTopButton, SIGNAL( clicked( bool ) ), this, SLOT( alwaysOnTopClicked( bool ) ) );
|
|
|
|
|
2011-05-01 23:52:11 +00:00
|
|
|
ui.pinButton->setChecked( cfg.pinPopupWindow );
|
|
|
|
|
|
|
|
if ( cfg.pinPopupWindow )
|
|
|
|
{
|
|
|
|
dictionaryBar.setMovable( true );
|
2019-05-11 10:55:52 +00:00
|
|
|
Qt::WindowFlags flags = pinnedWindowFlags;
|
2017-07-13 15:00:19 +00:00
|
|
|
if( cfg.popupWindowAlwaysOnTop )
|
|
|
|
flags |= Qt::WindowStaysOnTopHint;
|
|
|
|
setWindowFlags( flags );
|
2011-05-01 23:52:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dictionaryBar.setMovable( false );
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
setWindowFlags( unpinnedWindowFlags() );
|
2011-05-01 23:52:11 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
connect( &configEvents, SIGNAL( mutedDictionariesChanged() ),
|
|
|
|
this, SLOT( mutedDictionariesChanged() ) );
|
2009-02-06 17:16:33 +00:00
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
definition->focus();
|
2009-02-06 13:05:25 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
#if 0 // Experimental code to give window a non-rectangular shape (i.e.
|
|
|
|
// balloon) using a colorkey mask.
|
|
|
|
QPixmap pixMask( size() );
|
|
|
|
render( &pixMask );
|
|
|
|
|
|
|
|
setMask( pixMask.createMaskFromColor( QColor( 255, 0, 0 ) ) );
|
|
|
|
|
|
|
|
// This helps against flickering
|
|
|
|
setAttribute( Qt::WA_NoSystemBackground );
|
|
|
|
#endif
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
escapeAction.setShortcut( QKeySequence( "Esc" ) );
|
|
|
|
addAction( &escapeAction );
|
|
|
|
connect( &escapeAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( escapePressed() ) );
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
focusTranslateLineAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
addAction( &focusTranslateLineAction );
|
|
|
|
focusTranslateLineAction.setShortcuts( QList< QKeySequence >() <<
|
|
|
|
QKeySequence( "Alt+D" ) <<
|
|
|
|
QKeySequence( "Ctrl+L" ) );
|
|
|
|
|
|
|
|
connect( &focusTranslateLineAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( focusTranslateLine() ) );
|
|
|
|
|
2018-05-16 11:48:27 +00:00
|
|
|
QAction * const focusArticleViewAction = new QAction( this );
|
|
|
|
focusArticleViewAction->setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
focusArticleViewAction->setShortcut( QKeySequence( "Ctrl+N" ) );
|
|
|
|
addAction( focusArticleViewAction );
|
|
|
|
connect( focusArticleViewAction, SIGNAL( triggered() ), definition, SLOT( focus() ) );
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
switchExpandModeAction.setShortcuts( QList< QKeySequence >() <<
|
|
|
|
QKeySequence( Qt::CTRL + Qt::Key_8 ) <<
|
|
|
|
QKeySequence( Qt::CTRL + Qt::Key_Asterisk ) <<
|
|
|
|
QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_8 ) );
|
|
|
|
|
|
|
|
addAction( &switchExpandModeAction );
|
|
|
|
connect( &switchExpandModeAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT(switchExpandOptionalPartsMode() ) );
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
connect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ),
|
|
|
|
this, SLOT( currentGroupChanged( QString const & ) ) );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
connect( &wordFinder, SIGNAL( finished() ),
|
|
|
|
this, SLOT( prefixMatchFinished() ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
connect( ui.pinButton, SIGNAL( clicked( bool ) ),
|
|
|
|
this, SLOT( pinButtonClicked( bool ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-14 19:42:04 +00:00
|
|
|
connect( definition, SIGNAL( pageLoaded( ArticleView * ) ),
|
|
|
|
this, SLOT( pageLoaded( ArticleView * ) ) );
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2011-07-14 20:11:57 +00:00
|
|
|
connect( definition, SIGNAL( statusBarMessage( QString const &, int, QPixmap const & ) ),
|
|
|
|
this, SLOT( showStatusBarMessage( QString const &, int, QPixmap const & ) ) );
|
2011-07-14 08:17:59 +00:00
|
|
|
|
2017-11-07 14:46:59 +00:00
|
|
|
connect( definition, SIGNAL( titleChanged( ArticleView *, QString const & ) ),
|
|
|
|
this, SLOT( titleChanged( ArticleView *, QString const & ) ) );
|
|
|
|
|
2013-05-31 05:28:36 +00:00
|
|
|
#ifdef HAVE_X11
|
2009-01-28 20:55:45 +00:00
|
|
|
connect( QApplication::clipboard(), SIGNAL( changed( QClipboard::Mode ) ),
|
|
|
|
this, SLOT( clipboardChanged( QClipboard::Mode ) ) );
|
2014-01-09 14:17:50 +00:00
|
|
|
#else
|
|
|
|
if( cfg.preferences.trackClipboardChanges )
|
|
|
|
connect( QApplication::clipboard(), SIGNAL( changed( QClipboard::Mode ) ),
|
|
|
|
this, SLOT( clipboardChanged( QClipboard::Mode ) ) );
|
2013-04-05 12:21:01 +00:00
|
|
|
#endif
|
2009-02-02 20:28:53 +00:00
|
|
|
|
2012-09-24 13:20:58 +00:00
|
|
|
connect( &MouseOver::instance(), SIGNAL( hovered( QString const &, bool ) ),
|
|
|
|
this, SLOT( mouseHovered( QString const &, bool ) ) );
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
connect( &MouseOver::instance(), SIGNAL( isGoldenDictWindow( HWND ) ),
|
|
|
|
this, SIGNAL( isGoldenDictWindow( HWND ) ) );
|
|
|
|
#endif
|
2009-04-10 13:56:38 +00:00
|
|
|
|
|
|
|
hideTimer.setSingleShot( true );
|
|
|
|
hideTimer.setInterval( 400 );
|
|
|
|
|
|
|
|
connect( &hideTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT( hideTimerExpired() ) );
|
2009-04-11 16:44:14 +00:00
|
|
|
|
|
|
|
altModeExpirationTimer.setSingleShot( true );
|
|
|
|
altModeExpirationTimer.setInterval( cfg.preferences.scanPopupAltModeSecs * 1000 );
|
|
|
|
|
|
|
|
connect( &altModeExpirationTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT( altModeExpired() ) );
|
|
|
|
|
|
|
|
// This one polls constantly for modifiers while alt mode lasts
|
|
|
|
altModePollingTimer.setSingleShot( false );
|
|
|
|
altModePollingTimer.setInterval( 50 );
|
|
|
|
connect( &altModePollingTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT( altModePoll() ) );
|
2010-07-03 16:24:30 +00:00
|
|
|
|
|
|
|
mouseGrabPollTimer.setSingleShot( false );
|
|
|
|
mouseGrabPollTimer.setInterval( 10 );
|
|
|
|
connect( &mouseGrabPollTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT(mouseGrabPoll()) );
|
2011-06-17 12:15:41 +00:00
|
|
|
|
2011-07-28 13:04:06 +00:00
|
|
|
MouseOver::instance().setPreferencesPtr( &( cfg.preferences ) );
|
2012-12-24 12:59:46 +00:00
|
|
|
|
|
|
|
ui.goBackButton->setEnabled( false );
|
|
|
|
ui.goForwardButton->setEnabled( false );
|
2013-02-27 13:12:46 +00:00
|
|
|
|
2014-02-05 13:41:49 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
|
|
|
|
grabGesture( Gestures::GDPinchGestureType );
|
|
|
|
grabGesture( Gestures::GDSwipeGestureType );
|
|
|
|
#endif
|
2017-06-05 13:15:38 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_X11
|
2017-06-06 15:00:48 +00:00
|
|
|
scanFlag = new ScanFlag( this );
|
2017-06-05 13:15:38 +00:00
|
|
|
|
|
|
|
connect( this, SIGNAL( showScanFlag( bool ) ),
|
|
|
|
scanFlag, SLOT( showScanFlag() ) );
|
|
|
|
|
2017-06-06 15:00:48 +00:00
|
|
|
connect( this, SIGNAL( hideScanFlag() ),
|
|
|
|
scanFlag, SLOT( hideWindow() ) );
|
|
|
|
|
2017-06-05 13:15:38 +00:00
|
|
|
connect( scanFlag, SIGNAL( showScanPopup() ),
|
|
|
|
this, SLOT( showEngagePopup() ) );
|
2017-06-13 15:00:16 +00:00
|
|
|
|
|
|
|
delayTimer.setSingleShot( true );
|
|
|
|
delayTimer.setInterval( 200 );
|
|
|
|
|
|
|
|
connect( &delayTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT( delayShow() ) );
|
2017-06-05 13:15:38 +00:00
|
|
|
#endif
|
2019-01-28 12:43:06 +00:00
|
|
|
|
|
|
|
applyZoomFactor();
|
|
|
|
applyWordsZoomLevel();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-08 20:20:02 +00:00
|
|
|
ScanPopup::~ScanPopup()
|
|
|
|
{
|
2018-03-27 18:01:33 +00:00
|
|
|
saveConfigData();
|
2010-03-30 20:15:55 +00:00
|
|
|
|
2009-02-08 20:20:02 +00:00
|
|
|
disableScanning();
|
2014-02-05 13:41:49 +00:00
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
|
|
|
|
ungrabGesture( Gestures::GDPinchGestureType );
|
|
|
|
ungrabGesture( Gestures::GDSwipeGestureType );
|
|
|
|
#endif
|
2009-02-08 20:20:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 18:01:33 +00:00
|
|
|
void ScanPopup::saveConfigData()
|
|
|
|
{
|
|
|
|
// Save state, geometry and pin status
|
|
|
|
cfg.popupWindowState = saveState( 1 );
|
|
|
|
cfg.popupWindowGeometry = saveGeometry();
|
|
|
|
cfg.pinPopupWindow = ui.pinButton->isChecked();
|
|
|
|
cfg.popupWindowAlwaysOnTop = ui.onTopButton->isChecked();
|
|
|
|
}
|
|
|
|
|
2009-02-08 20:20:02 +00:00
|
|
|
void ScanPopup::enableScanning()
|
|
|
|
{
|
|
|
|
if ( !isScanningEnabled )
|
|
|
|
{
|
|
|
|
isScanningEnabled = true;
|
|
|
|
MouseOver::instance().enableMouseOver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::disableScanning()
|
|
|
|
{
|
|
|
|
if ( isScanningEnabled )
|
|
|
|
{
|
|
|
|
MouseOver::instance().disableMouseOver();
|
|
|
|
isScanningEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-30 20:20:05 +00:00
|
|
|
void ScanPopup::applyZoomFactor()
|
|
|
|
{
|
|
|
|
definition->setZoomFactor( cfg.preferences.zoomFactor );
|
|
|
|
}
|
|
|
|
|
2017-07-14 13:02:21 +00:00
|
|
|
void ScanPopup::applyWordsZoomLevel()
|
|
|
|
{
|
|
|
|
QFont font( wordListDefaultFont );
|
|
|
|
int ps = font.pointSize();
|
|
|
|
|
|
|
|
if ( cfg.preferences.wordsZoomLevel != 0 )
|
|
|
|
{
|
|
|
|
ps += cfg.preferences.wordsZoomLevel;
|
|
|
|
if ( ps < 1 )
|
|
|
|
ps = 1;
|
|
|
|
font.setPointSize( ps );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ui.translateBox->wordList()->font().pointSize() != ps )
|
|
|
|
ui.translateBox->wordList()->setFont( font );
|
|
|
|
|
|
|
|
font = translateLineDefaultFont;
|
|
|
|
ps = font.pointSize();
|
|
|
|
|
|
|
|
if ( cfg.preferences.wordsZoomLevel != 0 )
|
|
|
|
{
|
|
|
|
ps += cfg.preferences.wordsZoomLevel;
|
|
|
|
if ( ps < 1 )
|
|
|
|
ps = 1;
|
|
|
|
font.setPointSize( ps );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ui.translateBox->translateLine()->font().pointSize() != ps )
|
|
|
|
ui.translateBox->translateLine()->setFont( font );
|
|
|
|
|
2019-01-26 19:11:27 +00:00
|
|
|
font = groupListDefaultFont;
|
|
|
|
ps = font.pointSize();
|
|
|
|
|
|
|
|
if ( cfg.preferences.wordsZoomLevel != 0 )
|
|
|
|
{
|
|
|
|
ps += cfg.preferences.wordsZoomLevel;
|
|
|
|
if ( ps < 1 )
|
|
|
|
ps = 1;
|
|
|
|
font.setPointSize( ps );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ui.groupList->font().pointSize() != ps )
|
|
|
|
{
|
|
|
|
disconnect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ),
|
|
|
|
this, SLOT( currentGroupChanged( QString const & ) ) );
|
|
|
|
int n = ui.groupList->currentIndex();
|
|
|
|
ui.groupList->clear();
|
|
|
|
ui.groupList->setFont( font );
|
|
|
|
ui.groupList->fill( groups );
|
|
|
|
ui.groupList->setCurrentIndex( n );
|
|
|
|
connect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ),
|
|
|
|
this, SLOT( currentGroupChanged( QString const & ) ) );
|
|
|
|
}
|
2017-07-14 13:02:21 +00:00
|
|
|
|
2019-01-26 19:11:27 +00:00
|
|
|
ui.outerFrame->layout()->activate();
|
2017-07-14 13:02:21 +00:00
|
|
|
}
|
|
|
|
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
Qt::WindowFlags ScanPopup::unpinnedWindowFlags() const
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_SPWF_CUSTOMIZATION
|
|
|
|
const Config::ScanPopupWindowFlags spwf = cfg.preferences.scanPopupUnpinnedWindowFlags;
|
|
|
|
Qt::WindowFlags result;
|
|
|
|
if( spwf == Config::SPWF_Popup )
|
|
|
|
result = Qt::Popup;
|
|
|
|
else
|
|
|
|
if( spwf == Config::SPWF_Tool )
|
|
|
|
result = Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
|
|
|
|
else
|
|
|
|
return defaultUnpinnedWindowFlags; // Ignore BypassWMHint option.
|
|
|
|
|
|
|
|
if( cfg.preferences.scanPopupUnpinnedBypassWMHint )
|
|
|
|
result |= Qt::X11BypassWindowManagerHint;
|
|
|
|
return result;
|
|
|
|
#else
|
|
|
|
return defaultUnpinnedWindowFlags;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
void ScanPopup::translateWordFromClipboard()
|
|
|
|
{
|
2009-07-31 11:40:54 +00:00
|
|
|
return translateWordFromClipboard(QClipboard::Clipboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::translateWordFromSelection()
|
|
|
|
{
|
|
|
|
return translateWordFromClipboard(QClipboard::Selection);
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:01:59 +00:00
|
|
|
void ScanPopup::editGroupRequested()
|
|
|
|
{
|
|
|
|
emit editGroupRequested( ui.groupList->getCurrentGroup() );
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:40:54 +00:00
|
|
|
void ScanPopup::translateWordFromClipboard(QClipboard::Mode m)
|
|
|
|
{
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "translating from clipboard or selection\n" );
|
2009-04-21 18:27:26 +00:00
|
|
|
|
|
|
|
QString subtype = "plain";
|
|
|
|
|
2009-07-31 11:40:54 +00:00
|
|
|
QString str = QApplication::clipboard()->text( subtype, m);
|
2009-04-21 18:27:26 +00:00
|
|
|
|
2011-11-16 13:02:56 +00:00
|
|
|
translateWord( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::translateWord( QString const & word )
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
pendingInputPhrase = cfg.preferences.sanitizeInputPhrase( word );
|
2009-04-21 18:27:26 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( !pendingInputPhrase.isValid() )
|
2009-04-21 18:27:26 +00:00
|
|
|
return; // Nothing there
|
|
|
|
|
|
|
|
// In case we had any timers engaged before, cancel them now.
|
|
|
|
altModePollingTimer.stop();
|
|
|
|
altModeExpirationTimer.stop();
|
|
|
|
|
2017-06-06 15:00:48 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
emit hideScanFlag();
|
|
|
|
#endif
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
inputPhrase = pendingInputPhrase;
|
2013-01-28 12:19:11 +00:00
|
|
|
engagePopup( false,
|
2013-05-31 04:28:29 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2013-01-28 12:19:11 +00:00
|
|
|
true // We only focus popup under Windows when activated via Ctrl+C+C
|
|
|
|
// -- on Linux it already has an implicit focus
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
);
|
2009-04-21 18:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 15:00:16 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
void ScanPopup::delayShow()
|
|
|
|
{
|
|
|
|
QString subtype = "plain";
|
|
|
|
handleInputWord( QApplication::clipboard()->text( subtype, QClipboard::Selection ) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ScanPopup::clipboardChanged( QClipboard::Mode m )
|
|
|
|
{
|
2009-02-08 20:20:02 +00:00
|
|
|
if ( !isScanningEnabled )
|
|
|
|
return;
|
2018-04-16 12:23:22 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
if( cfg.preferences.ignoreOwnClipboardChanges && ownsClipboardMode( m ) )
|
|
|
|
return;
|
|
|
|
#endif
|
2017-06-05 13:15:38 +00:00
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "clipboard changed\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2017-06-13 15:00:16 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
if( m == QClipboard::Selection )
|
|
|
|
{
|
|
|
|
// Use delay show to prevent multiple popups while selection in progress
|
|
|
|
delayTimer.start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
QString subtype = "plain";
|
|
|
|
|
2009-02-02 20:28:53 +00:00
|
|
|
handleInputWord( QApplication::clipboard()->text( subtype, m ) );
|
|
|
|
}
|
|
|
|
|
2012-09-24 13:20:58 +00:00
|
|
|
void ScanPopup::mouseHovered( QString const & str, bool forcePopup )
|
2009-02-02 20:28:53 +00:00
|
|
|
{
|
2012-09-24 13:20:58 +00:00
|
|
|
handleInputWord( str, forcePopup );
|
2009-02-02 20:28:53 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 13:20:58 +00:00
|
|
|
void ScanPopup::handleInputWord( QString const & str, bool forcePopup )
|
2009-02-02 20:28:53 +00:00
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
Config::InputPhrase sanitizedPhrase = cfg.preferences.sanitizeInputPhrase( str );
|
2010-12-03 20:10:18 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( isVisible() && sanitizedPhrase == inputPhrase )
|
2010-12-03 20:10:18 +00:00
|
|
|
{
|
|
|
|
// Attempt to translate the same word we already have shown in scan popup.
|
|
|
|
// Ignore it, as it is probably a spurious mouseover event.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
pendingInputPhrase = sanitizedPhrase;
|
2009-02-05 16:56:57 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( !pendingInputPhrase.isValid() )
|
2009-04-11 16:44:14 +00:00
|
|
|
{
|
|
|
|
if ( cfg.preferences.scanPopupAltMode )
|
|
|
|
{
|
|
|
|
// In case we had any timers engaged before, cancel them now, since
|
|
|
|
// we're not going to translate anything anymore.
|
|
|
|
altModePollingTimer.stop();
|
|
|
|
altModeExpirationTimer.stop();
|
|
|
|
}
|
2009-02-05 16:56:57 +00:00
|
|
|
return;
|
2009-04-11 16:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:15:38 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
if ( cfg.preferences.showScanFlag ) {
|
2021-06-10 16:13:11 +00:00
|
|
|
inputPhrase = pendingInputPhrase;
|
2017-06-05 13:15:38 +00:00
|
|
|
emit showScanFlag( forcePopup );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-11 16:44:14 +00:00
|
|
|
// Check key modifiers
|
2009-02-05 16:56:57 +00:00
|
|
|
|
2009-04-11 16:44:14 +00:00
|
|
|
if ( cfg.preferences.enableScanPopupModifiers && !checkModifiersPressed( cfg.preferences.scanPopupModifiers ) )
|
|
|
|
{
|
|
|
|
if ( cfg.preferences.scanPopupAltMode )
|
|
|
|
{
|
|
|
|
altModePollingTimer.start();
|
|
|
|
altModeExpirationTimer.start();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
return;
|
2009-04-11 16:44:14 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
inputPhrase = pendingInputPhrase;
|
2012-09-24 13:20:58 +00:00
|
|
|
engagePopup( forcePopup );
|
2009-04-11 16:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:15:38 +00:00
|
|
|
#ifdef HAVE_X11
|
|
|
|
void ScanPopup::showEngagePopup()
|
|
|
|
{
|
|
|
|
engagePopup(false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-24 13:20:58 +00:00
|
|
|
void ScanPopup::engagePopup( bool forcePopup, bool giveFocus )
|
2009-04-11 16:44:14 +00:00
|
|
|
{
|
2012-09-24 13:20:58 +00:00
|
|
|
if( cfg.preferences.scanToMainWindow && !forcePopup )
|
2011-11-16 12:52:25 +00:00
|
|
|
{
|
|
|
|
// Send translated word to main window istead of show popup
|
2021-06-10 16:13:11 +00:00
|
|
|
emit sendPhraseToMainWindow( inputPhrase );
|
2011-11-16 12:52:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:59:48 +00:00
|
|
|
definition->setSelectionBySingleClick( cfg.preferences.selectWordBySingleClick );
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
if ( !isVisible() )
|
|
|
|
{
|
2009-12-27 12:00:59 +00:00
|
|
|
// Need to show the window
|
2009-02-08 16:35:30 +00:00
|
|
|
|
2009-12-27 12:00:59 +00:00
|
|
|
if ( !ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
// Decide where should the window land
|
|
|
|
|
|
|
|
QPoint currentPos = QCursor::pos();
|
|
|
|
|
|
|
|
QRect desktop = QApplication::desktop()->screenGeometry();
|
|
|
|
|
|
|
|
QSize windowSize = geometry().size();
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
/// Try the to-the-right placement
|
|
|
|
if ( currentPos.x() + 4 + windowSize.width() <= desktop.topRight().x() )
|
|
|
|
x = currentPos.x() + 4;
|
|
|
|
else
|
|
|
|
/// Try the to-the-left placement
|
|
|
|
if ( currentPos.x() - 4 - windowSize.width() >= desktop.x() )
|
|
|
|
x = currentPos.x() - 4 - windowSize.width();
|
|
|
|
else
|
|
|
|
// Center it
|
|
|
|
x = desktop.x() + ( desktop.width() - windowSize.width() ) / 2;
|
|
|
|
|
|
|
|
/// Try the to-the-bottom placement
|
|
|
|
if ( currentPos.y() + 15 + windowSize.height() <= desktop.bottomLeft().y() )
|
|
|
|
y = currentPos.y() + 15;
|
|
|
|
else
|
|
|
|
/// Try the to-the-top placement
|
|
|
|
if ( currentPos.y() - 15 - windowSize.height() >= desktop.y() )
|
|
|
|
y = currentPos.y() - 15 - windowSize.height();
|
|
|
|
else
|
|
|
|
// Center it
|
|
|
|
y = desktop.y() + ( desktop.height() - windowSize.height() ) / 2;
|
|
|
|
|
|
|
|
move( x, y );
|
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
show();
|
|
|
|
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
#ifdef ENABLE_SPWF_CUSTOMIZATION
|
|
|
|
// Ensure that the window always has focus on X11 with Qt::Tool flag.
|
|
|
|
// This also often prevents the window from disappearing prematurely with Qt::Popup flag,
|
|
|
|
// especially when combined with Qt::X11BypassWindowManagerHint flag.
|
|
|
|
if ( !ui.pinButton->isChecked() )
|
|
|
|
giveFocus = true;
|
|
|
|
#endif
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
if ( giveFocus )
|
|
|
|
{
|
|
|
|
activateWindow();
|
|
|
|
raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
mouseEnteredOnce = false;
|
|
|
|
// Need to monitor the mouse so we know when to hide the window
|
|
|
|
interceptMouse();
|
|
|
|
}
|
|
|
|
|
2009-02-08 16:35:30 +00:00
|
|
|
// This produced some funky mouse grip-related bugs so we commented it out
|
|
|
|
//QApplication::processEvents(); // Make window appear immediately no matter what
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
2009-12-27 12:00:59 +00:00
|
|
|
else
|
|
|
|
if ( ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
// Pinned-down window isn't always on top, so we need to raise it
|
|
|
|
show();
|
|
|
|
activateWindow();
|
|
|
|
raise();
|
|
|
|
}
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
#ifdef ENABLE_SPWF_CUSTOMIZATION
|
|
|
|
else
|
|
|
|
if ( ( windowFlags() & Qt::Tool ) == Qt::Tool )
|
|
|
|
{
|
|
|
|
// Ensure that the window with Qt::Tool flag always has focus on X11.
|
|
|
|
activateWindow();
|
|
|
|
raise();
|
|
|
|
}
|
|
|
|
#endif
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2013-01-26 15:55:17 +00:00
|
|
|
if ( ui.pinButton->isChecked() )
|
2013-02-01 12:36:01 +00:00
|
|
|
setWindowTitle( tr( "%1 - %2" ).arg( elideInputWord(), "GoldenDict" ) );
|
2013-01-26 15:55:17 +00:00
|
|
|
|
2014-05-09 18:53:19 +00:00
|
|
|
/// Too large strings make window expand which is probably not what user
|
|
|
|
/// wants
|
2021-06-30 15:54:35 +00:00
|
|
|
ui.translateBox->setText( Folding::escapeWildcardSymbols( inputPhrase.phrase ), false );
|
2021-06-10 16:13:11 +00:00
|
|
|
translateBoxSuffix = inputPhrase.punctuationSuffix;
|
2014-05-09 18:53:19 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
showTranslationFor( inputPhrase );
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 20:55:00 +00:00
|
|
|
QString ScanPopup::elideInputWord()
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
QString const & inputWord = inputPhrase.phrase;
|
2009-02-05 20:55:00 +00:00
|
|
|
return inputWord.size() > 32 ? inputWord.mid( 0, 32 ) + "..." : inputWord;
|
|
|
|
}
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
void ScanPopup::currentGroupChanged( QString const & )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
cfg.lastPopupGroupId = ui.groupList->getCurrentGroup();
|
|
|
|
Instances::Group const * igrp = groups.findGroup( cfg.lastPopupGroupId );
|
|
|
|
if( cfg.lastPopupGroupId == Instances::Group::AllGroupId )
|
|
|
|
{
|
|
|
|
if( igrp )
|
|
|
|
igrp->checkMutedDictionaries( &cfg.popupMutedDictionaries );
|
|
|
|
dictionaryBar.setMutedDictionaries( &cfg.popupMutedDictionaries );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Config::Group * grp = cfg.getGroup( cfg.lastPopupGroupId );
|
|
|
|
if( grp )
|
|
|
|
{
|
|
|
|
if( igrp )
|
|
|
|
igrp->checkMutedDictionaries( &grp->popupMutedDictionaries );
|
|
|
|
dictionaryBar.setMutedDictionaries( &grp->popupMutedDictionaries );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dictionaryBar.setMutedDictionaries( 0 );
|
|
|
|
}
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
updateDictionaryBar();
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
if ( isVisible() )
|
2013-01-27 22:12:00 +00:00
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
updateSuggestionList();
|
2013-01-27 22:12:00 +00:00
|
|
|
translateInputFinished();
|
|
|
|
}
|
2009-02-06 17:04:11 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
cfg.lastPopupGroupId = ui.groupList->getCurrentGroup();
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
void ScanPopup::wordListItemActivated( QListWidgetItem * item )
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
showTranslationFor( Config::InputPhrase::fromPhrase( item->text() ) );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::translateInputChanged( QString const & text )
|
2021-06-10 16:13:11 +00:00
|
|
|
{
|
|
|
|
updateSuggestionList( text );
|
|
|
|
translateBoxSuffix = QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::updateSuggestionList()
|
|
|
|
{
|
|
|
|
updateSuggestionList( ui.translateBox->translateLine()->text() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::updateSuggestionList( QString const & text )
|
2013-01-25 15:42:44 +00:00
|
|
|
{
|
|
|
|
mainStatusBar->clearMessage();
|
|
|
|
ui.translateBox->wordList()->setCurrentItem( 0, QItemSelectionModel::Clear );
|
|
|
|
|
|
|
|
QString req = text.trimmed();
|
|
|
|
|
|
|
|
if ( !req.size() )
|
|
|
|
{
|
|
|
|
// An empty request always results in an empty result
|
|
|
|
wordFinder.cancel();
|
|
|
|
ui.translateBox->wordList()->clear();
|
|
|
|
ui.translateBox->wordList()->unsetCursor();
|
|
|
|
|
|
|
|
// Reset the noResults mark if it's on right now
|
|
|
|
if ( ui.translateBox->translateLine()->property( "noResults" ).toBool() )
|
|
|
|
{
|
|
|
|
ui.translateBox->translateLine()->setProperty( "noResults", false );
|
|
|
|
setStyleSheet( styleSheet() );
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.translateBox->wordList()->setCursor( Qt::WaitCursor );
|
|
|
|
|
|
|
|
wordFinder.prefixMatch( req, getActiveDicts() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::translateInputFinished()
|
|
|
|
{
|
2022-10-29 07:05:26 +00:00
|
|
|
QString const word = ui.translateBox->translateLine()->text().trimmed();
|
|
|
|
if( word.isEmpty() )
|
|
|
|
return;
|
|
|
|
inputPhrase.phrase = Folding::unescapeWildcardSymbols( word );
|
2021-06-30 15:54:35 +00:00
|
|
|
inputPhrase.punctuationSuffix = translateBoxSuffix;
|
2021-06-10 16:13:11 +00:00
|
|
|
showTranslationFor( inputPhrase );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
void ScanPopup::showTranslationFor( Config::InputPhrase const & inputPhrase )
|
2013-01-25 15:42:44 +00:00
|
|
|
{
|
2009-04-11 16:44:14 +00:00
|
|
|
ui.pronounceButton->hide();
|
|
|
|
|
2017-10-23 14:21:43 +00:00
|
|
|
unsigned groupId = ui.groupList->getCurrentGroup();
|
2021-06-10 16:13:11 +00:00
|
|
|
definition->showDefinition( inputPhrase, groupId );
|
2013-01-25 15:42:44 +00:00
|
|
|
definition->focus();
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > const & ScanPopup::getActiveDicts()
|
|
|
|
{
|
2010-04-30 09:56:40 +00:00
|
|
|
int current = ui.groupList->currentIndex();
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2010-04-30 09:56:40 +00:00
|
|
|
if ( current < 0 || current >= (int) groups.size() )
|
|
|
|
{
|
|
|
|
// This shouldn't ever happen
|
|
|
|
return allDictionaries;
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
Config::MutedDictionaries const * mutedDictionaries = dictionaryBar.getMutedDictionaries();
|
|
|
|
if ( !dictionaryBar.toggleViewAction()->isChecked() || mutedDictionaries == 0 )
|
2010-04-30 09:56:40 +00:00
|
|
|
return groups[ current ].dictionaries;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > const & activeDicts =
|
|
|
|
groups[ current ].dictionaries;
|
|
|
|
|
|
|
|
// Populate the special dictionariesUnmuted array with only unmuted
|
|
|
|
// dictionaries
|
|
|
|
|
|
|
|
dictionariesUnmuted.clear();
|
|
|
|
dictionariesUnmuted.reserve( activeDicts.size() );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < activeDicts.size(); ++x )
|
2012-09-26 13:13:47 +00:00
|
|
|
if ( !mutedDictionaries->contains(
|
2010-04-30 09:56:40 +00:00
|
|
|
QString::fromStdString( activeDicts[ x ]->getId() ) ) )
|
|
|
|
dictionariesUnmuted.push_back( activeDicts[ x ] );
|
|
|
|
|
|
|
|
return dictionariesUnmuted;
|
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
void ScanPopup::typingEvent( QString const & t )
|
|
|
|
{
|
|
|
|
if ( t == "\n" || t == "\r" )
|
|
|
|
{
|
|
|
|
focusTranslateLine();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui.translateBox->translateLine()->setFocus();
|
2013-01-27 22:12:00 +00:00
|
|
|
ui.translateBox->setText( t, true );
|
2013-01-25 15:42:44 +00:00
|
|
|
ui.translateBox->translateLine()->setCursorPosition( t.size() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
bool ScanPopup::eventFilter( QObject * watched, QEvent * event )
|
|
|
|
{
|
2019-01-29 11:01:30 +00:00
|
|
|
if ( watched == ui.translateBox->translateLine() )
|
2013-01-25 15:42:44 +00:00
|
|
|
{
|
2019-01-29 11:01:30 +00:00
|
|
|
if ( event->type() == QEvent::FocusIn )
|
|
|
|
{
|
|
|
|
QFocusEvent * focusEvent = static_cast< QFocusEvent * >( event );
|
2013-01-25 15:42:44 +00:00
|
|
|
|
2019-01-29 11:01:30 +00:00
|
|
|
// select all on mouse click
|
|
|
|
if ( focusEvent->reason() == Qt::MouseFocusReason ) {
|
|
|
|
QTimer::singleShot(0, this, SLOT(focusTranslateLine()));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event->type() == QEvent::Resize )
|
|
|
|
{
|
|
|
|
// The UI looks ugly when group combobox is higher than translate line.
|
|
|
|
// Make the height of the combobox the same as the line edit's height.
|
|
|
|
// The fonts of these UI items should be kept in sync by applyWordsZoomLevel()
|
|
|
|
// so that text in the combobox is not clipped.
|
|
|
|
const QResizeEvent * const resizeEvent = static_cast< const QResizeEvent * >( event );
|
|
|
|
ui.groupList->setFixedHeight( resizeEvent->size().height() );
|
|
|
|
return false;
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
if ( mouseIntercepted )
|
|
|
|
{
|
|
|
|
// We're only interested in our events
|
|
|
|
|
|
|
|
if ( event->type() == QEvent::MouseMove )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "Object: %s\n", watched->objectName().toUtf8().data() );
|
2010-01-02 23:26:09 +00:00
|
|
|
QMouseEvent * mouseEvent = ( QMouseEvent * ) event;
|
2010-07-03 16:24:30 +00:00
|
|
|
reactOnMouseMove( mouseEvent->globalPos() );
|
|
|
|
}
|
|
|
|
}
|
2010-01-02 23:26:09 +00:00
|
|
|
|
2010-07-03 16:24:30 +00:00
|
|
|
return QMainWindow::eventFilter( watched, event );
|
|
|
|
}
|
2010-01-02 23:26:09 +00:00
|
|
|
|
2010-07-03 16:24:30 +00:00
|
|
|
void ScanPopup::reactOnMouseMove( QPoint const & p )
|
|
|
|
{
|
|
|
|
if ( geometry().contains( p ) )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "got inside\n" );
|
2010-01-02 23:26:09 +00:00
|
|
|
|
2010-07-03 16:24:30 +00:00
|
|
|
hideTimer.stop();
|
|
|
|
mouseEnteredOnce = true;
|
|
|
|
uninterceptMouse();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "outside\n" );
|
2010-07-03 16:24:30 +00:00
|
|
|
// We're in grab mode and outside the window - calculate the
|
|
|
|
// distance from it. We might want to hide it.
|
|
|
|
|
|
|
|
// When the mouse has entered once, we don't allow it stayng outside,
|
|
|
|
// but we give a grace period for it to return.
|
|
|
|
int proximity = mouseEnteredOnce ? 0 : 60;
|
|
|
|
|
|
|
|
// Note: watched == this ensures no other child objects popping out are
|
|
|
|
// receiving this event, meaning there's basically nothing under the
|
|
|
|
// cursor.
|
|
|
|
if ( /*watched == this &&*/
|
|
|
|
!frameGeometry().adjusted( -proximity, -proximity, proximity, proximity ).
|
|
|
|
contains( p ) )
|
|
|
|
{
|
|
|
|
// We've way too far from the window -- hide the popup
|
|
|
|
|
|
|
|
// If the mouse never entered the popup, hide the window instantly --
|
|
|
|
// the user just moved the cursor further away from the window.
|
|
|
|
|
|
|
|
if ( !mouseEnteredOnce )
|
|
|
|
hideWindow();
|
|
|
|
else
|
|
|
|
hideTimer.start();
|
2010-01-02 23:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 18:43:07 +00:00
|
|
|
void ScanPopup::mousePressEvent( QMouseEvent * ev )
|
|
|
|
{
|
2010-01-02 23:26:09 +00:00
|
|
|
// With mouse grabs, the press can occur anywhere on the screen, which
|
|
|
|
// might mean hiding the window.
|
|
|
|
|
|
|
|
if ( !frameGeometry().contains( ev->globalPos() ) )
|
|
|
|
{
|
|
|
|
hideWindow();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
if ( ev->button() == Qt::LeftButton )
|
|
|
|
{
|
|
|
|
startPos = ev->globalPos();
|
|
|
|
setCursor( Qt::ClosedHandCursor );
|
|
|
|
}
|
2009-02-06 18:43:07 +00:00
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::mousePressEvent( ev );
|
2009-02-06 18:43:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 13:05:25 +00:00
|
|
|
void ScanPopup::mouseMoveEvent( QMouseEvent * event )
|
|
|
|
{
|
2010-01-02 23:26:09 +00:00
|
|
|
if ( event->buttons() && cursor().shape() == Qt::ClosedHandCursor )
|
2009-02-06 18:43:07 +00:00
|
|
|
{
|
|
|
|
QPoint newPos = event->globalPos();
|
|
|
|
|
|
|
|
QPoint delta = newPos - startPos;
|
|
|
|
|
|
|
|
startPos = newPos;
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
// Move the window
|
2009-02-06 18:43:07 +00:00
|
|
|
|
|
|
|
move( pos() + delta );
|
|
|
|
}
|
2017-06-05 13:15:38 +00:00
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::mouseMoveEvent( event );
|
2009-02-06 13:05:25 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 18:43:07 +00:00
|
|
|
void ScanPopup::mouseReleaseEvent( QMouseEvent * ev )
|
|
|
|
{
|
|
|
|
unsetCursor();
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::mouseReleaseEvent( ev );
|
2009-02-06 18:43:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
void ScanPopup::leaveEvent( QEvent * event )
|
|
|
|
{
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::leaveEvent( event );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
// We hide the popup when the mouse leaves it.
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
// Combo-boxes seem to generate leave events for their parents when
|
|
|
|
// unfolded, so we check coordinates as well.
|
2009-02-06 18:43:07 +00:00
|
|
|
// If the dialog is pinned, we don't hide the popup.
|
|
|
|
// If some mouse buttons are pressed, we don't hide the popup either,
|
|
|
|
// since it indicates the move operation is underway.
|
|
|
|
if ( !ui.pinButton->isChecked() && !geometry().contains( QCursor::pos() ) &&
|
|
|
|
QApplication::mouseButtons() == Qt::NoButton )
|
|
|
|
{
|
2009-04-10 13:56:38 +00:00
|
|
|
hideTimer.start();
|
2009-02-06 18:43:07 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 13:56:38 +00:00
|
|
|
void ScanPopup::enterEvent( QEvent * event )
|
|
|
|
{
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::enterEvent( event );
|
2009-04-10 13:56:38 +00:00
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
if ( mouseEnteredOnce )
|
|
|
|
{
|
|
|
|
// We "enter" first time via our event filter. This seems to evade some
|
|
|
|
// unexpected behavior under Windows.
|
|
|
|
|
|
|
|
// If there was a countdown to hide the window, stop it.
|
|
|
|
hideTimer.stop();
|
|
|
|
}
|
2009-04-10 13:56:38 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:30:24 +00:00
|
|
|
void ScanPopup::requestWindowFocus()
|
|
|
|
{
|
|
|
|
// One of the rare, actually working workarounds for requesting a user keyboard focus on X11,
|
|
|
|
// works for Qt::Popup windows, exactly like our Scan Popup (in unpinned state).
|
|
|
|
// Modern window managers actively resist to automatically focus pop-up windows.
|
2018-04-14 13:25:09 +00:00
|
|
|
#if defined HAVE_X11 && QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
|
2013-01-29 21:30:24 +00:00
|
|
|
if ( !ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
QMenu m( this );
|
|
|
|
m.addAction( "" );
|
|
|
|
m.show();
|
|
|
|
m.hide();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-08 21:26:35 +00:00
|
|
|
void ScanPopup::showEvent( QShowEvent * ev )
|
|
|
|
{
|
2010-03-30 20:15:55 +00:00
|
|
|
QMainWindow::showEvent( ev );
|
2013-01-29 21:30:24 +00:00
|
|
|
|
|
|
|
QTimer::singleShot(100, this, SLOT( requestWindowFocus() ) );
|
2017-06-05 13:15:38 +00:00
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
if ( groups.size() <= 1 ) // Only the default group? Hide then.
|
2009-02-08 21:26:35 +00:00
|
|
|
ui.groupList->hide();
|
2010-03-30 20:15:55 +00:00
|
|
|
|
|
|
|
if ( ui.showDictionaryBar->isChecked() != dictionaryBar.isVisible() )
|
|
|
|
{
|
|
|
|
ui.showDictionaryBar->setChecked( dictionaryBar.isVisible() );
|
|
|
|
updateDictionaryBar();
|
|
|
|
}
|
2009-02-08 21:26:35 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ScanPopup::prefixMatchFinished()
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Check that there's a window there at all
|
|
|
|
if ( isVisible() )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( wordFinder.getErrorString().size() )
|
|
|
|
{
|
|
|
|
ui.queryError->setToolTip( wordFinder.getErrorString() );
|
|
|
|
ui.queryError->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ui.queryError->hide();
|
2009-04-10 15:52:08 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
void ScanPopup::on_pronounceButton_clicked()
|
|
|
|
{
|
|
|
|
definition->playSound();
|
|
|
|
}
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
void ScanPopup::pinButtonClicked( bool checked )
|
|
|
|
{
|
|
|
|
if ( checked )
|
2009-02-05 20:55:00 +00:00
|
|
|
{
|
2010-01-02 23:26:09 +00:00
|
|
|
uninterceptMouse();
|
|
|
|
|
2017-07-13 15:00:19 +00:00
|
|
|
ui.onTopButton->setVisible( true );
|
2019-05-11 10:55:52 +00:00
|
|
|
Qt::WindowFlags flags = pinnedWindowFlags;
|
2017-07-13 15:00:19 +00:00
|
|
|
if( ui.onTopButton->isChecked() )
|
|
|
|
flags |= Qt::WindowStaysOnTopHint;
|
|
|
|
setWindowFlags( flags );
|
|
|
|
|
2013-02-01 12:36:01 +00:00
|
|
|
setWindowTitle( tr( "%1 - %2" ).arg( elideInputWord(), "GoldenDict" ) );
|
2010-03-30 20:15:55 +00:00
|
|
|
dictionaryBar.setMovable( true );
|
2009-04-10 13:56:38 +00:00
|
|
|
hideTimer.stop();
|
2009-02-05 20:55:00 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
else
|
2010-01-02 23:26:09 +00:00
|
|
|
{
|
2017-07-13 15:00:19 +00:00
|
|
|
ui.onTopButton->setVisible( false );
|
2010-03-30 20:15:55 +00:00
|
|
|
dictionaryBar.setMovable( false );
|
Allow customizing unpinned scan popup window flags on X11 with Qt5
My tests in many desktop environments and window managers indicate that
no single configuration works perfectly in all environments. There are
also behavior differences between Qt::Popup and Qt::Tool flags, which
are not exactly bugs, so I suppose users might subjectively prefer
different options.
Customizing the flags allows the user to prevent unpinned scan popup
window flickering with Qt5 on Linux. In a way adding these options fixes
issue #645, which is: the scan popup window blinks rapidly, barely
noticeably in some applications, such as Calibre ebook-viewer
and Chromium. In this case the scan popup window usually ends up hidden
when selection ends, unless it was finished with a jerk.
I have tested the new options in 9 desktop environments and window
managers: at least one configuration for each eliminates #645 and makes
the scan popup window work the same as with Qt4 in this regard:
the popup window remains visible, text in the popup's translation line
keeps up with the text selection in the external application,
and the selected text is being translated on the fly.
Moreover, for each tested DE/WM, at least one configuration makes
the scan popup window work perfectly as far as I am concerned.
This issue was partially worked around with a 200ms scan popup delay
timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8
for the duplicate issue #854. However the timer solution is incomplete
because it requires the user to select text quickly and without delays.
If global mouse selection does not change for 200ms while the left mouse
button is held down, the user will likely not see the scan popup when
(s)he finishes selection, and will have to try selecting again -
hopefully faster this time.
The 200ms delay is no longer critically important after this commit,
but it is still beneficial: the lookup query changes less often,
which in turn reduces article definition update frequency.
So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
|
|
|
setWindowFlags( unpinnedWindowFlags() );
|
2010-01-02 23:26:09 +00:00
|
|
|
|
|
|
|
mouseEnteredOnce = true;
|
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
show();
|
|
|
|
}
|
2009-04-10 13:56:38 +00:00
|
|
|
|
2013-01-25 15:42:44 +00:00
|
|
|
void ScanPopup::focusTranslateLine()
|
|
|
|
{
|
|
|
|
if ( !isActiveWindow() )
|
|
|
|
activateWindow();
|
|
|
|
|
|
|
|
ui.translateBox->translateLine()->setFocus();
|
|
|
|
ui.translateBox->translateLine()->selectAll();
|
|
|
|
}
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
void ScanPopup::on_showDictionaryBar_clicked( bool checked )
|
|
|
|
{
|
|
|
|
dictionaryBar.setVisible( checked );
|
|
|
|
updateDictionaryBar();
|
|
|
|
definition->updateMutedContents();
|
|
|
|
}
|
|
|
|
|
2009-04-10 13:56:38 +00:00
|
|
|
void ScanPopup::hideTimerExpired()
|
|
|
|
{
|
|
|
|
if ( isVisible() )
|
2010-01-02 23:26:09 +00:00
|
|
|
hideWindow();
|
2009-04-10 13:56:38 +00:00
|
|
|
}
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2009-04-11 16:44:14 +00:00
|
|
|
void ScanPopup::altModeExpired()
|
|
|
|
{
|
|
|
|
// The alt mode duration has expired, so there's no need to poll for modifiers
|
|
|
|
// anymore.
|
|
|
|
altModePollingTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::altModePoll()
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
if ( !pendingInputPhrase.isValid() )
|
2009-04-11 16:44:14 +00:00
|
|
|
{
|
|
|
|
altModePollingTimer.stop();
|
|
|
|
altModeExpirationTimer.stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( checkModifiersPressed( cfg.preferences.scanPopupModifiers ) )
|
|
|
|
{
|
|
|
|
altModePollingTimer.stop();
|
|
|
|
altModeExpirationTimer.stop();
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
inputPhrase = pendingInputPhrase;
|
2012-09-24 13:20:58 +00:00
|
|
|
engagePopup( false );
|
2009-04-11 16:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:42:04 +00:00
|
|
|
void ScanPopup::pageLoaded( ArticleView * )
|
2009-04-10 21:07:03 +00:00
|
|
|
{
|
|
|
|
ui.pronounceButton->setVisible( definition->hasSound() );
|
|
|
|
|
2012-12-24 12:59:46 +00:00
|
|
|
updateBackForwardButtons();
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
if ( cfg.preferences.pronounceOnLoadPopup )
|
|
|
|
definition->playSound();
|
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
|
2011-07-14 20:11:57 +00:00
|
|
|
void ScanPopup::showStatusBarMessage( QString const & message, int timeout, QPixmap const & icon )
|
2011-07-14 08:17:59 +00:00
|
|
|
{
|
2011-07-14 20:11:57 +00:00
|
|
|
mainStatusBar->showMessage( message, timeout, icon );
|
2011-07-14 08:17:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
void ScanPopup::escapePressed()
|
|
|
|
{
|
|
|
|
if ( !definition->closeSearch() )
|
2010-01-02 23:26:09 +00:00
|
|
|
hideWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::hideWindow()
|
|
|
|
{
|
|
|
|
uninterceptMouse();
|
|
|
|
|
2012-09-12 14:18:16 +00:00
|
|
|
emit closeMenu();
|
2010-01-02 23:26:09 +00:00
|
|
|
hideTimer.stop();
|
|
|
|
unsetCursor();
|
2013-01-27 22:12:00 +00:00
|
|
|
ui.translateBox->setPopupEnabled( false );
|
2013-01-29 22:04:18 +00:00
|
|
|
ui.translateBox->translateLine()->deselect();
|
2010-01-02 23:26:09 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::interceptMouse()
|
|
|
|
{
|
|
|
|
if ( !mouseIntercepted )
|
2009-05-16 11:14:43 +00:00
|
|
|
{
|
2010-07-03 16:24:30 +00:00
|
|
|
// We used to grab the mouse -- but this doesn't always work reliably
|
|
|
|
// (e.g. doesn't work at all in Windows 7 for some reason). Therefore
|
|
|
|
// we use a polling timer now.
|
|
|
|
|
|
|
|
// grabMouse();
|
|
|
|
mouseGrabPollTimer.start();
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
qApp->installEventFilter( this );
|
|
|
|
|
|
|
|
mouseIntercepted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-03 16:24:30 +00:00
|
|
|
void ScanPopup::mouseGrabPoll()
|
|
|
|
{
|
|
|
|
if ( mouseIntercepted )
|
|
|
|
reactOnMouseMove( QCursor::pos() );
|
|
|
|
}
|
|
|
|
|
2010-01-02 23:26:09 +00:00
|
|
|
void ScanPopup::uninterceptMouse()
|
|
|
|
{
|
|
|
|
if ( mouseIntercepted )
|
|
|
|
{
|
|
|
|
qApp->removeEventFilter( this );
|
2010-07-03 16:24:30 +00:00
|
|
|
mouseGrabPollTimer.stop();
|
|
|
|
// releaseMouse();
|
2010-01-02 23:26:09 +00:00
|
|
|
|
|
|
|
mouseIntercepted = false;
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-30 20:15:55 +00:00
|
|
|
|
|
|
|
void ScanPopup::updateDictionaryBar()
|
|
|
|
{
|
|
|
|
if ( !dictionaryBar.toggleViewAction()->isChecked() )
|
|
|
|
return; // It's not enabled, therefore hidden -- don't waste time
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
unsigned currentId = ui.groupList->getCurrentGroup();
|
|
|
|
Instances::Group const * grp = groups.findGroup( currentId );
|
2010-04-30 09:56:40 +00:00
|
|
|
|
|
|
|
if ( grp ) // Should always be !0, but check as a safeguard
|
|
|
|
dictionaryBar.setDictionaries( grp->dictionaries );
|
2012-09-26 13:13:47 +00:00
|
|
|
|
|
|
|
if( currentId == Instances::Group::AllGroupId )
|
|
|
|
dictionaryBar.setMutedDictionaries( &cfg.popupMutedDictionaries );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Config::Group * grp = cfg.getGroup( currentId );
|
|
|
|
dictionaryBar.setMutedDictionaries( grp ? &grp->popupMutedDictionaries : 0 );
|
|
|
|
}
|
2013-02-27 13:12:46 +00:00
|
|
|
|
|
|
|
setDictionaryIconSize();
|
2010-03-30 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::mutedDictionariesChanged()
|
|
|
|
{
|
2021-06-10 16:13:11 +00:00
|
|
|
updateSuggestionList();
|
2010-03-30 20:15:55 +00:00
|
|
|
if ( dictionaryBar.toggleViewAction()->isChecked() )
|
|
|
|
definition->updateMutedContents();
|
|
|
|
}
|
2011-11-16 12:52:25 +00:00
|
|
|
|
|
|
|
void ScanPopup::on_sendWordButton_clicked()
|
|
|
|
{
|
|
|
|
if ( !isVisible() )
|
|
|
|
return;
|
|
|
|
if( !ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
definition->closeSearch();
|
|
|
|
hideWindow();
|
|
|
|
}
|
2021-06-10 16:13:11 +00:00
|
|
|
emit sendPhraseToMainWindow( definition->getPhrase() );
|
2011-11-16 12:52:25 +00:00
|
|
|
}
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2017-05-15 15:08:06 +00:00
|
|
|
void ScanPopup::on_sendWordToFavoritesButton_clicked()
|
|
|
|
{
|
|
|
|
if ( !isVisible() )
|
|
|
|
return;
|
|
|
|
emit sendWordToFavorites( definition->getTitle(), cfg.lastPopupGroupId );
|
2017-10-23 14:21:43 +00:00
|
|
|
|
|
|
|
ui.sendWordToFavoritesButton->setIcon( blueStarIcon );
|
2017-05-15 15:08:06 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
void ScanPopup::switchExpandOptionalPartsMode()
|
|
|
|
{
|
|
|
|
if( isVisible() )
|
|
|
|
emit switchExpandMode();
|
|
|
|
}
|
2012-12-24 12:59:46 +00:00
|
|
|
|
|
|
|
void ScanPopup::updateBackForwardButtons()
|
|
|
|
{
|
|
|
|
ui.goBackButton->setEnabled(definition->canGoBack());
|
|
|
|
ui.goForwardButton->setEnabled(definition->canGoForward());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::on_goBackButton_clicked()
|
|
|
|
{
|
|
|
|
definition->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::on_goForwardButton_clicked()
|
|
|
|
{
|
|
|
|
definition->forward();
|
|
|
|
}
|
2013-02-27 13:12:46 +00:00
|
|
|
|
|
|
|
void ScanPopup::setDictionaryIconSize()
|
|
|
|
{
|
|
|
|
int extent = cfg.usingSmallIconsInToolbars ?
|
|
|
|
QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize ) :
|
|
|
|
21;
|
|
|
|
dictionaryBar.setDictionaryIconSize( extent );
|
|
|
|
}
|
2017-03-10 13:29:23 +00:00
|
|
|
|
|
|
|
void ScanPopup::setGroupByName( QString const & name )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < ui.groupList->count(); i++ )
|
|
|
|
{
|
|
|
|
if( ui.groupList->itemText( i ) == name )
|
|
|
|
{
|
|
|
|
ui.groupList->setCurrentIndex( i );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i >= ui.groupList->count() )
|
|
|
|
gdWarning( "Group \"%s\" for popup window is not found\n", name.toUtf8().data() );
|
|
|
|
}
|
2017-07-13 15:00:19 +00:00
|
|
|
|
|
|
|
void ScanPopup::alwaysOnTopClicked( bool checked )
|
|
|
|
{
|
|
|
|
bool wasVisible = isVisible();
|
|
|
|
if( ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
Qt::WindowFlags flags = this->windowFlags();
|
|
|
|
if( checked )
|
|
|
|
setWindowFlags(flags | Qt::WindowStaysOnTopHint );
|
|
|
|
else
|
|
|
|
setWindowFlags(flags ^ Qt::WindowStaysOnTopHint );
|
|
|
|
if( wasVisible )
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 14:46:59 +00:00
|
|
|
|
|
|
|
void ScanPopup::titleChanged( ArticleView *, QString const & title )
|
|
|
|
{
|
|
|
|
unsigned groupId = ui.groupList->getCurrentGroup();
|
|
|
|
|
|
|
|
// Set icon for "Add to Favorites" button
|
|
|
|
ui.sendWordToFavoritesButton->setIcon( isWordPresentedInFavorites( title, groupId ) ?
|
|
|
|
blueStarIcon : starIcon );
|
|
|
|
}
|