helpplugin.cpp 27 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include "helpplugin.h"
#include "docsettingspage.h"
#include "filtersettingspage.h"
#include "helpindexfilter.h"
#include "helpmode.h"
#include "helpviewer.h"
#include "contentwindow.h"
#include "indexwindow.h"
#include "bookmarkmanager.h"
#include "centralwidget.h"
#include "helpfindsupport.h"
#include "searchwidget.h"

hjk's avatar
hjk committed
43
44
45
46
47
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/uniqueidmanager.h>
48
#include <coreplugin/actionmanager/actionmanager.h>
hjk's avatar
hjk committed
49
50
51
52
53
54
#include <coreplugin/minisplitter.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/rightpane.h>
#include <coreplugin/sidebar.h>
#include <coreplugin/welcomemode.h>

con's avatar
con committed
55
56
57
58
59
60
#include <QtCore/QDebug>
#include <QtCore/qplugin.h>
#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtCore/QDir>
#include <QtCore/QResource>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
61
62
#include <QtCore/QLibraryInfo>
#include <QtCore/QTranslator>
con's avatar
con committed
63
64
65
66
67
68
#include <QtGui/QAction>
#include <QtGui/QShortcut>
#include <QtGui/QSplitter>
#include <QtGui/QStyle>
#include <QtGui/QToolBar>
#include <QtGui/QComboBox>
69
#include <QtGui/QDesktopServices>
con's avatar
con committed
70
71
#include <QtHelp/QHelpEngine>

72
73
74
75
76
#ifndef QT_NO_WEBKIT
#include <QtGui/QApplication>
#include <QtWebKit/QWebSettings>
#endif

con's avatar
con committed
77
78
79
80
81
82
83
84
85
86
87
88
89
using namespace Help;
using namespace Help::Internal;

HelpManager::HelpManager(QHelpEngine *helpEngine)
    : m_helpEngine(helpEngine)
{
}

void HelpManager::registerDocumentation(const QStringList &fileNames)
{
    bool needsSetup = false;
    {
        QHelpEngineCore hc(m_helpEngine->collectionFile());
90
91
        if (!hc.setupData())
            qWarning() << "Could not initialize help engine:" << hc.error();
con's avatar
con committed
92
        foreach (const QString &fileName, fileNames) {
kh's avatar
kh committed
93
            if (!QFileInfo(fileName).exists())
con's avatar
con committed
94
                continue;
kh's avatar
kh committed
95
96
97
            const QString &nameSpace = QHelpEngineCore::namespaceName(fileName);
            if (!nameSpace.isEmpty()
                && !hc.registeredDocumentations().contains(nameSpace)) {
con's avatar
con committed
98
99
100
101
102
103
104
105
                if (hc.registerDocumentation(fileName))
                    needsSetup = true;
                else
                    qDebug() << "error registering" << fileName << hc.error();
            }
        }
    }
    if (needsSetup)
106
        m_helpEngine->setupData();
con's avatar
con committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
}

HelpPlugin::HelpPlugin() :
    m_core(0),
    m_helpEngine(0),
    m_contextHelpEngine(0),
    m_contentWidget(0),
    m_indexWidget(0),
    m_centralWidget(0),
    m_helpViewerForSideBar(0),
    m_mode(0),
    m_shownLastPages(false),
    m_contentItem(0),
    m_indexItem(0),
    m_searchItem(0),
    m_bookmarkItem(0),
    m_rightPaneSideBar(0)
{
}

HelpPlugin::~HelpPlugin()
{
}

131
bool HelpPlugin::initialize(const QStringList &arguments, QString *error)
con's avatar
con committed
132
{
133
134
135
    Q_UNUSED(arguments);
    Q_UNUSED(error);
    m_core = Core::ICore::instance();
con's avatar
con committed
136
137
138
139
140
    QList<int> globalcontext;
    globalcontext << Core::Constants::C_GLOBAL_ID;
    QList<int> modecontext;
    modecontext << m_core->uniqueIDManager()->uniqueIdentifier(Constants::C_MODE_HELP);

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
141
142
143
144
    QString locale = qApp->property("qtc_locale").toString();
    if (!locale.isEmpty()) {
        QTranslator *qtr = new QTranslator(this);
        qtr->load(QLatin1String("assistant_") + locale,
kh's avatar
kh committed
145
            QLibraryInfo::location(QLibraryInfo::TranslationsPath));
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
146
147
148
        qApp->installTranslator(qtr);
    }

149
150
151
152
153
154
155
#ifndef QT_NO_WEBKIT
    QWebSettings *webSettings = QWebSettings::globalSettings();
    const QFont applicationFont = QApplication::font();
    webSettings->setFontFamily(QWebSettings::StandardFont, applicationFont.family());
    //webSettings->setFontSize(QWebSettings::DefaultFontSize, applicationFont.pointSize());
#endif

con's avatar
con committed
156
157
    // FIXME shouldn't the help engine create the directory if it doesn't exist?
    QFileInfo fi(m_core->settings()->fileName());
158
    QDir directory(fi.absolutePath()+"/qtcreator");
con's avatar
con committed
159
160
    if (!directory.exists())
        directory.mkpath(directory.absolutePath());
kh's avatar
kh committed
161
162
163
164
    m_helpEngine = new QHelpEngine(directory.absolutePath() +
        QLatin1String("/helpcollection.qhc"), this);
    connect(m_helpEngine, SIGNAL(setupFinished()), this,
        SLOT(updateFilterComboBox()));
con's avatar
con committed
165
166
167
168
169
170
171
172
173
174

    addAutoReleasedObject(new HelpManager(m_helpEngine));

    m_docSettingsPage = new DocSettingsPage(m_helpEngine);
    addAutoReleasedObject(m_docSettingsPage);

    m_filterSettingsPage = new FilterSettingsPage(m_helpEngine);
    addAutoReleasedObject(m_filterSettingsPage);
    connect(m_docSettingsPage, SIGNAL(documentationAdded()),
        m_filterSettingsPage, SLOT(updateFilterPage()));
kh's avatar
kh committed
175
176
    connect(m_docSettingsPage, SIGNAL(dialogAccepted()), this,
        SLOT(checkForHelpChanges()));
con's avatar
con committed
177
178
179
180
181
182
183
184
185
186

    m_contentWidget = new ContentWindow(m_helpEngine);
    m_contentWidget->setWindowTitle(tr("Contents"));
    m_indexWidget = new IndexWindow(m_helpEngine);
    m_indexWidget->setWindowTitle(tr("Index"));
    m_searchWidget = new SearchWidget(m_helpEngine->searchEngine());
    m_searchWidget->setWindowTitle(tr("Search"));
    m_bookmarkManager = new BookmarkManager(m_helpEngine);
    m_bookmarkWidget = new BookmarkWidget(m_bookmarkManager, 0, false);
    m_bookmarkWidget->setWindowTitle(tr("Bookmarks"));
kh's avatar
kh committed
187
    connect(m_bookmarkWidget, SIGNAL(addBookmark()), this, SLOT(addBookmark()));
con's avatar
con committed
188

189
    Core::ActionManager *am = m_core->actionManager();
con's avatar
con committed
190
    Core::Command *cmd;
con's avatar
con committed
191
192

    // Add Home, Previous and Next actions (used in the toolbar)
kh's avatar
kh committed
193
194
195
    QAction *homeAction =
        new QAction(QIcon(QLatin1String(":/help/images/home.png")), tr("Home"),
        this);
con's avatar
con committed
196
197
    cmd = am->registerAction(homeAction, QLatin1String("Help.Home"), globalcontext);

kh's avatar
kh committed
198
199
    QAction *previousAction =
        new QAction(QIcon(QLatin1String(":/help/images/previous.png")),
con's avatar
con committed
200
        tr("Previous"), this);
kh's avatar
kh committed
201
202
    cmd = am->registerAction(previousAction, QLatin1String("Help.Previous"),
        modecontext);
203
    cmd->setDefaultKeySequence(QKeySequence::Back);
con's avatar
con committed
204

kh's avatar
kh committed
205
206
207
    QAction *nextAction =
        new QAction(QIcon(QLatin1String(":/help/images/next.png")), tr("Next"),
        this);
con's avatar
con committed
208
    cmd = am->registerAction(nextAction, QLatin1String("Help.Next"), modecontext);
209
    cmd->setDefaultKeySequence(QKeySequence::Forward);
con's avatar
con committed
210

kh's avatar
kh committed
211
212
    QAction *addBookmarkAction =
        new QAction(QIcon(QLatin1String(":/help/images/bookmark.png")),
con's avatar
con committed
213
        tr("Add Bookmark"), this);
kh's avatar
kh committed
214
215
    cmd = am->registerAction(addBookmarkAction, QLatin1String("Help.AddBookmark"),
        modecontext);
con's avatar
con committed
216
217
218
219
    cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_M));

    // Add Index, Contents, and Context menu items and a separator to the Help menu
    QAction *indexAction = new QAction(tr("Index"), this);
kh's avatar
kh committed
220
221
222
223
    cmd = am->registerAction(indexAction, QLatin1String("Help.Index"),
        globalcontext);
    am->actionContainer(Core::Constants::M_HELP)->addAction(cmd,
        Core::Constants::G_HELP_HELP);
con's avatar
con committed
224
225

    QAction *contentsAction = new QAction(tr("Contents"), this);
kh's avatar
kh committed
226
227
228
229
    cmd = am->registerAction(contentsAction, QLatin1String("Help.Contents"),
        globalcontext);
    am->actionContainer(Core::Constants::M_HELP)->addAction(cmd,
        Core::Constants::G_HELP_HELP);
con's avatar
con committed
230
231

    QAction *searchAction = new QAction(tr("Search"), this);
kh's avatar
kh committed
232
233
234
235
    cmd = am->registerAction(searchAction, QLatin1String("Help.Search"),
        globalcontext);
    am->actionContainer(Core::Constants::M_HELP)->addAction(cmd,
        Core::Constants::G_HELP_HELP);
con's avatar
con committed
236
237

    QAction *contextAction = new QAction(tr("Context Help"), this);
kh's avatar
kh committed
238
239
    cmd = am->registerAction(contextAction, QLatin1String("Help.Context"),
        globalcontext);
con's avatar
con committed
240
    cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F1));
kh's avatar
kh committed
241
242
    am->actionContainer(Core::Constants::M_HELP)->addAction(cmd,
        Core::Constants::G_HELP_HELP);
con's avatar
con committed
243

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
244
#ifndef Q_WS_MAC
con's avatar
con committed
245
246
247
    QAction *sep = new QAction(this);
    sep->setSeparator(true);
    cmd = am->registerAction(sep, QLatin1String("Help.Separator"), globalcontext);
kh's avatar
kh committed
248
249
    am->actionContainer(Core::Constants::M_HELP)->addAction(cmd,
        Core::Constants::G_HELP_HELP);
con's avatar
con committed
250
251
#endif

Friedemann Kleint's avatar
Friedemann Kleint committed
252
    m_centralWidget = new Help::Internal::CentralWidget(m_helpEngine);
con's avatar
con committed
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
    Aggregation::Aggregate *agg = new Aggregation::Aggregate;
    agg->add(m_centralWidget);
    agg->add(new HelpFindSupport(m_centralWidget));
    QWidget *mainWidget = new QWidget;
    QVBoxLayout *mainWidgetLayout = new QVBoxLayout(mainWidget);
    mainWidgetLayout->setMargin(0);
    mainWidgetLayout->setSpacing(0);
    mainWidgetLayout->addWidget(createToolBar());
    mainWidgetLayout->addWidget(m_centralWidget);

    m_contentItem = new Core::SideBarItem(m_contentWidget);
    m_indexItem = new Core::SideBarItem(m_indexWidget);
    m_searchItem = new Core::SideBarItem(m_searchWidget);
    m_bookmarkItem = new Core::SideBarItem(m_bookmarkWidget);
    QList<Core::SideBarItem*> itemList;
    itemList << m_contentItem << m_indexItem << m_searchItem << m_bookmarkItem;
    m_sideBar = new Core::SideBar(itemList, QList<Core::SideBarItem*>() << m_indexItem);

    QSplitter *splitter = new Core::MiniSplitter;
    splitter->setOpaqueResize(false);
    splitter->addWidget(m_sideBar);
    splitter->addWidget(mainWidget);
    splitter->setStretchFactor(0, 0);
    splitter->setStretchFactor(1, 1);
    splitter->setSizes(QList<int>() << 300 << 300);

    m_mode = new HelpMode(splitter, m_centralWidget);
    m_mode->setContext(QList<int>() << modecontext);
    addAutoReleasedObject(m_mode);

    QAction *printAction = new QAction(this);
    am->registerAction(printAction, Core::Constants::PRINT, modecontext);
    connect(printAction, SIGNAL(triggered()), m_centralWidget, SLOT(print()));

    QAction *copyAction = new QAction(this);
    cmd = am->registerAction(copyAction, Core::Constants::COPY, modecontext);
    connect(copyAction, SIGNAL(triggered()), m_centralWidget, SLOT(copy()));
    copyAction->setText(cmd->action()->text());
    copyAction->setIcon(cmd->action()->icon());

con's avatar
con committed
293
    QMap<QString, Core::Command*> shortcutMap;
con's avatar
con committed
294
295
    QShortcut *shortcut = new QShortcut(splitter);
    shortcut->setWhatsThis(tr("Activate Index in Help mode"));
kh's avatar
kh committed
296
297
    cmd = am->registerShortcut(shortcut, QLatin1String("Help.IndexShortcut"),
        modecontext);
con's avatar
con committed
298
299
300
301
302
303
    cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_I));
    connect(shortcut, SIGNAL(activated()), this, SLOT(activateIndex()));
    shortcutMap.insert(m_indexWidget->windowTitle(), cmd);

    shortcut = new QShortcut(splitter);
    shortcut->setWhatsThis(tr("Activate Contents in Help mode"));
kh's avatar
kh committed
304
305
    cmd = am->registerShortcut(shortcut, QLatin1String("Help.ContentsShortcut"),
        modecontext);
con's avatar
con committed
306
307
308
309
310
311
    cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_T));
    connect(shortcut, SIGNAL(activated()), this, SLOT(activateContents()));
    shortcutMap.insert(m_contentWidget->windowTitle(), cmd);

    shortcut = new QShortcut(splitter);
    shortcut->setWhatsThis(tr("Activate Search in Help mode"));
kh's avatar
kh committed
312
313
    cmd = am->registerShortcut(shortcut, QLatin1String("Help.SearchShortcut"),
        modecontext);
con's avatar
con committed
314
315
316
317
318
319
320
321
322
323
324
    cmd->setDefaultKeySequence(QKeySequence(Qt::CTRL + Qt::Key_S));
    connect(shortcut, SIGNAL(activated()), this, SLOT(activateSearch()));
    shortcutMap.insert(m_searchWidget->windowTitle(), cmd);
    shortcutMap.insert(m_bookmarkWidget->windowTitle(), 0);

    m_sideBar->setShortcutMap(shortcutMap);

    connect(homeAction, SIGNAL(triggered()), m_centralWidget, SLOT(home()));
    connect(previousAction, SIGNAL(triggered()), m_centralWidget, SLOT(backward()));
    connect(nextAction, SIGNAL(triggered()), m_centralWidget, SLOT(forward()));
    connect(addBookmarkAction, SIGNAL(triggered()), this, SLOT(addBookmark()));
kh's avatar
kh committed
325
326
327
328
329
330
331
332
333
334
    connect(m_contentWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
        SLOT(setSource(QUrl)));
    connect(m_indexWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
        SLOT(setSource(QUrl)));
    connect(m_searchWidget, SIGNAL(requestShowLink(QUrl)), m_centralWidget,
        SLOT(setSource(QUrl)));
    connect(m_searchWidget, SIGNAL(requestShowLinkInNewTab(QUrl)),
        m_centralWidget, SLOT(setSourceInNewTab(QUrl)));
    connect(m_bookmarkWidget, SIGNAL(requestShowLink(QUrl)), m_centralWidget,
        SLOT(setSource(const QUrl&)));
con's avatar
con committed
335
336
337
338
339

    connect(m_centralWidget, SIGNAL(backwardAvailable(bool)),
        previousAction, SLOT(setEnabled(bool)));
    connect(m_centralWidget, SIGNAL(forwardAvailable(bool)),
        nextAction, SLOT(setEnabled(bool)));
kh's avatar
kh committed
340
341
    connect(m_centralWidget, SIGNAL(addNewBookmark(QString, QString)), this,
        SLOT(addNewBookmark(QString, QString)));
con's avatar
con committed
342
343
344
345
346

    QList<QAction*> actionList;
    actionList << previousAction
        << nextAction
        << homeAction
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
347
#ifndef Q_WS_MAC
con's avatar
con committed
348
349
350
351
352
353
354
355
356
357
358
359
360
        << sep
#endif
        << copyAction;
    m_centralWidget->setGlobalActions(actionList);

    connect(contextAction, SIGNAL(triggered()), this, SLOT(activateContext()));
    connect(indexAction, SIGNAL(triggered()), this, SLOT(activateIndex()));
    connect(contentsAction, SIGNAL(triggered()), this, SLOT(activateContents()));
    connect(searchAction, SIGNAL(triggered()), this, SLOT(activateSearch()));

    connect(m_core->modeManager(), SIGNAL(currentModeChanged(Core::IMode*)),
        this, SLOT(modeChanged(Core::IMode*)));

kh's avatar
kh committed
361
362
363
364
365
366
    connect(m_contentWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
        SLOT(setSource(QUrl)));
    connect(m_indexWidget, SIGNAL(linkActivated(QUrl)), m_centralWidget,
        SLOT(setSource(QUrl)));
    connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
        m_centralWidget, SLOT(showTopicChooser(QMap<QString, QUrl>, QString)));
con's avatar
con committed
367
368
369

    HelpIndexFilter *helpIndexFilter = new HelpIndexFilter(this, m_helpEngine);
    addAutoReleasedObject(helpIndexFilter);
kh's avatar
kh committed
370
371
372
373
    connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this,
        SLOT(switchToHelpMode(QUrl)));
    connect(helpIndexFilter, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
        this, SLOT(switchToHelpMode(QMap<QString, QUrl>, QString)));
con's avatar
con committed
374
375
376
377
378
379

    previousAction->setEnabled(m_centralWidget->isBackwardAvailable());
    nextAction->setEnabled(m_centralWidget->isForwardAvailable());

    createRightPaneSideBar();

380
381
    QDesktopServices::setUrlHandler("qthelp", this, "openHelpPage");

con's avatar
con committed
382
383
384
385
386
387
    return true;
}

void HelpPlugin::createRightPaneSideBar()
{
    QAction *switchToHelpMode = new QAction("Go to Help Mode", this);
kh's avatar
kh committed
388
389
390
391
392
393
    m_rightPaneBackwardAction =
        new QAction(QIcon(QLatin1String(":/help/images/previous.png")),
        tr("Previous"), this);
    m_rightPaneForwardAction =
        new QAction(QIcon(QLatin1String(":/help/images/next.png")), tr("Next"),
        this);
con's avatar
con committed
394
395
396
397
398
399
400

    QToolBar *rightPaneToolBar = new QToolBar();
    rightPaneToolBar->addAction(switchToHelpMode);
    rightPaneToolBar->addAction(m_rightPaneBackwardAction);
    rightPaneToolBar->addAction(m_rightPaneForwardAction);

    connect(switchToHelpMode, SIGNAL(triggered()), this, SLOT(switchToHelpMode()));
kh's avatar
kh committed
401
402
403
404
    connect(m_rightPaneBackwardAction, SIGNAL(triggered()), this,
        SLOT(rightPaneBackward()));
    connect(m_rightPaneForwardAction, SIGNAL(triggered()), this,
        SLOT(rightPaneForward()));
con's avatar
con committed
405
406
407

    QToolButton *closeButton = new QToolButton();
    closeButton->setProperty("type", QLatin1String("dockbutton"));
408
    closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
con's avatar
con committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

    // Dummy layout to align the close button to the right
    QHBoxLayout *hboxLayout = new QHBoxLayout();
    hboxLayout->setSpacing(0);
    hboxLayout->setMargin(0);
    hboxLayout->addStretch(5);
    hboxLayout->addWidget(closeButton);

    QWidget *w = new QWidget(rightPaneToolBar);
    w->setLayout(hboxLayout);
    rightPaneToolBar->addWidget(w);
    connect(closeButton, SIGNAL(clicked()), this, SLOT(slotHideRightPane()));

    QVBoxLayout *rightPaneLayout = new QVBoxLayout;
    rightPaneLayout->setMargin(0);
    rightPaneLayout->setSpacing(0);
    rightPaneLayout->addWidget(rightPaneToolBar);

    m_helpViewerForSideBar = new HelpViewer(m_helpEngine, 0);
con's avatar
con committed
428
429
430
    Aggregation::Aggregate *agg = new Aggregation::Aggregate();
    agg->add(m_helpViewerForSideBar);
    agg->add(new HelpViewerFindSupport(m_helpViewerForSideBar));
con's avatar
con committed
431
    rightPaneLayout->addWidget(m_helpViewerForSideBar);
432
433
434
    m_core->addContextObject(new Core::BaseContext(m_helpViewerForSideBar, QList<int>()
        << m_core->uniqueIDManager()->uniqueIdentifier(Constants::C_HELP_SIDEBAR),
        this));
435
436
437
438
439
    connect(m_centralWidget, SIGNAL(sourceChanged(QUrl)), this,
        SLOT(updateSideBarSource(QUrl)));
    connect(m_centralWidget, SIGNAL(currentViewerChanged()), this,
        SLOT(updateSideBarSource()));

440
441
442
443
444
445
446
    QAction *copyActionSideBar = new QAction(this);
    Core::Command *cmd = m_core->actionManager()->registerAction(copyActionSideBar,
        Core::Constants::COPY, QList<int>()
        << m_core->uniqueIDManager()->uniqueIdentifier(Constants::C_HELP_SIDEBAR));
    connect(copyActionSideBar, SIGNAL(triggered()), this, SLOT(copyFromSideBar()));
    copyActionSideBar->setText(cmd->action()->text());
    copyActionSideBar->setIcon(cmd->action()->icon());
con's avatar
con committed
447
448
449
450
451
452
453

    m_rightPaneSideBar = new QWidget;
    m_rightPaneSideBar->setLayout(rightPaneLayout);
    m_rightPaneSideBar->setFocusProxy(m_helpViewerForSideBar);
    addAutoReleasedObject(new Core::BaseRightPaneWidget(m_rightPaneSideBar));
}

454
455
456
457
458
void HelpPlugin::copyFromSideBar()
{
    m_helpViewerForSideBar->copy();
}

con's avatar
con committed
459
460
461
462
463
464
465
466
467
468
void HelpPlugin::rightPaneBackward()
{
    m_helpViewerForSideBar->backward();
}

void HelpPlugin::rightPaneForward()
{
    m_helpViewerForSideBar->forward();
}

hjk's avatar
hjk committed
469
470
471
472
473
void HelpPlugin::activateHelpMode()
{
    m_core->modeManager()->activateMode(QLatin1String(Constants::ID_MODE_HELP));
}

con's avatar
con committed
474
475
476
477
478
479
480
void HelpPlugin::switchToHelpMode()
{
    switchToHelpMode(m_helpViewerForSideBar->source());
}

void HelpPlugin::switchToHelpMode(const QUrl &source)
{
hjk's avatar
hjk committed
481
    activateHelpMode();
con's avatar
con committed
482
483
484
485
    m_centralWidget->setSource(source);
    m_centralWidget->setFocus();
}

kh's avatar
kh committed
486
487
void HelpPlugin::switchToHelpMode(const QMap<QString, QUrl> &urls,
    const QString &keyword)
con's avatar
con committed
488
{
hjk's avatar
hjk committed
489
    activateHelpMode();
con's avatar
con committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    m_centralWidget->showTopicChooser(urls, keyword);
}

void HelpPlugin::slotHideRightPane()
{
    Core::RightPaneWidget::instance()->setShown(false);
}

void HelpPlugin::extensionsInitialized()
{
    m_sideBar->readSettings(m_core->settings());
    if (!m_helpEngine->setupData()) {
        qWarning() << "Could not initialize help engine: " << m_helpEngine->error();
        return;
    }

    bool needsSetup = false;
    bool assistantInternalDocRegistered = false;

kh's avatar
kh committed
509
510
511
512
513
    const QString &docInternal = QString("com.nokia.qtcreator.%1%2")
        .arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR);
    const QStringList &docs = m_helpEngine->registeredDocumentations();
    foreach (const QString &ns, docs) {
        if (ns == docInternal) {
con's avatar
con committed
514
515
516
517
518
519
520
521
522
523
524
525
526
            assistantInternalDocRegistered = true;
            break;
        }
    }

    if (!assistantInternalDocRegistered) {
        QFileInfo fi(m_helpEngine->collectionFile());

        const QString qchFileName =
            QDir::cleanPath(QCoreApplication::applicationDirPath()
#if defined(Q_OS_MAC)
            + QLatin1String("/../Resources/doc/qtcreator.qch"));
#else
Daniel Molkentin's avatar
Daniel Molkentin committed
527
            + QLatin1String("../../share/doc/qtcreator/qtcreator.qch"));
con's avatar
con committed
528
529
530
#endif
        QHelpEngineCore hc(fi.absoluteFilePath());
        hc.setupData();
531
        QString fileNamespace = QHelpEngineCore::namespaceName(qchFileName);
kh's avatar
kh committed
532
533
534
535
536
        if (!fileNamespace.isEmpty()
            && !hc.registeredDocumentations().contains(fileNamespace)) {
                if (!hc.registerDocumentation(qchFileName))
                    qDebug() << hc.error();
                needsSetup = true;
537
        }
con's avatar
con committed
538
539
    }

kh's avatar
kh committed
540
541
    QLatin1String key("UnfilteredFilterInserted");
    int i = m_helpEngine->customValue(key).toInt();
con's avatar
con committed
542
543
544
545
546
    if (i != 1) {
        {
            QHelpEngineCore hc(m_helpEngine->collectionFile());
            hc.setupData();
            hc.addCustomFilter(tr("Unfiltered"), QStringList());
kh's avatar
kh committed
547
            hc.setCustomValue(key, 1);
con's avatar
con committed
548
549
550
551
552
553
554
555
556
557
558
559
560
        }
        m_helpEngine->blockSignals(true);
        m_helpEngine->setCurrentFilter(tr("Unfiltered"));
        m_helpEngine->blockSignals(false);
        needsSetup = true;
    }

    if (needsSetup)
        m_helpEngine->setupData();

    updateFilterComboBox();
    m_bookmarkManager->setupBookmarkModels();

kh's avatar
kh committed
561
562
563
564
565
566
567
568
569
    using namespace Core::Internal;
    using namespace Core::Constants;
    WelcomeMode *welcomeMode =
        qobject_cast<WelcomeMode*>(m_core->modeManager()->mode(MODE_WELCOME));
    if (welcomeMode) {
        connect(welcomeMode, SIGNAL(openHelpPage(QString)), this,
            SLOT(openHelpPage(QString)));
        connect(welcomeMode, SIGNAL(openContextHelpPage(QString)), this,
            SLOT(openContextHelpPage(QString)));
con's avatar
con committed
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
    }
}

void HelpPlugin::shutdown()
{
    m_sideBar->saveSettings(m_core->settings());
    m_bookmarkManager->saveBookmarks();
    delete m_bookmarkManager;
}

void HelpPlugin::setIndexFilter(const QString &filter)
{
    m_indexWidget->setSearchLineEditText(filter);
}

QString HelpPlugin::indexFilter() const
{
    return m_indexWidget->searchLineEditText();
}

void HelpPlugin::modeChanged(Core::IMode *mode)
{
    if (mode == m_mode && !m_shownLastPages) {
        m_shownLastPages = true;
        qApp->processEvents();
        qApp->setOverrideCursor(Qt::WaitCursor);
        m_centralWidget->setLastShownPages();
        qApp->restoreOverrideCursor();
    }
}

601
602
603
604
605
606
void HelpPlugin::openContextHelpPage(const QString &url)
{
    Core::RightPaneWidget::instance()->setShown(true);
    m_helpViewerForSideBar->setSource(QUrl(url));
}

607
608
609
610
611
612
613
614
615
616
617
618
619
void HelpPlugin::updateSideBarSource()
{
    const QUrl &url = m_centralWidget->currentSource();
    if (url.isValid())
        updateSideBarSource(url);
}

void HelpPlugin::updateSideBarSource(const QUrl &newUrl)
{
    if (m_helpViewerForSideBar)
        m_helpViewerForSideBar->setSource(newUrl);
}

con's avatar
con committed
620
621
void HelpPlugin::activateContext()
{
kh's avatar
kh committed
622
    Core::RightPanePlaceHolder* placeHolder = Core::RightPanePlaceHolder::current();
hjk's avatar
hjk committed
623
    if (placeHolder && Core::RightPaneWidget::instance()->hasFocus()) {
con's avatar
con committed
624
625
        switchToHelpMode();
        return;
kh's avatar
kh committed
626
627
    } else if (m_core->modeManager()->currentMode() == m_mode)
        return;
con's avatar
con committed
628

kh's avatar
kh committed
629
630
    QString id;
    QMap<QString, QUrl> links;
con's avatar
con committed
631
632

    // Find out what to show
kh's avatar
kh committed
633
    if (Core::IContext *context = m_core->currentContextObject()) {
con's avatar
con committed
634
        if (!m_contextHelpEngine) {
kh's avatar
kh committed
635
636
            m_contextHelpEngine =
                new QHelpEngineCore(m_helpEngine->collectionFile(), this);
con's avatar
con committed
637
638
639
640
            m_contextHelpEngine->setupData();
            m_contextHelpEngine->setCurrentFilter(tr("Unfiltered"));
        }

kh's avatar
kh committed
641
642
643
        id = context->contextHelpId();
        links = m_contextHelpEngine->linksForIdentifier(id);
    }
con's avatar
con committed
644

kh's avatar
kh committed
645
646
647
648
    HelpViewer *viewer = 0;
    if (placeHolder && !Core::RightPaneWidget::instance()->hasFocus()) {
        Core::RightPaneWidget::instance()->setShown(true);
        viewer = m_helpViewerForSideBar;
con's avatar
con committed
649
    } else {
kh's avatar
kh committed
650
651
652
        viewer = m_centralWidget->currentHelpViewer();
        activateHelpMode();
    }
con's avatar
con committed
653

kh's avatar
kh committed
654
655
656
657
658
659
    if (viewer) {
        if (links.isEmpty()) {
            // No link found or no context object
            viewer->setHtml(tr("<html><head><title>No Documentation</title>"
                "</head><body><br/><center><b>%1</b><br/>No documentation "
                "available.</center></body></html>").arg(id));
con's avatar
con committed
660
            viewer->setSource(QUrl());
kh's avatar
kh committed
661
662
663
664
665
        } else {
            QUrl source = *links.begin();
            if (viewer->source() != source)
                viewer->setSource(source);
            viewer->setFocus();
con's avatar
con committed
666
667
668
669
670
671
        }
    }
}

void HelpPlugin::activateIndex()
{
hjk's avatar
hjk committed
672
    activateHelpMode();
con's avatar
con committed
673
674
675
676
677
    m_sideBar->activateItem(m_indexItem);
}

void HelpPlugin::activateContents()
{
hjk's avatar
hjk committed
678
    activateHelpMode();
con's avatar
con committed
679
680
681
682
683
    m_sideBar->activateItem(m_contentItem);
}

void HelpPlugin::activateSearch()
{
hjk's avatar
hjk committed
684
    activateHelpMode();
con's avatar
con committed
685
686
687
688
689
690
    m_sideBar->activateItem(m_searchItem);
}

QToolBar *HelpPlugin::createToolBar()
{
    QToolBar *toolWidget = new QToolBar;
691
    Core::ActionManager *am = m_core->actionManager();
con's avatar
con committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
    toolWidget->addAction(am->command(QLatin1String("Help.Home"))->action());
    toolWidget->addAction(am->command(QLatin1String("Help.Previous"))->action());
    toolWidget->addAction(am->command(QLatin1String("Help.Next"))->action());
    toolWidget->addSeparator();
    toolWidget->addAction(am->command(QLatin1String("Help.AddBookmark"))->action());
    //int size = toolWidget->style()->pixelMetric(QStyle::PM_SmallIconSize);
    //toolWidget->setIconSize(QSize(size, size));
    toolWidget->setMovable(false);

    toolWidget->addSeparator();

    QWidget *w = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout(w);
    layout->setMargin(0);
    layout->addSpacing(10);
    layout->addWidget(new QLabel(tr("Filtered by:")));
    m_filterComboBox = new QComboBox;
    m_filterComboBox->setMinimumContentsLength(20);
kh's avatar
kh committed
710
711
    connect(m_filterComboBox, SIGNAL(activated(QString)), this,
        SLOT(filterDocumentation(QString)));
con's avatar
con committed
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
    layout->addWidget(m_filterComboBox);
    toolWidget->addWidget(w);

    return toolWidget;
}

void HelpPlugin::updateFilterComboBox()
{
    QString curFilter = m_filterComboBox->currentText();
    if (curFilter.isEmpty())
        curFilter = m_helpEngine->currentFilter();
    m_filterComboBox->clear();
    m_filterComboBox->addItems(m_helpEngine->customFilters());
    int idx = m_filterComboBox->findText(curFilter);
    if (idx < 0)
        idx = 0;
    m_filterComboBox->setCurrentIndex(idx);
}

void HelpPlugin::checkForHelpChanges()
{
    bool changed = m_docSettingsPage->applyChanges();
    changed |= m_filterSettingsPage->applyChanges();
    if (changed)
        m_helpEngine->setupData();
}

void HelpPlugin::filterDocumentation(const QString &customFilter)
{
    m_helpEngine->setCurrentFilter(customFilter);
}

void HelpPlugin::addBookmark()
{
746
747
    addNewBookmark(m_centralWidget->currentTitle(),
        m_centralWidget->currentSource().toString());
con's avatar
con committed
748
749
750
751
}

void HelpPlugin::addNewBookmark(const QString &title, const QString &url)
{
752
    if (url.isEmpty() || url == QLatin1String("about:blank"))
con's avatar
con committed
753
754
755
756
757
        return;

    m_bookmarkManager->showBookmarkDialog(m_centralWidget, title, url);
}

758
void HelpPlugin::openHelpPage(const QUrl& url)
con's avatar
con committed
759
{
760
    openHelpPage(url.toString());
con's avatar
con committed
761
762
}

763
764
765
766
767
768
void HelpPlugin::openHelpPage(const QString& url)
{
    activateHelpMode();
    if (m_helpEngine->findFile(url).isValid())
        m_centralWidget->setSource(url);
    else {
769
770
        QDesktopServices::openUrl(QLatin1String("http://doc.trolltech.com/latest/")
            + url.mid(url.lastIndexOf('/') + 1));
771
772
    }
}
con's avatar
con committed
773
774

Q_EXPORT_PLUGIN(HelpPlugin)