2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "scanpopup.hh"
|
2009-02-01 00:08:08 +00:00
|
|
|
#include "folding.hh"
|
2009-02-02 20:28:53 +00:00
|
|
|
#include "mouseover.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QUrl>
|
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>
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
using std::wstring;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
ScanPopup::ScanPopup( QWidget * parent,
|
2009-02-06 17:04:11 +00:00
|
|
|
Config::Class & cfg_,
|
2009-02-05 20:55:00 +00:00
|
|
|
ArticleNetworkAccessManager & articleNetMgr,
|
2009-02-01 00:08:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
|
|
|
|
Instances::Groups const & groups_ ):
|
|
|
|
QDialog( 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-02-06 13:05:25 +00:00
|
|
|
wordFinder( this ),
|
2009-04-10 13:56:38 +00:00
|
|
|
mouseEnteredOnce( false ),
|
|
|
|
hideTimer( this )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
ui.queryError->hide();
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
definition = new ArticleView( ui.outerFrame, articleNetMgr, groups, true,
|
|
|
|
cfg );
|
2009-02-01 00:08:08 +00:00
|
|
|
ui.mainLayout->addWidget( definition );
|
|
|
|
|
2009-04-10 15:52:08 +00:00
|
|
|
ui.wordListButton->hide();
|
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
|
|
|
|
2009-02-01 05:15:25 +00:00
|
|
|
setWindowFlags( Qt::Popup );
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-02-06 17:16:33 +00:00
|
|
|
if ( cfg.lastPopupSize.isValid() )
|
|
|
|
resize( cfg.lastPopupSize );
|
|
|
|
|
2009-02-06 13:05:25 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// On Windows, leaveEvent() doesn't seem to work with popups and we're trying
|
|
|
|
// to emulate it.
|
|
|
|
setMouseTracking( true );
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-04-10 21:07:03 +00:00
|
|
|
connect( definition, SIGNAL( pageLoaded() ),
|
|
|
|
this, SLOT( pageLoaded() ) );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
connect( QApplication::clipboard(), SIGNAL( changed( QClipboard::Mode ) ),
|
|
|
|
this, SLOT( clipboardChanged( QClipboard::Mode ) ) );
|
2009-02-02 20:28:53 +00:00
|
|
|
|
|
|
|
connect( &MouseOver::instance(), SIGNAL( hovered( QString const & ) ),
|
|
|
|
this, SLOT( mouseHovered( QString const & ) ) );
|
2009-04-10 13:56:38 +00:00
|
|
|
|
|
|
|
hideTimer.setSingleShot( true );
|
|
|
|
hideTimer.setInterval( 400 );
|
|
|
|
|
|
|
|
connect( &hideTimer, SIGNAL( timeout() ),
|
|
|
|
this, SLOT( hideTimerExpired() ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-08 20:20:02 +00:00
|
|
|
ScanPopup::~ScanPopup()
|
|
|
|
{
|
|
|
|
disableScanning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::enableScanning()
|
|
|
|
{
|
|
|
|
if ( !isScanningEnabled )
|
|
|
|
{
|
|
|
|
isScanningEnabled = true;
|
|
|
|
MouseOver::instance().enableMouseOver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::disableScanning()
|
|
|
|
{
|
|
|
|
if ( isScanningEnabled )
|
|
|
|
{
|
|
|
|
MouseOver::instance().disableMouseOver();
|
|
|
|
isScanningEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
printf( "clipboard changed\n" );
|
|
|
|
|
|
|
|
QString subtype = "plain";
|
|
|
|
|
2009-02-02 20:28:53 +00:00
|
|
|
handleInputWord( QApplication::clipboard()->text( subtype, m ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::mouseHovered( QString const & str )
|
|
|
|
{
|
|
|
|
handleInputWord( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::handleInputWord( QString const & str )
|
|
|
|
{
|
2009-02-05 20:55:00 +00:00
|
|
|
if ( !cfg.preferences.enableScanPopup )
|
|
|
|
return;
|
|
|
|
|
2009-02-05 16:56:57 +00:00
|
|
|
// Check key modifiers
|
|
|
|
|
2009-02-05 20:55:00 +00:00
|
|
|
if ( cfg.preferences.enableScanPopupModifiers &&
|
|
|
|
!checkModifiersPressed( cfg.preferences.scanPopupModifiers ) )
|
2009-02-05 16:56:57 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-09 23:33:37 +00:00
|
|
|
inputWord = QString::fromStdWString( Folding::trimWhitespaceOrPunct( str.toStdWString() ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
if ( !inputWord.size() )
|
2009-01-28 20:55:45 +00:00
|
|
|
return;
|
|
|
|
|
2009-02-05 20:55:00 +00:00
|
|
|
/// Too large strings make window expand which is probably not what user
|
|
|
|
/// wants
|
|
|
|
ui.word->setText( elideInputWord() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
ui.wordListButton->hide();
|
|
|
|
ui.pronounceButton->hide();
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
if ( !isVisible() )
|
|
|
|
{
|
2009-02-08 16:35:30 +00:00
|
|
|
// Decide where should the window land
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
QPoint currentPos = QCursor::pos();
|
|
|
|
|
2009-02-08 16:35:30 +00:00
|
|
|
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-buttom 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();
|
|
|
|
|
2009-02-06 13:05:25 +00:00
|
|
|
mouseEnteredOnce = false; // Windows-only
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
initiateTranslation();
|
|
|
|
}
|
|
|
|
|
2009-02-05 20:55:00 +00:00
|
|
|
QString ScanPopup::elideInputWord()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
if ( isVisible() )
|
|
|
|
initiateTranslation();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ScanPopup::initiateTranslation()
|
|
|
|
{
|
2009-04-10 12:48:40 +00:00
|
|
|
definition->showDefinition( inputWord, ui.groupList->getCurrentGroup() );
|
2009-03-26 19:00:08 +00:00
|
|
|
wordFinder.prefixMatch( inputWord, getActiveDicts() );
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > const & ScanPopup::getActiveDicts()
|
|
|
|
{
|
|
|
|
int currentGroup = ui.groupList->currentIndex();
|
|
|
|
|
|
|
|
return
|
|
|
|
currentGroup < 0 || currentGroup >= (int)groups.size() ? allDictionaries :
|
|
|
|
groups[ currentGroup ].dictionaries;
|
|
|
|
}
|
|
|
|
|
2009-02-06 18:43:07 +00:00
|
|
|
void ScanPopup::mousePressEvent( QMouseEvent * ev )
|
|
|
|
{
|
|
|
|
startPos = ev->globalPos();
|
|
|
|
setCursor( Qt::ClosedHandCursor );
|
|
|
|
|
|
|
|
QDialog::mousePressEvent( ev );
|
|
|
|
}
|
|
|
|
|
2009-02-06 13:05:25 +00:00
|
|
|
void ScanPopup::mouseMoveEvent( QMouseEvent * event )
|
|
|
|
{
|
2009-02-06 18:43:07 +00:00
|
|
|
if ( event->buttons() )
|
|
|
|
{
|
|
|
|
QPoint newPos = event->globalPos();
|
|
|
|
|
|
|
|
QPoint delta = newPos - startPos;
|
|
|
|
|
|
|
|
startPos = newPos;
|
|
|
|
|
|
|
|
// Find a top-level window
|
|
|
|
|
|
|
|
move( pos() + delta );
|
|
|
|
}
|
2009-02-06 13:05:25 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2009-02-06 18:43:07 +00:00
|
|
|
else
|
2009-02-06 13:05:25 +00:00
|
|
|
if ( !ui.pinButton->isChecked() )
|
|
|
|
{
|
|
|
|
if ( !mouseEnteredOnce )
|
|
|
|
{
|
|
|
|
// We're waiting for mouse to enter window
|
|
|
|
if ( geometry().contains( event->globalPos() ) )
|
2009-04-10 13:56:38 +00:00
|
|
|
{
|
2009-02-06 13:05:25 +00:00
|
|
|
mouseEnteredOnce = true;
|
2009-04-10 13:56:38 +00:00
|
|
|
hideTimer.stop();
|
|
|
|
}
|
2009-02-06 13:05:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We're waiting for mouse to leave window
|
|
|
|
if ( !geometry().contains( event->globalPos() ) )
|
|
|
|
{
|
|
|
|
mouseEnteredOnce = false;
|
2009-04-10 13:56:38 +00:00
|
|
|
hideTimer.start();
|
2009-02-06 13:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QDialog::mouseMoveEvent( event );
|
|
|
|
}
|
|
|
|
|
2009-02-06 18:43:07 +00:00
|
|
|
void ScanPopup::mouseReleaseEvent( QMouseEvent * ev )
|
|
|
|
{
|
|
|
|
unsetCursor();
|
|
|
|
QDialog::mouseReleaseEvent( ev );
|
|
|
|
}
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
void ScanPopup::leaveEvent( QEvent * event )
|
|
|
|
{
|
|
|
|
QDialog::leaveEvent( event );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
// We hide the popup when the mouse leaves it. So in order to close it
|
|
|
|
// without any clicking the cursor has to get inside and then to leave.
|
|
|
|
|
|
|
|
// 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 )
|
|
|
|
{
|
|
|
|
QDialog::enterEvent( event );
|
|
|
|
|
|
|
|
// If there was a countdown to hide the window, stop it.
|
|
|
|
hideTimer.stop();
|
|
|
|
}
|
|
|
|
|
2009-02-06 17:16:33 +00:00
|
|
|
void ScanPopup::resizeEvent( QResizeEvent * event )
|
|
|
|
{
|
|
|
|
cfg.lastPopupSize = event->size();
|
|
|
|
|
|
|
|
QDialog::resizeEvent( event );
|
|
|
|
}
|
|
|
|
|
2009-02-08 21:26:35 +00:00
|
|
|
void ScanPopup::showEvent( QShowEvent * ev )
|
|
|
|
{
|
|
|
|
QDialog::showEvent( ev );
|
|
|
|
|
|
|
|
if ( groups.empty() )
|
|
|
|
ui.groupList->hide();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ui.wordListButton->setVisible( wordFinder.getPrefixMatchResults().size() );
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-10 15:52:08 +00:00
|
|
|
void ScanPopup::on_wordListButton_clicked()
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
|
|
|
if ( !isVisible() )
|
|
|
|
return;
|
|
|
|
|
2009-04-10 15:52:08 +00:00
|
|
|
WordFinder::SearchResults const & results = wordFinder.getPrefixMatchResults();
|
|
|
|
|
|
|
|
if ( results.empty() )
|
2009-02-01 00:08:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
QMenu menu( this );
|
|
|
|
|
2009-04-10 15:52:08 +00:00
|
|
|
unsigned total = results.size() < 20 ? results.size() : 20;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < total; ++x )
|
|
|
|
{
|
|
|
|
// Some items are just too large! For now skip them.
|
|
|
|
|
|
|
|
if ( results[ x ].first.size() > 64 )
|
|
|
|
{
|
|
|
|
if ( total < results.size() )
|
|
|
|
++total;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
menu.addAction( results[ x ].first );
|
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-04-10 15:52:08 +00:00
|
|
|
QAction * result = menu.exec( mapToGlobal( ui.wordListButton->pos() ) +
|
|
|
|
QPoint( 0, ui.wordListButton->height() ) );
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
if ( result )
|
2009-04-10 12:48:40 +00:00
|
|
|
definition->showDefinition( result->text(), ui.groupList->getCurrentGroup() );
|
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
|
|
|
{
|
|
|
|
setWindowFlags( Qt::Dialog );
|
|
|
|
setWindowTitle( elideInputWord() );
|
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
|
2009-02-01 05:15:25 +00:00
|
|
|
setWindowFlags( Qt::Popup );
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
show();
|
|
|
|
}
|
2009-04-10 13:56:38 +00:00
|
|
|
|
|
|
|
void ScanPopup::hideTimerExpired()
|
|
|
|
{
|
|
|
|
if ( isVisible() )
|
|
|
|
{
|
|
|
|
unsetCursor(); // Just in case
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
2009-04-10 21:07:03 +00:00
|
|
|
|
|
|
|
void ScanPopup::pageLoaded()
|
|
|
|
{
|
|
|
|
ui.pronounceButton->setVisible( definition->hasSound() );
|
|
|
|
|
|
|
|
if ( cfg.preferences.pronounceOnLoadPopup )
|
|
|
|
definition->playSound();
|
|
|
|
}
|