2014-06-23 15:11:15 +00:00
|
|
|
/* This file is (c) 2014 Abs62
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QSplitter>
|
|
|
|
#include <QHelpContentWidget>
|
|
|
|
#include <QHelpIndexWidget>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
|
|
|
#include "helpwindow.hh"
|
|
|
|
#include "gddebug.hh"
|
2022-01-08 13:16:22 +00:00
|
|
|
#include <QHelpLink>
|
2014-06-23 15:11:15 +00:00
|
|
|
|
|
|
|
namespace Help {
|
|
|
|
|
|
|
|
HelpBrowser::HelpBrowser( QHelpEngineCore * engine, QWidget *parent ) :
|
|
|
|
QTextBrowser( parent ),
|
|
|
|
helpEngine( engine )
|
|
|
|
{
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &QTextBrowser::anchorClicked, this, &HelpBrowser::linkClicked );
|
2014-06-23 15:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HelpBrowser::showHelpForKeyword( QString const & id )
|
|
|
|
{
|
|
|
|
if ( helpEngine )
|
|
|
|
{
|
2022-01-08 13:16:22 +00:00
|
|
|
QList<QHelpLink> links = helpEngine->documentsForIdentifier(id);
|
|
|
|
if (!links.isEmpty()) {
|
|
|
|
QHelpLink link=links.constFirst();
|
|
|
|
setSource(link.url);
|
|
|
|
}
|
2014-06-23 15:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant HelpBrowser::loadResource( int type, QUrl const & name )
|
|
|
|
{
|
|
|
|
QByteArray ba;
|
|
|
|
if( type < 4 && helpEngine )
|
|
|
|
{
|
|
|
|
QUrl url(name);
|
|
|
|
if( name.isRelative() )
|
|
|
|
url = source().resolved( url );
|
|
|
|
|
|
|
|
ba = helpEngine->fileData(url );
|
|
|
|
}
|
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpBrowser::linkClicked( QUrl const & url )
|
|
|
|
{
|
|
|
|
if( url.scheme() == "http" || url.scheme() == "https" )
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl( url );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setSource( url );
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpWindow::HelpWindow( QWidget * parent, Config::Class & cfg_ ) :
|
|
|
|
QDialog( parent ),
|
|
|
|
cfg( cfg_ ),
|
|
|
|
helpEngine( 0 )
|
|
|
|
{
|
|
|
|
resize( QSize( 600, 450 ) );
|
|
|
|
setWindowTitle( tr( "GoldenDict help" ) );
|
|
|
|
setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
|
|
|
|
|
|
|
|
QVBoxLayout * mainLayout = new QVBoxLayout( this );
|
|
|
|
setLayout( mainLayout );
|
|
|
|
|
|
|
|
navToolBar = new QToolBar( this );
|
2022-01-24 14:23:38 +00:00
|
|
|
navHome = navToolBar->addAction( QIcon( ":/icons/home.svg" ), tr( "Home" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( navHome )->setObjectName( "helpHomeButton" );
|
2022-01-24 14:23:38 +00:00
|
|
|
navBack = navToolBar->addAction( QIcon( ":/icons/previous.svg" ), tr( "Back" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( navBack )->setObjectName( "helpBackButton" );
|
2022-01-24 14:23:38 +00:00
|
|
|
navForward = navToolBar->addAction( QIcon( ":/icons/next.svg" ), tr( "Forward" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( navForward )->setObjectName( "helpForwardButton" );
|
|
|
|
|
|
|
|
navToolBar->addSeparator();
|
|
|
|
|
2022-12-05 01:54:37 +00:00
|
|
|
zoomInAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoomin.png" ), tr( "Zoom In" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( zoomInAction )->setObjectName( "zoomInButton" );
|
2022-12-05 01:54:37 +00:00
|
|
|
zoomOutAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoomout.png" ), tr( "Zoom Out" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( zoomInAction )->setObjectName( "zoomOutButton" );
|
2022-12-05 01:54:37 +00:00
|
|
|
zoomBaseAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoombase.png" ), tr( "Normal Size" ) );
|
2014-06-23 15:11:15 +00:00
|
|
|
navToolBar->widgetForAction( zoomBaseAction )->setObjectName( "zoomBaseButton" );
|
|
|
|
|
|
|
|
navForward->setEnabled( false );
|
|
|
|
navBack->setEnabled( false );
|
|
|
|
|
|
|
|
mainLayout->addWidget( navToolBar );
|
|
|
|
|
2014-06-25 14:01:11 +00:00
|
|
|
QString localeName = cfg.preferences.helpLanguage;
|
|
|
|
|
|
|
|
if( localeName.isEmpty() )
|
|
|
|
localeName = cfg.preferences.interfaceLanguage;
|
|
|
|
|
2014-06-23 15:11:15 +00:00
|
|
|
if( localeName.isEmpty() )
|
|
|
|
localeName = QLocale::system().name();
|
|
|
|
|
2014-06-24 13:55:06 +00:00
|
|
|
QString helpDir = Config::getHelpDir();
|
|
|
|
helpFile = QDir::toNativeSeparators( helpDir + "/gdhelp_" + localeName + ".qch" );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
|
|
|
if( !QFileInfo( helpFile ).isFile() )
|
2014-06-24 13:55:06 +00:00
|
|
|
helpFile = QDir::toNativeSeparators( helpDir + "/gdhelp_" + localeName.left( 2 ) + ".qch" );
|
|
|
|
|
|
|
|
if( !QFileInfo( helpFile ).isFile() )
|
|
|
|
helpFile = QDir::toNativeSeparators( helpDir + "/gdhelp_en.qch" );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
|
|
|
helpCollectionFile = QDir::toNativeSeparators( Config::getConfigDir() + "gdhelp.qhc" );
|
|
|
|
|
|
|
|
helpEngine = new QHelpEngine( helpCollectionFile );
|
|
|
|
|
2022-09-20 12:06:27 +00:00
|
|
|
#if (QT_VERSION > QT_VERSION_CHECK(6,0,0))
|
|
|
|
helpEngine->setReadOnly( false );
|
|
|
|
#endif
|
|
|
|
|
2014-06-23 15:11:15 +00:00
|
|
|
if( !helpEngine->setupData() )
|
|
|
|
{
|
|
|
|
gdWarning( "Help engine initialization error: %s", helpEngine->error().toUtf8().data() );
|
|
|
|
delete helpEngine;
|
|
|
|
helpEngine = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !helpEngine->registerDocumentation( helpFile ) )
|
|
|
|
{
|
|
|
|
gdWarning( "Help engine set file error: %s", helpEngine->error().toUtf8().data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
tabWidget = new QTabWidget( this );
|
|
|
|
tabWidget->addTab( helpEngine->contentWidget(), tr( "Content" ) );
|
|
|
|
tabWidget->addTab( helpEngine->indexWidget(), tr( "Index" ) );
|
|
|
|
|
|
|
|
helpBrowser = new HelpBrowser( helpEngine, this );
|
|
|
|
|
|
|
|
helpBrowser->setOpenLinks( false );
|
|
|
|
|
|
|
|
connect( helpEngine->contentWidget(), SIGNAL( linkActivated( QUrl ) ),
|
|
|
|
helpBrowser, SLOT( setSource( QUrl ) ) );
|
|
|
|
connect( helpEngine->indexWidget(), SIGNAL( linkActivated( QUrl, QString ) ),
|
|
|
|
helpBrowser, SLOT( setSource( QUrl ) ) );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( navHome, &QAction::triggered, helpBrowser, &QTextBrowser::home );
|
|
|
|
connect( navForward, &QAction::triggered, helpBrowser, &QTextBrowser::forward );
|
|
|
|
connect( navBack, &QAction::triggered, helpBrowser, &QTextBrowser::backward );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( helpBrowser, &QTextBrowser::forwardAvailable, this, &HelpWindow::forwardEnabled );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( helpBrowser, &QTextBrowser::backwardAvailable, this, &HelpWindow::backwardEnabled );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( helpEngine->contentWidget(), &QAbstractItemView::clicked, this, &HelpWindow::contentsItemClicked );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( zoomInAction, &QAction::triggered, this, &HelpWindow::zoomIn );
|
|
|
|
connect( zoomOutAction, &QAction::triggered, this, &HelpWindow::zoomOut );
|
|
|
|
connect( zoomBaseAction, &QAction::triggered, this, &HelpWindow::zoomBase );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
|
|
|
splitter = new QSplitter( this );
|
|
|
|
splitter->addWidget( tabWidget );
|
|
|
|
splitter->addWidget( helpBrowser );
|
|
|
|
|
|
|
|
splitter->setStretchFactor( 0, 1 );
|
|
|
|
splitter->setStretchFactor( 1, 4 );
|
|
|
|
mainLayout->addWidget( splitter );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !cfg.helpWindowGeometry.isEmpty() )
|
|
|
|
restoreGeometry( cfg.helpWindowGeometry );
|
|
|
|
if( !cfg.helpSplitterState.isEmpty() )
|
|
|
|
splitter->restoreState( cfg.helpSplitterState );
|
|
|
|
|
|
|
|
QFont f = helpBrowser->font();
|
|
|
|
fontSize = f.pointSize();
|
|
|
|
if( fontSize < 10 )
|
|
|
|
{
|
|
|
|
fontSize = 10;
|
|
|
|
f.setPointSize( fontSize );
|
|
|
|
helpBrowser->setFont( f );
|
|
|
|
}
|
|
|
|
|
|
|
|
applyZoomFactor();
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpWindow::~HelpWindow()
|
|
|
|
{
|
|
|
|
if( helpEngine )
|
|
|
|
delete helpEngine;
|
|
|
|
|
|
|
|
QFile f( helpCollectionFile );
|
|
|
|
f.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::reject()
|
|
|
|
{
|
|
|
|
cfg.helpWindowGeometry = saveGeometry();
|
|
|
|
cfg.helpSplitterState = splitter->saveState();
|
|
|
|
emit needClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::accept()
|
|
|
|
{
|
|
|
|
cfg.helpWindowGeometry = saveGeometry();
|
|
|
|
cfg.helpSplitterState = splitter->saveState();
|
|
|
|
emit needClose();
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:55:06 +00:00
|
|
|
void HelpWindow::showHelpFor( QString const & keyword )
|
|
|
|
{
|
|
|
|
helpBrowser->showHelpForKeyword( keyword );
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:11:15 +00:00
|
|
|
void HelpWindow::forwardEnabled( bool enabled )
|
|
|
|
{
|
|
|
|
navForward->setEnabled( enabled );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::backwardEnabled( bool enabled )
|
|
|
|
{
|
|
|
|
navBack->setEnabled( enabled );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::contentsItemClicked( QModelIndex const & index )
|
|
|
|
{
|
|
|
|
QHelpContentItem * item = helpEngine->contentModel()->contentItemAt( index );
|
|
|
|
if( !item->url().isEmpty() )
|
|
|
|
helpBrowser->setSource( item->url() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::zoomIn()
|
|
|
|
{
|
|
|
|
cfg.preferences.helpZoomFactor += 0.2;
|
|
|
|
applyZoomFactor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::zoomOut()
|
|
|
|
{
|
|
|
|
cfg.preferences.helpZoomFactor -= 0.2;
|
|
|
|
applyZoomFactor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::zoomBase()
|
|
|
|
{
|
|
|
|
cfg.preferences.helpZoomFactor = 1;
|
|
|
|
applyZoomFactor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpWindow::applyZoomFactor()
|
|
|
|
{
|
|
|
|
if ( cfg.preferences.helpZoomFactor >= 5 )
|
|
|
|
cfg.preferences.helpZoomFactor = 5;
|
|
|
|
else if ( cfg.preferences.helpZoomFactor <= 0.2 )
|
|
|
|
cfg.preferences.helpZoomFactor = 0.2;
|
|
|
|
|
|
|
|
zoomInAction->setEnabled( cfg.preferences.helpZoomFactor < 5 );
|
|
|
|
zoomOutAction->setEnabled( cfg.preferences.helpZoomFactor > 0.2 );
|
2022-02-18 16:16:11 +00:00
|
|
|
zoomBaseAction->setEnabled( !qFuzzyCompare(cfg.preferences.helpZoomFactor, 1.0) );
|
2014-06-23 15:11:15 +00:00
|
|
|
|
|
|
|
if( fontSize > 0 )
|
|
|
|
{
|
|
|
|
QFont f = helpBrowser->font();
|
|
|
|
f.setPointSize( fontSize * cfg.preferences.helpZoomFactor );
|
|
|
|
helpBrowser->setFont( f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Help
|