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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "helpplugin.h"
#include "centralwidget.h"
#include "docsettingspage.h"
#include "filtersettingspage.h"
#include "generalsettingspage.h"
#include "helpconstants.h"
#include "helpfindsupport.h"
#include "helpindexfilter.h"
#include "helpmode.h"
#include "helpviewer.h"
#include "localhelpmanager.h"
#include "openpagesmanager.h"
#include "openpagesmodel.h"
#include "qtwebkithelpviewer.h"
#include "remotehelpfilter.h"
#include "searchwidget.h"
#include "searchtaskhandler.h"
#include "textbrowserhelpviewer.h"
#ifdef QTC_MAC_NATIVE_HELPVIEWER
#include "macwebkithelpviewer.h"
#endif
#include <bookmarkmanager.h>
#include <contentwindow.h>
#include <indexwindow.h>
#include <app/app_version.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/id.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/findplaceholder.h>
#include <coreplugin/icore.h>
#include <coreplugin/minisplitter.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/rightpane.h>
#include <coreplugin/sidebar.h>
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/find/findplugin.h>
#include <texteditor/texteditorconstants.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <utils/styledbar.h>
#include <QDir>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QTimer>
#include <QTranslator>
#include <qplugin.h>
#include <QRegExp>
#include <QAction>
#include <QComboBox>
#include <QDesktopServices>
#include <QMenu>
#include <QStackedLayout>
#include <QSplitter>
#include <QHelpEngine>
using namespace Help::Internal;
static const char kExternalWindowStateKey[] = "Help/ExternalWindowState";
static const char kHelpModeSideBarKey[] = "Help/ModeSideBar";
#define IMAGEPATH ":/help/images/"
using namespace Core;
HelpPlugin::HelpPlugin()
: m_mode(0),
m_centralWidget(0),
m_rightPaneSideBarWidget(0),
m_sideBar(0),
m_setupNeeded(true),
m_helpManager(0),
m_openPagesManager(0)
{
}
HelpPlugin::~HelpPlugin()
{
delete m_centralWidget;
delete m_openPagesManager;
delete m_rightPaneSideBarWidget;
delete m_helpManager;
}
bool HelpPlugin::initialize(const QStringList &arguments, QString *error)
{
Q_UNUSED(arguments)
Q_UNUSED(error)
Context globalcontext(Core::Constants::C_GLOBAL);
Context modecontext(Constants::C_MODE_HELP);
const QString &locale = ICore::userInterfaceLanguage();
if (!locale.isEmpty()) {
QTranslator *qtr = new QTranslator(this);
QTranslator *qhelptr = new QTranslator(this);
const QString &creatorTrPath = ICore::resourcePath()
+ QLatin1String("/translations");
const QString &qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
const QString &trFile = QLatin1String("assistant_") + locale;
const QString &helpTrFile = QLatin1String("qt_help_") + locale;
if (qtr->load(trFile, qtTrPath) || qtr->load(trFile, creatorTrPath))
qApp->installTranslator(qtr);
if (qhelptr->load(helpTrFile, qtTrPath) || qhelptr->load(helpTrFile, creatorTrPath))
qApp->installTranslator(qhelptr);
}
m_helpManager = new LocalHelpManager(this);
m_openPagesManager = new OpenPagesManager(this);
addAutoReleasedObject(m_docSettingsPage = new DocSettingsPage());
addAutoReleasedObject(m_filterSettingsPage = new FilterSettingsPage());
addAutoReleasedObject(m_generalSettingsPage = new GeneralSettingsPage());
addAutoReleasedObject(m_searchTaskHandler = new SearchTaskHandler);
m_centralWidget = new Help::Internal::CentralWidget(modecontext);
connect(m_centralWidget, SIGNAL(sourceChanged(QUrl)), this,
SLOT(updateSideBarSource(QUrl)));
connect(m_centralWidget, &CentralWidget::closeButtonClicked,
&OpenPagesManager::instance(), &OpenPagesManager::closeCurrentPage);
connect(m_generalSettingsPage, SIGNAL(fontChanged()), this,
SLOT(fontChanged()));
connect(m_generalSettingsPage, SIGNAL(returnOnCloseChanged()), m_centralWidget,
SLOT(updateCloseButton()));
connect(HelpManager::instance(), SIGNAL(helpRequested(QUrl,Core::HelpManager::HelpViewerLocation)),
this, SLOT(handleHelpRequest(QUrl,Core::HelpManager::HelpViewerLocation)));
connect(m_searchTaskHandler, SIGNAL(search(QUrl)), this,
SLOT(switchToHelpMode(QUrl)));
connect(m_filterSettingsPage, SIGNAL(filtersChanged()), this,
SLOT(setupHelpEngineIfNeeded()));
connect(HelpManager::instance(), SIGNAL(documentationChanged()), this,
SLOT(setupHelpEngineIfNeeded()));
connect(HelpManager::instance(), SIGNAL(collectionFileChanged()), this,
SLOT(setupHelpEngineIfNeeded()));
connect(HelpManager::instance(), SIGNAL(setupFinished()), this,
SLOT(unregisterOldQtCreatorDocumentation()));
m_splitter = new MiniSplitter;
Command *cmd;
QAction *action;
// Add Contents, Index, and Context menu items
action = new QAction(QIcon::fromTheme(QLatin1String("help-contents")),
tr(Constants::SB_CONTENTS), this);
cmd = ActionManager::registerAction(action, Constants::HELP_CONTENTS, globalcontext);
ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_HELP);
connect(action, SIGNAL(triggered()), this, SLOT(activateContents()));
action = new QAction(tr(Constants::SB_INDEX), this);
cmd = ActionManager::registerAction(action, "Help.Index", globalcontext);
ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_HELP);
connect(action, SIGNAL(triggered()), this, SLOT(activateIndex()));
action = new QAction(tr("Context Help"), this);
cmd = ActionManager::registerAction(action, Help::Constants::CONTEXT_HELP, globalcontext);
ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_HELP);
cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F1));
connect(action, SIGNAL(triggered()), this, SLOT(showContextHelp()));
action = new QAction(tr("Technical Support"), this);
cmd = ActionManager::registerAction(action, "Help.TechSupport", globalcontext);
ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_SUPPORT);
connect(action, SIGNAL(triggered()), this, SLOT(slotOpenSupportPage()));
action = new QAction(tr("Report Bug..."), this);
cmd = ActionManager::registerAction(action, "Help.ReportBug", globalcontext);
ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_SUPPORT);
connect(action, SIGNAL(triggered()), this, SLOT(slotReportBug()));
if (ActionContainer *windowMenu = ActionManager::actionContainer(Core::Constants::M_WINDOW)) {
// reuse EditorManager constants to avoid a second pair of menu actions
// Goto Previous In History Action
action = new QAction(this);
Command *ctrlTab = ActionManager::registerAction(action, Core::Constants::GOTOPREVINHISTORY,
modecontext);
windowMenu->addAction(ctrlTab, Core::Constants::G_WINDOW_NAVIGATE);
connect(action, SIGNAL(triggered()), &OpenPagesManager::instance(),
SLOT(gotoPreviousPage()));
// Goto Next In History Action
action = new QAction(this);
Command *ctrlShiftTab = ActionManager::registerAction(action, Core::Constants::GOTONEXTINHISTORY,
modecontext);
windowMenu->addAction(ctrlShiftTab, Core::Constants::G_WINDOW_NAVIGATE);
connect(action, SIGNAL(triggered()), &OpenPagesManager::instance(),
SLOT(gotoNextPage()));
}
m_splitter->addWidget(m_centralWidget);
HelpIndexFilter *helpIndexFilter = new HelpIndexFilter();
addAutoReleasedObject(helpIndexFilter);
connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this,
SLOT(switchToHelpMode(QUrl)));
RemoteHelpFilter *remoteHelpFilter = new RemoteHelpFilter();
addAutoReleasedObject(remoteHelpFilter);
connect(remoteHelpFilter, SIGNAL(linkActivated(QUrl)), this,
SLOT(switchToHelpMode(QUrl)));
QDesktopServices::setUrlHandler(QLatin1String("qthelp"), this, "handleHelpRequest");
connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*,Core::IMode*)),
this, SLOT(modeChanged(Core::IMode*,Core::IMode*)));
m_mode = new HelpMode;
m_mode->setWidget(m_splitter);
addAutoReleasedObject(m_mode);
return true;
}
void HelpPlugin::extensionsInitialized()
{
QStringList filesToRegister;
// we might need to register creators inbuild help
filesToRegister.append(ICore::documentationPath() + QLatin1String("/qtcreator.qch"));
HelpManager::registerDocumentation(filesToRegister);
}
ExtensionSystem::IPlugin::ShutdownFlag HelpPlugin::aboutToShutdown()
{
if (m_sideBar)
m_sideBar->saveSettings(ICore::settings(), QLatin1String(kHelpModeSideBarKey));
return SynchronousShutdown;
}
void HelpPlugin::unregisterOldQtCreatorDocumentation()
{
const QString &nsInternal = QString::fromLatin1("org.qt-project.qtcreator.%1%2%3")
.arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR).arg(IDE_VERSION_RELEASE);
QStringList documentationToUnregister;
foreach (const QString &ns, HelpManager::registeredNamespaces()) {
if (ns.startsWith(QLatin1String("org.qt-project.qtcreator."))
&& ns != nsInternal) {
documentationToUnregister << ns;
}
}
if (!documentationToUnregister.isEmpty())
HelpManager::unregisterDocumentation(documentationToUnregister);
}
void HelpPlugin::setupUi()
{
// side bar widgets and shortcuts
Context modecontext(Constants::C_MODE_HELP);
IndexWindow *indexWindow = new IndexWindow();
indexWindow->setWindowTitle(tr(Constants::SB_INDEX));
auto indexItem = new SideBarItem(indexWindow, QLatin1String(Constants::HELP_INDEX));
connect(indexWindow, &IndexWindow::linkActivated,
m_centralWidget, &HelpWidget::open);
connect(indexWindow, &IndexWindow::linksActivated,
m_centralWidget, &HelpWidget::showTopicChooser);
QMap<QString, Command*> shortcutMap;
QAction *action = new QAction(tr("Activate Help Index View"), m_splitter);
Command *cmd = ActionManager::registerAction(action, Constants::HELP_INDEX, modecontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+I") : tr("Ctrl+Shift+I")));
connect(action, SIGNAL(triggered()), this, SLOT(activateIndex()));
shortcutMap.insert(QLatin1String(Constants::HELP_INDEX), cmd);
ContentWindow *contentWindow = new ContentWindow();
contentWindow->setWindowTitle(tr(Constants::SB_CONTENTS));
auto contentItem = new SideBarItem(contentWindow, QLatin1String(Constants::HELP_CONTENTS));
connect(contentWindow, &ContentWindow::linkActivated, m_centralWidget, &HelpWidget::open);
action = new QAction(tr("Activate Help Contents View"), m_splitter);
cmd = ActionManager::registerAction(action, Constants::HELP_CONTENTS, modecontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+C") : tr("Ctrl+Shift+C")));
connect(action, SIGNAL(triggered()), this, SLOT(activateContents()));
shortcutMap.insert(QLatin1String(Constants::HELP_CONTENTS), cmd);
auto searchItem = new SearchSideBarItem;
connect(searchItem, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSourceFromSearch(QUrl)));
action = new QAction(tr("Activate Help Search View"), m_splitter);
cmd = ActionManager::registerAction(action, Constants::HELP_SEARCH, modecontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+/") : tr("Ctrl+Shift+/")));
connect(action, SIGNAL(triggered()), this, SLOT(activateSearch()));
shortcutMap.insert(searchItem->id(), cmd);
BookmarkManager *manager = &LocalHelpManager::bookmarkManager();
BookmarkWidget *bookmarkWidget = new BookmarkWidget(manager);
bookmarkWidget->setWindowTitle(tr(Constants::SB_BOOKMARKS));
auto bookmarkItem = new SideBarItem(bookmarkWidget, QLatin1String(Constants::HELP_BOOKMARKS));
connect(bookmarkWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
SLOT(setSource(QUrl)));
connect(bookmarkWidget, SIGNAL(createPage(QUrl,bool)), &OpenPagesManager::instance(),
SLOT(createPage(QUrl,bool)));
action = new QAction(tr("Activate Help Bookmarks View"), m_splitter);
cmd = ActionManager::registerAction(action, Constants::HELP_BOOKMARKS, modecontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+B") : tr("Ctrl+Shift+B")));
connect(action, SIGNAL(triggered()), this, SLOT(activateBookmarks()));
shortcutMap.insert(QLatin1String(Constants::HELP_BOOKMARKS), cmd);
QWidget *openPagesWidget = OpenPagesManager::instance().openPagesWidget();
openPagesWidget->setWindowTitle(tr(Constants::SB_OPENPAGES));
auto openPagesItem = new SideBarItem(openPagesWidget, QLatin1String(Constants::HELP_OPENPAGES));
action = new QAction(tr("Activate Open Help Pages View"), m_splitter);
cmd = ActionManager::registerAction(action, Constants::HELP_OPENPAGES, modecontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+O") : tr("Ctrl+Shift+O")));
connect(action, SIGNAL(triggered()), this, SLOT(activateOpenPages()));
shortcutMap.insert(QLatin1String(Constants::HELP_OPENPAGES), cmd);
QList<SideBarItem*> itemList;
itemList << contentItem << indexItem << searchItem << bookmarkItem
<< openPagesItem;
m_sideBar = new SideBar(itemList, QList<SideBarItem*>()
<< contentItem << openPagesItem);
m_sideBar->setCloseWhenEmpty(true);
m_sideBar->setShortcutMap(shortcutMap);
connect(m_sideBar, SIGNAL(sideBarClosed()), this, SLOT(onSideBarVisibilityChanged()));
m_splitter->setOpaqueResize(false);
m_splitter->insertWidget(0, m_sideBar);
m_splitter->setStretchFactor(0, 0);
m_splitter->setStretchFactor(1, 1);
m_sideBar->readSettings(ICore::settings(), QLatin1String(kHelpModeSideBarKey));
m_splitter->setSizes(QList<int>() << m_sideBar->size().width() << 300);
m_toggleSideBarAction = new QAction(QIcon(QLatin1String(Core::Constants::ICON_TOGGLE_SIDEBAR)),
tr(Core::Constants::TR_SHOW_SIDEBAR), this);
m_toggleSideBarAction->setCheckable(true);
m_toggleSideBarAction->setChecked(m_sideBar->isVisibleTo(m_splitter));
connect(m_toggleSideBarAction, SIGNAL(triggered(bool)), this, SLOT(setSideBarVisible(bool)));
cmd = ActionManager::registerAction(m_toggleSideBarAction, Core::Constants::TOGGLE_SIDEBAR, modecontext);
}
void HelpPlugin::resetFilter()
{
const QString &filterInternal = QString::fromLatin1("Qt Creator %1.%2.%3")
.arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR).arg(IDE_VERSION_RELEASE);
QRegExp filterRegExp(QLatin1String("Qt Creator \\d*\\.\\d*\\.\\d*"));
QHelpEngineCore *engine = &LocalHelpManager::helpEngine();
const QStringList &filters = engine->customFilters();
foreach (const QString &filter, filters) {
if (filterRegExp.exactMatch(filter) && filter != filterInternal)
engine->removeCustomFilter(filter);
}
// we added a filter at some point, remove previously added filter
if (engine->customValue(Help::Constants::WeAddedFilterKey).toInt() == 1) {
const QString &filter =
engine->customValue(Help::Constants::PreviousFilterNameKey).toString();
if (!filter.isEmpty())
engine->removeCustomFilter(filter);
}
// potentially remove a filter with new name
const QString filterName = tr("Unfiltered");
engine->removeCustomFilter(filterName);
engine->addCustomFilter(filterName, QStringList());
engine->setCustomValue(Help::Constants::WeAddedFilterKey, 1);
engine->setCustomValue(Help::Constants::PreviousFilterNameKey, filterName);
engine->setCurrentFilter(filterName);
LocalHelpManager::updateFilterModel();
connect(engine, &QHelpEngineCore::setupFinished,
LocalHelpManager::instance(), &LocalHelpManager::updateFilterModel);
}
void HelpPlugin::saveExternalWindowSettings()
{
if (!m_externalWindow)
return;
m_externalWindowState = m_externalWindow->geometry();
QSettings *settings = Core::ICore::settings();
settings->setValue(QLatin1String(kExternalWindowStateKey),
qVariantFromValue(m_externalWindowState));
}
HelpWidget *HelpPlugin::createHelpWidget(const Context &context, HelpWidget::WidgetStyle style)
{
HelpWidget *widget = new HelpWidget(context, style);
connect(widget->currentViewer(), SIGNAL(loadFinished()),
this, SLOT(highlightSearchTermsInContextHelp()));
connect(widget, SIGNAL(openHelpMode(QUrl)),
this, SLOT(switchToHelpMode(QUrl)));
connect(widget, SIGNAL(closeButtonClicked()),
this, SLOT(slotHideRightPane()));
connect(widget, SIGNAL(aboutToClose()),
this, SLOT(saveExternalWindowSettings()));
// force setup, as we might have never switched to full help mode
// thus the help engine might still run without collection file setup
m_helpManager->setupGuiHelpEngine();
return widget;
}
void HelpPlugin::createRightPaneContextViewer()
{
if (m_rightPaneSideBarWidget)
return;
m_rightPaneSideBarWidget = createHelpWidget(Core::Context(Constants::C_HELP_SIDEBAR),
HelpWidget::SideBarWidget);
}
HelpViewer *HelpPlugin::externalHelpViewer()
{
if (m_externalWindow)
return m_externalWindow->currentViewer();
doSetupIfNeeded();
m_externalWindow = createHelpWidget(Core::Context(Constants::C_HELP_EXTERNAL),
HelpWidget::ExternalWindow);
if (m_externalWindowState.isNull()) {
QSettings *settings = Core::ICore::settings();
m_externalWindowState = settings->value(QLatin1String(kExternalWindowStateKey)).toRect();
}
if (!m_externalWindowState.isNull())
m_externalWindow->setGeometry(m_externalWindowState);
m_externalWindow->show();
m_externalWindow->setFocus();
return m_externalWindow->currentViewer();
}
HelpViewer *HelpPlugin::createHelpViewer(qreal zoom)
{
HelpViewer *viewer = 0;
const QString backend = QLatin1String(qgetenv("QTC_HELPVIEWER_BACKEND"));
if (backend.compare(QLatin1String("native"), Qt::CaseInsensitive) == 0) {
#ifdef QTC_MAC_NATIVE_HELPVIEWER
viewer = new MacWebKitHelpViewer(zoom);
#else
qWarning() << "native help viewer is requested, but was not enabled during compilation";
viewer = new TextBrowserHelpViewer(zoom);
#endif
} else if (backend.compare(QLatin1String("textbrowser"), Qt::CaseInsensitive) == 0) {
viewer = new TextBrowserHelpViewer(zoom);
} else {
#ifndef QT_NO_WEBKIT
viewer = new QtWebKitHelpViewer(zoom);
#else
viewer = new TextBrowserHelpViewer(zoom);
#endif
}
// initialize font
QVariant fontSetting = LocalHelpManager::engineFontSettings();
if (fontSetting.isValid())
viewer->setViewerFont(fontSetting.value<QFont>());
// add find support
Aggregation::Aggregate *agg = new Aggregation::Aggregate();
agg->add(viewer);
agg->add(new HelpViewerFindSupport(viewer));
return viewer;
}
void HelpPlugin::activateHelpMode()
{
ModeManager::activateMode(Id(Constants::ID_MODE_HELP));
}
void HelpPlugin::switchToHelpMode(const QUrl &source)
{
activateHelpMode();
Core::ICore::raiseWindow(m_mode->widget());
m_centralWidget->setSource(source);
m_centralWidget->setFocus();
}
void HelpPlugin::slotHideRightPane()
{
RightPaneWidget::instance()->setShown(false);
}
void HelpPlugin::setSideBarVisible(bool visible)
{
if (visible == m_sideBar->isVisible())
return;
m_sideBar->setVisible(visible);
onSideBarVisibilityChanged();
}
void HelpPlugin::modeChanged(IMode *mode, IMode *old)
{
Q_UNUSED(old)
if (mode == m_mode) {
qApp->setOverrideCursor(Qt::WaitCursor);
doSetupIfNeeded();
qApp->restoreOverrideCursor();
}
}
void HelpPlugin::updateSideBarSource()
{
if (HelpViewer *viewer = m_centralWidget->currentViewer()) {
const QUrl &url = viewer->source();
if (url.isValid())
updateSideBarSource(url);
}
}
void HelpPlugin::updateSideBarSource(const QUrl &newUrl)
{
if (m_rightPaneSideBarWidget)
m_rightPaneSideBarWidget->currentViewer()->setSource(newUrl);
}
void HelpPlugin::fontChanged()
{
if (!m_rightPaneSideBarWidget)
createRightPaneContextViewer();
QVariant fontSetting = LocalHelpManager::engineFontSettings();
QFont font = fontSetting.isValid() ? fontSetting.value<QFont>()
: m_rightPaneSideBarWidget->currentViewer()->viewerFont();
m_rightPaneSideBarWidget->setViewerFont(font);
CentralWidget::instance()->setViewerFont(font);
}
void HelpPlugin::setupHelpEngineIfNeeded()
{
m_helpManager->setEngineNeedsUpdate();
if (ModeManager::currentMode() == m_mode
|| contextHelpOption() == Core::HelpManager::ExternalHelpAlways)
m_helpManager->setupGuiHelpEngine();
}
bool HelpPlugin::canShowHelpSideBySide() const
{
RightPanePlaceHolder *placeHolder = RightPanePlaceHolder::current();
if (!placeHolder)
return false;
if (placeHolder->isVisible())
return true;
IEditor *editor = EditorManager::currentEditor();
if (!editor)
return true;
QTC_ASSERT(editor->widget(), return true);
if (!editor->widget()->isVisible())
return true;
if (editor->widget()->width() < 800)
return false;
return true;
}
HelpViewer *HelpPlugin::viewerForHelpViewerLocation(Core::HelpManager::HelpViewerLocation location)
{
Core::HelpManager::HelpViewerLocation actualLocation = location;
if (location == Core::HelpManager::SideBySideIfPossible)
actualLocation = canShowHelpSideBySide() ? Core::HelpManager::SideBySideAlways
: Core::HelpManager::HelpModeAlways;
if (actualLocation == Core::HelpManager::ExternalHelpAlways)
return externalHelpViewer();
if (actualLocation == Core::HelpManager::SideBySideAlways) {
createRightPaneContextViewer();
RightPaneWidget::instance()->setWidget(m_rightPaneSideBarWidget);
RightPaneWidget::instance()->setShown(true);
return m_rightPaneSideBarWidget->currentViewer();
}
QTC_CHECK(actualLocation == Core::HelpManager::HelpModeAlways);
activateHelpMode(); // should trigger an createPage...
HelpViewer *viewer = m_centralWidget->currentViewer();
if (!viewer)
viewer = OpenPagesManager::instance().createPage();
return viewer;
}
HelpViewer *HelpPlugin::viewerForContextHelp()
{
return viewerForHelpViewerLocation(contextHelpOption());
}
static QUrl findBestLink(const QMap<QString, QUrl> &links, QString *highlightId)
{
if (highlightId)
highlightId->clear();
if (links.isEmpty())
return QUrl();
QUrl source = links.constBegin().value();
// workaround to show the latest Qt version
int version = 0;
QRegExp exp(QLatin1String("(\\d+)"));
foreach (const QUrl &link, links) {
const QString &authority = link.authority();
if (authority.startsWith(QLatin1String("com.trolltech."))
|| authority.startsWith(QLatin1String("org.qt-project."))) {
if (exp.indexIn(authority) >= 0) {
const int tmpVersion = exp.cap(1).toInt();
if (tmpVersion > version) {
source = link;
version = tmpVersion;
if (highlightId)
*highlightId = source.fragment();
}
}
}
}
return source;
}
void HelpPlugin::showContextHelp()
{
if (ModeManager::currentMode() == m_mode)
return;
// Find out what to show
QMap<QString, QUrl> links;
QString idFromContext;
if (IContext *context = Core::ICore::currentContextObject()) {
idFromContext = context->contextHelpId();
links = HelpManager::linksForIdentifier(idFromContext);
// Maybe the id is already an URL
if (links.isEmpty() && LocalHelpManager::isValidUrl(idFromContext))
links.insert(idFromContext, idFromContext);
}
if (HelpViewer *viewer = viewerForContextHelp()) {
QUrl source = findBestLink(links, &m_contextHelpHighlightId);
if (!source.isValid()) {
// No link found or no context object
viewer->setSource(QUrl(Help::Constants::AboutBlank));
viewer->setHtml(tr("<html><head><title>No Documentation</title>"
"</head><body><br/><center><b>%1</b><br/>No documentation "
"available.</center></body></html>").arg(idFromContext));
} else {
const QUrl &oldSource = viewer->source();
if (source != oldSource) {
viewer->stop();
viewer->setSource(source); // triggers loadFinished which triggers id highlighting
} else {
viewer->scrollToAnchor(source.fragment());
}
viewer->setFocus();
Core::ICore::raiseWindow(viewer);
}
}
}
void HelpPlugin::activateIndex()
{
activateHelpMode();
m_sideBar->activateItem(QLatin1String(Constants::HELP_INDEX));
}
void HelpPlugin::activateContents()
{
activateHelpMode();
m_sideBar->activateItem(QLatin1String(Constants::HELP_CONTENTS));
}
void HelpPlugin::activateSearch()
{
activateHelpMode();
m_sideBar->activateItem(QLatin1String(Constants::HELP_SEARCH));
}
void HelpPlugin::activateOpenPages()
{
activateHelpMode();
m_sideBar->activateItem(QLatin1String(Constants::HELP_OPENPAGES));
}
void HelpPlugin::activateBookmarks()
{
activateHelpMode();
m_sideBar->activateItem(QLatin1String(Constants::HELP_BOOKMARKS));
}
void HelpPlugin::highlightSearchTermsInContextHelp()
{
if (m_contextHelpHighlightId.isEmpty())
return;
HelpViewer *viewer = viewerForContextHelp();
QTC_ASSERT(viewer, return);
viewer->highlightId(m_contextHelpHighlightId);
m_contextHelpHighlightId.clear();
}
void HelpPlugin::handleHelpRequest(const QUrl &url, Core::HelpManager::HelpViewerLocation location)
{
if (HelpViewer::launchWithExternalApp(url))
return;
QString address = url.toString();
if (!HelpManager::findFile(url).isValid()) {
if (address.startsWith(QLatin1String("qthelp://org.qt-project."))
|| address.startsWith(QLatin1String("qthelp://com.nokia."))
|| address.startsWith(QLatin1String("qthelp://com.trolltech."))) {
// local help not installed, resort to external web help
QString urlPrefix = QLatin1String("http://qt-project.org/doc/");
if (url.authority() == QLatin1String("org.qt-project.qtcreator"))
urlPrefix.append(QString::fromLatin1("qtcreator"));
else
urlPrefix.append(QLatin1String("latest"));
address = urlPrefix + address.mid(address.lastIndexOf(QLatin1Char('/')));
}
}
const QUrl newUrl(address);
HelpViewer *viewer = viewerForHelpViewerLocation(location);
QTC_ASSERT(viewer, return);
viewer->setSource(newUrl);
Core::ICore::raiseWindow(viewer);
}
void HelpPlugin::slotOpenSupportPage()
{
switchToHelpMode(QUrl(QLatin1String("qthelp://org.qt-project.qtcreator/doc/technical-support.html")));
}
void HelpPlugin::slotReportBug()
{
QDesktopServices::openUrl(QUrl(QLatin1String("https://bugreports.qt-project.org")));
}
void HelpPlugin::onSideBarVisibilityChanged()
{
bool visible = m_sideBar->isVisibleTo(m_splitter);
m_toggleSideBarAction->setChecked(visible);
m_toggleSideBarAction->setToolTip(visible ? tr(Core::Constants::TR_HIDE_SIDEBAR)
: tr(Core::Constants::TR_SHOW_SIDEBAR));
}
void HelpPlugin::doSetupIfNeeded()
{
m_helpManager->setupGuiHelpEngine();
if (m_setupNeeded) {
qApp->processEvents();
setupUi();
resetFilter();
m_setupNeeded = false;
OpenPagesManager::instance().setupInitialPages();
}
}
Core::HelpManager::HelpViewerLocation HelpPlugin::contextHelpOption() const
{
QSettings *settings = Core::ICore::settings();
const QString key = QLatin1String(Help::Constants::ID_MODE_HELP) + QLatin1String("/ContextHelpOption");
if (settings->contains(key))
return Core::HelpManager::HelpViewerLocation(
settings->value(key, Core::HelpManager::SideBySideIfPossible).toInt());
const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
return Core::HelpManager::HelpViewerLocation(engine.customValue(QLatin1String("ContextHelpOption"),
Core::HelpManager::SideBySideIfPossible).toInt());
}
|