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