helpwidget.cpp 21.4 KB
Newer Older
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
/****************************************************************************
**
** 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 "helpwidget.h"

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

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/findplaceholder.h>
46
#include <coreplugin/helpmanager.h>
47 48
#include <coreplugin/minisplitter.h>
#include <coreplugin/sidebar.h>
49
#include <texteditor/texteditorconstants.h>
50
#include <utils/qtcassert.h>
51 52 53
#include <utils/styledbar.h>

#include <QHBoxLayout>
54 55 56
#include <QHelpEngine>
#include <QHelpSearchEngine>
#include <QHelpSearchQuery>
57
#include <QMenu>
58 59 60
#include <QPrinter>
#include <QPrintDialog>
#include <QStackedWidget>
61 62
#include <QToolButton>

Eike Ziller's avatar
Eike Ziller committed
63
static const char kSideBarSettingsKey[] = "Help/WindowSideBar";
64 65 66 67

namespace Help {
namespace Internal {

68 69
HelpWidget::HelpWidget(const Core::Context &context, WidgetStyle style, QWidget *parent) :
    QWidget(parent),
70
    m_style(style),
71
    m_toggleSideBarAction(0),
72 73 74
    m_switchToHelp(0),
    m_filterComboBox(0),
    m_closeAction(0),
75 76
    m_scaleUp(0),
    m_scaleDown(0),
77
    m_resetScale(0),
78 79 80
    m_printer(0),
    m_sideBar(0),
    m_indexAction(0)
81
{
82 83 84 85 86 87 88 89 90 91
    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);

92
    Utils::StyledBar *toolBar = new Utils::StyledBar();
93 94 95
    QHBoxLayout *layout = new QHBoxLayout(toolBar);
    layout->setSpacing(0);
    layout->setMargin(0);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

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

114
    Core::Command *cmd;
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
    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

        m_toggleSideBarAction = new QAction(QIcon(QLatin1String(Core::Constants::ICON_TOGGLE_SIDEBAR)),
                                            tr(Core::Constants::TR_SHOW_SIDEBAR), toolBar);
        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) {
                    m_toggleSideBarAction->setText(checked ? tr(Core::Constants::TR_HIDE_SIDEBAR)
                                                           : tr(Core::Constants::TR_SHOW_SIDEBAR));
                });
        addSideBar();
        connect(m_toggleSideBarAction, &QAction::triggered, m_sideBar, &Core::SideBar::setVisible);
        connect(m_sideBar, &Core::SideBar::sideBarClosed, m_toggleSideBarAction, [this]() {
            m_toggleSideBarAction->setChecked(false);
        });
        layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_toggleSideBarAction, cmd));
    }
142

143 144 145 146
    if (style != ModeWidget) {
        m_switchToHelp = new QAction(tr("Go to Help Mode"), toolBar);
        cmd = Core::ActionManager::registerAction(m_switchToHelp, Constants::CONTEXT_HELP, context);
        connect(m_switchToHelp, SIGNAL(triggered()), this, SLOT(helpModeButtonClicked()));
147
        layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_switchToHelp, cmd));
148
    }
149

150 151 152 153
    m_homeAction = new QAction(QIcon(QLatin1String(":/help/images/home.png")),
        tr("Home"), this);
    cmd = Core::ActionManager::registerAction(m_homeAction, Constants::HELP_HOME, context);
    connect(m_homeAction, &QAction::triggered, this, &HelpWidget::goHome);
154
    layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_homeAction, cmd));
155

156
    m_backAction = new QAction(QIcon(QLatin1String(":/help/images/previous.png")),
157
        tr("Back"), toolBar);
158
    connect(m_backAction, &QAction::triggered, this, &HelpWidget::backward);
159 160
    m_backMenu = new QMenu(toolBar);
    connect(m_backMenu, SIGNAL(aboutToShow()), this, SLOT(updateBackMenu()));
161 162 163
    m_backAction->setMenu(m_backMenu);
    cmd = Core::ActionManager::registerAction(m_backAction, Constants::HELP_PREVIOUS, context);
    cmd->setDefaultKeySequence(QKeySequence::Back);
164 165 166
    button = Core::Command::toolButtonWithAppendedShortcut(m_backAction, cmd);
    button->setPopupMode(QToolButton::DelayedPopup);
    layout->addWidget(button);
167 168

    m_forwardAction = new QAction(QIcon(QLatin1String(":/help/images/next.png")),
169
        tr("Forward"), toolBar);
170
    connect(m_forwardAction, &QAction::triggered, this, &HelpWidget::forward);
171 172
    m_forwardMenu = new QMenu(toolBar);
    connect(m_forwardMenu, SIGNAL(aboutToShow()), this, SLOT(updateForwardMenu()));
173 174 175
    m_forwardAction->setMenu(m_forwardMenu);
    cmd = Core::ActionManager::registerAction(m_forwardAction, Constants::HELP_NEXT, context);
    cmd->setDefaultKeySequence(QKeySequence::Forward);
176 177 178
    button = Core::Command::toolButtonWithAppendedShortcut(m_forwardAction, cmd);
    button->setPopupMode(QToolButton::DelayedPopup);
    layout->addWidget(button);
179

180 181
    m_addBookmarkAction = new QAction(QIcon(QLatin1String(":/help/images/bookmark.png")),
        tr("Add Bookmark"), this);
Eike Ziller's avatar
Eike Ziller committed
182
    cmd = Core::ActionManager::registerAction(m_addBookmarkAction, Constants::HELP_ADDBOOKMARK, context);
183 184 185
    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));
186
    layout->addWidget(Core::Command::toolButtonWithAppendedShortcut(m_addBookmarkAction, cmd));
187

188 189 190
    if (style == ModeWidget) {
        layout->addWidget(new Utils::StyledSeparator(toolBar));
        layout->addWidget(OpenPagesManager::instance().openPagesComboBox(), 10);
191 192 193 194
    } else {
        layout->addWidget(new QLabel(), 10);
    }
    if (style != SideBarWidget) {
195 196 197
        m_filterComboBox = new QComboBox;
        m_filterComboBox->setMinimumContentsLength(15);
        m_filterComboBox->setModel(LocalHelpManager::filterModel());
198
        m_filterComboBox->setCurrentIndex(LocalHelpManager::filterIndex());
199 200 201 202 203 204 205
        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);
    }

206 207
    layout->addStretch();

208 209 210
    m_printAction = new QAction(this);
    Core::ActionManager::registerAction(m_printAction, Core::Constants::PRINT, context);
    connect(m_printAction, &QAction::triggered, this, [this]() { print(currentViewer()); });
211

212
    m_copy = new QAction(this);
213
    Core::ActionManager::registerAction(m_copy, Core::Constants::COPY, context);
214
    connect(m_copy, &QAction::triggered, this, &HelpWidget::copy);
215 216 217 218

    Core::ActionContainer *advancedMenu = Core::ActionManager::actionContainer(Core::Constants::M_EDIT_ADVANCED);
    QTC_CHECK(advancedMenu);
    if (advancedMenu) {
219
        // reuse TextEditor constants to avoid a second pair of menu actions
220 221
        m_scaleUp = new QAction(tr("Increase Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleUp, TextEditor::Constants::INCREASE_FONT_SIZE,
222
                                                  context);
223
        connect(m_scaleUp, &QAction::triggered, this, &HelpWidget::scaleUp);
224 225
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

226 227
        m_scaleDown = new QAction(tr("Decrease Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleDown, TextEditor::Constants::DECREASE_FONT_SIZE,
228
                                                  context);
229
        connect(m_scaleDown, &QAction::triggered, this, &HelpWidget::scaleDown);
230 231
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

232 233
        m_resetScale = new QAction(tr("Reset Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_resetScale, TextEditor::Constants::RESET_FONT_SIZE,
234
                                                  context);
235
        connect(m_resetScale, &QAction::triggered, this, &HelpWidget::resetScale);
236 237
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);
    }
238

239
    if (style != ExternalWindow) {
240
        m_closeAction = new QAction(QIcon(QLatin1String(Core::Constants::ICON_BUTTON_CLOSE)),
241
            QString(), toolBar);
242
        connect(m_closeAction, SIGNAL(triggered()), this, SIGNAL(closeButtonClicked()));
243 244 245
        button = new QToolButton;
        button->setDefaultAction(m_closeAction);
        layout->addWidget(button);
246 247 248 249 250 251
    }

    if (style != ModeWidget) {
        HelpViewer *viewer = HelpPlugin::createHelpViewer(qreal(0.0));
        addViewer(viewer);
        setCurrentViewer(viewer);
252 253 254 255 256
    }
}

HelpWidget::~HelpWidget()
{
257 258 259 260
    if (m_sideBar) {
        m_sideBar->saveSettings(Core::ICore::settings(), QLatin1String(kSideBarSettingsKey));
        Core::ActionManager::unregisterAction(m_indexAction, Constants::HELP_INDEX);
    }
261
    Core::ICore::removeContextObject(m_context);
262
    Core::ActionManager::unregisterAction(m_copy, Core::Constants::COPY);
263
    Core::ActionManager::unregisterAction(m_printAction, Core::Constants::PRINT);
264 265
    if (m_toggleSideBarAction)
        Core::ActionManager::unregisterAction(m_toggleSideBarAction, Core::Constants::TOGGLE_SIDEBAR);
266 267
    if (m_switchToHelp)
        Core::ActionManager::unregisterAction(m_switchToHelp, Constants::CONTEXT_HELP);
268
    Core::ActionManager::unregisterAction(m_homeAction, Constants::HELP_HOME);
269 270
    Core::ActionManager::unregisterAction(m_forwardAction, Constants::HELP_NEXT);
    Core::ActionManager::unregisterAction(m_backAction, Constants::HELP_PREVIOUS);
Eike Ziller's avatar
Eike Ziller committed
271
    Core::ActionManager::unregisterAction(m_addBookmarkAction, Constants::HELP_ADDBOOKMARK);
272 273 274 275 276 277
    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);
278 279
}

280 281 282 283 284 285
void HelpWidget::addSideBar()
{
    QMap<QString, Core::Command *> shortcutMap;
    Core::Command *cmd;

    auto indexWindow = new IndexWindow();
Eike Ziller's avatar
Eike Ziller committed
286
    auto indexItem = new Core::SideBarItem(indexWindow, QLatin1String(Constants::HELP_INDEX));
287 288 289 290 291 292
    indexWindow->setOpenInNewPageActionVisible(false);
    indexWindow->setWindowTitle(tr(Constants::SB_INDEX));
    connect(indexWindow, &IndexWindow::linkActivated,
            this, &HelpWidget::open);
    connect(indexWindow, &IndexWindow::linksActivated,
        this, &HelpWidget::showTopicChooser);
Eike Ziller's avatar
Eike Ziller committed
293
    m_indexAction = new QAction(tr("Activate Help Index View"), this);
294 295 296
    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
297
    shortcutMap.insert(QLatin1String(Constants::HELP_INDEX), cmd);
298 299 300 301 302 303 304 305 306 307 308 309 310 311

    QList<Core::SideBarItem *> itemList;
    itemList << indexItem;
    m_sideBar = new Core::SideBar(itemList, QList<Core::SideBarItem *>() << indexItem);
    m_sideBar->setShortcutMap(shortcutMap);
    m_sideBar->setCloseWhenEmpty(true);
    m_sideBarSplitter->insertWidget(0, m_sideBar);
    m_sideBarSplitter->setStretchFactor(0, 0);
    m_sideBarSplitter->setStretchFactor(1, 1);
    m_sideBar->setVisible(false);
    m_sideBar->readSettings(Core::ICore::settings(), QLatin1String(kSideBarSettingsKey));
    m_sideBarSplitter->setSizes(QList<int>() << m_sideBar->size().width() << 300);
    m_toggleSideBarAction->setChecked(m_sideBar->isVisibleTo(this));

Eike Ziller's avatar
Eike Ziller committed
312 313
    connect(m_indexAction, &QAction::triggered, m_sideBar, [this]() {
        m_sideBar->activateItem(QLatin1String(Constants::HELP_INDEX));
314 315 316
    });
}

317 318
HelpViewer *HelpWidget::currentViewer() const
{
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
    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();
}

void HelpWidget::addViewer(HelpViewer *viewer, bool highlightSearchTerms)
{
    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)
        connect(viewer, SIGNAL(titleChanged()), this, SLOT(updateWindowTitle()));

    if (highlightSearchTerms)
        connect(viewer, &HelpViewer::loadFinished, this, &HelpWidget::highlightSearchTerms);

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

void HelpWidget::setViewerFont(const QFont &font)
{
    for (int i = 0; i < m_viewerStack->count(); ++i) {
        HelpViewer *viewer = qobject_cast<HelpViewer *>(m_viewerStack->widget(i));
        QTC_ASSERT(viewer, continue);
        viewer->setFont(font);
    }
}

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

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

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
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);
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
void HelpWidget::setSource(const QUrl &url)
{
    HelpViewer* viewer = currentViewer();
    QTC_ASSERT(viewer, return);
    viewer->setSource(url);
    viewer->setFocus(Qt::OtherFocusReason);
}

void HelpWidget::setSourceFromSearch(const QUrl &url)
{
    HelpViewer* viewer = currentViewer();
    QTC_ASSERT(viewer, return);
    connect(viewer, &HelpViewer::loadFinished, this, &HelpWidget::highlightSearchTerms);
    viewer->setSource(url);
    viewer->setFocus(Qt::OtherFocusReason);
429 430
}

431 432 433 434 435
void HelpWidget::closeEvent(QCloseEvent *)
{
    emit aboutToClose();
}

436 437 438
void HelpWidget::updateBackMenu()
{
    m_backMenu->clear();
439 440
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->addBackHistoryItems(m_backMenu);
441 442 443 444 445
}

void HelpWidget::updateForwardMenu()
{
    m_forwardMenu->clear();
446 447
    QTC_ASSERT(currentViewer(), return);
    currentViewer()->addForwardHistoryItems(m_forwardMenu);
448 449
}

450 451
void HelpWidget::updateWindowTitle()
{
452 453
    QTC_ASSERT(currentViewer(), return);
    const QString pageTitle = currentViewer()->title();
454 455 456 457 458 459
    if (pageTitle.isEmpty())
        setWindowTitle(tr("Help"));
    else
        setWindowTitle(tr("Help - %1").arg(pageTitle));
}

460
void HelpWidget::helpModeButtonClicked()
461
{
462 463
    QTC_ASSERT(currentViewer(), return);
    emit openHelpMode(currentViewer()->source());
464 465
    if (m_style == ExternalWindow)
        close();
466 467
}

468 469 470 471 472 473 474 475 476
void HelpWidget::updateCloseButton()
{
    if (m_style == ModeWidget) {
        const bool closeOnReturn = Core::HelpManager::customValue(QLatin1String("ReturnOnClose"),
            false).toBool();
        m_closeAction->setEnabled(closeOnReturn || m_viewerStack->count() > 1);
    }
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
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);
}

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
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()
{
    if (HelpViewer *viewer = qobject_cast<HelpViewer *>(sender())) {
        QHelpSearchEngine *searchEngine =
            LocalHelpManager::helpEngine().searchEngine();
        QList<QHelpSearchQuery> queryList = searchEngine->query();

        QStringList terms;
        foreach (const QHelpSearchQuery &query, queryList) {
            switch (query.fieldName) {
                default: break;
                case QHelpSearchQuery::ALL: {
                case QHelpSearchQuery::PHRASE:
                case QHelpSearchQuery::DEFAULT:
                case QHelpSearchQuery::ATLEAST:
                    foreach (QString term, query.wordList)
                        terms.append(term.remove(QLatin1Char('"')));
                }
            }
        }

        foreach (const QString& term, terms)
            viewer->findText(term, 0, false, true);
        disconnect(viewer, &HelpViewer::loadFinished, this, &HelpWidget::highlightSearchTerms);
    }
}

575 576
} // Internal
} // Help