helpwidget.cpp 25.9 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30 31 32
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "helpwidget.h"

33
#include "bookmarkmanager.h"
34
#include "contentwindow.h"
35
#include "helpconstants.h"
36
#include "helpicons.h"
37 38
#include "helpplugin.h"
#include "helpviewer.h"
39
#include "indexwindow.h"
40
#include "localhelpmanager.h"
41
#include "openpagesmanager.h"
42
#include "searchwidget.h"
43
#include "topicchooser.h"
44 45 46

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
47
#include <coreplugin/actionmanager/command.h>
48
#include <coreplugin/coreconstants.h>
49
#include <coreplugin/coreicons.h>
50 51
#include <coreplugin/icore.h>
#include <coreplugin/findplaceholder.h>
52 53
#include <coreplugin/minisplitter.h>
#include <coreplugin/sidebar.h>
54
#include <texteditor/texteditorconstants.h>
55
#include <utils/qtcassert.h>
56 57
#include <utils/styledbar.h>

Sergey Belyashov's avatar
Sergey Belyashov committed
58
#include <QCoreApplication>
59
#include <QHBoxLayout>
60 61 62
#include <QHelpEngine>
#include <QHelpSearchEngine>
#include <QHelpSearchQuery>
63
#include <QMenu>
64 65 66
#include <QPrinter>
#include <QPrintDialog>
#include <QStackedWidget>
67 68
#include <QToolButton>

69 70
static const char kWindowSideBarSettingsKey[] = "Help/WindowSideBar";
static const char kModeSideBarSettingsKey[] = "Help/ModeSideBar";
71 72 73 74

namespace Help {
namespace Internal {

75 76
HelpWidget::HelpWidget(const Core::Context &context, WidgetStyle style, QWidget *parent) :
    QWidget(parent),
77
    m_style(style),
78
    m_toggleSideBarAction(0),
79 80 81
    m_switchToHelp(0),
    m_filterComboBox(0),
    m_closeAction(0),
82 83
    m_scaleUp(0),
    m_scaleDown(0),
84
    m_resetScale(0),
85 86
    m_printer(0),
    m_sideBar(0),
87 88 89 90 91
    m_contentsAction(0),
    m_indexAction(0),
    m_bookmarkAction(0),
    m_searchAction(0),
    m_openPagesAction(0)
92
{
93 94 95 96 97 98 99 100 101 102
    m_viewerStack = new QStackedWidget;

    auto hLayout = new QHBoxLayout(this);
    hLayout->setMargin(0);
    hLayout->setSpacing(0);

    m_sideBarSplitter = new Core::MiniSplitter(this);
    m_sideBarSplitter->setOpaqueResize(false);
    hLayout->addWidget(m_sideBarSplitter);

103
    Utils::StyledBar *toolBar = new Utils::StyledBar();
104 105 106
    QHBoxLayout *layout = new QHBoxLayout(toolBar);
    layout->setSpacing(0);
    layout->setMargin(0);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    auto rightSide = new QWidget(this);
    m_sideBarSplitter->insertWidget(1, rightSide);
    QVBoxLayout *vLayout = new QVBoxLayout(rightSide);
    vLayout->setMargin(0);
    vLayout->setSpacing(0);
    vLayout->addWidget(toolBar);
    vLayout->addWidget(m_viewerStack);
    Core::FindToolBarPlaceHolder *fth = new Core::FindToolBarPlaceHolder(this);
    vLayout->addWidget(fth);

    setFocusProxy(m_viewerStack);

    m_context = new Core::IContext(this);
    m_context->setContext(context);
    m_context->setWidget(m_sideBarSplitter);
    Core::ICore::addContextObject(m_context);

125
    Core::Command *cmd;
126 127 128 129 130 131 132 133
    QToolButton *button;

    if (style == ExternalWindow) {
        static int windowId = 0;
        Core::ICore::registerWindow(this,
                                    Core::Context(Core::Id("Help.Window.").withSuffix(++windowId)));
        setAttribute(Qt::WA_DeleteOnClose);
        setAttribute(Qt::WA_QuitOnClose, false); // don't prevent Qt Creator from closing
134 135
    }
    if (style != SideBarWidget) {
136
        m_toggleSideBarAction = new QAction(Core::Icons::TOGGLE_SIDEBAR.icon(),
Sergey Belyashov's avatar
Sergey Belyashov committed
137 138
                                            QCoreApplication::translate("Core", Core::Constants::TR_SHOW_SIDEBAR),
                                            toolBar);
139 140 141 142 143 144
        m_toggleSideBarAction->setCheckable(true);
        m_toggleSideBarAction->setChecked(false);
        cmd = Core::ActionManager::registerAction(m_toggleSideBarAction,
                                                  Core::Constants::TOGGLE_SIDEBAR, context);
        connect(m_toggleSideBarAction, &QAction::toggled, m_toggleSideBarAction,
                [this](bool checked) {
Sergey Belyashov's avatar
Sergey Belyashov committed
145 146 147 148
                    m_toggleSideBarAction->setText(
                        QCoreApplication::translate("Core",
                                                    checked ? Core::Constants::TR_HIDE_SIDEBAR
                                                            : Core::Constants::TR_SHOW_SIDEBAR));
149 150
                });
        addSideBar();
151
        m_toggleSideBarAction->setChecked(m_sideBar->isVisibleTo(this));
152 153 154 155 156
        connect(m_toggleSideBarAction, &QAction::triggered, m_sideBar, &Core::SideBar::setVisible);
        connect(m_sideBar, &Core::SideBar::sideBarClosed, m_toggleSideBarAction, [this]() {
            m_toggleSideBarAction->setChecked(false);
        });
    }
157 158
    if (style == ExternalWindow)
        layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_toggleSideBarAction, cmd));
159

160 161 162
    if (style != ModeWidget) {
        m_switchToHelp = new QAction(tr("Go to Help Mode"), toolBar);
        cmd = Core::ActionManager::registerAction(m_switchToHelp, Constants::CONTEXT_HELP, context);
Montel Laurent's avatar
Montel Laurent committed
163
        connect(m_switchToHelp, &QAction::triggered, this, &HelpWidget::helpModeButtonClicked);
164
        layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_switchToHelp, cmd));
165
    }
166

167
    m_homeAction = new QAction(Icons::HOME.icon(), tr("Home"), this);
168 169
    cmd = Core::ActionManager::registerAction(m_homeAction, Constants::HELP_HOME, context);
    connect(m_homeAction, &QAction::triggered, this, &HelpWidget::goHome);
170
    layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_homeAction, cmd));
171

172
    m_backAction = new QAction(Core::Icons::PREV.icon(), tr("Back"), toolBar);
173
    connect(m_backAction, &QAction::triggered, this, &HelpWidget::backward);
174
    m_backMenu = new QMenu(toolBar);
Montel Laurent's avatar
Montel Laurent committed
175
    connect(m_backMenu, &QMenu::aboutToShow, this, &HelpWidget::updateBackMenu);
176 177 178
    m_backAction->setMenu(m_backMenu);
    cmd = Core::ActionManager::registerAction(m_backAction, Constants::HELP_PREVIOUS, context);
    cmd->setDefaultKeySequence(QKeySequence::Back);
179 180 181
    button = Core::Command::toolButtonWithAppendedShortcut(m_backAction, cmd);
    button->setPopupMode(QToolButton::DelayedPopup);
    layout->addWidget(button);
182

183
    m_forwardAction = new QAction(Core::Icons::NEXT.icon(), tr("Forward"), toolBar);
184
    connect(m_forwardAction, &QAction::triggered, this, &HelpWidget::forward);
185
    m_forwardMenu = new QMenu(toolBar);
Montel Laurent's avatar
Montel Laurent committed
186
    connect(m_forwardMenu, &QMenu::aboutToShow, this, &HelpWidget::updateForwardMenu);
187 188 189
    m_forwardAction->setMenu(m_forwardMenu);
    cmd = Core::ActionManager::registerAction(m_forwardAction, Constants::HELP_NEXT, context);
    cmd->setDefaultKeySequence(QKeySequence::Forward);
190 191 192
    button = Core::Command::toolButtonWithAppendedShortcut(m_forwardAction, cmd);
    button->setPopupMode(QToolButton::DelayedPopup);
    layout->addWidget(button);
193

194
    m_addBookmarkAction = new QAction(Icons::BOOKMARK.icon(), tr("Add Bookmark"), this);
Eike Ziller's avatar
Eike Ziller committed
195
    cmd = Core::ActionManager::registerAction(m_addBookmarkAction, Constants::HELP_ADDBOOKMARK, context);
196 197 198
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+M") : tr("Ctrl+M")));
    connect(m_addBookmarkAction, &QAction::triggered, this, &HelpWidget::addBookmark);
    layout->addWidget(new Utils::StyledSeparator(toolBar));
199
    layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_addBookmarkAction, cmd));
200

201 202 203
    if (style == ModeWidget) {
        layout->addWidget(new Utils::StyledSeparator(toolBar));
        layout->addWidget(OpenPagesManager::instance().openPagesComboBox(), 10);
204 205 206 207
    } else {
        layout->addWidget(new QLabel(), 10);
    }
    if (style != SideBarWidget) {
208 209 210
        m_filterComboBox = new QComboBox;
        m_filterComboBox->setMinimumContentsLength(15);
        m_filterComboBox->setModel(LocalHelpManager::filterModel());
211
        m_filterComboBox->setCurrentIndex(LocalHelpManager::filterIndex());
212 213 214 215 216 217 218
        layout->addWidget(m_filterComboBox);
        connect(m_filterComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
                LocalHelpManager::instance(), &LocalHelpManager::setFilterIndex);
        connect(LocalHelpManager::instance(), &LocalHelpManager::filterIndexChanged,
                m_filterComboBox, &QComboBox::setCurrentIndex);
    }

219 220
    layout->addStretch();

221 222 223
    m_printAction = new QAction(this);
    Core::ActionManager::registerAction(m_printAction, Core::Constants::PRINT, context);
    connect(m_printAction, &QAction::triggered, this, [this]() { print(currentViewer()); });
224

225
    m_copy = new QAction(this);
226
    Core::ActionManager::registerAction(m_copy, Core::Constants::COPY, context);
227
    connect(m_copy, &QAction::triggered, this, &HelpWidget::copy);
228 229 230 231

    Core::ActionContainer *advancedMenu = Core::ActionManager::actionContainer(Core::Constants::M_EDIT_ADVANCED);
    QTC_CHECK(advancedMenu);
    if (advancedMenu) {
232
        // reuse TextEditor constants to avoid a second pair of menu actions
233 234
        m_scaleUp = new QAction(tr("Increase Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleUp, TextEditor::Constants::INCREASE_FONT_SIZE,
235
                                                  context);
236
        connect(m_scaleUp, &QAction::triggered, this, &HelpWidget::scaleUp);
237 238
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

239 240
        m_scaleDown = new QAction(tr("Decrease Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleDown, TextEditor::Constants::DECREASE_FONT_SIZE,
241
                                                  context);
242
        connect(m_scaleDown, &QAction::triggered, this, &HelpWidget::scaleDown);
243 244
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

245 246
        m_resetScale = new QAction(tr("Reset Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_resetScale, TextEditor::Constants::RESET_FONT_SIZE,
247
                                                  context);
248
        connect(m_resetScale, &QAction::triggered, this, &HelpWidget::resetScale);
249 250
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);
    }
251

252
    if (style != ExternalWindow) {
253
        m_closeAction = new QAction(Core::Icons::CLOSE_TOOLBAR.icon(), QString(), toolBar);
254
        connect(m_closeAction, SIGNAL(triggered()), this, SIGNAL(closeButtonClicked()));
255 256 257
        button = new QToolButton;
        button->setDefaultAction(m_closeAction);
        layout->addWidget(button);
258 259 260 261 262 263
    }

    if (style != ModeWidget) {
        HelpViewer *viewer = HelpPlugin::createHelpViewer(qreal(0.0));
        addViewer(viewer);
        setCurrentViewer(viewer);
264 265 266 267 268
    }
}

HelpWidget::~HelpWidget()
{
269
    if (m_sideBar) {
270
        m_sideBar->saveSettings(Core::ICore::settings(), sideBarSettingsKey());
271
        Core::ActionManager::unregisterAction(m_contentsAction, Constants::HELP_CONTENTS);
272
        Core::ActionManager::unregisterAction(m_indexAction, Constants::HELP_INDEX);
Eike Ziller's avatar
Eike Ziller committed
273
        Core::ActionManager::unregisterAction(m_bookmarkAction, Constants::HELP_BOOKMARKS);
274
        Core::ActionManager::unregisterAction(m_searchAction, Constants::HELP_SEARCH);
275 276
        if (m_openPagesAction)
            Core::ActionManager::unregisterAction(m_openPagesAction, Constants::HELP_OPENPAGES);
277
    }
278
    Core::ICore::removeContextObject(m_context);
279
    Core::ActionManager::unregisterAction(m_copy, Core::Constants::COPY);
280
    Core::ActionManager::unregisterAction(m_printAction, Core::Constants::PRINT);
281 282
    if (m_toggleSideBarAction)
        Core::ActionManager::unregisterAction(m_toggleSideBarAction, Core::Constants::TOGGLE_SIDEBAR);
283 284
    if (m_switchToHelp)
        Core::ActionManager::unregisterAction(m_switchToHelp, Constants::CONTEXT_HELP);
285
    Core::ActionManager::unregisterAction(m_homeAction, Constants::HELP_HOME);
286 287
    Core::ActionManager::unregisterAction(m_forwardAction, Constants::HELP_NEXT);
    Core::ActionManager::unregisterAction(m_backAction, Constants::HELP_PREVIOUS);
Eike Ziller's avatar
Eike Ziller committed
288
    Core::ActionManager::unregisterAction(m_addBookmarkAction, Constants::HELP_ADDBOOKMARK);
289 290 291 292 293 294
    if (m_scaleUp)
        Core::ActionManager::unregisterAction(m_scaleUp, TextEditor::Constants::INCREASE_FONT_SIZE);
    if (m_scaleDown)
        Core::ActionManager::unregisterAction(m_scaleDown, TextEditor::Constants::DECREASE_FONT_SIZE);
    if (m_resetScale)
        Core::ActionManager::unregisterAction(m_resetScale, TextEditor::Constants::RESET_FONT_SIZE);
295 296
}

297 298 299 300
void HelpWidget::addSideBar()
{
    QMap<QString, Core::Command *> shortcutMap;
    Core::Command *cmd;
301
    bool supportsNewPages = (m_style == ModeWidget);
302

303 304
    auto contentWindow = new ContentWindow;
    auto contentItem = new Core::SideBarItem(contentWindow, QLatin1String(Constants::HELP_CONTENTS));
305
    contentWindow->setOpenInNewPageActionVisible(supportsNewPages);
306
    contentWindow->setWindowTitle(HelpPlugin::tr(Constants::SB_CONTENTS));
307 308
    connect(contentWindow, &ContentWindow::linkActivated,
            this, &HelpWidget::open);
309
    m_contentsAction = new QAction(tr(Constants::SB_CONTENTS), this);
310 311 312 313 314
    cmd = Core::ActionManager::registerAction(m_contentsAction, Constants::HELP_CONTENTS, m_context->context());
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+C")
                                                                  : tr("Ctrl+Shift+C")));
    shortcutMap.insert(QLatin1String(Constants::HELP_CONTENTS), cmd);

315
    auto indexWindow = new IndexWindow();
Eike Ziller's avatar
Eike Ziller committed
316
    auto indexItem = new Core::SideBarItem(indexWindow, QLatin1String(Constants::HELP_INDEX));
317
    indexWindow->setOpenInNewPageActionVisible(supportsNewPages);
318
    indexWindow->setWindowTitle(HelpPlugin::tr(Constants::SB_INDEX));
319 320 321 322
    connect(indexWindow, &IndexWindow::linkActivated,
            this, &HelpWidget::open);
    connect(indexWindow, &IndexWindow::linksActivated,
        this, &HelpWidget::showTopicChooser);
323
    m_indexAction = new QAction(tr(Constants::SB_INDEX), this);
324 325 326
    cmd = Core::ActionManager::registerAction(m_indexAction, Constants::HELP_INDEX, m_context->context());
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+I")
                                                                  : tr("Ctrl+Shift+I")));
Eike Ziller's avatar
Eike Ziller committed
327
    shortcutMap.insert(QLatin1String(Constants::HELP_INDEX), cmd);
328

Eike Ziller's avatar
Eike Ziller committed
329
    auto bookmarkWidget = new BookmarkWidget(&LocalHelpManager::bookmarkManager());
330
    bookmarkWidget->setWindowTitle(HelpPlugin::tr(Constants::SB_BOOKMARKS));
331
    bookmarkWidget->setOpenInNewPageActionVisible(supportsNewPages);
Eike Ziller's avatar
Eike Ziller committed
332 333 334 335 336 337
    auto bookmarkItem = new Core::SideBarItem(bookmarkWidget,
                                              QLatin1String(Constants::HELP_BOOKMARKS));
    connect(bookmarkWidget, &BookmarkWidget::linkActivated, this, &HelpWidget::setSource);
    m_bookmarkAction = new QAction(tr("Activate Help Bookmarks View"), this);
    cmd = Core::ActionManager::registerAction(m_bookmarkAction, Constants::HELP_BOOKMARKS,
                                              m_context->context());
338
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Alt+Meta+M")
Eike Ziller's avatar
Eike Ziller committed
339 340 341
                                                                  : tr("Ctrl+Shift+B")));
    shortcutMap.insert(QLatin1String(Constants::HELP_BOOKMARKS), cmd);

342 343 344 345 346 347 348 349 350
    auto searchItem = new SearchSideBarItem;
    connect(searchItem, &SearchSideBarItem::linkActivated, this, &HelpWidget::openFromSearch);
    m_searchAction = new QAction(tr("Activate Help Search View"), this);
    cmd = Core::ActionManager::registerAction(m_searchAction, Constants::HELP_SEARCH,
                                              m_context->context());
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+/")
                                                                  : tr("Ctrl+Shift+/")));
    shortcutMap.insert(QLatin1String(Constants::HELP_SEARCH), cmd);

351 352 353
    Core::SideBarItem *openPagesItem = 0;
    if (m_style == ModeWidget) {
        QWidget *openPagesWidget = OpenPagesManager::instance().openPagesWidget();
354
        openPagesWidget->setWindowTitle(HelpPlugin::tr(Constants::SB_OPENPAGES));
355 356 357 358 359 360 361 362 363 364
        openPagesItem = new Core::SideBarItem(openPagesWidget,
                                              QLatin1String(Constants::HELP_OPENPAGES));
        m_openPagesAction = new QAction(tr("Activate Open Help Pages View"), this);
        cmd = Core::ActionManager::registerAction(m_openPagesAction, Constants::HELP_OPENPAGES,
                                                  m_context->context());
        cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+O")
                                                                      : tr("Ctrl+Shift+O")));
        shortcutMap.insert(QLatin1String(Constants::HELP_OPENPAGES), cmd);
    }

365
    QList<Core::SideBarItem *> itemList;
366
    itemList << contentItem << indexItem << bookmarkItem << searchItem;
367 368
    if (openPagesItem)
         itemList << openPagesItem;
369
    m_sideBar = new Core::SideBar(itemList,
370 371
                                  QList<Core::SideBarItem *>() << contentItem
                                  << (openPagesItem ? openPagesItem : indexItem));
372 373 374 375 376
    m_sideBar->setShortcutMap(shortcutMap);
    m_sideBar->setCloseWhenEmpty(true);
    m_sideBarSplitter->insertWidget(0, m_sideBar);
    m_sideBarSplitter->setStretchFactor(0, 0);
    m_sideBarSplitter->setStretchFactor(1, 1);
377 378
    if (m_style != ModeWidget)
        m_sideBar->setVisible(false);
379
    m_sideBar->resize(250, size().height());
380
    m_sideBar->readSettings(Core::ICore::settings(), sideBarSettingsKey());
381 382
    m_sideBarSplitter->setSizes(QList<int>() << m_sideBar->size().width() << 300);

383 384 385
    connect(m_contentsAction, &QAction::triggered, m_sideBar, [this]() {
        m_sideBar->activateItem(QLatin1String(Constants::HELP_CONTENTS));
    });
Eike Ziller's avatar
Eike Ziller committed
386 387
    connect(m_indexAction, &QAction::triggered, m_sideBar, [this]() {
        m_sideBar->activateItem(QLatin1String(Constants::HELP_INDEX));
388
    });
Eike Ziller's avatar
Eike Ziller committed
389 390 391
    connect(m_bookmarkAction, &QAction::triggered, m_sideBar, [this]() {
        m_sideBar->activateItem(QLatin1String(Constants::HELP_BOOKMARKS));
    });
392 393 394
    connect(m_searchAction, &QAction::triggered, m_sideBar, [this]() {
        m_sideBar->activateItem(QLatin1String(Constants::HELP_SEARCH));
    });
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    if (m_openPagesAction) {
        connect(m_openPagesAction, &QAction::triggered, m_sideBar, [this]() {
            m_sideBar->activateItem(QLatin1String(Constants::HELP_OPENPAGES));
        });
    }
}

QString HelpWidget::sideBarSettingsKey() const
{
    switch (m_style) {
    case ModeWidget:
        return QLatin1String(kModeSideBarSettingsKey);
    case ExternalWindow:
        return QLatin1String(kWindowSideBarSettingsKey);
    case SideBarWidget:
        QTC_CHECK(false);
        break;
    }
    return QString();
414 415
}

416 417
HelpViewer *HelpWidget::currentViewer() const
{
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    return qobject_cast<HelpViewer *>(m_viewerStack->currentWidget());
}

void HelpWidget::setCurrentViewer(HelpViewer *viewer)
{
    m_viewerStack->setCurrentWidget(viewer);
    m_backAction->setEnabled(viewer->isBackwardAvailable());
    m_forwardAction->setEnabled(viewer->isForwardAvailable());
    if (m_style == ExternalWindow)
        updateWindowTitle();
    emit sourceChanged(viewer->source());
}

int HelpWidget::currentIndex() const
{
    return m_viewerStack->currentIndex();
}

436
void HelpWidget::addViewer(HelpViewer *viewer)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
{
    m_viewerStack->addWidget(viewer);
    viewer->setFocus(Qt::OtherFocusReason);
    if (m_style == SideBarWidget || m_style == ExternalWindow)
        viewer->setOpenInNewPageActionVisible(false);
    connect(viewer, &HelpViewer::sourceChanged, this, [viewer, this](const QUrl &url) {
        if (currentViewer() == viewer)
            emit sourceChanged(url);
    });
    connect(viewer, &HelpViewer::forwardAvailable, this, [viewer, this](bool available) {
        if (currentViewer() == viewer)
            m_forwardAction->setEnabled(available);
    });
    connect(viewer, &HelpViewer::backwardAvailable, this, [viewer, this](bool available) {
        if (currentViewer() == viewer)
            m_backAction->setEnabled(available);
    });
    connect(viewer, &HelpViewer::printRequested, this, [viewer, this]() {
        print(viewer);
    });
    if (m_style == ExternalWindow)
Montel Laurent's avatar
Montel Laurent committed
458
        connect(viewer, &HelpViewer::titleChanged, this, &HelpWidget::updateWindowTitle);
459

460
    connect(viewer, &HelpViewer::loadFinished, this, &HelpWidget::highlightSearchTerms);
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

    updateCloseButton();
}

void HelpWidget::removeViewerAt(int index)
{
    QWidget *viewerWidget = m_viewerStack->widget(index);
    QTC_ASSERT(viewerWidget, return);
    m_viewerStack->removeWidget(viewerWidget);
    // do not delete, that is done in the model
    // delete viewerWidget;
    if (m_viewerStack->currentWidget())
        setCurrentViewer(qobject_cast<HelpViewer *>(m_viewerStack->currentWidget()));
    updateCloseButton();
}

int HelpWidget::viewerCount() const
{
    return m_viewerStack->count();
}

HelpViewer *HelpWidget::viewerAt(int index) const
{
    return qobject_cast<HelpViewer *>(m_viewerStack->widget(index));
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
void HelpWidget::open(const QUrl &url, bool newPage)
{
    if (newPage)
        OpenPagesManager::instance().createPage(url);
    else
        setSource(url);
}

void HelpWidget::showTopicChooser(const QMap<QString, QUrl> &links,
    const QString &keyword, bool newPage)
{
    TopicChooser tc(this, keyword, links);
    if (tc.exec() == QDialog::Accepted)
        open(tc.link(), newPage);
}

503 504 505 506 507 508
void HelpWidget::activateSideBarItem(const QString &id)
{
    QTC_ASSERT(m_sideBar, return);
    m_sideBar->activateItem(id);
}

509 510 511 512 513 514 515 516
void HelpWidget::setSource(const QUrl &url)
{
    HelpViewer* viewer = currentViewer();
    QTC_ASSERT(viewer, return);
    viewer->setSource(url);
    viewer->setFocus(Qt::OtherFocusReason);
}

517
void HelpWidget::openFromSearch(const QUrl &url, const QStringList &searchTerms, bool newPage)
518
{
519
    m_searchTerms = searchTerms;
520
    if (newPage)
521
        OpenPagesManager::instance().createPage(url);
522 523 524 525 526 527
    else {
        HelpViewer* viewer = currentViewer();
        QTC_ASSERT(viewer, return);
        viewer->setSource(url);
        viewer->setFocus(Qt::OtherFocusReason);
    }
528 529
}

530 531 532 533 534
void HelpWidget::closeEvent(QCloseEvent *)
{
    emit aboutToClose();
}

535 536 537
void HelpWidget::updateBackMenu()
{
    m_backMenu->clear();
538 539
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->addBackHistoryItems(m_backMenu);
540 541 542 543 544
}

void HelpWidget::updateForwardMenu()
{
    m_forwardMenu->clear();
545 546
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->addForwardHistoryItems(m_forwardMenu);
547 548
}

549 550
void HelpWidget::updateWindowTitle()
{
551 552
    QTC_ASSERT(currentViewer(), return);
    const QString pageTitle = currentViewer()->title();
553 554 555 556 557 558
    if (pageTitle.isEmpty())
        setWindowTitle(tr("Help"));
    else
        setWindowTitle(tr("Help - %1").arg(pageTitle));
}

559
void HelpWidget::helpModeButtonClicked()
560
{
561 562
    QTC_ASSERT(currentViewer(), return);
    emit openHelpMode(currentViewer()->source());
563 564
    if (m_style == ExternalWindow)
        close();
565 566
}

567 568 569
void HelpWidget::updateCloseButton()
{
    if (m_style == ModeWidget) {
570
        const bool closeOnReturn = LocalHelpManager::returnOnClose();
571 572 573 574
        m_closeAction->setEnabled(closeOnReturn || m_viewerStack->count() > 1);
    }
}

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
void HelpWidget::goHome()
{
    if (HelpViewer *viewer = currentViewer())
        viewer->home();
}

void HelpWidget::addBookmark()
{
    HelpViewer *viewer = currentViewer();
    QTC_ASSERT(viewer, return);

    const QString &url = viewer->source().toString();
    if (url.isEmpty() || url == Help::Constants::AboutBlank)
        return;

    BookmarkManager *manager = &LocalHelpManager::bookmarkManager();
    manager->showBookmarkDialog(this, viewer->title(), url);
}

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
void HelpWidget::copy()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->copy();
}

void HelpWidget::forward()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->forward();
}

void HelpWidget::backward()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->backward();
}

void HelpWidget::scaleUp()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->scaleUp();
}

void HelpWidget::scaleDown()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->scaleDown();
}

void HelpWidget::resetScale()
{
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->resetScale();
}

void HelpWidget::print(HelpViewer *viewer)
{
    QTC_ASSERT(viewer, return);
    if (!m_printer)
        m_printer = new QPrinter(QPrinter::HighResolution);
    QPrintDialog dlg(m_printer, this);
    dlg.setWindowTitle(tr("Print Documentation"));
    if (!viewer->selectedText().isEmpty())
        dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
    dlg.addEnabledOption(QAbstractPrintDialog::PrintPageRange);
    dlg.addEnabledOption(QAbstractPrintDialog::PrintCollateCopies);

    if (dlg.exec() == QDialog::Accepted)
        viewer->print(m_printer);
}

void HelpWidget::highlightSearchTerms()
{
648 649 650 651 652 653 654
    if (m_searchTerms.isEmpty())
        return;
    HelpViewer *viewer = qobject_cast<HelpViewer *>(sender());
    QTC_ASSERT(viewer, return);
    foreach (const QString& term, m_searchTerms)
        viewer->findText(term, 0, false, true);
    m_searchTerms.clear();
655 656
}

657 658
} // Internal
} // Help