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 "articleview.hh"
|
|
|
|
#include "externalviewer.hh"
|
2009-04-12 16:22:42 +00:00
|
|
|
#include <map>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QWebHitTestResult>
|
|
|
|
#include <QMenu>
|
2009-02-01 00:08:08 +00:00
|
|
|
#include <QDesktopServices>
|
2009-05-11 22:25:22 +00:00
|
|
|
#include <QWebHistory>
|
2009-05-12 10:52:11 +00:00
|
|
|
#include <QClipboard>
|
2009-05-12 13:25:18 +00:00
|
|
|
#include <QKeyEvent>
|
2013-02-22 12:44:23 +00:00
|
|
|
#include <QFileDialog>
|
2009-05-12 10:52:11 +00:00
|
|
|
#include "folding.hh"
|
|
|
|
#include "wstring_qt.hh"
|
2010-05-29 11:33:04 +00:00
|
|
|
#include "webmultimediadownload.hh"
|
2011-05-29 05:08:37 +00:00
|
|
|
#include "programs.hh"
|
2011-06-19 18:50:11 +00:00
|
|
|
#include "dprintf.hh"
|
2011-07-02 13:04:49 +00:00
|
|
|
#include <QDebug>
|
2013-02-23 21:08:27 +00:00
|
|
|
#include <QWebElement>
|
2013-04-24 14:52:04 +00:00
|
|
|
#include <QCryptographicHash>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-11-14 15:38:41 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h> // For PlaySound
|
2012-11-23 12:43:36 +00:00
|
|
|
#include "bass.hh"
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2013-04-24 14:52:04 +00:00
|
|
|
#include "speechclient.hh"
|
|
|
|
|
2012-09-18 23:01:31 +00:00
|
|
|
#include <QPainter>
|
2010-11-14 15:38:41 +00:00
|
|
|
#endif
|
2012-09-18 23:01:31 +00:00
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
#include <QBuffer>
|
2010-03-29 13:13:29 +00:00
|
|
|
|
|
|
|
// Phonon headers are a mess. How to include them properly? Send patches if you
|
|
|
|
// know.
|
|
|
|
#ifdef __WIN32
|
2010-01-22 22:50:28 +00:00
|
|
|
#include <Phonon/AudioOutput>
|
|
|
|
#include <Phonon/MediaObject>
|
2010-03-29 13:13:29 +00:00
|
|
|
#else
|
|
|
|
#include <phonon/audiooutput.h>
|
|
|
|
#include <phonon/mediaobject.h>
|
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
using std::map;
|
2009-03-26 19:00:08 +00:00
|
|
|
using std::list;
|
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
/// A phonon-based audio player, created on demand
|
|
|
|
struct AudioPlayer
|
|
|
|
{
|
|
|
|
Phonon::AudioOutput output;
|
|
|
|
Phonon::MediaObject object;
|
|
|
|
|
|
|
|
static AudioPlayer & instance();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
AudioPlayer();
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioPlayer::AudioPlayer():
|
|
|
|
output( Phonon::AccessibilityCategory )
|
|
|
|
{
|
|
|
|
Phonon::createPath( &object, &output );
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioPlayer & AudioPlayer::instance()
|
|
|
|
{
|
|
|
|
static AudioPlayer a;
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
|
2009-04-12 16:22:42 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
|
2009-04-10 21:07:03 +00:00
|
|
|
Instances::Groups const & groups_, bool popupView_,
|
2009-05-14 19:27:19 +00:00
|
|
|
Config::Class const & cfg_,
|
2009-09-23 18:44:38 +00:00
|
|
|
QAction * dictionaryBarToggled_,
|
2009-05-14 19:27:19 +00:00
|
|
|
GroupComboBox const * groupComboBox_ ):
|
2009-01-28 20:55:45 +00:00
|
|
|
QFrame( parent ),
|
|
|
|
articleNetMgr( nm ),
|
2009-04-12 16:22:42 +00:00
|
|
|
allDictionaries( allDictionaries_ ),
|
2009-01-28 20:55:45 +00:00
|
|
|
groups( groups_ ),
|
2009-04-10 21:07:03 +00:00
|
|
|
popupView( popupView_ ),
|
2009-05-12 10:52:11 +00:00
|
|
|
cfg( cfg_ ),
|
2009-05-14 19:27:19 +00:00
|
|
|
pasteAction( this ),
|
2009-05-15 14:11:54 +00:00
|
|
|
articleUpAction( this ),
|
|
|
|
articleDownAction( this ),
|
2009-05-29 22:04:43 +00:00
|
|
|
goBackAction( this ),
|
|
|
|
goForwardAction( this ),
|
2009-05-16 11:14:43 +00:00
|
|
|
openSearchAction( this ),
|
2013-01-18 14:37:24 +00:00
|
|
|
selectCurrentArticleAction( this ),
|
2009-05-16 11:14:43 +00:00
|
|
|
searchIsOpened( false ),
|
2009-09-23 18:44:38 +00:00
|
|
|
dictionaryBarToggled( dictionaryBarToggled_ ),
|
2009-05-14 19:27:19 +00:00
|
|
|
groupComboBox( groupComboBox_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
goBackAction.setShortcut( QKeySequence( "Alt+Left" ) );
|
|
|
|
ui.definition->addAction( &goBackAction );
|
|
|
|
connect( &goBackAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( back() ) );
|
2009-05-15 14:24:37 +00:00
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
goForwardAction.setShortcut( QKeySequence( "Alt+Right" ) );
|
|
|
|
ui.definition->addAction( &goForwardAction );
|
|
|
|
connect( &goForwardAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( forward() ) );
|
2009-05-15 14:24:37 +00:00
|
|
|
|
2009-04-03 21:57:23 +00:00
|
|
|
ui.definition->pageAction( QWebPage::Copy )->setShortcut( QKeySequence::Copy );
|
|
|
|
ui.definition->addAction( ui.definition->pageAction( QWebPage::Copy ) );
|
|
|
|
|
2013-01-18 13:22:24 +00:00
|
|
|
QAction * selectAll = ui.definition->pageAction( QWebPage::SelectAll );
|
|
|
|
selectAll->setShortcut( QKeySequence::SelectAll );
|
|
|
|
selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
ui.definition->addAction( selectAll );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
ui.definition->setContextMenuPolicy( Qt::CustomContextMenu );
|
|
|
|
|
|
|
|
ui.definition->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
|
|
|
|
|
|
|
|
ui.definition->page()->setNetworkAccessManager( &articleNetMgr );
|
|
|
|
|
2009-02-08 16:50:18 +00:00
|
|
|
connect( ui.definition, SIGNAL( loadFinished( bool ) ),
|
|
|
|
this, SLOT( loadFinished( bool ) ) );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
attachToJavaScript();
|
|
|
|
connect( ui.definition->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ),
|
|
|
|
this, SLOT( attachToJavaScript() ) );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
connect( ui.definition, SIGNAL( titleChanged( QString const & ) ),
|
|
|
|
this, SLOT( handleTitleChanged( QString const & ) ) );
|
|
|
|
|
|
|
|
connect( ui.definition, SIGNAL( urlChanged( QUrl const & ) ),
|
|
|
|
this, SLOT( handleUrlChanged( QUrl const & ) ) );
|
|
|
|
|
|
|
|
connect( ui.definition, SIGNAL( customContextMenuRequested( QPoint const & ) ),
|
|
|
|
this, SLOT( contextMenuRequested( QPoint const & ) ) );
|
|
|
|
|
|
|
|
connect( ui.definition, SIGNAL( linkClicked( QUrl const & ) ),
|
|
|
|
this, SLOT( linkClicked( QUrl const & ) ) );
|
2009-05-11 15:33:57 +00:00
|
|
|
|
2011-07-02 13:04:49 +00:00
|
|
|
connect( ui.definition->page(), SIGNAL( linkHovered ( const QString &, const QString &, const QString & ) ),
|
|
|
|
this, SLOT( linkHovered ( const QString &, const QString &, const QString & ) ) );
|
|
|
|
|
2010-04-08 20:37:59 +00:00
|
|
|
connect( ui.definition, SIGNAL(doubleClicked()),this,SLOT(doubleClicked()) );
|
|
|
|
|
2009-05-12 10:52:11 +00:00
|
|
|
pasteAction.setShortcut( QKeySequence::Paste );
|
|
|
|
ui.definition->addAction( &pasteAction );
|
|
|
|
connect( &pasteAction, SIGNAL( triggered() ), this, SLOT( pasteTriggered() ) );
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
articleUpAction.setShortcut( QKeySequence( "Alt+Up" ) );
|
|
|
|
ui.definition->addAction( &articleUpAction );
|
|
|
|
connect( &articleUpAction, SIGNAL( triggered() ), this, SLOT( moveOneArticleUp() ) );
|
|
|
|
|
|
|
|
articleDownAction.setShortcut( QKeySequence( "Alt+Down" ) );
|
|
|
|
ui.definition->addAction( &articleDownAction );
|
|
|
|
connect( &articleDownAction, SIGNAL( triggered() ), this, SLOT( moveOneArticleDown() ) );
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
openSearchAction.setShortcut( QKeySequence( "Ctrl+F" ) );
|
|
|
|
ui.definition->addAction( &openSearchAction );
|
|
|
|
connect( &openSearchAction, SIGNAL( triggered() ), this, SLOT( openSearch() ) );
|
|
|
|
|
2013-01-18 14:37:24 +00:00
|
|
|
selectCurrentArticleAction.setShortcut( QKeySequence( "Ctrl+Shift+A" ));
|
|
|
|
selectCurrentArticleAction.setText( tr( "Select Current Article" ) );
|
|
|
|
ui.definition->addAction( &selectCurrentArticleAction );
|
|
|
|
connect( &selectCurrentArticleAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( selectCurrentArticle() ) );
|
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
ui.definition->installEventFilter( this );
|
2013-01-22 21:04:45 +00:00
|
|
|
ui.searchFrame->installEventFilter( this );
|
2009-05-12 13:25:18 +00:00
|
|
|
|
2009-05-11 15:33:57 +00:00
|
|
|
// Load the default blank page instantly, so there would be no flicker.
|
|
|
|
|
|
|
|
QString contentType;
|
|
|
|
QUrl blankPage( "gdlookup://localhost?blank=1" );
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > r = articleNetMgr.getResource( blankPage,
|
|
|
|
contentType );
|
|
|
|
|
2011-08-02 06:50:15 +00:00
|
|
|
ui.definition->setHtml( QString::fromUtf8( &( r->getFullData().front() ),
|
|
|
|
r->getFullData().size() ),
|
|
|
|
blankPage );
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
expandOptionalParts = cfg.preferences.alwaysExpandOptionalParts;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 20:32:16 +00:00
|
|
|
// explicitly report the minimum size, to avoid
|
|
|
|
// sidebar widgets' improper resize during restore
|
|
|
|
QSize ArticleView::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return ui.searchFrame->minimumSizeHint();
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
void ArticleView::setGroupComboBox( GroupComboBox const * g )
|
|
|
|
{
|
|
|
|
groupComboBox = g;
|
|
|
|
}
|
|
|
|
|
2009-02-06 15:37:37 +00:00
|
|
|
ArticleView::~ArticleView()
|
|
|
|
{
|
2009-02-08 21:54:19 +00:00
|
|
|
cleanupTemp();
|
2010-11-14 15:38:41 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
if ( winWavData.size() )
|
|
|
|
{
|
|
|
|
// If we were playing some sound some time ago, make sure it stopped
|
|
|
|
// playing before freeing the waveform memory.
|
|
|
|
PlaySoundA( 0, 0, 0 );
|
|
|
|
}
|
|
|
|
#endif
|
2009-02-06 15:37:37 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
void ArticleView::showDefinition( QString const & word, unsigned group,
|
2009-05-29 19:48:50 +00:00
|
|
|
QString const & scrollTo,
|
|
|
|
Contexts const & contexts )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
QUrl req;
|
|
|
|
|
|
|
|
req.setScheme( "gdlookup" );
|
|
|
|
req.setHost( "localhost" );
|
|
|
|
req.addQueryItem( "word", word );
|
2009-04-10 12:48:40 +00:00
|
|
|
req.addQueryItem( "group", QString::number( group ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
if ( scrollTo.size() )
|
2009-05-29 19:48:50 +00:00
|
|
|
req.addQueryItem( "scrollto", scrollTo );
|
|
|
|
|
|
|
|
if ( contexts.size() )
|
|
|
|
{
|
|
|
|
QBuffer buf;
|
|
|
|
|
|
|
|
buf.open( QIODevice::WriteOnly );
|
|
|
|
|
|
|
|
QDataStream stream( &buf );
|
|
|
|
|
|
|
|
stream << contexts;
|
|
|
|
|
|
|
|
buf.close();
|
|
|
|
|
2013-02-03 20:19:55 +00:00
|
|
|
req.addQueryItem( "contexts", QString::fromLatin1( buf.buffer().toBase64() ) );
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
2009-05-11 19:14:28 +00:00
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QString mutedDicts = getMutedForGroup( group );
|
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
|
|
|
req.addQueryItem( "muted", mutedDicts );
|
|
|
|
|
2013-01-18 11:00:38 +00:00
|
|
|
// Update both histories (pages history and headwords history)
|
2009-05-29 22:04:43 +00:00
|
|
|
saveHistoryUserData();
|
2013-01-18 11:00:38 +00:00
|
|
|
emit sendWordToHistory( word );
|
2009-05-11 22:25:22 +00:00
|
|
|
|
2010-04-27 07:49:37 +00:00
|
|
|
// Any search opened is probably irrelevant now
|
|
|
|
closeSearch();
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
// Clear highlight all button selection
|
|
|
|
ui.highlightAllButton->setChecked(false);
|
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
emit setExpandMode( expandOptionalParts );
|
|
|
|
|
2009-04-11 20:37:11 +00:00
|
|
|
ui.definition->load( req );
|
2009-05-11 22:25:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-08 16:50:18 +00:00
|
|
|
void ArticleView::showAnticipation()
|
|
|
|
{
|
|
|
|
ui.definition->setHtml( "" );
|
2009-03-26 19:00:08 +00:00
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
2009-02-08 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::loadFinished( bool )
|
|
|
|
{
|
2009-05-11 22:25:22 +00:00
|
|
|
QUrl url = ui.definition->url();
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
// See if we have any iframes in need of expansion
|
|
|
|
|
|
|
|
QList< QWebFrame * > frames = ui.definition->page()->mainFrame()->childFrames();
|
|
|
|
|
|
|
|
bool wereFrames = false;
|
|
|
|
|
|
|
|
for( QList< QWebFrame * >::iterator i = frames.begin(); i != frames.end(); ++i )
|
|
|
|
{
|
|
|
|
if ( (*i)->frameName().startsWith( "gdexpandframe-" ) )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "Name: %s\n", (*i)->frameName().toUtf8().data() );
|
|
|
|
//DPRINTF( "Size: %d\n", (*i)->contentsSize().height() );
|
|
|
|
//DPRINTF( ">>>>>>>>Height = %s\n", (*i)->evaluateJavaScript( "document.body.offsetHeight;" ).toString().toUtf8().data() );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
// Set the height
|
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript( QString( "document.getElementById('%1').height = %2;" ).
|
|
|
|
arg( (*i)->frameName() ).
|
|
|
|
arg( (*i)->contentsSize().height() ) );
|
|
|
|
|
|
|
|
// Show it
|
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript( QString( "document.getElementById('%1').style.display = 'block';" ).
|
|
|
|
arg( (*i)->frameName() ) );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
(*i)->evaluateJavaScript( "var gdLastUrlText;" );
|
2009-05-29 20:06:30 +00:00
|
|
|
(*i)->evaluateJavaScript( "document.addEventListener( 'click', function() { gdLastUrlText = window.event.srcElement.textContent; }, true );" );
|
|
|
|
(*i)->evaluateJavaScript( "document.addEventListener( 'contextmenu', function() { gdLastUrlText = window.event.srcElement.textContent; }, true );" );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
wereFrames = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wereFrames )
|
|
|
|
{
|
|
|
|
// There's some sort of glitch -- sometimes you need to move a mouse
|
|
|
|
|
|
|
|
QMouseEvent ev( QEvent::MouseMove, QPoint(), Qt::MouseButton(), 0, 0 );
|
|
|
|
|
|
|
|
qApp->sendEvent( ui.definition, &ev );
|
|
|
|
}
|
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
QVariant userDataVariant = ui.definition->history()->currentItem().userData();
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
if ( userDataVariant.type() == QVariant::Map )
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
2009-05-29 22:04:43 +00:00
|
|
|
QMap< QString, QVariant > userData = userDataVariant.toMap();
|
|
|
|
|
|
|
|
QString currentArticle = userData.value( "currentArticle" ).toString();
|
|
|
|
|
|
|
|
if ( currentArticle.size() )
|
|
|
|
{
|
|
|
|
// There's an active article saved, so set it to be active.
|
|
|
|
setCurrentArticle( currentArticle );
|
|
|
|
}
|
|
|
|
|
|
|
|
double sx = 0, sy = 0;
|
|
|
|
|
|
|
|
if ( userData.value( "sx" ).type() == QVariant::Double )
|
|
|
|
sx = userData.value( "sx" ).toDouble();
|
|
|
|
|
|
|
|
if ( userData.value( "sy" ).type() == QVariant::Double )
|
|
|
|
sy = userData.value( "sy" ).toDouble();
|
|
|
|
|
|
|
|
if ( sx != 0 || sy != 0 )
|
|
|
|
{
|
|
|
|
// Restore scroll position
|
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript(
|
|
|
|
QString( "window.scroll( %1, %2 );" ).arg( sx ).arg( sy ) );
|
|
|
|
}
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( url.queryItemValue( "scrollto" ).startsWith( "gdfrom-" ) )
|
|
|
|
{
|
|
|
|
// There is no active article saved in history, but we have it as a parameter.
|
|
|
|
// setCurrentArticle will save it and scroll there.
|
|
|
|
setCurrentArticle( url.queryItemValue( "scrollto" ), true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
ui.definition->unsetCursor();
|
|
|
|
//QApplication::restoreOverrideCursor();
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
// Jump to current article after page reloading
|
|
|
|
if( !articleToJump.isEmpty() )
|
|
|
|
{
|
|
|
|
setCurrentArticle( articleToJump, true );
|
|
|
|
articleToJump.clear();
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
emit pageLoaded( this );
|
2009-02-08 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::handleTitleChanged( QString const & title )
|
|
|
|
{
|
|
|
|
emit titleChanged( this, title );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::handleUrlChanged( QUrl const & url )
|
|
|
|
{
|
|
|
|
QIcon icon;
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned group = getGroup( url );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( group )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Find the group's instance corresponding to the fragment value
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( groups[ x ].id == group )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Found it
|
|
|
|
|
2010-07-05 19:47:22 +00:00
|
|
|
icon = groups[ x ].makeIcon();
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit iconChanged( this, icon );
|
|
|
|
}
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned ArticleView::getGroup( QUrl const & url )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
if ( url.scheme() == "gdlookup" && url.hasQueryItem( "group" ) )
|
2009-04-10 12:48:40 +00:00
|
|
|
return url.queryItemValue( "group" ).toUInt();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
return 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 22:25:22 +00:00
|
|
|
QStringList ArticleView::getArticlesList()
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
return ui.definition->page()->mainFrame()->
|
2009-05-11 22:25:22 +00:00
|
|
|
evaluateJavaScript( "gdArticleContents;" ).toString().
|
|
|
|
trimmed().split( ' ', QString::SkipEmptyParts );
|
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
QString ArticleView::getActiveArticleId()
|
|
|
|
{
|
|
|
|
QString currentArticle = getCurrentArticle();
|
|
|
|
if ( !currentArticle.startsWith( "gdfrom-" ) )
|
|
|
|
return QString(); // Incorrect id
|
|
|
|
|
|
|
|
return currentArticle.mid( 7 );
|
|
|
|
}
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
QString ArticleView::getCurrentArticle()
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
QVariant v = ui.definition->page()->mainFrame()->evaluateJavaScript(
|
2009-05-11 19:14:28 +00:00
|
|
|
QString( "gdCurrentArticle;" ) );
|
|
|
|
|
|
|
|
if ( v.type() == QVariant::String )
|
|
|
|
return v.toString();
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
void ArticleView::jumpToDictionary( QString const & id )
|
2011-06-05 11:49:50 +00:00
|
|
|
{
|
2011-07-03 12:27:08 +00:00
|
|
|
QString targetArticle = "gdfrom-" + id;
|
|
|
|
|
|
|
|
// jump only if neceessary
|
|
|
|
if ( targetArticle != getCurrentArticle() )
|
|
|
|
{
|
|
|
|
setCurrentArticle( targetArticle, true );
|
|
|
|
}
|
2011-06-05 11:49:50 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
void ArticleView::setCurrentArticle( QString const & id, bool moveToIt )
|
2009-05-11 22:25:22 +00:00
|
|
|
{
|
|
|
|
if ( !id.startsWith( "gdfrom-" ) )
|
|
|
|
return; // Incorrect id
|
|
|
|
|
|
|
|
if ( getArticlesList().contains( id.mid( 7 ) ) )
|
|
|
|
{
|
2009-05-15 14:11:54 +00:00
|
|
|
if ( moveToIt )
|
2009-05-29 19:48:50 +00:00
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript( QString( "document.getElementById('%1').scrollIntoView(true);" ).arg( id ) );
|
2009-05-15 14:11:54 +00:00
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
QMap< QString, QVariant > userData = ui.definition->history()->
|
|
|
|
currentItem().userData().toMap();
|
|
|
|
userData[ "currentArticle" ] = id;
|
|
|
|
ui.definition->history()->currentItem().setUserData( userData );
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript(
|
2009-05-14 20:38:17 +00:00
|
|
|
QString( "gdMakeArticleActive( '%1' );" ).arg( id.mid( 7 ) ) );
|
2009-05-11 22:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 14:37:24 +00:00
|
|
|
void ArticleView::selectCurrentArticle()
|
|
|
|
{
|
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript(
|
|
|
|
QString( "gdSelectArticle( '%1' );" ).arg( getActiveArticleId() ) );
|
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
bool ArticleView::isFramedArticle( QString const & ca )
|
|
|
|
{
|
|
|
|
if ( ca.isEmpty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ui.definition->page()->mainFrame()->
|
|
|
|
evaluateJavaScript( QString( "!!document.getElementById('gdexpandframe-%1');" ).arg( ca.mid( 7 ) ) ).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::isExternalLink( QUrl const & url )
|
|
|
|
{
|
|
|
|
return url.scheme() == "http" || url.scheme() == "https" ||
|
2010-06-30 16:43:08 +00:00
|
|
|
url.scheme() == "ftp" || url.scheme() == "mailto" ||
|
|
|
|
url.scheme() == "file";
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts )
|
|
|
|
{
|
2010-05-29 11:33:04 +00:00
|
|
|
// Don't try mangling audio urls, even if they are from the framed websites
|
|
|
|
|
|
|
|
if( ( url.scheme() == "http" || url.scheme() == "https" )
|
|
|
|
&& ! Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
2009-05-29 19:48:50 +00:00
|
|
|
{
|
|
|
|
// Maybe a link inside a website was clicked?
|
|
|
|
|
|
|
|
QString ca = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( isFramedArticle( ca ) )
|
|
|
|
{
|
|
|
|
QVariant result = ui.definition->page()->currentFrame()->evaluateJavaScript( "gdLastUrlText;" );
|
|
|
|
|
|
|
|
if ( result.type() == QVariant::String )
|
|
|
|
{
|
|
|
|
// Looks this way
|
|
|
|
|
2013-02-03 20:19:55 +00:00
|
|
|
contexts[ ca.mid( 7 ) ] = QString::fromLatin1( url.toEncoded() );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
|
|
|
QUrl target;
|
|
|
|
|
2009-05-29 20:30:45 +00:00
|
|
|
QString queryWord = result.toString();
|
|
|
|
|
|
|
|
// Empty requests are treated as no request, so we work this around by
|
|
|
|
// adding a space.
|
|
|
|
if ( queryWord.isEmpty() )
|
|
|
|
queryWord = " ";
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
target.setScheme( "gdlookup" );
|
|
|
|
target.setHost( "localhost" );
|
2009-05-29 20:30:45 +00:00
|
|
|
target.setPath( "/" + queryWord );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
|
|
|
url = target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::updateCurrentArticleFromCurrentFrame( QWebFrame * frame )
|
|
|
|
{
|
|
|
|
if ( !frame )
|
|
|
|
frame = ui.definition->page()->currentFrame();
|
|
|
|
|
|
|
|
for( ; frame; frame = frame->parentFrame() )
|
|
|
|
{
|
|
|
|
QString frameName = frame->frameName();
|
|
|
|
|
|
|
|
if ( frameName.startsWith( "gdexpandframe-" ) )
|
|
|
|
{
|
|
|
|
QString newCurrent = "gdfrom-" + frameName.mid( 14 );
|
|
|
|
|
|
|
|
if ( getCurrentArticle() != newCurrent )
|
|
|
|
setCurrentArticle( newCurrent, false );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 22:04:43 +00:00
|
|
|
void ArticleView::saveHistoryUserData()
|
|
|
|
{
|
|
|
|
QMap< QString, QVariant > userData = ui.definition->history()->
|
|
|
|
currentItem().userData().toMap();
|
|
|
|
|
|
|
|
// Save current article, which can be empty
|
|
|
|
|
|
|
|
userData[ "currentArticle" ] = getCurrentArticle();
|
|
|
|
|
|
|
|
// We also save window position. We restore it when the page is fully loaded,
|
|
|
|
// when any hidden frames are expanded.
|
|
|
|
|
|
|
|
userData[ "sx" ] = ui.definition->page()->mainFrame()->evaluateJavaScript( "window.scrollX;" ).toDouble();
|
|
|
|
userData[ "sy" ] = ui.definition->page()->mainFrame()->evaluateJavaScript( "window.scrollY;" ).toDouble();
|
|
|
|
|
|
|
|
ui.definition->history()->currentItem().setUserData( userData );
|
|
|
|
}
|
|
|
|
|
2009-02-08 21:54:19 +00:00
|
|
|
void ArticleView::cleanupTemp()
|
|
|
|
{
|
|
|
|
if ( desktopOpenedTempFile.size() )
|
|
|
|
{
|
|
|
|
QFile( desktopOpenedTempFile ).remove();
|
|
|
|
desktopOpenedTempFile.clear();
|
2010-09-16 18:53:39 +00:00
|
|
|
}
|
2009-02-08 21:54:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
|
2009-05-12 13:25:18 +00:00
|
|
|
{
|
2013-01-22 21:04:45 +00:00
|
|
|
if ( ev->type() == QEvent::ShortcutOverride ) {
|
2013-01-23 18:36:45 +00:00
|
|
|
QKeyEvent * ke = static_cast<QKeyEvent *>( ev );
|
|
|
|
if ( ke->key() == Qt::Key_F3 && isSearchOpened() ) {
|
|
|
|
if ( !ke->modifiers() )
|
|
|
|
{
|
|
|
|
on_searchNext_clicked();
|
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ke->modifiers() == Qt::ShiftModifier )
|
|
|
|
{
|
|
|
|
on_searchPrevious_clicked();
|
|
|
|
ev->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-22 21:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:36:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::eventFilter( QObject * obj, QEvent * ev )
|
|
|
|
{
|
|
|
|
|
|
|
|
if ( handleF3( obj, ev ) )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
if ( obj == ui.definition )
|
|
|
|
{
|
2011-06-07 08:26:49 +00:00
|
|
|
if ( ev->type() == QEvent::MouseButtonPress ) {
|
|
|
|
QMouseEvent * event = static_cast< QMouseEvent * >( ev );
|
|
|
|
if ( event->button() == Qt::XButton1 ) {
|
|
|
|
back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( event->button() == Qt::XButton2 ) {
|
|
|
|
forward();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-05-12 13:25:18 +00:00
|
|
|
if ( ev->type() == QEvent::KeyPress )
|
|
|
|
{
|
|
|
|
QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev );
|
|
|
|
|
2009-05-14 20:43:32 +00:00
|
|
|
if ( keyEvent->modifiers() &
|
2011-05-08 21:24:41 +00:00
|
|
|
( Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier ) )
|
2009-05-14 20:43:32 +00:00
|
|
|
return false; // A non-typing modifier is pressed
|
|
|
|
|
2009-05-12 15:54:37 +00:00
|
|
|
if ( keyEvent->key() == Qt::Key_Space ||
|
2009-05-12 17:57:53 +00:00
|
|
|
keyEvent->key() == Qt::Key_Backspace ||
|
2011-07-04 08:35:56 +00:00
|
|
|
keyEvent->key() == Qt::Key_Tab ||
|
|
|
|
keyEvent->key() == Qt::Key_Backtab )
|
2009-05-12 15:54:37 +00:00
|
|
|
return false; // Those key have other uses than to start typing
|
|
|
|
|
2009-05-12 13:25:18 +00:00
|
|
|
QString text = keyEvent->text();
|
|
|
|
|
|
|
|
if ( text.size() )
|
|
|
|
{
|
|
|
|
emit typingEvent( text );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QFrame::eventFilter( obj, ev );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QString ArticleView::getMutedForGroup( unsigned group )
|
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
if ( dictionaryBarToggled && dictionaryBarToggled->isChecked() )
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
|
|
|
// Dictionary bar is active -- mute the muted dictionaries
|
|
|
|
Instances::Group const * groupInstance = groups.findGroup( group );
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
// Find muted dictionaries for current group
|
|
|
|
Config::Group const * grp = cfg.getGroup( group );
|
|
|
|
Config::MutedDictionaries const * mutedDictionaries;
|
|
|
|
if( group == Instances::Group::AllGroupId )
|
|
|
|
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
|
|
|
|
else
|
|
|
|
mutedDictionaries = grp ? ( popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries ) : 0;
|
|
|
|
if( !mutedDictionaries )
|
|
|
|
return QString();
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
QStringList mutedDicts;
|
|
|
|
|
|
|
|
if ( groupInstance )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groupInstance->dictionaries.size(); ++x )
|
|
|
|
{
|
|
|
|
QString id = QString::fromStdString(
|
|
|
|
groupInstance->dictionaries[ x ]->getId() );
|
|
|
|
|
2010-03-30 20:15:55 +00:00
|
|
|
if ( mutedDictionaries->contains( id ) )
|
2009-09-23 18:44:38 +00:00
|
|
|
mutedDicts.append( id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
|
|
|
return mutedDicts.join( "," );
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2011-07-02 13:04:49 +00:00
|
|
|
void ArticleView::linkHovered ( const QString & link, const QString & , const QString & )
|
|
|
|
{
|
|
|
|
QString msg;
|
|
|
|
QUrl url(link);
|
|
|
|
|
|
|
|
if ( url.scheme() == "bres" )
|
|
|
|
{
|
|
|
|
msg = tr( "Resource" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( url.scheme() == "gdau" || Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
|
|
|
{
|
|
|
|
msg = tr( "Audio" );
|
|
|
|
}
|
|
|
|
else
|
2013-04-24 16:01:44 +00:00
|
|
|
if ( url.scheme() == "gdtts" )
|
|
|
|
{
|
|
|
|
msg = tr( "TTS Voice" );
|
|
|
|
}
|
|
|
|
else
|
2012-12-07 11:59:29 +00:00
|
|
|
if ( url.scheme() == "gdpicture" )
|
|
|
|
{
|
|
|
|
msg = tr( "Picture" );
|
|
|
|
}
|
|
|
|
else
|
2011-07-02 13:04:49 +00:00
|
|
|
if (url.scheme() == "gdlookup" || url.scheme().compare( "bword" ) == 0)
|
|
|
|
{
|
|
|
|
QString def = url.path();
|
|
|
|
if (def.startsWith("/"))
|
|
|
|
{
|
|
|
|
def = def.mid( 1 );
|
|
|
|
}
|
2012-11-26 13:13:56 +00:00
|
|
|
|
|
|
|
if( url.hasQueryItem( "dict" ) )
|
|
|
|
{
|
|
|
|
// Link to other dictionary
|
|
|
|
QString dictName( url.queryItemValue( "dict" ) );
|
|
|
|
if( !dictName.isEmpty() )
|
|
|
|
msg = tr( "Definition from dictionary \"%1\": %2" ).arg( dictName ).arg( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( msg.isEmpty() )
|
|
|
|
msg = tr( "Definition: %1").arg( def );
|
2011-07-02 13:04:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = link;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit statusBarMessage( msg );
|
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
void ArticleView::attachToJavaScript()
|
|
|
|
{
|
|
|
|
ui.definition->page()->mainFrame()->addToJavaScriptWindowObject( QString( "articleview" ), this );
|
|
|
|
}
|
2011-07-02 13:04:49 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
void ArticleView::linkClicked( QUrl const & url_ )
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2009-05-29 19:48:50 +00:00
|
|
|
updateCurrentArticleFromCurrentFrame();
|
|
|
|
|
|
|
|
QUrl url( url_ );
|
|
|
|
Contexts contexts;
|
|
|
|
|
|
|
|
tryMangleWebsiteClickedUrl( url, contexts );
|
|
|
|
|
2011-07-06 06:53:42 +00:00
|
|
|
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
|
|
|
|
if ( !popupView &&
|
|
|
|
( ui.definition->isMidButtonPressed() ||
|
|
|
|
( kmod & ( Qt::ControlModifier | Qt::ShiftModifier ) ) ) )
|
2009-10-22 11:38:11 +00:00
|
|
|
{
|
2011-07-06 06:53:42 +00:00
|
|
|
// Mid button or Control/Shift is currently pressed - open the link in new tab
|
2009-10-22 11:38:11 +00:00
|
|
|
emit openLinkInNewTab( url, ui.definition->url(), getCurrentArticle(), contexts );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
openLink( url, ui.definition->url(), getCurrentArticle(), contexts );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 19:14:28 +00:00
|
|
|
void ArticleView::openLink( QUrl const & url, QUrl const & ref,
|
2009-05-29 19:48:50 +00:00
|
|
|
QString const & scrollTo,
|
|
|
|
Contexts const & contexts )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2011-07-02 13:04:49 +00:00
|
|
|
qDebug() << "clicked" << url;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-12-07 11:59:29 +00:00
|
|
|
if( url.scheme().compare( "gdpicture" ) == 0 )
|
|
|
|
ui.definition->load( url );
|
|
|
|
else
|
2010-10-07 05:33:41 +00:00
|
|
|
if ( url.scheme().compare( "bword" ) == 0 )
|
2009-08-31 14:27:19 +00:00
|
|
|
{
|
2010-10-07 05:33:41 +00:00
|
|
|
showDefinition( url.path(),
|
2009-05-29 19:48:50 +00:00
|
|
|
getGroup( ref ), scrollTo, contexts );
|
2009-08-31 14:27:19 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
|
|
|
if ( url.scheme() == "gdlookup" ) // Plain html links inherit gdlookup scheme
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
if ( url.hasFragment() )
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
ui.definition->page()->mainFrame()->evaluateJavaScript(
|
2009-03-26 19:00:08 +00:00
|
|
|
QString( "window.location = \"%1\"" ).arg( QString::fromUtf8( url.toEncoded() ) ) );
|
|
|
|
}
|
|
|
|
else
|
2012-11-12 13:52:54 +00:00
|
|
|
{
|
2012-11-24 20:30:32 +00:00
|
|
|
QString newScrollTo( scrollTo );
|
|
|
|
if( url.hasQueryItem( "dict" ) )
|
|
|
|
{
|
|
|
|
// Link to other dictionary
|
|
|
|
QString dictName( url.queryItemValue( "dict" ) );
|
|
|
|
for( unsigned i = 0; i < allDictionaries.size(); i++ )
|
|
|
|
{
|
|
|
|
if( dictName.compare( QString::fromUtf8( allDictionaries[ i ]->getName().c_str() ) ) == 0 )
|
|
|
|
{
|
|
|
|
newScrollTo = QString( "gdfrom-" ) + QString::fromUtf8( allDictionaries[ i ]->getId().c_str() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-12 13:52:54 +00:00
|
|
|
showDefinition( url.path().mid( 1 ),
|
2012-11-24 20:30:32 +00:00
|
|
|
getGroup( ref ), newScrollTo, contexts );
|
2012-11-12 13:52:54 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2010-05-29 11:33:04 +00:00
|
|
|
if ( url.scheme() == "bres" || url.scheme() == "gdau" ||
|
|
|
|
Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-02-06 16:19:05 +00:00
|
|
|
// Download it
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Clear any pending ones
|
|
|
|
|
|
|
|
resourceDownloadRequests.clear();
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadUrl = url;
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2010-05-29 11:33:04 +00:00
|
|
|
if ( Dictionary::WebMultimediaDownload::isAudioUrl( url ) )
|
|
|
|
{
|
|
|
|
sptr< Dictionary::DataRequest > req =
|
|
|
|
new Dictionary::WebMultimediaDownload( url, articleNetMgr );
|
|
|
|
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
|
|
|
else
|
2009-04-25 21:04:49 +00:00
|
|
|
if ( url.scheme() == "gdau" && url.host() == "search" )
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
|
|
|
// Since searches should be limited to current group, we just do them
|
|
|
|
// here ourselves since otherwise we'd need to pass group id to netmgr
|
|
|
|
// and it should've been having knowledge of the current groups, too.
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned currentGroup = getGroup( ref );
|
2009-02-06 16:19:05 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const * activeDicts = 0;
|
|
|
|
|
|
|
|
if ( groups.size() )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
if ( groups[ x ].id == currentGroup )
|
|
|
|
{
|
|
|
|
activeDicts = &( groups[ x ].dictionaries );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
activeDicts = &allDictionaries;
|
|
|
|
|
|
|
|
if ( activeDicts )
|
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
2010-09-16 18:53:39 +00:00
|
|
|
sptr< Dictionary::DataRequest > req =
|
2009-04-25 21:04:49 +00:00
|
|
|
(*activeDicts)[ x ]->getResource(
|
|
|
|
url.path().mid( 1 ).toUtf8().data() );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
if ( req->isFinished() && req->dataSize() >= 0 )
|
2009-02-06 16:19:05 +00:00
|
|
|
{
|
2009-04-25 21:04:49 +00:00
|
|
|
// A request was instantly finished with success.
|
|
|
|
// If we've managed to spawn some lingering requests already,
|
|
|
|
// erase them.
|
|
|
|
resourceDownloadRequests.clear();
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
// Handle the result
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
resourceDownloadFinished();
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( !req->isFinished() )
|
|
|
|
{
|
|
|
|
resourceDownloadRequests.push_back( req );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
2009-02-06 16:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Normal resource download
|
|
|
|
QString contentType;
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2010-09-16 18:53:39 +00:00
|
|
|
sptr< Dictionary::DataRequest > req =
|
2009-03-26 19:00:08 +00:00
|
|
|
articleNetMgr.getResource( url, contentType );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-25 21:04:49 +00:00
|
|
|
if ( !req.get() )
|
|
|
|
{
|
|
|
|
// Request failed, fail
|
|
|
|
}
|
|
|
|
else
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( req->isFinished() && req->dataSize() >= 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Have data ready, handle it
|
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
resourceDownloadFinished();
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
|
|
|
if ( !req->isFinished() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Queue to be handled when done
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadRequests.push_back( req );
|
|
|
|
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( resourceDownloadFinished() ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-02-08 14:40:26 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( resourceDownloadRequests.empty() ) // No requests were queued
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "The referenced resource doesn't exist." ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
2009-02-08 21:54:19 +00:00
|
|
|
}
|
2009-04-25 21:04:49 +00:00
|
|
|
else
|
|
|
|
resourceDownloadFinished(); // Check any requests finished already
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
else
|
2011-05-29 05:08:37 +00:00
|
|
|
if ( url.scheme() == "gdprg" )
|
|
|
|
{
|
|
|
|
// Program. Run it.
|
|
|
|
QString id( url.host() );
|
|
|
|
|
|
|
|
for( Config::Programs::const_iterator i = cfg.programs.begin();
|
|
|
|
i != cfg.programs.end(); ++i )
|
|
|
|
{
|
|
|
|
if ( i->id == id )
|
|
|
|
{
|
|
|
|
// Found the corresponding program.
|
2011-06-04 21:35:09 +00:00
|
|
|
Programs::RunInstance * req = new Programs::RunInstance;
|
2011-05-29 05:08:37 +00:00
|
|
|
|
2011-06-04 21:35:09 +00:00
|
|
|
connect( req, SIGNAL(finished(QByteArray,QString)),
|
|
|
|
req, SLOT( deleteLater() ) );
|
2011-05-29 05:08:37 +00:00
|
|
|
|
2011-06-04 21:35:09 +00:00
|
|
|
QString error;
|
|
|
|
|
|
|
|
// Delete the request if it fails to start
|
|
|
|
if ( !req->start( *i, url.path().mid( 1 ), error ) )
|
|
|
|
{
|
2011-05-29 05:08:37 +00:00
|
|
|
delete req;
|
|
|
|
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2011-06-04 21:35:09 +00:00
|
|
|
error );
|
|
|
|
}
|
|
|
|
|
2011-05-29 05:08:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still here? No such program exists.
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2011-05-29 05:08:37 +00:00
|
|
|
tr( "The referenced audio program doesn't exist." ) );
|
|
|
|
}
|
|
|
|
else
|
2013-04-24 14:52:04 +00:00
|
|
|
if ( url.scheme() == "gdtts" )
|
|
|
|
{
|
|
|
|
// TODO: Port TTS
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// Text to speech
|
|
|
|
QString md5Id = url.queryItemValue( "engine" );
|
|
|
|
QString text( url.path().mid( 1 ) );
|
|
|
|
|
|
|
|
for ( Config::VoiceEngines::const_iterator i = cfg.voiceEngines.begin();
|
|
|
|
i != cfg.voiceEngines.end(); ++i )
|
|
|
|
{
|
|
|
|
QString itemMd5Id = QString( QCryptographicHash::hash(
|
|
|
|
i->id.toUtf8(),
|
2013-04-24 16:01:44 +00:00
|
|
|
QCryptographicHash::Md5 ).toHex() );
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
if ( itemMd5Id == md5Id )
|
|
|
|
{
|
2013-04-26 13:41:39 +00:00
|
|
|
SpeechClient * speechClient = new SpeechClient( *i, this );
|
2013-04-24 14:52:04 +00:00
|
|
|
connect( speechClient, SIGNAL( finished() ), speechClient, SLOT( deleteLater() ) );
|
2013-04-24 16:01:44 +00:00
|
|
|
speechClient->tell( text );
|
2013-04-24 14:52:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2010-06-30 16:43:08 +00:00
|
|
|
if ( isExternalLink( url ) )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
2010-06-30 16:43:08 +00:00
|
|
|
// Use the system handler for the conventional external links
|
2009-02-01 00:08:08 +00:00
|
|
|
QDesktopServices::openUrl( url );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 12:44:23 +00:00
|
|
|
void ArticleView::saveResource( const QUrl & url, QUrl const & ref )
|
|
|
|
{
|
|
|
|
resourceToSaveDownloadRequests.clear();
|
|
|
|
resourceToSaveUrl = url;
|
|
|
|
sptr< Dictionary::DataRequest > req;
|
|
|
|
|
|
|
|
if( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau")
|
|
|
|
{
|
|
|
|
if ( url.host() == "search" )
|
|
|
|
{
|
|
|
|
// Since searches should be limited to current group, we just do them
|
|
|
|
// here ourselves since otherwise we'd need to pass group id to netmgr
|
|
|
|
// and it should've been having knowledge of the current groups, too.
|
|
|
|
|
|
|
|
unsigned currentGroup = getGroup( ref );
|
|
|
|
|
|
|
|
std::vector< sptr< Dictionary::Class > > const * activeDicts = 0;
|
|
|
|
|
|
|
|
if ( groups.size() )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
if ( groups[ x ].id == currentGroup )
|
|
|
|
{
|
|
|
|
activeDicts = &( groups[ x ].dictionaries );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
activeDicts = &allDictionaries;
|
|
|
|
|
|
|
|
if ( activeDicts )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < activeDicts->size(); ++x )
|
|
|
|
{
|
|
|
|
req = (*activeDicts)[ x ]->getResource(
|
|
|
|
url.path().mid( 1 ).toUtf8().data() );
|
|
|
|
|
|
|
|
if ( req->isFinished() && req->dataSize() >= 0 )
|
|
|
|
{
|
|
|
|
// A request was instantly finished with success.
|
|
|
|
// If we've managed to spawn some lingering requests already,
|
|
|
|
// erase them.
|
|
|
|
resourceToSaveDownloadRequests.clear();
|
|
|
|
|
|
|
|
// Handle the result
|
|
|
|
resourceToSaveDownloadRequests.push_back( req );
|
|
|
|
resourceToSaveDownloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( !req->isFinished() )
|
|
|
|
{
|
|
|
|
resourceToSaveDownloadRequests.push_back( req );
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
2013-02-28 15:36:53 +00:00
|
|
|
this, SLOT( resourceToSaveDownloadFinished() ), Qt::QueuedConnection );
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Normal resource download
|
|
|
|
QString contentType;
|
|
|
|
req = articleNetMgr.getResource( url, contentType );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
req = new Dictionary::WebMultimediaDownload( url, articleNetMgr );
|
|
|
|
|
|
|
|
if( url.host().compare( "search" ) != 0 )
|
|
|
|
{
|
|
|
|
if ( !req.get() )
|
|
|
|
{
|
|
|
|
// Request failed, fail
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( req->isFinished() && req->dataSize() >= 0 )
|
|
|
|
{
|
|
|
|
// Have data ready, handle it
|
|
|
|
resourceToSaveDownloadRequests.push_back( req );
|
|
|
|
resourceToSaveDownloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( !req->isFinished() )
|
|
|
|
{
|
|
|
|
// Queue to be handled when done
|
|
|
|
|
|
|
|
resourceToSaveDownloadRequests.push_back( req );
|
|
|
|
connect( req.get(), SIGNAL( finished() ),
|
2013-02-28 15:36:53 +00:00
|
|
|
this, SLOT( resourceToSaveDownloadFinished() ), Qt::QueuedConnection );
|
2013-02-22 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( resourceToSaveDownloadRequests.empty() ) // No requests were queued
|
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( tr( "The referenced resource doesn't exist." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resourceToSaveDownloadFinished(); // Check any requests finished already
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::resourceToSaveDownloadFinished()
|
|
|
|
{
|
|
|
|
if ( resourceToSaveDownloadRequests.empty() )
|
|
|
|
return; // Stray signal
|
|
|
|
|
|
|
|
QByteArray resourceData;
|
|
|
|
|
|
|
|
// Find any finished resources
|
|
|
|
|
|
|
|
for( list< sptr< Dictionary::DataRequest > >::iterator i =
|
|
|
|
resourceToSaveDownloadRequests.begin(); i != resourceToSaveDownloadRequests.end(); )
|
|
|
|
{
|
|
|
|
if ( (*i)->isFinished() )
|
|
|
|
{
|
|
|
|
if ( (*i)->dataSize() >= 0 )
|
|
|
|
{
|
|
|
|
// Ok, got one finished, all others are irrelevant now
|
|
|
|
|
|
|
|
vector< char > const & data = (*i)->getFullData();
|
2013-02-28 19:29:13 +00:00
|
|
|
resourceData = QByteArray( data.data(), data.size() );
|
2013-02-22 12:44:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This one had no data. Erase it.
|
|
|
|
resourceToSaveDownloadRequests.erase( i++ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Unfinished, try the next one.
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !resourceData.isEmpty() )
|
|
|
|
{
|
2013-02-28 15:36:53 +00:00
|
|
|
// Resource found, clear all requests
|
|
|
|
resourceToSaveDownloadRequests.clear();
|
|
|
|
|
2013-02-22 12:44:23 +00:00
|
|
|
QString fileName;
|
|
|
|
QString savePath;
|
|
|
|
if( cfg.resourceSavePath.isEmpty() )
|
|
|
|
savePath = QDir::homePath();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
savePath = QDir::fromNativeSeparators( cfg.resourceSavePath );
|
|
|
|
if( !QDir( savePath ).exists() )
|
|
|
|
savePath = QDir::homePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString name = resourceToSaveUrl.path().section( '/', -1 );
|
|
|
|
|
|
|
|
if ( resourceToSaveUrl.scheme() == "gdau" ||
|
|
|
|
Dictionary::WebMultimediaDownload::isAudioUrl( resourceToSaveUrl ) )
|
|
|
|
{
|
|
|
|
// Audio data
|
|
|
|
if( name.indexOf( '.' ) < 0 )
|
|
|
|
name += ".wav";
|
|
|
|
|
|
|
|
fileName = savePath + "/" + name;
|
|
|
|
fileName = QFileDialog::getSaveFileName( parentWidget(), tr( "Save sound" ),
|
|
|
|
fileName,
|
|
|
|
tr( "Sound files (*.wav *.ogg *.mp3 *.mp4 *.aac *.flac *.mid *.wv *.ape);;All files (*.*)" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Image data
|
|
|
|
|
|
|
|
// Check for babylon image name
|
|
|
|
if( name[ 0 ] == '\x1E' )
|
|
|
|
name.remove( 0, 1 );
|
|
|
|
if( name[ name.length() - 1 ] == '\x1F' )
|
|
|
|
name.chop( 1 );
|
|
|
|
|
|
|
|
fileName = savePath + "/" + name;
|
|
|
|
fileName = QFileDialog::getSaveFileName( parentWidget(), tr( "Save image" ),
|
|
|
|
fileName,
|
|
|
|
tr( "Image files (*.bmp *.jpg *.png *.tif);;All files (*.*)" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write data to file
|
|
|
|
|
|
|
|
if( !fileName.isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo( fileName );
|
|
|
|
emit storeResourceSavePath( QDir::toNativeSeparators( fileInfo.absoluteDir().absolutePath() ) );
|
|
|
|
QFile file( fileName );
|
|
|
|
if ( file.open( QFile::WriteOnly ) )
|
|
|
|
{
|
|
|
|
file.write( resourceData.data(), resourceData.size() );
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
if( file.error() )
|
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( tr( "Resource saving error: " ) + file.errorString() ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( resourceToSaveDownloadRequests.empty() )
|
|
|
|
{
|
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "ERROR: %1" ).arg( tr( "The referenced resource failed to download." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
void ArticleView::updateMutedContents()
|
|
|
|
{
|
|
|
|
QUrl currentUrl = ui.definition->url();
|
|
|
|
|
|
|
|
if ( currentUrl.scheme() != "gdlookup" )
|
|
|
|
return; // Weird url -- do nothing
|
|
|
|
|
|
|
|
unsigned group = getGroup( currentUrl );
|
|
|
|
|
|
|
|
if ( !group )
|
|
|
|
return; // No group in url -- do nothing
|
|
|
|
|
|
|
|
QString mutedDicts = getMutedForGroup( group );
|
|
|
|
|
|
|
|
if ( currentUrl.queryItemValue( "muted" ) != mutedDicts )
|
|
|
|
{
|
|
|
|
// The list has changed -- update the url
|
|
|
|
|
|
|
|
currentUrl.removeQueryItem( "muted" );
|
|
|
|
|
|
|
|
if ( mutedDicts.size() )
|
|
|
|
currentUrl.addQueryItem( "muted", mutedDicts );
|
|
|
|
|
|
|
|
saveHistoryUserData();
|
|
|
|
|
|
|
|
ui.definition->load( currentUrl );
|
|
|
|
|
|
|
|
//QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
ui.definition->setCursor( Qt::WaitCursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-07 11:27:19 +00:00
|
|
|
bool ArticleView::canGoBack()
|
|
|
|
{
|
|
|
|
// First entry in a history is always an empty page,
|
|
|
|
// so we skip it.
|
|
|
|
return ui.definition->history()->currentItemIndex() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleView::canGoForward()
|
|
|
|
{
|
|
|
|
return ui.definition->history()->canGoForward();
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:59:48 +00:00
|
|
|
void ArticleView::setSelectionBySingleClick( bool set )
|
|
|
|
{
|
|
|
|
ui.definition->setSelectionBySingleClick( set );
|
|
|
|
}
|
|
|
|
|
2011-06-07 07:57:25 +00:00
|
|
|
void ArticleView::back()
|
|
|
|
{
|
|
|
|
// Don't allow navigating back to page 0, which is usually the initial
|
|
|
|
// empty page
|
2011-06-07 11:27:19 +00:00
|
|
|
if ( canGoBack() )
|
2011-06-07 07:57:25 +00:00
|
|
|
{
|
|
|
|
saveHistoryUserData();
|
|
|
|
ui.definition->back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::forward()
|
|
|
|
{
|
|
|
|
saveHistoryUserData();
|
|
|
|
ui.definition->forward();
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:07:03 +00:00
|
|
|
bool ArticleView::hasSound()
|
|
|
|
{
|
2012-09-29 10:11:06 +00:00
|
|
|
QVariant v = ui.definition->page()->mainFrame()->evaluateJavaScript( "gdAudioLink;" );
|
|
|
|
if ( v.type() == QVariant::String )
|
|
|
|
soundScript = v.toString();
|
|
|
|
else
|
|
|
|
soundScript.clear();
|
|
|
|
return !soundScript.isEmpty();
|
2009-04-10 21:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::playSound()
|
|
|
|
{
|
2012-09-29 10:11:06 +00:00
|
|
|
if ( !soundScript.isEmpty() )
|
|
|
|
openLink( QUrl::fromEncoded( soundScript.toUtf8() ), ui.definition->url() );
|
2009-04-10 21:07:03 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 11:17:29 +00:00
|
|
|
QString ArticleView::toHtml()
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
return ui.definition->page()->mainFrame()->toHtml();
|
2009-05-01 11:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::getTitle()
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
return ui.definition->page()->mainFrame()->title();
|
2009-05-01 11:17:29 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 12:20:33 +00:00
|
|
|
void ArticleView::print( QPrinter * printer ) const
|
|
|
|
{
|
|
|
|
ui.definition->print( printer );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::contextMenuRequested( QPoint const & pos )
|
|
|
|
{
|
|
|
|
// Is that a link? Is there a selection?
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
QWebHitTestResult r = ui.definition->page()->mainFrame()->
|
2009-01-28 20:55:45 +00:00
|
|
|
hitTestContent( pos );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
updateCurrentArticleFromCurrentFrame( r.frame() );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
QMenu menu( this );
|
|
|
|
|
|
|
|
QAction * followLink = 0;
|
2009-05-29 19:48:50 +00:00
|
|
|
QAction * followLinkExternal = 0;
|
2009-02-08 15:49:17 +00:00
|
|
|
QAction * followLinkNewTab = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
QAction * lookupSelection = 0;
|
2009-05-14 19:27:19 +00:00
|
|
|
QAction * lookupSelectionGr = 0;
|
2009-02-08 15:49:17 +00:00
|
|
|
QAction * lookupSelectionNewTab = 0;
|
2009-05-14 19:27:19 +00:00
|
|
|
QAction * lookupSelectionNewTabGr = 0;
|
2011-07-31 00:11:07 +00:00
|
|
|
QAction * maxDictionaryRefsAction = 0;
|
2012-09-12 14:11:30 +00:00
|
|
|
QAction * addWordToHistoryAction = 0;
|
2012-09-15 13:24:04 +00:00
|
|
|
QAction * addHeaderToHistoryAction = 0;
|
2012-11-26 13:13:13 +00:00
|
|
|
QAction * sendWordToInputLineAction = 0;
|
2013-02-22 12:44:23 +00:00
|
|
|
QAction * saveImageAction = 0;
|
|
|
|
QAction * saveSoundAction = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
QUrl targetUrl( r.linkUrl() );
|
|
|
|
Contexts contexts;
|
|
|
|
|
|
|
|
tryMangleWebsiteClickedUrl( targetUrl, contexts );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !r.linkUrl().isEmpty() )
|
|
|
|
{
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !isExternalLink( targetUrl ) )
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2009-05-29 19:48:50 +00:00
|
|
|
followLink = new QAction( tr( "&Open Link" ), &menu );
|
|
|
|
menu.addAction( followLink );
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !popupView )
|
|
|
|
{
|
|
|
|
followLinkNewTab = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Open Link in New &Tab" ), &menu );
|
|
|
|
menu.addAction( followLinkNewTab );
|
|
|
|
}
|
|
|
|
}
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( isExternalLink( r.linkUrl() ) )
|
|
|
|
{
|
|
|
|
followLinkExternal = new QAction( tr( "Open Link in &External Browser" ), &menu );
|
|
|
|
menu.addAction( followLinkExternal );
|
2009-04-29 23:18:26 +00:00
|
|
|
menu.addAction( ui.definition->pageAction( QWebPage::CopyLinkToClipboard ) );
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 12:44:23 +00:00
|
|
|
QWebElement el = r.element();
|
|
|
|
QUrl imageUrl;
|
|
|
|
if( !popupView && el.tagName().compare( "img", Qt::CaseInsensitive ) == 0 )
|
|
|
|
{
|
|
|
|
imageUrl = QUrl::fromPercentEncoding( el.attribute( "src" ).toLatin1() );
|
|
|
|
if( !imageUrl.isEmpty() )
|
|
|
|
{
|
2013-02-23 08:05:56 +00:00
|
|
|
saveImageAction = new QAction( tr( "Save &image..." ), &menu );
|
2013-02-22 12:44:23 +00:00
|
|
|
menu.addAction( saveImageAction );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !popupView && ( targetUrl.scheme() == "gdau"
|
|
|
|
|| Dictionary::WebMultimediaDownload::isAudioUrl( targetUrl ) ) )
|
|
|
|
{
|
2013-02-23 08:05:56 +00:00
|
|
|
saveSoundAction = new QAction( tr( "Save s&ound..." ), &menu );
|
2013-02-22 12:44:23 +00:00
|
|
|
menu.addAction( saveSoundAction );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
QString selectedText = ui.definition->selectedText();
|
2009-09-08 20:31:31 +00:00
|
|
|
|
|
|
|
if ( selectedText.size() && selectedText.size() < 60 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-09-08 20:31:31 +00:00
|
|
|
// We don't prompt for selections larger or equal to 60 chars, since
|
|
|
|
// it ruins the menu and it's hardly a single word anyway.
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
lookupSelection = new QAction( tr( "&Look up \"%1\"" ).
|
|
|
|
arg( ui.definition->selectedText() ),
|
|
|
|
&menu );
|
2009-01-28 20:55:45 +00:00
|
|
|
menu.addAction( lookupSelection );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-02-08 15:49:17 +00:00
|
|
|
if ( !popupView )
|
|
|
|
{
|
2009-05-14 19:27:19 +00:00
|
|
|
lookupSelectionNewTab = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Look up \"%1\" in &New Tab" ).
|
|
|
|
arg( ui.definition->selectedText() ),
|
|
|
|
&menu );
|
2009-02-08 15:49:17 +00:00
|
|
|
menu.addAction( lookupSelectionNewTab );
|
2012-11-26 13:13:13 +00:00
|
|
|
|
|
|
|
sendWordToInputLineAction = new QAction( tr( "Send \"%1\" to input line" ).
|
|
|
|
arg( ui.definition->selectedText() ),
|
|
|
|
&menu );
|
|
|
|
menu.addAction( sendWordToInputLineAction );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
2009-04-03 21:57:23 +00:00
|
|
|
|
2012-09-16 10:30:14 +00:00
|
|
|
addWordToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).
|
|
|
|
arg( ui.definition->selectedText() ),
|
|
|
|
&menu );
|
|
|
|
menu.addAction( addWordToHistoryAction );
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
Instances::Group const * altGroup =
|
|
|
|
( groupComboBox && groupComboBox->getCurrentGroup() != getGroup( ui.definition->url() ) ) ?
|
|
|
|
groups.findGroup( groupComboBox->getCurrentGroup() ) : 0;
|
|
|
|
|
|
|
|
if ( altGroup )
|
|
|
|
{
|
|
|
|
QIcon icon = altGroup->icon.size() ? QIcon( ":/flags/" + altGroup->icon ) :
|
|
|
|
QIcon();
|
|
|
|
|
2009-05-24 16:40:53 +00:00
|
|
|
lookupSelectionGr = new QAction( icon, tr( "Look up \"%1\" in %2" ).
|
2009-05-14 19:27:19 +00:00
|
|
|
arg( ui.definition->selectedText() ).
|
|
|
|
arg( altGroup->name ), &menu );
|
|
|
|
menu.addAction( lookupSelectionGr );
|
|
|
|
|
|
|
|
if ( !popupView )
|
|
|
|
{
|
|
|
|
lookupSelectionNewTabGr = new QAction( QIcon( ":/icons/addtab.png" ),
|
|
|
|
tr( "Look up \"%1\" in %2 in &New Tab" ).
|
|
|
|
arg( ui.definition->selectedText() ).
|
|
|
|
arg( altGroup->name ), &menu );
|
|
|
|
menu.addAction( lookupSelectionNewTabGr );
|
|
|
|
}
|
|
|
|
}
|
2009-09-08 20:31:31 +00:00
|
|
|
}
|
2009-05-14 19:27:19 +00:00
|
|
|
|
2013-01-18 13:22:24 +00:00
|
|
|
if( selectedText.isEmpty() && !cfg.preferences.storeHistory)
|
2013-01-17 18:38:49 +00:00
|
|
|
{
|
|
|
|
addHeaderToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).
|
|
|
|
arg( ui.definition->title() ),
|
|
|
|
&menu );
|
|
|
|
menu.addAction( addHeaderToHistoryAction );
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:22:24 +00:00
|
|
|
if ( selectedText.size() )
|
|
|
|
{
|
|
|
|
menu.addAction( ui.definition->pageAction( QWebPage::Copy ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-18 14:37:24 +00:00
|
|
|
menu.addAction( &selectCurrentArticleAction );
|
2013-01-18 13:22:24 +00:00
|
|
|
menu.addAction( ui.definition->pageAction( QWebPage::SelectAll ) );
|
|
|
|
}
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
map< QAction *, QString > tableOfContents;
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
// Add table of contents
|
2009-05-11 22:25:22 +00:00
|
|
|
QStringList ids = getArticlesList();
|
2009-04-12 16:22:42 +00:00
|
|
|
|
|
|
|
if ( !menu.isEmpty() && ids.size() )
|
|
|
|
menu.addSeparator();
|
2009-04-14 18:35:27 +00:00
|
|
|
|
|
|
|
unsigned refsAdded = 0;
|
2011-07-31 00:11:07 +00:00
|
|
|
bool maxDictionaryRefsReached = false;
|
2009-04-14 18:35:27 +00:00
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
for( QStringList::const_iterator i = ids.constBegin(); i != ids.constEnd();
|
2009-04-14 18:35:27 +00:00
|
|
|
++i, ++refsAdded )
|
2009-04-12 16:22:42 +00:00
|
|
|
{
|
|
|
|
// Find this dictionary
|
|
|
|
|
|
|
|
for( unsigned x = allDictionaries.size(); x--; )
|
|
|
|
{
|
|
|
|
if ( allDictionaries[ x ]->getId() == i->toUtf8().data() )
|
|
|
|
{
|
2011-07-31 00:11:07 +00:00
|
|
|
QAction * action = 0;
|
|
|
|
if ( refsAdded == cfg.maxDictionaryRefsInContextMenu )
|
|
|
|
{
|
|
|
|
// Enough! Or the menu would become too large.
|
|
|
|
maxDictionaryRefsAction = new QAction( ".........", &menu );
|
|
|
|
action = maxDictionaryRefsAction;
|
|
|
|
maxDictionaryRefsReached = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
action = new QAction(
|
|
|
|
allDictionaries[ x ]->getIcon(),
|
|
|
|
QString::fromUtf8( allDictionaries[ x ]->getName().c_str() ),
|
|
|
|
&menu );
|
|
|
|
// Force icons in menu on all platfroms,
|
|
|
|
// since without them it will be much harder
|
|
|
|
// to find things.
|
|
|
|
action->setIconVisibleInMenu( true );
|
|
|
|
}
|
2009-04-12 16:22:42 +00:00
|
|
|
menu.addAction( action );
|
|
|
|
|
|
|
|
tableOfContents[ action ] = *i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-31 00:11:07 +00:00
|
|
|
if( maxDictionaryRefsReached )
|
|
|
|
break;
|
2009-04-12 16:22:42 +00:00
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !menu.isEmpty() )
|
|
|
|
{
|
2012-09-12 14:18:16 +00:00
|
|
|
connect( this, SIGNAL( closePopupMenu() ), &menu, SLOT( close() ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
QAction * result = menu.exec( ui.definition->mapToGlobal( pos ) );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
if ( !result )
|
|
|
|
return;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( result == followLink )
|
2009-05-29 19:48:50 +00:00
|
|
|
openLink( targetUrl, ui.definition->url(), getCurrentArticle(), contexts );
|
|
|
|
else
|
|
|
|
if ( result == followLinkExternal )
|
|
|
|
QDesktopServices::openUrl( r.linkUrl() );
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
|
|
|
if ( result == lookupSelection )
|
2009-05-11 19:14:28 +00:00
|
|
|
showDefinition( selectedText, getGroup( ui.definition->url() ), getCurrentArticle() );
|
2009-02-08 15:49:17 +00:00
|
|
|
else
|
2009-05-14 19:27:19 +00:00
|
|
|
if ( result == lookupSelectionGr && groupComboBox )
|
|
|
|
showDefinition( selectedText, groupComboBox->getCurrentGroup(), QString() );
|
|
|
|
else
|
2012-09-12 14:11:30 +00:00
|
|
|
if ( result == addWordToHistoryAction )
|
|
|
|
emit forceAddWordToHistory( selectedText );
|
2012-09-15 13:24:04 +00:00
|
|
|
if ( result == addHeaderToHistoryAction )
|
|
|
|
emit forceAddWordToHistory( ui.definition->title() );
|
2012-09-12 14:11:30 +00:00
|
|
|
else
|
2012-11-26 13:13:13 +00:00
|
|
|
if( result == sendWordToInputLineAction )
|
|
|
|
emit sendWordToInputLine( selectedText );
|
|
|
|
else
|
2009-04-12 16:22:42 +00:00
|
|
|
if ( !popupView && result == followLinkNewTab )
|
2009-05-29 19:48:50 +00:00
|
|
|
emit openLinkInNewTab( targetUrl, ui.definition->url(), getCurrentArticle(), contexts );
|
2009-04-12 16:22:42 +00:00
|
|
|
else
|
|
|
|
if ( !popupView && result == lookupSelectionNewTab )
|
2009-05-11 19:14:28 +00:00
|
|
|
emit showDefinitionInNewTab( selectedText, getGroup( ui.definition->url() ),
|
2009-05-29 19:48:50 +00:00
|
|
|
getCurrentArticle(), Contexts() );
|
2009-04-12 16:22:42 +00:00
|
|
|
else
|
2009-05-14 19:27:19 +00:00
|
|
|
if ( !popupView && result == lookupSelectionNewTabGr && groupComboBox )
|
|
|
|
emit showDefinitionInNewTab( selectedText, groupComboBox->getCurrentGroup(),
|
2009-05-29 19:48:50 +00:00
|
|
|
QString(), Contexts() );
|
2009-05-14 19:27:19 +00:00
|
|
|
else
|
2013-02-22 12:44:23 +00:00
|
|
|
if( result == saveImageAction )
|
|
|
|
saveResource( imageUrl, ui.definition->url() );
|
|
|
|
else
|
|
|
|
if( result == saveSoundAction )
|
|
|
|
saveResource( targetUrl, ui.definition->url() );
|
|
|
|
else
|
2009-02-08 15:49:17 +00:00
|
|
|
{
|
2011-07-31 00:11:07 +00:00
|
|
|
if ( !popupView && result == maxDictionaryRefsAction )
|
|
|
|
emit showDictsPane();
|
|
|
|
|
2009-04-12 16:22:42 +00:00
|
|
|
// Match against table of contents
|
|
|
|
QString id = tableOfContents[ result ];
|
|
|
|
|
|
|
|
if ( id.size() )
|
2009-05-15 14:11:54 +00:00
|
|
|
setCurrentArticle( "gdfrom-" + id, true );
|
2009-02-08 15:49:17 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
#if 0
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "%s\n", r.linkUrl().isEmpty() ? "null" : "not null" );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "url = %s\n", r.linkUrl().toString().toLocal8Bit().data() );
|
|
|
|
DPRINTF( "title = %s\n", r.title().toLocal8Bit().data() );
|
2009-01-28 20:55:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ArticleView::resourceDownloadFinished()
|
|
|
|
{
|
|
|
|
if ( resourceDownloadRequests.empty() )
|
|
|
|
return; // Stray signal
|
|
|
|
|
|
|
|
// Find any finished resources
|
|
|
|
for( list< sptr< Dictionary::DataRequest > >::iterator i =
|
|
|
|
resourceDownloadRequests.begin(); i != resourceDownloadRequests.end(); )
|
|
|
|
{
|
|
|
|
if ( (*i)->isFinished() )
|
|
|
|
{
|
|
|
|
if ( (*i)->dataSize() >= 0 )
|
|
|
|
{
|
|
|
|
// Ok, got one finished, all others are irrelevant now
|
|
|
|
|
|
|
|
vector< char > const & data = (*i)->getFullData();
|
|
|
|
|
2010-05-29 11:33:04 +00:00
|
|
|
if ( resourceDownloadUrl.scheme() == "gdau" ||
|
|
|
|
Dictionary::WebMultimediaDownload::isAudioUrl( resourceDownloadUrl ) )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2010-01-02 18:16:22 +00:00
|
|
|
// Audio data
|
|
|
|
|
2010-11-14 15:38:41 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// If we use Windows PlaySound, use that, not Phonon.
|
|
|
|
if ( !cfg.preferences.useExternalPlayer &&
|
|
|
|
cfg.preferences.useWindowsPlaySound )
|
|
|
|
{
|
|
|
|
// Stop any currently playing sound to make sure the previous data
|
|
|
|
// isn't used anymore
|
|
|
|
if ( winWavData.size() )
|
|
|
|
{
|
|
|
|
PlaySoundA( 0, 0, 0 );
|
|
|
|
winWavData.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( data.size() < 4 || memcmp( data.data(), "RIFF", 4 ) != 0 )
|
|
|
|
{
|
|
|
|
QMessageBox::information( this, tr( "Playing a non-WAV file" ),
|
|
|
|
tr( "To enable playback of files different than WAV, please go "
|
|
|
|
"to Edit|Preferences, choose the Audio tab and select "
|
|
|
|
"\"Play via DirectShow\" there." ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
winWavData = data;
|
|
|
|
PlaySoundA( &winWavData.front(), 0,
|
|
|
|
SND_ASYNC | SND_MEMORY | SND_NODEFAULT | SND_NOWAIT );
|
|
|
|
}
|
|
|
|
}
|
2012-11-23 12:43:36 +00:00
|
|
|
else if ( !cfg.preferences.useExternalPlayer &&
|
|
|
|
cfg.preferences.useBassLibrary )
|
|
|
|
{
|
|
|
|
if( !BassAudioPlayer::instance().canBeUsed() )
|
|
|
|
emit statusBarMessage( tr( "WARNING: %1" ).arg( tr( "Bass library not found." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !BassAudioPlayer::instance().playMemory( data.data(), data.size() ) )
|
|
|
|
emit statusBarMessage( tr( "WARNING: %1" ).arg( tr( "Bass library can't play this sound." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
|
|
|
}
|
|
|
|
}
|
2010-11-14 15:38:41 +00:00
|
|
|
else
|
|
|
|
#endif
|
2010-01-02 18:16:22 +00:00
|
|
|
if ( !cfg.preferences.useExternalPlayer )
|
|
|
|
{
|
|
|
|
// Play via Phonon
|
|
|
|
|
|
|
|
QBuffer * buf = new QBuffer;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
buf->buffer().append( &data.front(), data.size() );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
Phonon::MediaSource source( buf );
|
|
|
|
source.setAutoDelete( true ); // Dispose of our buf when done
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
AudioPlayer::instance().object.stop();
|
|
|
|
AudioPlayer::instance().object.clear();
|
|
|
|
AudioPlayer::instance().object.enqueue( source );
|
|
|
|
AudioPlayer::instance().object.play();
|
|
|
|
}
|
|
|
|
else
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
|
2010-01-02 18:16:22 +00:00
|
|
|
// Use external viewer to play the file
|
2009-03-26 19:00:08 +00:00
|
|
|
try
|
|
|
|
{
|
2010-01-02 18:16:22 +00:00
|
|
|
ExternalViewer * viewer = new ExternalViewer( this, data, "wav", cfg.preferences.audioPlaybackProgram.trimmed() );
|
|
|
|
|
2011-05-29 05:08:37 +00:00
|
|
|
// Once started, it will erase itself
|
|
|
|
viewer->start();
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2010-01-02 18:16:22 +00:00
|
|
|
catch( ExternalViewer::Ex & e )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "Failed to run a player to play sound file: %1" ).arg( e.what() ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create a temporary file
|
2010-09-16 18:53:39 +00:00
|
|
|
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Remove the one previously used, if any
|
|
|
|
cleanupTemp();
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
QTemporaryFile tmp(
|
|
|
|
QDir::temp().filePath( "XXXXXX-" + resourceDownloadUrl.path().section( '/', -1 ) ), this );
|
|
|
|
|
2012-10-31 13:58:35 +00:00
|
|
|
if ( !tmp.open() || (size_t) tmp.write( &data.front(), data.size() ) != data.size() )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict", tr( "Failed to create temporary file." ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
tmp.setAutoRemove( false );
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
desktopOpenedTempFile = tmp.fileName();
|
|
|
|
}
|
2010-09-16 18:53:39 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !QDesktopServices::openUrl( QUrl::fromLocalFile( desktopOpenedTempFile ) ) )
|
2013-02-01 12:36:01 +00:00
|
|
|
QMessageBox::critical( this, "GoldenDict",
|
2011-07-14 20:11:57 +00:00
|
|
|
tr( "Failed to auto-open resource file, try opening manually: %1." ).arg( desktopOpenedTempFile ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, whatever it was, it's finished. Remove this and any other
|
|
|
|
// requests and finish.
|
|
|
|
|
|
|
|
resourceDownloadRequests.clear();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-29 05:08:37 +00:00
|
|
|
// This one had no data. Erase it.
|
2009-03-26 19:00:08 +00:00
|
|
|
resourceDownloadRequests.erase( i++ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Unfinished, try the next one.
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( resourceDownloadRequests.empty() )
|
|
|
|
{
|
2011-07-14 20:11:57 +00:00
|
|
|
emit statusBarMessage(
|
|
|
|
tr( "WARNING: %1" ).arg( tr( "The referenced resource failed to download." ) ),
|
|
|
|
10000, QPixmap( ":/icons/error.png" ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-12 10:52:11 +00:00
|
|
|
void ArticleView::pasteTriggered()
|
|
|
|
{
|
|
|
|
QString text =
|
|
|
|
gd::toQString(
|
|
|
|
Folding::trimWhitespaceOrPunct(
|
|
|
|
gd::toWString(
|
|
|
|
QApplication::clipboard()->text() ) ) );
|
|
|
|
|
|
|
|
if ( text.size() )
|
|
|
|
showDefinition( text, getGroup( ui.definition->url() ), getCurrentArticle() );
|
|
|
|
}
|
|
|
|
|
2009-05-15 14:11:54 +00:00
|
|
|
void ArticleView::moveOneArticleUp()
|
|
|
|
{
|
|
|
|
QString current = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( current.size() )
|
|
|
|
{
|
|
|
|
QStringList lst = getArticlesList();
|
|
|
|
|
|
|
|
int idx = lst.indexOf( current.mid( 7 ) );
|
|
|
|
|
|
|
|
if ( idx != -1 )
|
|
|
|
{
|
|
|
|
--idx;
|
|
|
|
|
|
|
|
if ( idx < 0 )
|
|
|
|
idx = lst.size() - 1;
|
|
|
|
|
|
|
|
setCurrentArticle( "gdfrom-" + lst[ idx ], true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::moveOneArticleDown()
|
|
|
|
{
|
|
|
|
QString current = getCurrentArticle();
|
|
|
|
|
|
|
|
if ( current.size() )
|
|
|
|
{
|
|
|
|
QStringList lst = getArticlesList();
|
|
|
|
|
|
|
|
int idx = lst.indexOf( current.mid( 7 ) );
|
|
|
|
|
|
|
|
if ( idx != -1 )
|
|
|
|
{
|
|
|
|
idx = ( idx + 1 ) % lst.size();
|
|
|
|
|
|
|
|
setCurrentArticle( "gdfrom-" + lst[ idx ], true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
void ArticleView::openSearch()
|
|
|
|
{
|
|
|
|
if ( !searchIsOpened )
|
|
|
|
{
|
|
|
|
ui.searchFrame->show();
|
|
|
|
ui.searchText->setText( getTitle() );
|
|
|
|
searchIsOpened = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.searchText->setFocus();
|
|
|
|
ui.searchText->selectAll();
|
|
|
|
|
|
|
|
// Clear any current selection
|
|
|
|
if ( ui.definition->selectedText().size() )
|
|
|
|
{
|
2009-05-16 18:04:21 +00:00
|
|
|
ui.definition->page()->currentFrame()->
|
|
|
|
evaluateJavaScript( "window.getSelection().removeAllRanges();" );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ui.searchText->property( "noResults" ).toBool() )
|
|
|
|
{
|
|
|
|
ui.searchText->setProperty( "noResults", false );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
// Reload stylesheet
|
2010-11-15 15:22:35 +00:00
|
|
|
reloadStyleSheet();
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchPrevious_clicked()
|
|
|
|
{
|
2013-01-22 21:04:45 +00:00
|
|
|
if ( searchIsOpened )
|
|
|
|
performFindOperation( false, true );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchNext_clicked()
|
|
|
|
{
|
2013-01-22 21:04:45 +00:00
|
|
|
if ( searchIsOpened )
|
|
|
|
performFindOperation( false, false );
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchText_textEdited()
|
|
|
|
{
|
|
|
|
performFindOperation( true, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchText_returnPressed()
|
|
|
|
{
|
|
|
|
on_searchNext_clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_searchCloseButton_clicked()
|
|
|
|
{
|
|
|
|
closeSearch();
|
|
|
|
}
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
void ArticleView::on_searchCaseSensitive_clicked()
|
|
|
|
{
|
|
|
|
performFindOperation( false, false, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::on_highlightAllButton_clicked()
|
|
|
|
{
|
|
|
|
performFindOperation( false, false, true );
|
|
|
|
}
|
|
|
|
|
2011-07-03 12:27:08 +00:00
|
|
|
void ArticleView::onJsActiveArticleChanged(QString const & id)
|
|
|
|
{
|
|
|
|
if ( !id.startsWith( "gdfrom-" ) )
|
|
|
|
return; // Incorrect id
|
|
|
|
|
|
|
|
emit activeArticleChanged( id.mid( 7 ) );
|
|
|
|
}
|
|
|
|
|
2010-04-08 20:37:59 +00:00
|
|
|
void ArticleView::doubleClicked()
|
|
|
|
{
|
|
|
|
// We might want to initiate translation of the selected word
|
|
|
|
|
|
|
|
if ( cfg.preferences.doubleClickTranslates )
|
|
|
|
{
|
|
|
|
QString selectedText = ui.definition->selectedText();
|
|
|
|
|
|
|
|
// Do some checks to make sure there's a sensible selection indeed
|
|
|
|
if ( Folding::applyWhitespaceOnly( gd::toWString( selectedText ) ).size() &&
|
2013-01-18 11:00:38 +00:00
|
|
|
selectedText.size() < 60 )
|
2010-04-08 20:37:59 +00:00
|
|
|
{
|
|
|
|
// Initiate translation
|
2010-09-16 18:53:39 +00:00
|
|
|
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
|
2011-05-02 00:37:56 +00:00
|
|
|
if (kmod & (Qt::ControlModifier | Qt::ShiftModifier))
|
2010-09-16 18:53:39 +00:00
|
|
|
{ // open in new tab
|
|
|
|
emit showDefinitionInNewTab( selectedText, getGroup( ui.definition->url() ),
|
|
|
|
getCurrentArticle(), Contexts() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
showDefinition( selectedText, getGroup( ui.definition->url() ), getCurrentArticle() );
|
2010-04-08 20:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
void ArticleView::performFindOperation( bool restart, bool backwards, bool checkHighlight )
|
2009-05-16 11:14:43 +00:00
|
|
|
{
|
|
|
|
QString text = ui.searchText->text();
|
|
|
|
|
2011-06-12 23:59:35 +00:00
|
|
|
if ( restart || checkHighlight )
|
2009-05-16 11:14:43 +00:00
|
|
|
{
|
2011-06-12 23:59:35 +00:00
|
|
|
if( restart ) {
|
|
|
|
// Anyone knows how we reset the search position?
|
|
|
|
// For now we resort to this hack:
|
|
|
|
if ( ui.definition->selectedText().size() )
|
|
|
|
{
|
|
|
|
ui.definition->page()->currentFrame()->
|
|
|
|
evaluateJavaScript( "window.getSelection().removeAllRanges();" );
|
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
2011-06-12 23:59:35 +00:00
|
|
|
|
|
|
|
QWebPage::FindFlags f( 0 );
|
|
|
|
|
|
|
|
if ( ui.searchCaseSensitive->isChecked() )
|
|
|
|
f |= QWebPage::FindCaseSensitively;
|
|
|
|
|
|
|
|
f |= QWebPage::HighlightAllOccurrences;
|
|
|
|
|
|
|
|
ui.definition->findText( "", f );
|
|
|
|
|
|
|
|
if( ui.highlightAllButton->isChecked() )
|
|
|
|
ui.definition->findText( text, f );
|
|
|
|
|
|
|
|
if( checkHighlight )
|
|
|
|
return;
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QWebPage::FindFlags f( 0 );
|
|
|
|
|
|
|
|
if ( ui.searchCaseSensitive->isChecked() )
|
|
|
|
f |= QWebPage::FindCaseSensitively;
|
|
|
|
|
|
|
|
if ( backwards )
|
|
|
|
f |= QWebPage::FindBackward;
|
|
|
|
|
|
|
|
bool setMark = text.size() && !ui.definition->findText( text, f );
|
|
|
|
|
|
|
|
if ( ui.searchText->property( "noResults" ).toBool() != setMark )
|
|
|
|
{
|
|
|
|
ui.searchText->setProperty( "noResults", setMark );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
// Reload stylesheet
|
2010-11-15 15:22:35 +00:00
|
|
|
reloadStyleSheet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::reloadStyleSheet()
|
|
|
|
{
|
|
|
|
for( QWidget * w = parentWidget(); w; w = w->parentWidget() )
|
|
|
|
{
|
|
|
|
if ( w->styleSheet().size() )
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
2010-11-15 15:22:35 +00:00
|
|
|
w->setStyleSheet( w->styleSheet() );
|
|
|
|
break;
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
2009-05-16 11:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 15:22:35 +00:00
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
bool ArticleView::closeSearch()
|
|
|
|
{
|
|
|
|
if ( searchIsOpened )
|
|
|
|
{
|
|
|
|
ui.searchFrame->hide();
|
|
|
|
ui.definition->setFocus();
|
|
|
|
searchIsOpened = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:12:46 +00:00
|
|
|
bool ArticleView::isSearchOpened()
|
|
|
|
{
|
|
|
|
return searchIsOpened;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void ArticleView::showEvent( QShowEvent * ev )
|
|
|
|
{
|
|
|
|
QFrame::showEvent( ev );
|
|
|
|
|
2009-05-16 11:14:43 +00:00
|
|
|
if ( !searchIsOpened )
|
|
|
|
ui.searchFrame->hide();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2012-09-16 10:19:47 +00:00
|
|
|
|
|
|
|
void ArticleView::receiveExpandOptionalParts( bool expand )
|
|
|
|
{
|
|
|
|
if( expandOptionalParts != expand )
|
|
|
|
{
|
|
|
|
int n = getArticlesList().indexOf( getActiveArticleId() );
|
|
|
|
if( n > 0 )
|
|
|
|
articleToJump = getCurrentArticle();
|
|
|
|
|
|
|
|
emit setExpandMode( expand );
|
|
|
|
expandOptionalParts = expand;
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleView::switchExpandOptionalParts()
|
|
|
|
{
|
|
|
|
expandOptionalParts = !expandOptionalParts;
|
|
|
|
|
|
|
|
int n = getArticlesList().indexOf( getActiveArticleId() );
|
|
|
|
if( n > 0 )
|
|
|
|
articleToJump = getCurrentArticle();
|
|
|
|
|
|
|
|
emit setExpandMode( expandOptionalParts );
|
|
|
|
reload();
|
|
|
|
}
|
2012-09-18 23:01:31 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
|
|
|
|
void ArticleView::readTag( const QString & from, QString & to, int & count )
|
|
|
|
{
|
|
|
|
QChar ch, prev_ch;
|
|
|
|
bool inQuote = false, inDoublequote = false;
|
|
|
|
|
|
|
|
to.append( ch = prev_ch = from[ count++ ] );
|
|
|
|
while( count < from.size() )
|
|
|
|
{
|
|
|
|
ch = from[ count ];
|
|
|
|
if( ch == '>' && !( inQuote || inDoublequote ) )
|
|
|
|
{
|
|
|
|
to.append( ch );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( ch == '\'' )
|
|
|
|
inQuote = !inQuote;
|
|
|
|
if( ch == '\"' )
|
|
|
|
inDoublequote = !inDoublequote;
|
|
|
|
to.append( prev_ch = ch );
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::insertSpans( QString const & html )
|
|
|
|
{
|
|
|
|
QChar ch;
|
|
|
|
QString newContent;
|
|
|
|
bool inSpan = false, escaped = false;
|
|
|
|
|
|
|
|
/// Enclose every word in string (exclude tags) with <span></span>
|
|
|
|
|
|
|
|
for( int i = 0; i < html.size(); i++ )
|
|
|
|
{
|
|
|
|
ch = html[ i ];
|
|
|
|
if( ch == '&' )
|
|
|
|
{
|
|
|
|
escaped = true;
|
|
|
|
if( inSpan )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
newContent.append( ch );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ch == '<' ) // Skip tag
|
|
|
|
{
|
|
|
|
escaped = false;
|
|
|
|
if( inSpan )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
readTag( html, newContent, i );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( escaped )
|
|
|
|
{
|
|
|
|
if( ch == ';' )
|
|
|
|
escaped = false;
|
|
|
|
newContent.append( ch );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !inSpan && ( ch.isLetterOrNumber() || ch.isLowSurrogate() ) )
|
|
|
|
{
|
|
|
|
newContent.append( "<span>");
|
|
|
|
inSpan = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( inSpan && !( ch.isLetterOrNumber() || ch.isLowSurrogate() ) )
|
|
|
|
{
|
|
|
|
newContent.append( "</span>");
|
|
|
|
inSpan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ch.isLowSurrogate() )
|
|
|
|
{
|
|
|
|
newContent.append( ch );
|
|
|
|
ch = html[ ++i ];
|
|
|
|
}
|
|
|
|
|
|
|
|
newContent.append( ch );
|
2012-09-27 13:54:09 +00:00
|
|
|
if( ch == '-' && !( html[ i + 1 ] == ' ' || ( i > 0 && html[ i - 1 ] == ' ' ) ) )
|
|
|
|
newContent.append( "<span style=\"font-size:0pt\"> </span>" );
|
2012-09-18 23:01:31 +00:00
|
|
|
}
|
|
|
|
if( inSpan )
|
|
|
|
newContent.append( "</span>" );
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::checkElement( QWebElement & elem, QPoint const & pt )
|
|
|
|
{
|
|
|
|
/// Search for lower-level matching element
|
|
|
|
|
|
|
|
QWebElement parentElem = elem;
|
|
|
|
QWebElement childElem = elem.firstChild();
|
|
|
|
while( !childElem.isNull() )
|
|
|
|
{
|
|
|
|
if( childElem.geometry().contains( pt ) )
|
|
|
|
{
|
|
|
|
parentElem = childElem;
|
|
|
|
childElem = parentElem.firstChild();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
childElem = childElem.nextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentElem.toPlainText();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ArticleView::wordAtPoint( int x, int y )
|
|
|
|
{
|
|
|
|
QString word;
|
|
|
|
|
|
|
|
if( popupView )
|
|
|
|
return word;
|
|
|
|
|
|
|
|
QPoint pos = mapFromGlobal( QPoint( x, y ) );
|
|
|
|
QWebFrame *frame = ui.definition->page()->frameAt( pos );
|
|
|
|
if( !frame )
|
|
|
|
return word;
|
|
|
|
|
|
|
|
QPoint posWithScroll = pos + frame->scrollPosition();
|
|
|
|
|
|
|
|
/// Find target HTML element
|
|
|
|
|
|
|
|
QWebHitTestResult result = frame->hitTestContent( pos );
|
|
|
|
QWebElement baseElem = result.enclosingBlockElement();
|
|
|
|
|
|
|
|
if( baseElem.tagName().compare( "BODY" ) == 0 || /// Assume empty field position
|
|
|
|
baseElem.tagName().compare( "HTML" ) == 0 ||
|
|
|
|
baseElem.tagName().compare( "HEAD" ) == 0 )
|
|
|
|
return word;
|
|
|
|
|
2012-09-24 13:42:36 +00:00
|
|
|
/// Save selection position
|
|
|
|
|
|
|
|
baseElem.evaluateJavaScript( "var __gd_sel=window.getSelection();"
|
|
|
|
"if(__gd_sel && __gd_sel.rangeCount>0) {"
|
|
|
|
"__gd_SelRange=__gd_sel.getRangeAt(0);"
|
2012-09-27 13:54:09 +00:00
|
|
|
"if(__gd_SelRange.collapsed) __gd_sel.removeAllRanges();"
|
|
|
|
"else {"
|
|
|
|
"__gd_StartTree=[]; __gd_EndTree=[];"
|
|
|
|
"var __gd_baseRange=document.createRange();"
|
|
|
|
"__gd_baseRange.selectNode(this);"
|
|
|
|
"if(__gd_baseRange.comparePoint(__gd_SelRange.startContainer,0)==0) {"
|
|
|
|
"__gd_StartOffset=__gd_SelRange.startOffset;"
|
|
|
|
"var __gd_child=__gd_SelRange.startContainer;"
|
|
|
|
"var __gd_parent='';"
|
|
|
|
"if(__gd_child==this) __gd_StartTree.push(-1);"
|
|
|
|
"else while(__gd_parent!=this) {"
|
|
|
|
"var n=0; __gd_parent=__gd_child.parentNode;"
|
|
|
|
"var __gd_el=__gd_parent.firstChild;"
|
|
|
|
"while(__gd_el!=__gd_child) { n++; __gd_el=__gd_el.nextSibling; }"
|
|
|
|
"__gd_StartTree.push(n);"
|
|
|
|
"__gd_child=__gd_parent;"
|
|
|
|
"}"
|
2012-09-24 13:42:36 +00:00
|
|
|
"}"
|
2012-09-27 13:54:09 +00:00
|
|
|
"if(__gd_baseRange.comparePoint(__gd_SelRange.endContainer,0)==0) {"
|
|
|
|
"__gd_EndOffset=__gd_SelRange.endOffset;"
|
|
|
|
"var __gd_child=__gd_SelRange.endContainer;"
|
|
|
|
"var __gd_parent='';"
|
|
|
|
"if(__gd_child==this) __gd_EndTree.push(-1);"
|
|
|
|
"else while(__gd_parent!=this) {"
|
|
|
|
"var n=0; __gd_parent=__gd_child.parentNode;"
|
|
|
|
"var __gd_el=__gd_parent.firstChild;"
|
|
|
|
"while(__gd_el!=__gd_child) { n++; __gd_el=__gd_el.nextSibling; }"
|
|
|
|
"__gd_EndTree.push(n);"
|
|
|
|
"__gd_child=__gd_parent;"
|
|
|
|
"}"
|
2012-09-24 13:42:36 +00:00
|
|
|
"}"
|
|
|
|
"}"
|
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
2012-09-18 23:01:31 +00:00
|
|
|
/// Enclose every word be <span> </span>
|
|
|
|
|
|
|
|
QString content = baseElem.toInnerXml();
|
|
|
|
QString newContent = insertSpans( content );
|
|
|
|
|
|
|
|
/// Set new code and re-render it to fill geometry
|
|
|
|
|
|
|
|
QImage img( baseElem.geometry().width(), baseElem.geometry().height(), QImage::Format_Mono );
|
|
|
|
img.fill( 0 );
|
|
|
|
QPainter painter( & img );
|
|
|
|
|
|
|
|
baseElem.setInnerXml( newContent );
|
|
|
|
baseElem.render( &painter );
|
|
|
|
|
|
|
|
/// Search in all child elements and check it
|
|
|
|
|
|
|
|
QWebElementCollection elemCollection = baseElem.findAll( "*" );
|
|
|
|
foreach ( QWebElement elem, elemCollection )
|
|
|
|
{
|
|
|
|
if( elem.geometry().contains( posWithScroll ) )
|
|
|
|
word = checkElement( elem, posWithScroll );
|
|
|
|
if( !word.isEmpty() )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Restore old content
|
|
|
|
baseElem.setInnerXml( content );
|
|
|
|
|
2012-09-24 13:42:36 +00:00
|
|
|
/// Restore selection
|
|
|
|
|
|
|
|
baseElem.evaluateJavaScript( "var flag=0;"
|
|
|
|
"if(__gd_StartTree && __gd_StartTree.length) {"
|
2012-09-27 13:54:09 +00:00
|
|
|
"var __gd_el=this;"
|
|
|
|
"while(__gd_StartTree.length) {"
|
|
|
|
"__gd_el=__gd_el.firstChild;"
|
|
|
|
"var n=__gd_StartTree.pop();"
|
|
|
|
"if(n<0) __gd_el=this;"
|
|
|
|
"else for(var i=0;i<n;i++) __gd_el=__gd_el.nextSibling;"
|
|
|
|
"}"
|
|
|
|
"__gd_SelRange.setStart(__gd_el, __gd_StartOffset);"
|
|
|
|
"__gd_StartTree.splice(0,__gd_StartTree.length);"
|
|
|
|
"flag+=1;"
|
2012-09-24 13:42:36 +00:00
|
|
|
"}"
|
|
|
|
"if(__gd_EndTree && __gd_EndTree.length) {"
|
2012-09-27 13:54:09 +00:00
|
|
|
"var __gd_el=this;"
|
|
|
|
"while(__gd_EndTree.length) {"
|
|
|
|
"__gd_el=__gd_el.firstChild;"
|
|
|
|
"var n=__gd_EndTree.pop();"
|
|
|
|
"if(n<0) __gd_el=this;"
|
|
|
|
"else for(var i=0;i<n;i++) __gd_el=__gd_el.nextSibling;"
|
|
|
|
"}"
|
|
|
|
"__gd_SelRange.setEnd(__gd_el, __gd_EndOffset);"
|
|
|
|
"__gd_EndTree.splice(0,__gd_EndTree.length);"
|
|
|
|
"flag+=1;"
|
2012-09-24 13:42:36 +00:00
|
|
|
"}"
|
|
|
|
"if(flag>0) {"
|
2012-09-27 13:54:09 +00:00
|
|
|
"var __gd_sel=window.getSelection();"
|
|
|
|
"__gd_sel.removeAllRanges();"
|
|
|
|
"__gd_sel.addRange(__gd_SelRange);"
|
2012-09-24 13:42:36 +00:00
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
2012-09-18 23:01:31 +00:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|