forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpwindow.cc
264 lines (211 loc) · 7.53 KB
/
helpwindow.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* 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.helpLanguage;
if( localeName.isEmpty() )
localeName = cfg.preferences.interfaceLanguage;
if( localeName.isEmpty() )
localeName = QLocale::system().name();
QString helpDir = Config::getHelpDir();
helpFile = QDir::toNativeSeparators( helpDir + "/gdhelp_" + localeName + ".qch" );
if( !QFileInfo( helpFile ).isFile() )
helpFile = QDir::toNativeSeparators( helpDir + "/gdhelp_" + localeName.left( 2 ) + ".qch" );
if( !QFileInfo( helpFile ).isFile() )
helpFile = QDir::toNativeSeparators( helpDir + "/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::showHelpFor( QString const & keyword )
{
helpBrowser->showHelpForKeyword( keyword );
}
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