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
qhelpsearchenginecore.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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
7
10
11#include <QtCore/private/qobject_p.h>
12#include <QtCore/qdir.h>
13#include <QtCore/qfile.h>
14#include <QtCore/qfileinfo.h>
15#include <QtCore/qpointer.h>
16#include <QtCore/qtimer.h>
17
19
20using namespace fulltextsearch;
21using namespace Qt::StringLiterals;
22
24{
25 Q_DECLARE_PUBLIC(QHelpSearchEngineCore)
26
27public:
29 {
30 QString indexFilesFolder = ".fulltextsearch"_L1;
33 const QString fileName = fi.fileName();
34 // Only strip a real .qhc suffix, otherwise e.g. "ssh" would map onto "~/.ssh".
35 const QString baseName = fileName.endsWith(".qhc"_L1) ? fileName.chopped(4) : QString();
37 + (baseName.isEmpty() ? fileName + ".fulltextsearch"_L1 : baseName);
38 }
39 return indexFilesFolder;
40 }
41
63
86
88
91
94};
95
96/*!
97 \class QHelpSearchEngineCore
98 \since 6.8
99 \inmodule QtHelp
100 \brief The QHelpSearchEngineCore class provides access to index and
101 search documentation.
102
103 Before the search engine can be used, one has to instantiate at least a
104 QHelpEngineCore object that needs to be passed to the search engines constructor.
105 This is required as the search engine needs to be connected to the help
106 engines setupFinished() signal to know when it can start to index documentation.
107
108 After starting the indexing process the signal indexingStarted() is emitted and
109 on the end of the indexing process the indexingFinished() is emitted. To stop
110 the indexing one can call cancelIndexing().
111
112 When the indexing process has finished, the search engine can be used to
113 search through the index for a given term using the search() function. When
114 the search input is passed to the search engine, the searchingStarted()
115 signal is emitted. When the search finishes, the searchingFinished() signal
116 is emitted. The search process can be stopped by calling cancelSearching().
117
118 If the search succeeds, searchingFinished() is called with the search result
119 count to fetch the search results from the search engine. Calling the
120 searchResults() function with a range returns a list of QHelpSearchResult
121 objects within the range. The results consist of the document title and URL,
122 as well as a snippet from the document that contains the best match for the
123 search input.
124*/
125
126/*!
127 \fn void QHelpSearchEngineCore::indexingStarted()
128
129 This signal is emitted when indexing process is started.
130*/
131
132/*!
133 \fn void QHelpSearchEngineCore::indexingFinished()
134
135 This signal is emitted when the indexing process is complete.
136*/
137
138/*!
139 \fn void QHelpSearchEngineCore::searchingStarted()
140
141 This signal is emitted when the search process is started.
142*/
143
144/*!
145 \fn void QHelpSearchEngineCore::searchingFinished()
146
147 This signal is emitted when the search process is complete.
148*/
149
150/*!
151 Constructs a new search engine with the given \a parent. The search engine
152 uses the given \a helpEngine to access the documentation that needs to be indexed.
153 The QHelpEngineCore's setupFinished() signal is automatically connected to the
154 QHelpSearchEngineCore's indexing function, so that new documentation will
155 be indexed after the signal is emitted.
156*/
157QHelpSearchEngineCore::QHelpSearchEngineCore(QHelpEngineCore *helpEngine, QObject *parent)
158 : QObject(*new QHelpSearchEngineCorePrivate, parent)
159{
160 Q_D(QHelpSearchEngineCore);
161 d->m_helpEngine = helpEngine;
162 connect(helpEngine, &QHelpEngineCore::setupFinished,
163 this, &QHelpSearchEngineCore::scheduleIndexDocumentation);
164}
165
166/*!
167 Destructs the search engine.
168*/
169QHelpSearchEngineCore::~QHelpSearchEngineCore() = default;
170
171/*!
172 Returns the number of results the search engine found.
173*/
174int QHelpSearchEngineCore::searchResultCount() const
175{
176 Q_D(const QHelpSearchEngineCore);
177 return d->m_indexReader ? d->m_indexReader->searchResultCount() : 0;;
178}
179
180/*!
181 Returns a list of search results within the range from the index
182 specified by \a start to the index specified by \a end.
183*/
184QList<QHelpSearchResult> QHelpSearchEngineCore::searchResults(int start, int end) const
185{
186 Q_D(const QHelpSearchEngineCore);
187 return d->m_indexReader ? d->m_indexReader->searchResults(start, end)
188 : QList<QHelpSearchResult>();
189}
190
191/*!
192 Returns the phrase that was last searched for.
193*/
194QString QHelpSearchEngineCore::searchInput() const
195{
196 Q_D(const QHelpSearchEngineCore);
197 return d->m_searchInput;
198}
199
200/*!
201 Forces the search engine to reindex all documentation files.
202*/
203void QHelpSearchEngineCore::reindexDocumentation()
204{
205 Q_D(QHelpSearchEngineCore);
206 d->updateIndex(true);
207}
208
209/*!
210 Stops the indexing process.
211*/
212void QHelpSearchEngineCore::cancelIndexing()
213{
214 Q_D(QHelpSearchEngineCore);
215 if (d->m_indexWriter)
216 d->m_indexWriter->cancelIndexing();
217}
218
219/*!
220 Stops the search process.
221*/
222void QHelpSearchEngineCore::cancelSearching()
223{
224 Q_D(QHelpSearchEngineCore);
225 if (d->m_indexReader)
226 d->m_indexReader->cancelSearching();
227}
228
229/*!
230 Starts the search process using the given search phrase \a searchInput.
231
232 The phrase may consist of several words. By default, the search engine returns
233 the list of documents that contain all the specified words.
234 The phrase may contain any combination of the logical operators AND, OR, and
235 NOT. The operator must be written in all capital letters, otherwise it will
236 be considered a part of the search phrase.
237
238 If double quotation marks are used to group the words,
239 the search engine will search for an exact match of the quoted phrase.
240
241 For more information about the text query syntax,
242 see \l {https://sqlite.org/fts5.html#full_text_query_syntax}
243 {SQLite FTS5 Extension}.
244*/
245void QHelpSearchEngineCore::search(const QString &searchInput)
246{
247 Q_D(QHelpSearchEngineCore);
248 d->search(searchInput);
249}
250
251/*!
252 \internal
253*/
254void QHelpSearchEngineCore::scheduleIndexDocumentation()
255{
256 Q_D(QHelpSearchEngineCore);
257 if (d->m_isIndexingScheduled)
258 return;
259
260 d->m_isIndexingScheduled = true;
261 QTimer::singleShot(0, this, [this] {
262 Q_D(QHelpSearchEngineCore);
263 d->m_isIndexingScheduled = false;
264 d->updateIndex(false);
265 });
266}
267
268QT_END_NAMESPACE
std::unique_ptr< QHelpSearchIndexWriter > m_indexWriter
std::unique_ptr< QHelpSearchIndexReader > m_indexReader
QPointer< QHelpEngineCore > m_helpEngine
Combined button and popup list for selecting options.