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
indexwindow.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
4#include "indexwindow.h"
5
8#include "helpviewer.h"
10#include "topicchooser.h"
11#include "tracer.h"
12
13#include <QtWidgets/QLayout>
14#include <QtWidgets/QLabel>
15#include <QtWidgets/QLineEdit>
16#include <QtGui/QKeyEvent>
17#include <QtWidgets/QMenu>
18#include <QtGui/QContextMenuEvent>
19#include <QtWidgets/QListWidgetItem>
20
21#include <QtHelp/QHelpIndexWidget>
22#include <QtHelp/QHelpEngineCore>
23#include <QtHelp/QHelpLink>
24
26
27using namespace Qt::StringLiterals;
28
29IndexWindow::IndexWindow(QWidget *parent)
30 : QWidget(parent)
31 , m_searchLineEdit(new QLineEdit)
32 , m_indexWidget(HelpEngineWrapper::instance().indexWidget())
33{
35 QVBoxLayout *layout = new QVBoxLayout(this);
36 QLabel *l = new QLabel(tr("&Look for:"));
37 layout->addWidget(l);
38
39 l->setBuddy(m_searchLineEdit);
40 m_searchLineEdit->setClearButtonEnabled(true);
41 connect(m_searchLineEdit, &QLineEdit::textChanged,
42 this, &IndexWindow::filterIndices);
43 m_searchLineEdit->installEventFilter(this);
44 layout->setContentsMargins(4, 4, 4, 4);
45 layout->addWidget(m_searchLineEdit);
46
47 HelpEngineWrapper &helpEngine = HelpEngineWrapper::instance();
48 m_indexWidget->installEventFilter(this);
49 connect(helpEngine.indexModel(), &QHelpIndexModel::indexCreationStarted,
50 this, &IndexWindow::disableSearchLineEdit);
51 connect(helpEngine.indexModel(), &QHelpIndexModel::indexCreated,
52 this, &IndexWindow::enableSearchLineEdit);
53 connect(m_indexWidget, &QHelpIndexWidget::documentActivated,
54 this, [this](const QHelpLink &link) {
55 emit linkActivated(link.url);
56 });
57 connect(m_indexWidget, &QHelpIndexWidget::documentsActivated,
58 this, &IndexWindow::documentsActivated);
59 connect(m_searchLineEdit, &QLineEdit::returnPressed,
60 m_indexWidget, &QHelpIndexWidget::activateCurrentItem);
61 layout->addWidget(m_indexWidget);
62
63 m_indexWidget->viewport()->installEventFilter(this);
64}
65
66IndexWindow::~IndexWindow()
67{
69}
70
71void IndexWindow::filterIndices(const QString &filter)
72{
74 if (filter.contains(u'*'))
75 m_indexWidget->filterIndices(filter, filter);
76 else
77 m_indexWidget->filterIndices(filter, QString());
78}
79
80bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
81{
83 if (obj == m_searchLineEdit && e->type() == QEvent::KeyPress) {
84 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
85 QModelIndex idx = m_indexWidget->currentIndex();
86 switch (ke->key()) {
87 case Qt::Key_Up:
88 idx = m_indexWidget->model()->index(idx.row()-1,
89 idx.column(), idx.parent());
90 if (idx.isValid()) {
91 m_indexWidget->setCurrentIndex(idx);
92 return true;
93 }
94 break;
95 case Qt::Key_Down:
96 idx = m_indexWidget->model()->index(idx.row() + 1,
97 idx.column(), idx.parent());
98 if (idx.isValid()) {
99 m_indexWidget->setCurrentIndex(idx);
100 return true;
101 }
102 break;
103 case Qt::Key_Escape:
104 emit escapePressed();
105 return true;
106 default: ; // stop complaining
107 }
108 } else if (obj == m_indexWidget && e->type() == QEvent::ContextMenu) {
109 QContextMenuEvent *ctxtEvent = static_cast<QContextMenuEvent*>(e);
110 QModelIndex idx = m_indexWidget->indexAt(ctxtEvent->pos());
111 if (idx.isValid()) {
112 QMenu menu;
113 QAction *curTab = menu.addAction(tr("Open Link"));
114 QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
115 menu.move(m_indexWidget->mapToGlobal(ctxtEvent->pos()));
116
117 QAction *action = menu.exec();
118 if (curTab == action)
119 m_indexWidget->activateCurrentItem();
120 else if (newTab == action) {
121 open(m_indexWidget, idx);
122 }
123 }
124 } else if (m_indexWidget && obj == m_indexWidget->viewport()
125 && e->type() == QEvent::MouseButtonRelease) {
126 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
127 QModelIndex idx = m_indexWidget->indexAt(mouseEvent->pos());
128 if (idx.isValid()) {
129 Qt::MouseButtons button = mouseEvent->button();
130 if (((button == Qt::LeftButton) && (mouseEvent->modifiers() & Qt::ControlModifier))
131 || (button == Qt::MiddleButton)) {
132 open(m_indexWidget, idx);
133 }
134 }
135 }
136#ifdef Q_OS_MAC
137 else if (obj == m_indexWidget && e->type() == QEvent::KeyPress) {
138 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
139 if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)
140 m_indexWidget->activateCurrentItem();
141 }
142#endif
143 return QWidget::eventFilter(obj, e);
144}
145
146void IndexWindow::enableSearchLineEdit()
147{
149 m_searchLineEdit->setDisabled(false);
150 filterIndices(m_searchLineEdit->text());
151}
152
153void IndexWindow::disableSearchLineEdit()
154{
156 m_searchLineEdit->setDisabled(true);
157}
158
159void IndexWindow::setSearchLineEditText(const QString &text)
160{
162 m_searchLineEdit->setText(text);
163}
164
165void IndexWindow::focusInEvent(QFocusEvent *e)
166{
168 if (e->reason() != Qt::MouseFocusReason) {
169 m_searchLineEdit->selectAll();
170 m_searchLineEdit->setFocus();
171 }
172}
173
174void IndexWindow::open(QHelpIndexWidget* indexWidget, const QModelIndex &index)
175{
177 QHelpIndexModel *model = qobject_cast<QHelpIndexModel*>(indexWidget->model());
178 if (model) {
179 const QString keyword = model->data(index, Qt::DisplayRole).toString();
180 const QList<QHelpLink> docs = model->helpEngine()->documentsForKeyword(keyword);
181
182 QUrl url;
183 if (docs.size() > 1) {
184 TopicChooser tc(this, keyword, docs);
185 if (tc.exec() == QDialog::Accepted)
186 url = tc.link();
187 } else if (!docs.isEmpty()) {
188 url = docs.first().url;
189 } else {
190 return;
191 }
192
193 if (!HelpViewer::canOpenPage(url.path()))
194 CentralWidget::instance()->setSource(url);
195 else
196 OpenPagesManager::instance()->createPage(url);
197 }
198}
199
200QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34