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
qhelpsearchengine.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
10
12
14{
15public:
17 QHelpSearchQueryWidget *queryWidget = nullptr;
18 QHelpSearchResultWidget *resultWidget = nullptr;
19};
20
21/*!
22 \class QHelpSearchQuery
23 \deprecated
24 \since 4.4
25 \inmodule QtHelp
26 \brief The QHelpSearchQuery class contains the field name and the associated
27 search term.
28
29 The QHelpSearchQuery class contains the field name and the associated search
30 term. Depending on the field the search term might get split up into separate
31 terms to be parsed differently by the search engine.
32
33 \note This class has been deprecated in favor of QString.
34
35 \sa QHelpSearchQueryWidget
36*/
37
38/*!
39 \fn QHelpSearchQuery::QHelpSearchQuery()
40
41 Constructs a new empty QHelpSearchQuery.
42*/
43
44/*!
45 \fn QHelpSearchQuery::QHelpSearchQuery(FieldName field, const QStringList &wordList)
46
47 Constructs a new QHelpSearchQuery and initializes it with the given \a field and \a wordList.
48*/
49
50/*!
51 \enum QHelpSearchQuery::FieldName
52 This enum type specifies the field names that are handled by the search engine.
53
54 \value DEFAULT the default field provided by the search widget, several terms should be
55 split and stored in the word list except search terms enclosed in quotes.
56 \value FUZZY \deprecated Terms should be split in separate
57 words and passed to the search engine.
58 \value WITHOUT \deprecated Terms should be split in separate
59 words and passed to the search engine.
60 \value PHRASE \deprecated Terms should not be split in separate words.
61 \value ALL \deprecated Terms should be split in separate
62 words and passed to the search engine
63 \value ATLEAST \deprecated Terms should be split in separate
64 words and passed to the search engine
65*/
66
67/*!
68 \class QHelpSearchEngine
69 \since 4.4
70 \inmodule QtHelp
71 \brief The QHelpSearchEngine class provides access to widgets reusable
72 to integrate fulltext search as well as to index and search documentation.
73
74 Before the search engine can be used, one has to instantiate at least a
75 QHelpEngineCore object that needs to be passed to the search engines constructor.
76 This is required as the search engine needs to be connected to the help
77 engines setupFinished() signal to know when it can start to index documentation.
78
79 After starting the indexing process the signal indexingStarted() is emitted and
80 on the end of the indexing process the indexingFinished() is emitted. To stop
81 the indexing one can call cancelIndexing().
82
83 When the indexing process has finished, the search engine can be used to
84 search through the index for a given term using the search() function. When
85 the search input is passed to the search engine, the searchingStarted()
86 signal is emitted. When the search finishes, the searchingFinished() signal
87 is emitted. The search process can be stopped by calling cancelSearching().
88
89 If the search succeeds, searchingFinished() is called with the search result
90 count to fetch the search results from the search engine. Calling the
91 searchResults() function with a range returns a list of QHelpSearchResult
92 objects within the range. The results consist of the document title and URL,
93 as well as a snippet from the document that contains the best match for the
94 search input.
95
96 To display the given search results use the QHelpSearchResultWidget or build up your own one if you need
97 more advanced functionality. Note that the QHelpSearchResultWidget can not be instantiated
98 directly, you must retrieve the widget from the search engine in use as all connections will be
99 established for you by the widget itself.
100*/
101
102/*!
103 \fn void QHelpSearchEngine::indexingStarted()
104
105 This signal is emitted when indexing process is started.
106*/
107
108/*!
109 \fn void QHelpSearchEngine::indexingFinished()
110
111 This signal is emitted when the indexing process is complete.
112*/
113
114/*!
115 \fn void QHelpSearchEngine::searchingStarted()
116
117 This signal is emitted when the search process is started.
118*/
119
120/*!
121 \fn void QHelpSearchEngine::searchingFinished(int searchResultCount)
122
123 This signal is emitted when the search process is complete.
124 The search result count is stored in \a searchResultCount.
125*/
126
127/*!
128 Constructs a new search engine with the given \a parent. The search engine
129 uses the given \a helpEngine to access the documentation that needs to be indexed.
130 The QHelpEngine's setupFinished() signal is automatically connected to the
131 QHelpSearchEngine's indexing function, so that new documentation will be indexed
132 after the signal is emitted.
133*/
134QHelpSearchEngine::QHelpSearchEngine(QHelpEngineCore *helpEngine, QObject *parent)
135 : QObject(parent)
136 , d(new QHelpSearchEnginePrivate{QHelpSearchEngineCore(helpEngine)})
137{
138 connect(&d->m_searchEngine, &QHelpSearchEngineCore::indexingStarted,
139 this, &QHelpSearchEngine::indexingStarted);
140 connect(&d->m_searchEngine, &QHelpSearchEngineCore::indexingFinished,
141 this, &QHelpSearchEngine::indexingFinished);
142 connect(&d->m_searchEngine, &QHelpSearchEngineCore::searchingStarted,
143 this, &QHelpSearchEngine::searchingStarted);
144 connect(&d->m_searchEngine, &QHelpSearchEngineCore::searchingFinished,
145 this, [this] { emit searchingFinished(d->m_searchEngine.searchResultCount()); });
146}
147
148/*!
149 Destructs the search engine.
150*/
151QHelpSearchEngine::~QHelpSearchEngine()
152{
153 delete d;
154}
155
156/*!
157 Returns a widget to use as input widget. Depending on your search engine
158 configuration you will get a different widget with more or less subwidgets.
159*/
160QHelpSearchQueryWidget* QHelpSearchEngine::queryWidget()
161{
162 if (!d->queryWidget)
163 d->queryWidget = new QHelpSearchQueryWidget();
164 return d->queryWidget;
165}
166
167/*!
168 Returns a widget that can hold and display the search results.
169*/
170QHelpSearchResultWidget* QHelpSearchEngine::resultWidget()
171{
172 if (!d->resultWidget)
173 d->resultWidget = new QHelpSearchResultWidget(this);
174 return d->resultWidget;
175}
176
177#if QT_DEPRECATED_SINCE(5, 9)
178/*!
179 \deprecated
180 Use searchResultCount() instead.
181*/
182int QHelpSearchEngine::hitsCount() const
183{
184 return searchResultCount();
185}
186
187/*!
188 \since 4.6
189 \deprecated
190 Use searchResultCount() instead.
191*/
192int QHelpSearchEngine::hitCount() const
193{
194 return searchResultCount();
195}
196#endif // QT_DEPRECATED_SINCE(5, 9)
197
198/*!
199 \since 5.9
200 Returns the number of results the search engine found.
201*/
202int QHelpSearchEngine::searchResultCount() const
203{
204 return d->m_searchEngine.searchResultCount();
205}
206
207#if QT_DEPRECATED_SINCE(5, 9)
208/*!
209 \typedef QHelpSearchEngine::SearchHit
210 \deprecated
211
212 Use QHelpSearchResult instead.
213
214 Typedef for QPair<QString, QString>.
215 The values of that pair are the documentation file path and the page title.
216
217 \sa hits()
218*/
219
220/*!
221 \deprecated
222 Use searchResults() instead.
223*/
224QList<QHelpSearchEngine::SearchHit> QHelpSearchEngine::hits(int start, int end) const
225{
226 QList<QHelpSearchEngine::SearchHit> hits;
227 const auto &results = searchResults(start, end);
228 for (const QHelpSearchResult &result : results)
229 hits.append(qMakePair(result.url().toString(), result.title()));
230 return hits;
231}
232#endif // QT_DEPRECATED_SINCE(5, 9)
233
234/*!
235 \since 5.9
236 Returns a list of search results within the range from the index
237 specified by \a start to the index specified by \a end.
238*/
239QList<QHelpSearchResult> QHelpSearchEngine::searchResults(int start, int end) const
240{
241 return d->m_searchEngine.searchResults(start, end);
242}
243
244/*!
245 \since 5.9
246 Returns the phrase that was last searched for.
247*/
248QString QHelpSearchEngine::searchInput() const
249{
250 return d->m_searchEngine.searchInput();
251}
252
253#if QT_DEPRECATED_SINCE(5, 9)
254QT_WARNING_PUSH
255QT_WARNING_DISABLE_DEPRECATED
256/*!
257 \deprecated
258 \since 4.5
259 Use searchInput() instead.
260*/
261QList<QHelpSearchQuery> QHelpSearchEngine::query() const
262{
263 return {{QHelpSearchQuery::DEFAULT, searchInput().split(QChar::Space)}};
264}
265QT_WARNING_POP
266#endif // QT_DEPRECATED_SINCE(5, 9)
267
268/*!
269 Forces the search engine to reindex all documentation files.
270*/
271void QHelpSearchEngine::reindexDocumentation()
272{
273 d->m_searchEngine.reindexDocumentation();
274}
275
276/*!
277 Stops the indexing process.
278*/
279void QHelpSearchEngine::cancelIndexing()
280{
281 d->m_searchEngine.cancelIndexing();
282}
283
284/*!
285 Stops the search process.
286*/
287void QHelpSearchEngine::cancelSearching()
288{
289 d->m_searchEngine.cancelSearching();
290}
291
292/*!
293 \since 5.9
294 Starts the search process using the given search phrase \a searchInput.
295
296 The phrase may consist of several words. By default, the search engine returns
297 the list of documents that contain all the specified words.
298 The phrase may contain any combination of the logical operators AND, OR, and
299 NOT. The operator must be written in all capital letters, otherwise it will
300 be considered a part of the search phrase.
301
302 If double quotation marks are used to group the words,
303 the search engine will search for an exact match of the quoted phrase.
304
305 For more information about the text query syntax,
306 see \l {https://sqlite.org/fts5.html#full_text_query_syntax}
307 {SQLite FTS5 Extension}.
308*/
309void QHelpSearchEngine::search(const QString &searchInput)
310{
311 d->m_searchEngine.search(searchInput);
312}
313
314#if QT_DEPRECATED_SINCE(5, 9)
315/*!
316 \deprecated
317 Use search(const QString &searchInput) instead.
318*/
319void QHelpSearchEngine::search(const QList<QHelpSearchQuery> &queryList)
320{
321 if (queryList.isEmpty())
322 return;
323
324 d->m_searchEngine.search(queryList.first().wordList.join(QChar::Space));
325}
326#endif // QT_DEPRECATED_SINCE(5, 9)
327
328/*!
329 \internal
330*/
331void QHelpSearchEngine::scheduleIndexDocumentation()
332{
333 d->m_searchEngine.scheduleIndexDocumentation();
334}
335
336// TODO: Deprecate me (but it's private???)
337void QHelpSearchEngine::indexDocumentation()
338{}
339
340QT_END_NAMESPACE
QHelpSearchResultWidget * resultWidget
QHelpSearchQueryWidget * queryWidget
QHelpSearchEngineCore m_searchEngine
Combined button and popup list for selecting options.