Add scan popup flag

After select a word, show a flag window, click the flag to show
popup window.
This commit is contained in:
sunwxg 2017-06-05 21:15:38 +08:00
parent 971e7176ca
commit ad8008c37b
11 changed files with 260 additions and 7 deletions

View file

@ -109,6 +109,9 @@ Preferences::Preferences():
scanPopupUseIAccessibleEx( true ),
scanPopupUseGDMessage( true ),
scanToMainWindow( false ),
#ifdef HAVE_X11
showScanFlag( false ),
#endif
pronounceOnLoadMain( false ),
pronounceOnLoadPopup( false ),
#ifndef DISABLE_INTERNAL_PLAYER
@ -758,6 +761,9 @@ Class load() throw( exError )
if ( !preferences.namedItem( "scanPopupAltModeSecs" ).isNull() )
c.preferences.scanPopupAltModeSecs = preferences.namedItem( "scanPopupAltModeSecs" ).toElement().text().toUInt();
c.preferences.scanToMainWindow = ( preferences.namedItem( "scanToMainWindow" ).toElement().text() == "1" );
#ifdef HAVE_X11
c.preferences.showScanFlag= ( preferences.namedItem( "showScanFlag" ).toElement().text() == "1" );
#endif
c.preferences.scanPopupUseUIAutomation = ( preferences.namedItem( "scanPopupUseUIAutomation" ).toElement().text() == "1" );
c.preferences.scanPopupUseIAccessibleEx = ( preferences.namedItem( "scanPopupUseIAccessibleEx" ).toElement().text() == "1" );
c.preferences.scanPopupUseGDMessage = ( preferences.namedItem( "scanPopupUseGDMessage" ).toElement().text() == "1" );
@ -1622,6 +1628,12 @@ void save( Class const & c ) throw( exError )
opt.appendChild( dd.createTextNode( c.preferences.scanToMainWindow ? "1":"0" ) );
preferences.appendChild( opt );
#ifdef HAVE_X11
opt = dd.createElement( "showScanFlag" );
opt.appendChild( dd.createTextNode( c.preferences.showScanFlag? "1":"0" ) );
preferences.appendChild( opt );
#endif
opt = dd.createElement( "scanPopupUseUIAutomation" );
opt.appendChild( dd.createTextNode( c.preferences.scanPopupUseUIAutomation ? "1":"0" ) );
preferences.appendChild( opt );

View file

@ -214,6 +214,9 @@ struct Preferences
bool scanPopupUseIAccessibleEx;
bool scanPopupUseGDMessage;
bool scanToMainWindow;
#ifdef HAVE_X11
bool showScanFlag;
#endif
// Whether the word should be pronounced on page load, in main window/popup
bool pronounceOnLoadMain, pronounceOnLoadPopup;

View file

@ -79,8 +79,8 @@ win32 {
LIBS += -L$${PWD}/winlibs/lib
}
!x64:QMAKE_LFLAGS += -Wl,--large-address-aware
isEmpty(HUNSPELL_LIB) {
isEmpty(HUNSPELL_LIB) {
CONFIG(gcc48) {
LIBS += -lhunspell-1.3.2
} else {
@ -514,6 +514,12 @@ mac {
SOURCES += texttospeechsource.cc
}
unix:!mac {
HEADERS += scanflag.hh
FORMS += scanflag.ui
SOURCES += scanflag.cc
}
CONFIG( zim_support ) {
DEFINES += MAKE_ZIM_SUPPORT
LIBS += -llzma

View file

@ -22,6 +22,9 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
connect( ui.enableScanPopupModifiers, SIGNAL( toggled( bool ) ),
this, SLOT( enableScanPopupModifiersToggled( bool ) ) );
connect( ui.showScanFlag, SIGNAL( toggled( bool ) ),
this, SLOT( showScanFlagToggled( bool ) ) );
connect( ui.altKey, SIGNAL( clicked( bool ) ),
this, SLOT( wholeAltClicked( bool ) ) );
connect( ui.ctrlKey, SIGNAL( clicked( bool ) ),
@ -229,6 +232,12 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
// ui.tabWidget->removeTab( 5 );
#endif
#ifdef HAVE_X11
ui.showScanFlag->setChecked( p.showScanFlag);
#else
ui.showScanFlag->hide();
#endif
// Sound
ui.pronounceOnLoadMain->setChecked( p.pronounceOnLoadMain );
@ -366,6 +375,9 @@ Config::Preferences Preferences::getPreferences()
p.scanPopupAltMode = ui.scanPopupAltMode->isChecked();
p.scanPopupAltModeSecs = ui.scanPopupAltModeSecs->value();
p.scanToMainWindow = ui.scanToMainWindow->isChecked();
#ifdef HAVE_X11
p.showScanFlag= ui.showScanFlag->isChecked();
#endif
p.scanPopupUseUIAutomation = ui.scanPopupUseUIAutomation->isChecked();
p.scanPopupUseIAccessibleEx = ui.scanPopupUseIAccessibleEx->isChecked();
p.scanPopupUseGDMessage = ui.scanPopupUseGDMessage->isChecked();
@ -507,6 +519,14 @@ void Preferences::enableScanPopupToggled( bool b )
void Preferences::enableScanPopupModifiersToggled( bool b )
{
ui.scanPopupModifiers->setEnabled( b && ui.enableScanPopup->isChecked() );
if( b )
ui.showScanFlag->setChecked( false );
}
void Preferences::showScanFlagToggled( bool b )
{
if( b )
ui.enableScanPopupModifiers->setChecked( false );
}
void Preferences::wholeAltClicked( bool b )

View file

@ -33,6 +33,7 @@ private slots:
void enableScanPopupToggled( bool );
void enableScanPopupModifiersToggled( bool );
void showScanFlagToggled( bool b );
void wholeAltClicked( bool );
void wholeCtrlClicked( bool );

View file

@ -679,6 +679,16 @@ seconds, which is specified here.</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="showScanFlag">
<property name="toolTip">
<string>Show a flag window before showing popup window, click the flag to show popup window. </string>
</property>
<property name="text">
<string>Show scan flag when word is selected</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

90
scanflag.cc Normal file
View file

@ -0,0 +1,90 @@
#include <QCursor>
#include <QDesktopWidget>
#include "scanflag.hh"
#include "ui_scanflag.h"
static Qt::WindowFlags popupWindowFlags =
Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
#if QT_VERSION >= 0x050000
| Qt::WindowDoesNotAcceptFocus
#endif
;
ScanFlag::ScanFlag(QWidget *parent) :
QMainWindow(parent)
{
ui.setupUi( this );
setWindowFlags( popupWindowFlags );
setAttribute(Qt::WA_X11DoNotAcceptFocus);
hideTimer.setSingleShot( true );
hideTimer.setInterval( 1500 );
connect( &hideTimer, SIGNAL( timeout() ),
this, SLOT( hideWindow() ) );
connect( ui.pushButton, SIGNAL( clicked( bool ) ),
this, SLOT( pushButtonClicked() ) );
}
ScanFlag::~ScanFlag()
{
}
void ScanFlag::pushButtonClicked()
{
hideTimer.stop();
hide();
emit showScanPopup();
}
void ScanFlag::hideWindow()
{
if ( isVisible() )
hide();
}
void ScanFlag::showScanFlag()
{
if ( isVisible() )
hide();
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-top placement
if ( currentPos.y() - 15 - windowSize.height() >= desktop.y() )
y = currentPos.y() - 15 - windowSize.height();
else
/// Try the to-the-bottom placement
if ( currentPos.y() + 15 + windowSize.height() <= desktop.bottomLeft().y() )
y = currentPos.y() + 15;
else
// Center it
y = desktop.y() + ( desktop.height() - windowSize.height() ) / 2;
move( x, y );
show();
hideTimer.start();
}

33
scanflag.hh Normal file
View file

@ -0,0 +1,33 @@
#ifndef SCAN_FLAG_H
#define SCAN_FLAG_H
#include "config.hh"
#include <QMainWindow>
#include <QTimer>
#include "ui_scanflag.h"
class ScanFlag : public QMainWindow
{
Q_OBJECT
public:
ScanFlag( QWidget *parent );
~ScanFlag();
signals:
void showScanPopup ();
private:
Ui::ScanFlag ui;
QTimer hideTimer;
private slots:
void showScanFlag();
void pushButtonClicked();
void hideWindow();
};
#endif // SCAN_FLAG_H

38
scanflag.ui Normal file
View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ScanFlag</class>
<widget class="QMainWindow" name="ScanFlag">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>30</width>
<height>30</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>30</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>:/icons/programicon.png</normaloff>:/icons/programicon.png</iconset>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -35,7 +35,7 @@ Qt::Popup
ScanPopup::ScanPopup( QWidget * parent,
Config::Class & cfg_,
ArticleNetworkAccessManager & articleNetMgr,
ArticleNetworkAccessManager & articleNetMgr,
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
Instances::Groups const & groups_,
History & history_ ):
@ -92,7 +92,7 @@ ScanPopup::ScanPopup( QWidget * parent,
this, SLOT( typingEvent( QString const & ) ) );
applyZoomFactor();
ui.mainLayout->addWidget( definition );
ui.translateBox->wordList()->attachFinder( &wordFinder );
@ -270,6 +270,16 @@ ScanPopup::ScanPopup( QWidget * parent,
grabGesture( Gestures::GDPinchGestureType );
grabGesture( Gestures::GDSwipeGestureType );
#endif
#ifdef HAVE_X11
scanFlag = new ScanFlag( 0 );
connect( this, SIGNAL( showScanFlag( bool ) ),
scanFlag, SLOT( showScanFlag() ) );
connect( scanFlag, SIGNAL( showScanPopup() ),
this, SLOT( showEngagePopup() ) );
#endif
}
ScanPopup::~ScanPopup()
@ -362,7 +372,7 @@ void ScanPopup::clipboardChanged( QClipboard::Mode m )
{
if ( !isScanningEnabled )
return;
GD_DPRINTF( "clipboard changed\n" );
QString subtype = "plain";
@ -400,6 +410,14 @@ void ScanPopup::handleInputWord( QString const & str, bool forcePopup )
return;
}
#ifdef HAVE_X11
if ( cfg.preferences.showScanFlag ) {
inputWord = pendingInputWord;
emit showScanFlag( forcePopup );
return;
}
#endif
// Check key modifiers
if ( cfg.preferences.enableScanPopupModifiers && !checkModifiersPressed( cfg.preferences.scanPopupModifiers ) )
@ -417,6 +435,13 @@ void ScanPopup::handleInputWord( QString const & str, bool forcePopup )
engagePopup( forcePopup );
}
#ifdef HAVE_X11
void ScanPopup::showEngagePopup()
{
engagePopup(false);
}
#endif
void ScanPopup::engagePopup( bool forcePopup, bool giveFocus )
{
if( cfg.preferences.scanToMainWindow && !forcePopup )
@ -745,7 +770,7 @@ void ScanPopup::mouseMoveEvent( QMouseEvent * event )
move( pos() + delta );
}
QMainWindow::mouseMoveEvent( event );
}
@ -808,7 +833,7 @@ void ScanPopup::showEvent( QShowEvent * ev )
QMainWindow::showEvent( ev );
QTimer::singleShot(100, this, SLOT( requestWindowFocus() ) );
if ( groups.size() <= 1 ) // Only the default group? Hide then.
ui.groupList->hide();

View file

@ -15,6 +15,9 @@
#include "history.hh"
#include "dictionarybar.hh"
#include "mainstatusbar.hh"
#ifdef HAVE_X11
#include "scanflag.hh"
#endif
/// This is a popup dialog to show translations when clipboard scanning mode
/// is enabled.
@ -70,6 +73,10 @@ signals:
/// Put translated word into Favorites
void sendWordToFavorites( QString const & word, unsigned groupId );
#ifdef HAVE_X11
void showScanFlag( bool forcePopup );
#endif
#ifdef Q_OS_WIN32
/// Ask for source window is current translate tab
bool isGoldenDictWindow( HWND hwnd );
@ -87,6 +94,10 @@ public slots:
void setGroupByName( QString const & name );
#ifdef HAVE_X11
void showEngagePopup();
#endif
private:
// Translates the word from the clipboard or the clipboard selection
@ -118,6 +129,10 @@ private:
DictionaryBar dictionaryBar;
MainStatusBar * mainStatusBar;
#ifdef HAVE_X11
ScanFlag * scanFlag;
#endif
bool mouseEnteredOnce;
bool mouseIntercepted;