Skip to content

Commit

Permalink
Add help system
Browse files Browse the repository at this point in the history
  • Loading branch information
Abs62 committed Jun 23, 2014
1 parent ec0db9b commit d4c68d3
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 7 deletions.
26 changes: 26 additions & 0 deletions config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Preferences::Preferences():
enableWebPlugins( false ),
hideGoldenDictHeader( false ),
zoomFactor( 1 ),
helpZoomFactor( 1 ),
wordsZoomLevel( 0 ),
maxStringsInHistory( 500 ),
storeHistory( 1 ),
Expand Down Expand Up @@ -707,6 +708,9 @@ Class load() throw( exError )
if ( !preferences.namedItem( "zoomFactor" ).isNull() )
c.preferences.zoomFactor = preferences.namedItem( "zoomFactor" ).toElement().text().toDouble();

if ( !preferences.namedItem( "helpZoomFactor" ).isNull() )
c.preferences.helpZoomFactor = preferences.namedItem( "helpZoomFactor" ).toElement().text().toDouble();

if ( !preferences.namedItem( "wordsZoomLevel" ).isNull() )
c.preferences.wordsZoomLevel = preferences.namedItem( "wordsZoomLevel" ).toElement().text().toInt();

Expand Down Expand Up @@ -866,6 +870,16 @@ Class load() throw( exError )
if ( !mainWindowGeometry.isNull() )
c.mainWindowGeometry = QByteArray::fromBase64( mainWindowGeometry.toElement().text().toLatin1() );

QDomNode helpWindowGeometry = root.namedItem( "helpWindowGeometry" );

if ( !helpWindowGeometry.isNull() )
c.helpWindowGeometry = QByteArray::fromBase64( helpWindowGeometry.toElement().text().toLatin1() );

QDomNode helpSplitterState = root.namedItem( "helpSplitterState" );

if ( !helpSplitterState.isNull() )
c.helpSplitterState = QByteArray::fromBase64( helpSplitterState.toElement().text().toLatin1() );

#ifdef Q_OS_WIN
QDomNode maximizedMainWindowGeometry = root.namedItem( "maximizedMainWindowGeometry" );

Expand Down Expand Up @@ -1480,6 +1494,10 @@ void save( Class const & c ) throw( exError )
opt.appendChild( dd.createTextNode( QString::number( c.preferences.zoomFactor ) ) );
preferences.appendChild( opt );

opt = dd.createElement( "helpZoomFactor" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.helpZoomFactor ) ) );
preferences.appendChild( opt );

opt = dd.createElement( "wordsZoomLevel" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.wordsZoomLevel ) ) );
preferences.appendChild( opt );
Expand Down Expand Up @@ -1738,6 +1756,14 @@ void save( Class const & c ) throw( exError )
opt.appendChild( dd.createTextNode( QString::fromLatin1( c.mainWindowGeometry.toBase64() ) ) );
root.appendChild( opt );

opt = dd.createElement( "helpWindowGeometry" );
opt.appendChild( dd.createTextNode( QString::fromLatin1( c.helpWindowGeometry.toBase64() ) ) );
root.appendChild( opt );

opt = dd.createElement( "helpSplitterState" );
opt.appendChild( dd.createTextNode( QString::fromLatin1( c.helpSplitterState.toBase64() ) ) );
root.appendChild( opt );

#ifdef Q_OS_WIN
{
QDomElement maximizedMainWindowGeometry = dd.createElement( "maximizedMainWindowGeometry" );
Expand Down
3 changes: 3 additions & 0 deletions config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ struct Preferences
bool hideGoldenDictHeader;

qreal zoomFactor;
qreal helpZoomFactor;
int wordsZoomLevel;

unsigned maxStringsInHistory;
Expand Down Expand Up @@ -520,6 +521,8 @@ struct Class
QByteArray popupWindowGeometry; // Geometry saved by QMainWindow
QByteArray dictInfoGeometry; // Geometry of "Dictionary info" window
QByteArray inspectorGeometry; // Geometry of WebKit inspector window
QByteArray helpWindowGeometry; // Geometry of help window
QByteArray helpSplitterState; // Geometry of help splitter

QString historyExportPath; // Path for export/import history
QString resourceSavePath; // Path to save images/audio
Expand Down
17 changes: 13 additions & 4 deletions goldendict.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ QT += webkit
QT += xml
QT += network
QT += svg
QT += sql
CONFIG += exceptions \
rtti \
stl
stl \
help
OBJECTS_DIR = build
UI_DIR = build
MOC_DIR = build
Expand Down Expand Up @@ -139,6 +141,9 @@ unix:!mac {
desktops.path = $$PREFIX/share/applications
desktops.files = redist/*.desktop
INSTALLS += desktops
helps.path = $$PREFIX/share/goldendict/help/
helps.files = help/*.qch
INSTALLS += helps
}
mac {
TARGET = GoldenDict
Expand Down Expand Up @@ -171,7 +176,9 @@ mac {
QMAKE_POST_LINK = mkdir -p GoldenDict.app/Contents/Frameworks & \
cp -nR $${PWD}/maclibs/lib/ GoldenDict.app/Contents/Frameworks/ & \
mkdir -p GoldenDict.app/Contents/MacOS/locale & \
cp -R locale/*.qm GoldenDict.app/Contents/MacOS/locale/
cp -R locale/*.qm GoldenDict.app/Contents/MacOS/locale/ & \
mkdir -p GoldenDict.app/Contents/MacOS/help & \
cp -R help/*.qch GoldenDict.app/Contents/MacOS/help/

CONFIG += zim_support
}
Expand Down Expand Up @@ -285,7 +292,8 @@ HEADERS += folding.hh \
dictheadwords.hh \
fulltextsearch.hh \
ftshelpers.hh \
dictserver.hh
dictserver.hh \
helpwindow.hh

FORMS += groups.ui \
dictgroupwidget.ui \
Expand Down Expand Up @@ -405,7 +413,8 @@ SOURCES += folding.cc \
dictheadwords.cc \
fulltextsearch.cc \
ftshelpers.cc \
dictserver.cc
dictserver.cc \
helpwindow.cc

win32 {
FORMS += texttospeechsource.ui
Expand Down
Binary file added help/gdhelp_en.qch
Binary file not shown.
Binary file added help/gdhelp_ru.qch
Binary file not shown.
252 changes: 252 additions & 0 deletions helpwindow.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/* 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"

namespace Help {

HelpBrowser::HelpBrowser( QHelpEngineCore * engine, QWidget *parent ) :
QTextBrowser( parent ),
helpEngine( engine )
{
connect( this, SIGNAL( anchorClicked( QUrl ) ), this, SLOT( linkClicked( QUrl ) ) );
}

void HelpBrowser::showHelpForKeyword( QString const & id )
{
if ( helpEngine )
{
QMap< QString, QUrl > links = helpEngine->linksForIdentifier( id );
if( !links.isEmpty() )
setSource( links.constBegin().value() );
}
}

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 );
navHome = navToolBar->addAction( QIcon( ":/icons/home.png" ), tr( "Home" ) );
navToolBar->widgetForAction( navHome )->setObjectName( "helpHomeButton" );
navBack = navToolBar->addAction( QIcon( ":/icons/previous.png" ), tr( "Back" ) );
navToolBar->widgetForAction( navBack )->setObjectName( "helpBackButton" );
navForward = navToolBar->addAction( QIcon( ":/icons/next.png" ), tr( "Forward" ) );
navToolBar->widgetForAction( navForward )->setObjectName( "helpForwardButton" );

navToolBar->addSeparator();

zoomInAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoomin" ), tr( "Zoom In" ) );
navToolBar->widgetForAction( zoomInAction )->setObjectName( "zoomInButton" );
zoomOutAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoomout" ), tr( "Zoom Out" ) );
navToolBar->widgetForAction( zoomInAction )->setObjectName( "zoomOutButton" );
zoomBaseAction = navToolBar->addAction( QIcon( ":/icons/icon32_zoombase" ), tr( "Normal Size" ) );
navToolBar->widgetForAction( zoomBaseAction )->setObjectName( "zoomBaseButton" );

navForward->setEnabled( false );
navBack->setEnabled( false );

mainLayout->addWidget( navToolBar );

QString localeName = cfg.preferences.interfaceLanguage;
if( localeName.isEmpty() )
localeName = QLocale::system().name();

helpFile = QDir::toNativeSeparators( Config::getProgramDataDir() + "/help/gdhelp_"
+ localeName.left( 2 ) + ".qch" );

if( !QFileInfo( helpFile ).isFile() )
helpFile = QDir::toNativeSeparators( Config::getProgramDataDir() + "/help/gdhelp_en.qch" );

helpCollectionFile = QDir::toNativeSeparators( Config::getConfigDir() + "gdhelp.qhc" );

helpEngine = new QHelpEngine( helpCollectionFile );

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 ) ) );

connect( navHome, SIGNAL( triggered() ), helpBrowser, SLOT( home() ) );
connect( navForward, SIGNAL( triggered() ), helpBrowser, SLOT( forward() ) );
connect( navBack, SIGNAL( triggered() ), helpBrowser, SLOT( backward() ) );

connect( helpBrowser, SIGNAL( forwardAvailable( bool ) ),
this, SLOT( forwardEnabled( bool ) ) );

connect( helpBrowser, SIGNAL( backwardAvailable( bool ) ),
this, SLOT( backwardEnabled( bool ) ) );

connect( helpEngine->contentWidget(), SIGNAL( clicked( QModelIndex ) ),
this, SLOT( contentsItemClicked( QModelIndex ) ) );

connect( zoomInAction, SIGNAL( triggered( ) ), this, SLOT( zoomIn() ) );
connect( zoomOutAction, SIGNAL( triggered( ) ), this, SLOT( zoomOut() ) );
connect( zoomBaseAction, SIGNAL( triggered( ) ), this, SLOT( zoomBase() ) );

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();
}

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 );
zoomBaseAction->setEnabled( cfg.preferences.helpZoomFactor != 1.0 );

if( fontSize > 0 )
{
QFont f = helpBrowser->font();
f.setPointSize( fontSize * cfg.preferences.helpZoomFactor );
helpBrowser->setFont( f );
}
}

} // namespace Help
Loading

0 comments on commit d4c68d3

Please sign in to comment.