Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qhelpsearchenginecore.h"
6
#
include
"qhelpenginecore.h"
7
8
#
include
"qhelpsearchindexreader_p.h"
9
#
include
"qhelpsearchindexwriter_p.h"
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
18
QT_BEGIN_NAMESPACE
19
20
using
namespace
fulltextsearch;
21
using
namespace
Qt::StringLiterals;
22
23
class
QHelpSearchEngineCorePrivate
:
public
QObjectPrivate
24
{
25
Q_DECLARE_PUBLIC(QHelpSearchEngineCore)
26
27
public
:
28
QString
indexFilesFolder
()
const
29
{
30
QString
indexFilesFolder
=
".fulltextsearch"_L1
;
31
if
(
m_helpEngine
&& !
m_helpEngine
->
collectionFile
().
isEmpty
()) {
32
const
QFileInfo
fi
(
m_helpEngine
->
collectionFile
());
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
();
36
indexFilesFolder
=
fi
.
absolutePath
() +
QDir
::
separator
() + u'.'
37
+ (
baseName
.
isEmpty
() ?
fileName
+
".fulltextsearch"_L1
:
baseName
);
38
}
39
return
indexFilesFolder
;
40
}
41
42
void
updateIndex
(
bool
reindex
)
43
{
44
Q_Q
(
QHelpSearchEngineCore
);
45
if
(
m_helpEngine
.
isNull
())
46
return
;
47
48
if
(!
QFile
::
exists
(
QFileInfo
(
m_helpEngine
->
collectionFile
()).
path
()))
49
return
;
50
51
if
(!
m_indexWriter
) {
52
m_indexWriter
.
reset
(
new
QHelpSearchIndexWriter
);
53
54
QObject
::
connect
(
m_indexWriter
.
get
(), &
QHelpSearchIndexWriter
::
indexingStarted
,
55
q
, &
QHelpSearchEngineCore
::
indexingStarted
);
56
QObject
::
connect
(
m_indexWriter
.
get
(), &
QHelpSearchIndexWriter
::
indexingFinished
,
57
q
, &
QHelpSearchEngineCore
::
indexingFinished
);
58
}
59
60
m_indexWriter
->
cancelIndexing
();
61
m_indexWriter
->
updateIndex
(
m_helpEngine
->
collectionFile
(),
indexFilesFolder
(),
reindex
);
62
}
63
64
void
search
(
const
QString
&
searchInput
)
65
{
66
Q_Q
(
QHelpSearchEngineCore
);
67
if
(
m_helpEngine
.
isNull
())
68
return
;
69
70
if
(!
QFile
::
exists
(
QFileInfo
(
m_helpEngine
->
collectionFile
()).
path
()))
71
return
;
72
73
if
(!
m_indexReader
) {
74
m_indexReader
.
reset
(
new
QHelpSearchIndexReader
);
75
QObject
::
connect
(
m_indexReader
.
get
(), &
QHelpSearchIndexReader
::
searchingStarted
,
76
q
, &
QHelpSearchEngineCore
::
searchingStarted
);
77
QObject
::
connect
(
m_indexReader
.
get
(), &
QHelpSearchIndexReader
::
searchingFinished
,
78
q
, &
QHelpSearchEngineCore
::
searchingFinished
);
79
}
80
81
m_searchInput
=
searchInput
;
82
m_indexReader
->
cancelSearching
();
83
m_indexReader
->
search
(
m_helpEngine
->
collectionFile
(),
indexFilesFolder
(),
searchInput
,
84
m_helpEngine
->
usesFilterEngine
());
85
}
86
87
bool
m_isIndexingScheduled
=
false
;
88
89
std
::
unique_ptr
<
QHelpSearchIndexReader
>
m_indexReader
;
90
std
::
unique_ptr
<
QHelpSearchIndexWriter
>
m_indexWriter
;
91
92
QPointer
<
QHelpEngineCore
>
m_helpEngine
;
93
QString
m_searchInput
;
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
*/
157
QHelpSearchEngineCore::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
*/
169
QHelpSearchEngineCore::~QHelpSearchEngineCore() =
default
;
170
171
/*!
172
Returns the number of results the search engine found.
173
*/
174
int
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
*/
184
QList<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
*/
194
QString 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
*/
203
void
QHelpSearchEngineCore::reindexDocumentation()
204
{
205
Q_D(QHelpSearchEngineCore);
206
d->updateIndex(
true
);
207
}
208
209
/*!
210
Stops the indexing process.
211
*/
212
void
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
*/
222
void
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
*/
245
void
QHelpSearchEngineCore::search(
const
QString &searchInput)
246
{
247
Q_D(QHelpSearchEngineCore);
248
d->search(searchInput);
249
}
250
251
/*!
252
\internal
253
*/
254
void
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
268
QT_END_NAMESPACE
QHelpSearchEngineCorePrivate
Definition
qhelpsearchenginecore.cpp:24
QHelpSearchEngineCorePrivate::m_indexWriter
std::unique_ptr< QHelpSearchIndexWriter > m_indexWriter
Definition
qhelpsearchenginecore.cpp:90
QHelpSearchEngineCorePrivate::m_searchInput
QString m_searchInput
Definition
qhelpsearchenginecore.cpp:93
QHelpSearchEngineCorePrivate::m_indexReader
std::unique_ptr< QHelpSearchIndexReader > m_indexReader
Definition
qhelpsearchenginecore.cpp:89
QHelpSearchEngineCorePrivate::m_helpEngine
QPointer< QHelpEngineCore > m_helpEngine
Definition
qhelpsearchenginecore.cpp:92
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qttools
src
assistant
help
qhelpsearchenginecore.cpp
Generated on
for Qt by
1.18.0