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