Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
openpagesmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
8#include "helpviewer.h"
12#include "tracer.h"
13#include "../shared/collectionconfiguration.h"
14
15#include <QtWidgets/QApplication>
16#include <QtWidgets/QTreeView>
17
19
20using namespace Qt::StringLiterals;
21
22OpenPagesManager *OpenPagesManager::m_instance = nullptr;
23
24OpenPagesManager *OpenPagesManager::createInstance(QObject *parent,
25 bool defaultCollection, const QUrl &cmdLineUrl)
26{
28 Q_ASSERT(!m_instance);
29 m_instance = new OpenPagesManager(parent, defaultCollection, cmdLineUrl);
30 return m_instance;
31}
32
34{
36 Q_ASSERT(m_instance);
37 return m_instance;
38}
39
40OpenPagesManager::OpenPagesManager(QObject *parent, bool defaultCollection,
41 const QUrl &cmdLineUrl)
42 : QObject(parent)
43 , m_model(new OpenPagesModel(this))
44{
46 m_openPagesWidget = new OpenPagesWidget(m_model);
47 m_openPagesWidget->setFrameStyle(QFrame::NoFrame);
48 connect(m_openPagesWidget, &OpenPagesWidget::setCurrentPage,
49 this, QOverload<const QModelIndex &>::of(&OpenPagesManager::setCurrentPage));
50 connect(m_openPagesWidget, &OpenPagesWidget::closePage,
51 this, QOverload<const QModelIndex &>::of(&OpenPagesManager::closePage));
52 connect(m_openPagesWidget, &OpenPagesWidget::closePagesExcept,
53 this, &OpenPagesManager::closePagesExcept);
54
55 m_openPagesSwitcher = new OpenPagesSwitcher(m_model);
56 connect(m_openPagesSwitcher, &OpenPagesSwitcher::closePage,
57 this, QOverload<const QModelIndex &>::of(&OpenPagesManager::closePage));
58 connect(m_openPagesSwitcher, &OpenPagesSwitcher::setCurrentPage,
59 this, QOverload<const QModelIndex &>::of(&OpenPagesManager::setCurrentPage));
60
61 setupInitialPages(defaultCollection, cmdLineUrl);
62}
63
64OpenPagesManager ::~OpenPagesManager()
65{
67 m_instance = nullptr;
68 delete m_openPagesSwitcher;
69}
70
72{
74 return m_model->rowCount();
75}
76
77void OpenPagesManager::setupInitialPages(bool defaultCollection,
78 const QUrl &cmdLineUrl)
79{
81 if (cmdLineUrl.isValid()) {
82 createPage(cmdLineUrl);
83 return;
84 }
85
87 int initialPage = 0;
88 switch (helpEngine.startOption()) {
89 case ShowHomePage:
90 m_model->addPage(helpEngine.homePage());
91 break;
92 case ShowBlankPage:
93 m_model->addPage(QUrl("about:blank"_L1));
94 break;
95 case ShowLastPages: {
96 const QStringList &lastShownPageList = helpEngine.lastShownPages();
97 const int pageCount = lastShownPageList.size();
98 if (pageCount == 0) {
99 if (defaultCollection)
100 m_helpPageViewer = m_model->addPage(QUrl("help"_L1));
101 else
102 m_model->addPage(QUrl("about:blank"_L1));
103 } else {
104 QStringList zoomFactors = helpEngine.lastZoomFactors();
105 while (zoomFactors.size() < pageCount)
106 zoomFactors.append(CollectionConfiguration::DefaultZoomFactor);
107 initialPage = helpEngine.lastTabPage();
108 if (initialPage >= pageCount) {
109 qWarning("Initial page set to %d, maximum possible value is %d",
110 initialPage, pageCount - 1);
111 initialPage = 0;
112 }
113 for (int curPage = 0; curPage < pageCount; ++curPage) {
114 const QString &curFile = lastShownPageList.at(curPage);
115 if (helpEngine.findFile(curFile).isValid()
116 || curFile == "about:blank"_L1) {
117 m_model->addPage(curFile, zoomFactors.at(curPage).toFloat());
118 } else if (curPage <= initialPage && initialPage > 0)
119 --initialPage;
120 }
121 }
122 break;
123 }
124 default:
125 Q_ASSERT(0);
126 }
127
128 if (m_model->rowCount() == 0)
129 m_model->addPage(helpEngine.homePage());
130 for (int i = 0; i < m_model->rowCount(); ++i)
131 CentralWidget::instance()->addPage(m_model->pageAt(i));
132 setCurrentPage((initialPage >= m_model->rowCount())
133 ? m_model->rowCount() - 1 : initialPage);
134 m_openPagesSwitcher->selectCurrentPage();
135}
136
138{
140 return createPage(QUrl("about:blank"_L1));
141}
142
144{
146 Q_ASSERT(m_model->rowCount() > 1);
147 const QModelIndexList selectedIndexes
148 = m_openPagesWidget->selectionModel()->selectedRows();
149 if (selectedIndexes.isEmpty())
150 return;
151 Q_ASSERT(selectedIndexes.size() == 1);
152 removePage(selectedIndexes.first().row());
153}
154
155HelpViewer *OpenPagesManager::createPage(const QUrl &url, bool fromSearch)
156{
158 if (HelpViewer::launchWithExternalApp(url))
159 return nullptr;
160
161 emit aboutToAddPage();
162
163 m_model->addPage(url);
164 const int index = m_model->rowCount() - 1;
165 HelpViewer * const page = m_model->pageAt(index);
166 CentralWidget::instance()->addPage(page, fromSearch);
167 setCurrentPage(index);
168
169 emit pageAdded(index);
170 return page;
171}
172
174{
176 return createPage(url, true);
177}
178
180{
182 for (int i = 0; i < m_model->rowCount(); ++i) {
183 if (m_model->pageAt(i) == viewer) {
184 removePage(i);
185 break;
186 }
187 }
188}
189
190void OpenPagesManager::closePage(const QModelIndex &index)
191{
193 if (index.isValid())
194 removePage(index.row());
195}
196
197void OpenPagesManager::closePages(const QString &nameSpace)
198{
200 closeOrReloadPages(nameSpace, false);
201}
202
203void OpenPagesManager::reloadPages(const QString &nameSpace)
204{
206 closeOrReloadPages(nameSpace, true);
207 m_openPagesWidget->selectCurrentPage();
208}
209
210void OpenPagesManager::closeOrReloadPages(const QString &nameSpace, bool tryReload)
211{
213 for (int i = m_model->rowCount() - 1; i >= 0; --i) {
214 HelpViewer *page = m_model->pageAt(i);
215 if (page->source().host() != nameSpace)
216 continue;
217 if (tryReload && HelpEngineWrapper::instance().findFile(page->source()).isValid())
218 page->reload();
219 else if (m_model->rowCount() == 1)
220 page->setSource(QUrl("about:blank"_L1));
221 else
222 removePage(i);
223 }
224}
225
226bool OpenPagesManager::pagesOpenForNamespace(const QString &nameSpace) const
227{
229 for (int i = 0; i < m_model->rowCount(); ++i)
230 if (m_model->pageAt(i)->source().host() == nameSpace)
231 return true;
232 return false;
233}
234
235void OpenPagesManager::setCurrentPage(const QModelIndex &index)
236{
238 if (index.isValid())
239 setCurrentPage(index.row());
240}
241
243{
245 setCurrentPage(m_model->pageAt(index));
246}
247
249{
250 if (m_helpPageViewer)
251 m_helpPageViewer->reload();
252}
253
255{
257 CentralWidget::instance()->setCurrentPage(page);
258 m_openPagesWidget->selectCurrentPage();
259}
260
261void OpenPagesManager::removePage(int index)
262{
264 emit aboutToClosePage(index);
265
266 CentralWidget::instance()->removePage(index);
267 m_model->removePage(index);
268 m_openPagesWidget->selectCurrentPage();
269
270 emit pageClosed();
271}
272
273
274void OpenPagesManager::closePagesExcept(const QModelIndex &index)
275{
277 if (!index.isValid())
278 return;
279
280 int i = 0;
281 HelpViewer *viewer = m_model->pageAt(index.row());
282 while (m_model->rowCount() > 1) {
283 if (m_model->pageAt(i) != viewer)
284 removePage(i);
285 else
286 ++i;
287 }
288}
289
290QAbstractItemView *OpenPagesManager::openPagesWidget() const
291{
293 return m_openPagesWidget;
294}
295
297{
299 nextOrPreviousPage(1);
300}
301
303{
305 if (!m_openPagesSwitcher->isVisible()) {
306 m_openPagesSwitcher->selectCurrentPage();
307 m_openPagesSwitcher->gotoNextPage();
308 showSwitcherOrSelectPage();
309 } else {
310 m_openPagesSwitcher->gotoNextPage();
311 }
312}
313
315{
317 nextOrPreviousPage(-1);
318}
319
321{
323 if (!m_openPagesSwitcher->isVisible()) {
324 m_openPagesSwitcher->selectCurrentPage();
325 m_openPagesSwitcher->gotoPreviousPage();
326 showSwitcherOrSelectPage();
327 } else {
328 m_openPagesSwitcher->gotoPreviousPage();
329 }
330}
331
332void OpenPagesManager::nextOrPreviousPage(int offset)
333{
336 + m_model->rowCount()) % m_model->rowCount());
337}
338
339void OpenPagesManager::showSwitcherOrSelectPage() const
340{
342 if (QApplication::keyboardModifiers() != Qt::NoModifier) {
343 const int width = CentralWidget::instance()->width();
344 const int height = CentralWidget::instance()->height();
345 const QPoint p(CentralWidget::instance()->mapToGlobal(QPoint(0, 0)));
346 m_openPagesSwitcher->move((width - m_openPagesSwitcher->width()) / 2 + p.x(),
347 (height - m_openPagesSwitcher->height()) / 2 + p.y());
348 m_openPagesSwitcher->setVisible(true);
349 } else {
350 m_openPagesSwitcher->selectAndHide();
351 }
352}
353
354QT_END_NAMESPACE
int currentIndex() const
static CentralWidget * instance()
static HelpEngineWrapper & instance()
void reload()
QUrl source() const
QAbstractItemView * openPagesWidget() const
void setCurrentPage(HelpViewer *page)
bool pagesOpenForNamespace(const QString &nameSpace) const
HelpViewer * createNewPageFromSearch(const QUrl &url)
void closePage(HelpViewer *page)
void reloadPages(const QString &nameSpace)
HelpViewer * createBlankPage()
static OpenPagesManager * instance()
void setCurrentPage(int index)
void closePages(const QString &nameSpace)
void removePage(int index)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
HelpViewer * pageAt(int index) const
void setVisible(bool visible) override
void closePagesExcept(const QModelIndex &index)
\inmodule QtCore
@ ShowLastPages
@ ShowHomePage
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34