helpwidget.cpp 8.66 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 "helpconstants.h"
33
34
35
36
37
38
39
40
41
#include "helpplugin.h"
#include "helpviewer.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/findplaceholder.h>
#include <texteditor/texteditorconstants.h>
42
#include <utils/qtcassert.h>
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <utils/styledbar.h>

#include <QHBoxLayout>
#include <QMenu>
#include <QToolButton>

static QToolButton *toolButton(QAction *action)
{
    QToolButton *button = new QToolButton;
    button->setDefaultAction(action);
    button->setPopupMode(QToolButton::DelayedPopup);
    return button;
}

namespace Help {
namespace Internal {

60
61
62
63
HelpWidget::HelpWidget(const Core::Context &context, WidgetStyle style, QWidget *parent) :
    QWidget(parent),
    m_scaleUp(0),
    m_scaleDown(0),
64
65
    m_resetScale(0),
    m_style(style)
66
67
68
{
    Utils::StyledBar *toolBar = new Utils::StyledBar();

69
70
71
    m_switchToHelp = new QAction(tr("Go to Help Mode"), toolBar);
    connect(m_switchToHelp, SIGNAL(triggered()), this, SLOT(helpModeButtonClicked()));
    updateHelpModeButtonToolTip();
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    QAction *back = new QAction(QIcon(QLatin1String(":/help/images/previous.png")),
        tr("Back"), toolBar);
    m_backMenu = new QMenu(toolBar);
    connect(m_backMenu, SIGNAL(aboutToShow()), this, SLOT(updateBackMenu()));
    back->setMenu(m_backMenu);
    QAction *forward = new QAction(QIcon(QLatin1String(":/help/images/next.png")),
        tr("Forward"), toolBar);
    m_forwardMenu = new QMenu(toolBar);
    connect(m_forwardMenu, SIGNAL(aboutToShow()), this, SLOT(updateForwardMenu()));
    forward->setMenu(m_forwardMenu);

    QHBoxLayout *layout = new QHBoxLayout(toolBar);
    layout->setSpacing(0);
    layout->setMargin(0);

88
    layout->addWidget(toolButton(m_switchToHelp));
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
    layout->addWidget(toolButton(back));
    layout->addWidget(toolButton(forward));
    layout->addStretch();

    m_viewer = HelpPlugin::createHelpViewer(qreal(0.0));

    QVBoxLayout *vLayout = new QVBoxLayout(this);
    vLayout->setMargin(0);
    vLayout->setSpacing(0);
    vLayout->addWidget(toolBar);
    vLayout->addWidget(m_viewer);
    Core::FindToolBarPlaceHolder *fth = new Core::FindToolBarPlaceHolder(this);
    vLayout->addWidget(fth);

    setFocusProxy(m_viewer);

    Core::IContext *icontext = new Core::IContext(this);
    icontext->setContext(context);
    icontext->setWidget(m_viewer);
    Core::ICore::addContextObject(icontext);

    back->setEnabled(m_viewer->isBackwardAvailable());
    connect(back, SIGNAL(triggered()), m_viewer, SLOT(backward()));
    connect(m_viewer, SIGNAL(backwardAvailable(bool)), back,
        SLOT(setEnabled(bool)));

    forward->setEnabled(m_viewer->isForwardAvailable());
    connect(forward, SIGNAL(triggered()), m_viewer, SLOT(forward()));
    connect(m_viewer, SIGNAL(forwardAvailable(bool)), forward,
        SLOT(setEnabled(bool)));

120
121
122
123
124
125
126
127
    m_copy = new QAction(this);
    Core::Command *cmd = Core::ActionManager::registerAction(m_copy, Core::Constants::COPY, context);
    connect(m_copy, SIGNAL(triggered()), m_viewer, SLOT(copy()));

    m_openHelpMode = new QAction(this);
    cmd = Core::ActionManager::registerAction(m_openHelpMode,
                                              Help::Constants::CONTEXT_HELP,
                                              context);
128
    connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updateHelpModeButtonToolTip()));
129
    connect(m_openHelpMode, SIGNAL(triggered()), this, SLOT(helpModeButtonClicked()));
130
131
132
133

    Core::ActionContainer *advancedMenu = Core::ActionManager::actionContainer(Core::Constants::M_EDIT_ADVANCED);
    QTC_CHECK(advancedMenu);
    if (advancedMenu) {
134
        // reuse TextEditor constants to avoid a second pair of menu actions
135
136
        m_scaleUp = new QAction(tr("Increase Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleUp, TextEditor::Constants::INCREASE_FONT_SIZE,
137
                                                  context);
138
        connect(m_scaleUp, SIGNAL(triggered()), m_viewer, SLOT(scaleUp()));
139
140
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

141
142
        m_scaleDown = new QAction(tr("Decrease Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_scaleDown, TextEditor::Constants::DECREASE_FONT_SIZE,
143
                                                  context);
144
        connect(m_scaleDown, SIGNAL(triggered()), m_viewer, SLOT(scaleDown()));
145
146
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);

147
148
        m_resetScale = new QAction(tr("Reset Font Size"), this);
        cmd = Core::ActionManager::registerAction(m_resetScale, TextEditor::Constants::RESET_FONT_SIZE,
149
                                                  context);
150
        connect(m_resetScale, SIGNAL(triggered()), m_viewer, SLOT(resetScale()));
151
152
        advancedMenu->addAction(cmd, Core::Constants::G_EDIT_FONT);
    }
153
154
155
156
157
158
159
160

    if (style == SideBarWidget) {
        QAction *close = new QAction(QIcon(QLatin1String(Core::Constants::ICON_CLOSE_DOCUMENT)),
            QString(), toolBar);
        connect(close, SIGNAL(triggered()), this, SIGNAL(closeButtonClicked()));
        layout->addWidget(toolButton(close));
        m_viewer->setOpenInNewWindowActionVisible(false);
    } else if (style == ExternalWindow) {
161
162
163
        static int windowId = 0;
        Core::ICore::registerWindow(this,
                                    Core::Context(Core::Id("Help.Window.").withSuffix(++windowId)));
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        setAttribute(Qt::WA_DeleteOnClose);
        setAttribute(Qt::WA_QuitOnClose, false); // don't prevent Qt Creator from closing
        connect(m_viewer, SIGNAL(titleChanged()), this, SLOT(updateWindowTitle()));
        updateWindowTitle();
        m_viewer->setOpenInNewWindowActionVisible(false);
    }
}

HelpWidget::~HelpWidget()
{
    Core::ActionManager::unregisterAction(m_copy, Core::Constants::COPY);
    Core::ActionManager::unregisterAction(m_openHelpMode, Help::Constants::CONTEXT_HELP);
    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);
182
183
184
185
186
187
188
}

HelpViewer *HelpWidget::currentViewer() const
{
    return m_viewer;
}

189
190
191
192
193
void HelpWidget::closeEvent(QCloseEvent *)
{
    emit aboutToClose();
}

194
195
196
197
198
199
200
201
202
203
204
205
void HelpWidget::updateBackMenu()
{
    m_backMenu->clear();
    m_viewer->addBackHistoryItems(m_backMenu);
}

void HelpWidget::updateForwardMenu()
{
    m_forwardMenu->clear();
    m_viewer->addForwardHistoryItems(m_forwardMenu);
}

206
207
208
209
210
211
212
213
214
void HelpWidget::updateWindowTitle()
{
    const QString pageTitle = m_viewer->title();
    if (pageTitle.isEmpty())
        setWindowTitle(tr("Help"));
    else
        setWindowTitle(tr("Help - %1").arg(pageTitle));
}

215
void HelpWidget::helpModeButtonClicked()
216
217
{
    emit openHelpMode(m_viewer->source());
218
219
    if (m_style == ExternalWindow)
        close();
220
221
}

222
223
224
225
226
227
228
void HelpWidget::updateHelpModeButtonToolTip()
{
    Core::Command *cmd = Core::ActionManager::command(Constants::CONTEXT_HELP);
    QTC_ASSERT(cmd, return);
    m_switchToHelp->setToolTip(cmd->stringWithAppendedShortcut(m_switchToHelp->text()));
}

229
230
} // Internal
} // Help