aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/help/openpagesmanager.cpp
blob: 465868bf8e365b1223f8a40979d7ffb7be336301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "openpagesmanager.h"

#include "helpconstants.h"
#include "helptr.h"
#include "helpviewer.h"
#include "helpwidget.h"
#include "localhelpmanager.h"
#include "openpagesswitcher.h"
#include "openpageswidget.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/modemanager.h>

#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/styledbar.h>

#include <QApplication>
#include <QClipboard>
#include <QComboBox>
#include <QMenu>

#include <QHelpEngine>

using namespace Core;

namespace Help::Internal {

// -- OpenPagesManager

OpenPagesManager::OpenPagesManager(HelpWidget *helpWidget)
    : m_helpWidget(helpWidget)
{
    m_comboBox = new QComboBox;
    m_comboBox->setModel(m_helpWidget->model());
    m_comboBox->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(m_comboBox, &QComboBox::activated, m_helpWidget, &HelpWidget::setCurrentIndex);
    connect(m_helpWidget, &HelpWidget::currentIndexChanged, m_comboBox, &QComboBox::setCurrentIndex);
    connect(m_comboBox, &QWidget::customContextMenuRequested,
            this, &OpenPagesManager::openPagesContextMenu);

    m_openPagesSwitcher = new OpenPagesSwitcher(m_helpWidget->model());
    connect(m_openPagesSwitcher, &OpenPagesSwitcher::closePage, this,
        &OpenPagesManager::closePage);
    connect(m_openPagesSwitcher,
            &OpenPagesSwitcher::setCurrentPage,
            m_helpWidget,
            [this](const QModelIndex &index) { m_helpWidget->setCurrentIndex(index.row()); });
    connect(m_helpWidget,
            &HelpWidget::currentIndexChanged,
            m_openPagesSwitcher,
            &OpenPagesSwitcher::selectCurrentPage);
}

OpenPagesManager ::~OpenPagesManager()
{
    delete m_openPagesSwitcher;
}

QWidget *OpenPagesManager::openPagesWidget() const
{
    if (!m_openPagesWidget) {
        m_openPagesWidget = new OpenPagesWidget(m_helpWidget->model());
        connect(m_openPagesWidget,
                &OpenPagesWidget::setCurrentPage,
                m_helpWidget,
                [this](const QModelIndex &index) { m_helpWidget->setCurrentIndex(index.row()); });
        connect(m_helpWidget,
                &HelpWidget::currentIndexChanged,
                m_openPagesWidget,
                &OpenPagesWidget::selectCurrentPage);
        connect(m_openPagesWidget, &OpenPagesWidget::closePage,
                this, &OpenPagesManager::closePage);
        connect(m_openPagesWidget, &OpenPagesWidget::closePagesExcept,
                this, &OpenPagesManager::closePagesExcept);
    }
    return m_openPagesWidget;
}

QComboBox *OpenPagesManager::openPagesComboBox() const
{
    return m_comboBox;
}

QStringList splitString(const QVariant &value)
{
    using namespace Help::Constants;
    return value.toString().split(ListSeparator, Qt::SkipEmptyParts);
}

void OpenPagesManager::setupInitialPages()
{
    const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
    const LocalHelpManager::StartOption option = LocalHelpManager::startOption();
    QString homePage = LocalHelpManager::homePage();

    int initialPage = 0;
    switch (option) {
    case LocalHelpManager::ShowHomePage:
        m_helpWidget->addViewer(homePage);
        break;

    case LocalHelpManager::ShowBlankPage:
        m_helpWidget->addViewer(QUrl(Help::Constants::AboutBlank));
        break;

    case LocalHelpManager::ShowLastPages: {
        const QStringList &lastShownPageList = LocalHelpManager::lastShownPages();
        const int pageCount = lastShownPageList.count();

        if (pageCount > 0) {
            initialPage = LocalHelpManager::lastSelectedTab();
            for (int curPage = 0; curPage < pageCount; ++curPage) {
                const QString &curFile = lastShownPageList.at(curPage);
                if (engine.findFile(curFile).isValid() || curFile == Help::Constants::AboutBlank) {
                    m_helpWidget->addViewer(curFile);
                } else if (curPage <= initialPage && initialPage > 0) {
                    --initialPage;
                }
            }
        }
    } break;

    default:
        break;
    }

    if (m_helpWidget->viewerCount() == 0)
        m_helpWidget->addViewer(homePage);

    m_helpWidget->setCurrentIndex(std::max(initialPage, m_helpWidget->viewerCount() - 1));
}

void OpenPagesManager::closeCurrentPage()
{
    if (!m_openPagesWidget)
        return;

    QModelIndexList indexes = m_openPagesWidget->selectionModel()->selectedRows();
    if (indexes.isEmpty())
        return;

    const bool returnOnClose = LocalHelpManager::returnOnClose();

    if (m_helpWidget->viewerCount() == 1 && returnOnClose) {
        ModeManager::activateMode(Core::Constants::MODE_EDIT);
    } else {
        QTC_ASSERT(indexes.count() == 1, return );
        removePage(indexes.first().row());
    }
}

void OpenPagesManager::closePage(const QModelIndex &index)
{
    if (index.isValid())
        removePage(index.row());
}

void OpenPagesManager::closePagesExcept(const QModelIndex &index)
{
    if (index.isValid()) {
        int i = 0;
        HelpViewer *viewer = m_helpWidget->viewerAt(index.row());
        while (m_helpWidget->viewerCount() > 1) {
            if (m_helpWidget->viewerAt(i) != viewer)
                removePage(i);
            else
                i++;
        }
    }
}

void OpenPagesManager::gotoNextPage()
{
    if (!m_openPagesSwitcher->isVisible()) {
        m_openPagesSwitcher->gotoNextPage();
        showTwicherOrSelectPage();
    } else {
        m_openPagesSwitcher->gotoNextPage();
    }
}

void OpenPagesManager::gotoPreviousPage()
{
    if (!m_openPagesSwitcher->isVisible()) {
        m_openPagesSwitcher->gotoPreviousPage();
        showTwicherOrSelectPage();
    } else {
        m_openPagesSwitcher->gotoPreviousPage();
    }
}

// -- private

void OpenPagesManager::removePage(int index)
{
    QTC_ASSERT(index < m_helpWidget->viewerCount(), return );

    m_helpWidget->removeViewerAt(index);
}

void OpenPagesManager::showTwicherOrSelectPage() const
{
    if (QApplication::keyboardModifiers() != Qt::NoModifier) {
        const int width = m_helpWidget->width();
        const int height = m_helpWidget->height();
        const QPoint p(m_helpWidget->mapToGlobal(QPoint(0, 0)));
        m_openPagesSwitcher->move((width - m_openPagesSwitcher->width()) / 2 + p.x(),
            (height - m_openPagesSwitcher->height()) / 2 + p.y());
        m_openPagesSwitcher->setVisible(true);
    } else {
        m_openPagesSwitcher->selectAndHide();
    }
}

void OpenPagesManager::openPagesContextMenu(const QPoint &point)
{
    const QModelIndex &index = m_helpWidget->model()->index(m_comboBox->currentIndex(), 0);
    const QString &fileName = m_helpWidget->model()->data(index, Qt::ToolTipRole).toString();
    if (fileName.isEmpty())
        return;

    QMenu menu;
    menu.addAction(Tr::tr("Copy Full Path to Clipboard"));
    if (menu.exec(m_comboBox->mapToGlobal(point)))
        Utils::setClipboardAndSelection(fileName);
}

} // Help::Internal