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
contentwindow.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"
10#include "tracer.h"
11
12#include <QtWidgets/QLayout>
13#include <QtGui/QFocusEvent>
14#include <QtWidgets/QMenu>
15
16#include <QtHelp/QHelpContentWidget>
17
19
20ContentWindow::ContentWindow()
21 : m_contentWidget(HelpEngineWrapper::instance().contentWidget())
22 , m_expandDepth(-2)
23{
25 m_contentWidget->viewport()->installEventFilter(this);
26 m_contentWidget->setContextMenuPolicy(Qt::CustomContextMenu);
27
28 QVBoxLayout *layout = new QVBoxLayout(this);
29 layout->setContentsMargins(4, 4, 4, 4);
30 layout->addWidget(m_contentWidget);
31
32 connect(m_contentWidget, &QWidget::customContextMenuRequested,
33 this, &ContentWindow::showContextMenu);
34 connect(m_contentWidget, &QHelpContentWidget::linkActivated,
35 this, &ContentWindow::linkActivated);
36
37 QHelpContentModel *contentModel =
38 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
39 connect(contentModel, &QHelpContentModel::contentsCreated,
40 this, &ContentWindow::expandTOC);
41}
42
47
48bool ContentWindow::syncToContent(const QUrl& url)
49{
51 QModelIndex idx = m_contentWidget->indexOf(url);
52 if (!idx.isValid())
53 return false;
54 m_contentWidget->setCurrentIndex(idx);
55 m_contentWidget->scrollTo(idx);
56 return true;
57}
58
59void ContentWindow::expandTOC()
60{
62 Q_ASSERT(m_expandDepth >= -2);
63 if (m_expandDepth > -2) {
64 expandToDepth(m_expandDepth);
65 m_expandDepth = -2;
66 }
67}
68
70{
72 Q_ASSERT(depth >= -2);
73 m_expandDepth = depth;
74 if (depth == -1)
75 m_contentWidget->expandAll();
76 else if (depth == 0)
77 m_contentWidget->collapseAll();
78 else
79 m_contentWidget->expandToDepth(depth - 1);
80}
81
82void ContentWindow::focusInEvent(QFocusEvent *e)
83{
85 if (e->reason() != Qt::MouseFocusReason)
86 m_contentWidget->setFocus();
87}
88
89void ContentWindow::keyPressEvent(QKeyEvent *e)
90{
92 if (e->key() == Qt::Key_Escape)
93 emit escapePressed();
94}
95
96bool ContentWindow::eventFilter(QObject *o, QEvent *e)
97{
99 if (m_contentWidget && o == m_contentWidget->viewport()
100 && e->type() == QEvent::MouseButtonRelease) {
101 QMouseEvent *me = static_cast<QMouseEvent*>(e);
102 const QModelIndex &index = m_contentWidget->indexAt(me->pos());
103 if (!index.isValid())
104 return QWidget::eventFilter(o, e);
105
106 const Qt::MouseButtons button = me->button();
107 QItemSelectionModel *sm = m_contentWidget->selectionModel();
108 if (sm->isSelected(index)) {
109 if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier))
110 || (button == Qt::MiddleButton)) {
111 QHelpContentModel *contentModel =
112 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
113 if (contentModel) {
114 QHelpContentItem *itm = contentModel->contentItemAt(index);
115 if (itm && HelpViewer::canOpenPage(itm->url().path()))
116 OpenPagesManager::instance()->createPage(itm->url());
117 }
118 } else if (button == Qt::LeftButton) {
119 itemClicked(index);
120 }
121 }
122 }
123 return QWidget::eventFilter(o, e);
124}
125
126
127void ContentWindow::showContextMenu(const QPoint &pos)
128{
130 if (!m_contentWidget->indexAt(pos).isValid())
131 return;
132
133 QHelpContentModel *contentModel =
134 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
135 QHelpContentItem *itm =
136 contentModel->contentItemAt(m_contentWidget->currentIndex());
137
138 QMenu menu;
139 QAction *curTab = menu.addAction(tr("Open Link"));
140 QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
141 if (!HelpViewer::canOpenPage(itm->url().path()))
142 newTab->setEnabled(false);
143
144 menu.move(m_contentWidget->mapToGlobal(pos));
145
146 QAction *action = menu.exec();
147 if (curTab == action)
148 emit linkActivated(itm->url());
149 else if (newTab == action)
150 OpenPagesManager::instance()->createPage(itm->url());
151}
152
153void ContentWindow::itemClicked(const QModelIndex &index)
154{
156 QHelpContentModel *contentModel =
157 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
158
159 if (contentModel) {
160 if (QHelpContentItem *itm = contentModel->contentItemAt(index)) {
161 const QUrl &url = itm->url();
162 if (url != CentralWidget::instance()->currentSource())
163 emit linkActivated(url);
164 }
165 }
166}
167
168QT_END_NAMESPACE
static CentralWidget * instance()
void focusInEvent(QFocusEvent *e) override
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
void keyPressEvent(QKeyEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
void expandToDepth(int depth)
bool eventFilter(QObject *o, QEvent *e) override
Filters events if this object has been installed as an event filter for the watched object.
~ContentWindow() override
static OpenPagesManager * instance()
\inmodule QtCore
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34