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
qhelpsearchresultwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qcoreevent.h>
8#include <QtCore/qlist.h>
9#include <QtCore/qpointer.h>
10#include <QtCore/qtextstream.h>
11#include <QtWidgets/qlabel.h>
12#include <QtWidgets/qlayout.h>
13#include <QtWidgets/qlayoutitem.h>
14#include <QtWidgets/qtoolbutton.h>
15#include <QtWidgets/qtextbrowser.h>
16
18
19static constexpr int ResultsRange = 20;
20
22{
23 Q_OBJECT
24 Q_PROPERTY(QColor linkColor READ linkColor WRITE setLinkColor)
25
26public:
34
35 QColor linkColor() const { return m_linkColor; }
37 {
39#if QT_CONFIG(cssparser)
40 const QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }")
43#endif
44 }
45
47 {
50 str << "<html><head><title>" << tr("Search Results") << "</title></head><body>";
51
52 const int count = results.size();
53 if (count != 0) {
54 if (isIndexing) {
55 str << "<div style=\"text-align:left;"
56 " font-weight:bold; color:red\">" << tr("Note:")
57 << "&nbsp;<span style=\"font-weight:normal; color:black\">"
58 << tr("The search results may not be complete since the "
59 "documentation is still being indexed.")
60 << "</span></div></div><br>";
61 }
62
63 for (const QHelpSearchResult &result : results) {
64 str << "<div style=\"text-align:left\"><a href=\""
65 << result.url().toString() << "\">"
66 << result.title() << "</a></div>"
67 "<div style =\"margin:5px\">" << result.snippet() << "</div>";
68 }
69 } else {
70 str << "<div align=\"center\"><br><br><h2>"
71 << tr("Your search did not match any documents.")
72 << "</h2><div>";
73 if (isIndexing) {
74 str << "<div align=\"center\"><h3>"
75 << tr("(The reason for this might be that the documentation "
76 "is still being indexed.)") << "</h3><div>";
77 }
78 }
79
80 str << "</body></html>";
81#if QT_CONFIG(texthtmlparser)
83#else
85#endif
86 }
87
89 void requestShowLink(const QUrl &url);
90
91private slots:
92 void doSetSource(const QUrl & /*name*/, QTextDocument::ResourceType /*type*/) override {}
93
94private:
96};
97
99{
100public:
102 {
103 delete searchEngine; // TODO: This it probably wrong, why the widget owns the engine?
104 }
105
106 QToolButton* setupToolButton(const QString &iconPath)
107 {
108 QToolButton *button = new QToolButton;
109 button->setEnabled(false);
110 button->setAutoRaise(true);
111 button->setIcon(QIcon(iconPath));
112 button->setIconSize({12, 12});
113 button->setMaximumSize({16, 16});
114 return button;
115 }
116
118 {
119 int last = 0;
120 int first = 0;
121 int count = 0;
122
123 if (!searchEngine.isNull()) {
124 count = searchEngine->searchResultCount();
125 if (count > 0) {
126 last = qMin(resultFirstToShow + ResultsRange, count);
127 first = resultFirstToShow + 1;
128 }
129 resultTextBrowser->showResultPage(searchEngine->searchResults(resultFirstToShow, last),
130 isIndexing);
131 }
132
133 hitsLabel->setText(QHelpSearchResultWidget::tr("%1 - %2 of %n Hits", nullptr, count)
134 .arg(first).arg(last));
135 firstResultPage->setEnabled(resultFirstToShow);
136 previousResultPage->setEnabled(resultFirstToShow);
137 lastResultPage->setEnabled(count - last);
138 nextResultPage->setEnabled(count - last);
139 }
140
143
145
150 QLabel *hitsLabel = nullptr;
152 bool isIndexing = false;
153};
154
155/*!
156 \class QHelpSearchResultWidget
157 \since 4.4
158 \inmodule QtHelp
159 \brief The QHelpSearchResultWidget class provides a text browser to display
160 search results.
161*/
162
163/*!
164 \fn void QHelpSearchResultWidget::requestShowLink(const QUrl &link)
165
166 This signal is emitted when a item is activated and its associated
167 \a link should be shown.
168*/
169
170QHelpSearchResultWidget::QHelpSearchResultWidget(QHelpSearchEngine *engine)
171 : QWidget(0)
172 , d(new QHelpSearchResultWidgetPrivate)
173{
174 d->q = this;
175 d->searchEngine = engine;
176
177 connect(engine, &QHelpSearchEngine::indexingStarted, this, [this] { d->isIndexing = true; });
178 connect(engine, &QHelpSearchEngine::indexingFinished, this, [this] { d->isIndexing = false; });
179
180 QVBoxLayout *vLayout = new QVBoxLayout(this);
181 vLayout->setContentsMargins({});
182 vLayout->setSpacing(0);
183
184 QHBoxLayout *hBoxLayout = new QHBoxLayout();
185#ifndef Q_OS_MACOS
186 hBoxLayout->setContentsMargins({});
187 hBoxLayout->setSpacing(0);
188#endif
189 hBoxLayout->addWidget(d->firstResultPage = d->setupToolButton(
190 QString::fromUtf8(":/qt-project.org/assistant/images/3leftarrow.png")));
191
192 hBoxLayout->addWidget(d->previousResultPage = d->setupToolButton(
193 QString::fromUtf8(":/qt-project.org/assistant/images/1leftarrow.png")));
194
195 d->hitsLabel = new QLabel(tr("0 - 0 of 0 Hits"), this);
196 hBoxLayout->addWidget(d->hitsLabel);
197 d->hitsLabel->setAlignment(Qt::AlignCenter);
198 d->hitsLabel->setMinimumSize(QSize(150, d->hitsLabel->height()));
199
200 hBoxLayout->addWidget(d->nextResultPage = d->setupToolButton(
201 QString::fromUtf8(":/qt-project.org/assistant/images/1rightarrow.png")));
202
203 hBoxLayout->addWidget(d->lastResultPage = d->setupToolButton(
204 QString::fromUtf8(":/qt-project.org/assistant/images/3rightarrow.png")));
205
206 QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
207 hBoxLayout->addItem(spacer);
208
209 vLayout->addLayout(hBoxLayout);
210
211 d->resultTextBrowser = new QResultWidget(this);
212 vLayout->addWidget(d->resultTextBrowser);
213
214 connect(d->resultTextBrowser, &QResultWidget::requestShowLink,
215 this, &QHelpSearchResultWidget::requestShowLink);
216
217 connect(d->nextResultPage, &QAbstractButton::clicked, this, [this] {
218 if (!d->searchEngine.isNull()
219 && d->resultFirstToShow + ResultsRange < d->searchEngine->searchResultCount()) {
220 d->resultFirstToShow += ResultsRange;
221 }
222 d->updateHitRange();
223 });
224 connect(d->previousResultPage, &QAbstractButton::clicked, this, [this] {
225 if (!d->searchEngine.isNull()) {
226 d->resultFirstToShow -= ResultsRange;
227 if (d->resultFirstToShow < 0)
228 d->resultFirstToShow = 0;
229 }
230 d->updateHitRange();
231 });
232 connect(d->lastResultPage, &QAbstractButton::clicked, this, [this] {
233 if (!d->searchEngine.isNull())
234 d->resultFirstToShow = (d->searchEngine->searchResultCount() - 1) / ResultsRange * ResultsRange;
235 d->updateHitRange();
236 });
237 const auto showFirstPage = [this] {
238 if (!d->searchEngine.isNull())
239 d->resultFirstToShow = 0;
240 d->updateHitRange();
241 };
242 connect(d->firstResultPage, &QAbstractButton::clicked, this, showFirstPage);
243 connect(engine, &QHelpSearchEngine::searchingFinished, this, showFirstPage);
244}
245
246/*! \reimp
247*/
248void QHelpSearchResultWidget::changeEvent(QEvent *event)
249{
250 if (event->type() == QEvent::LanguageChange)
251 d->updateHitRange();
252}
253
254/*!
255 Destroys the search result widget.
256*/
257QHelpSearchResultWidget::~QHelpSearchResultWidget()
258{
259 delete d;
260}
261
262/*!
263 Returns a reference of the URL that the item at \a point owns, or an
264 empty URL if no item exists at that point.
265*/
266QUrl QHelpSearchResultWidget::linkAt(const QPoint &point)
267{
268 if (d->resultTextBrowser)
269 return QUrl(d->resultTextBrowser->anchorAt(point));
270 return {};
271}
272
273QT_END_NAMESPACE
274
275#include "qhelpsearchresultwidget.moc"
QToolButton * setupToolButton(const QString &iconPath)
QPointer< QHelpSearchEngine > searchEngine
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE constexpr int ResultsRange