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
qhelpfilterengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
9
10#include <QtCore/qthread.h>
11#include <QtCore/qversionnumber.h>
12
14
15static const char ActiveFilter[] = "activeFilter";
16
18{
19public:
20 bool setup();
21
22 QHelpFilterEngine *q = nullptr;
23 QHelpEngineCore *m_helpEngine = nullptr;
26 bool m_needsSetup = true;
27};
28
30{
32 return false;
33
34 if (!m_needsSetup)
35 return true;
36
37 // Prevent endless loop when connected to setupFinished() signal
38 // and using from there QHelpFilterEngine, causing setup() to be
39 // called in turn.
40 m_needsSetup = false;
41
42 if (!m_helpEngine->setupData()) {
43 m_needsSetup = true;
44 return false;
45 }
46
47 const QString filter = m_collectionHandler->customValue(
48 QLatin1StringView(ActiveFilter), QString()).toString();
49 if (!filter.isEmpty() && m_collectionHandler->filters().contains(filter))
50 m_currentFilter = filter;
51
52 emit q->filterActivated(m_currentFilter);
53 return true;
54}
55
56//////////////
57
58/*!
59 \class QHelpFilterEngine
60 \since 5.13
61 \inmodule QtHelp
62 \brief The QHelpFilterEngine class provides a filtered view of the
63 help contents.
64
65 The filter engine allows the management of filters associated with
66 a QHelpEngineCore instance. The help engine internally creates an
67 instance of the filter engine, which can be accessed by calling
68 QHelpEngineCore::filterEngine(). Therefore, the public constructor
69 of this class is disabled.
70
71 The filters are identified by a filter name string. Filter details are
72 described by the \l QHelpFilterData class.
73
74 The filter engine allows for adding new filters and changing the existing
75 filters' data through the setFilterData() method. An existing filter can
76 be removed through the removeFilter() method.
77
78 Out of the registered filters one can be marked as the active one.
79 The active filter will be used by the associated help engine for returning
80 filtered results of many different functions, such as content, index, or
81 search results. If no filter is marked active, the help engine returns the
82 full results list available.
83
84 The active filter is returned by activeFilter() and it can be changed by
85 setActiveFilter().
86
87 \sa QHelpEngineCore
88*/
89
90/*!
91 \fn void QHelpFilterEngine::filterActivated(const QString &newFilter)
92
93 This signal is emitted when the active filter is set. \a newFilter
94 specifies the name of the filter.
95
96 \sa setActiveFilter()
97*/
98
99/*!
100 \internal
101 Constructs the filter engine for \a helpEngine.
102*/
103QHelpFilterEngine::QHelpFilterEngine(QHelpEngineCore *helpEngine)
104 : QObject(helpEngine),
105 d(new QHelpFilterEnginePrivate)
106{
107 d->q = this;
108 d->m_helpEngine = helpEngine;
109}
110
111/*!
112 \internal
113 Destroys the existing filter engine.
114*/
115QHelpFilterEngine::~QHelpFilterEngine()
116{
117 delete d;
118}
119
120/*!
121 \internal
122 Sets the \a collectionHandler to be used for this filter engine.
123*/
124void QHelpFilterEngine::setCollectionHandler(QHelpCollectionHandler *collectionHandler)
125{
126 d->m_collectionHandler = collectionHandler;
127 d->m_currentFilter.clear();
128 d->m_needsSetup = true;
129}
130
131/*!
132 Returns the map of all the available namespaces as keys
133 together with their associated components as values.
134*/
135QMap<QString, QString> QHelpFilterEngine::namespaceToComponent() const
136{
137 if (!d->setup())
138 return {};
139 return d->m_collectionHandler->namespaceToComponent();
140}
141
142/*!
143 Returns the map of all the available namespaces as keys
144 together with their associated versions as values.
145*/
146QMap<QString, QVersionNumber> QHelpFilterEngine::namespaceToVersion() const
147{
148 if (!d->setup())
149 return {};
150 return d->m_collectionHandler->namespaceToVersion();
151}
152
153/*!
154 Returns the list of all filter names defined inside the filter engine.
155*/
156QStringList QHelpFilterEngine::filters() const
157{
158 if (!d->setup())
159 return {};
160 return d->m_collectionHandler->filters();
161}
162
163/*!
164 Returns the list of all available components defined in all
165 registered documentation files.
166*/
167QStringList QHelpFilterEngine::availableComponents() const
168{
169 if (!d->setup())
170 return {};
171 return d->m_collectionHandler->availableComponents();
172}
173
174/*!
175 \since 5.15
176
177 Returns the list of all available versions defined in all
178 registered documentation files.
179*/
180QList<QVersionNumber> QHelpFilterEngine::availableVersions() const
181{
182 if (!d->setup())
183 return {};
184 return d->m_collectionHandler->availableVersions();
185}
186
187/*!
188 Returns the filter details associated with \a filterName.
189*/
190QHelpFilterData QHelpFilterEngine::filterData(const QString &filterName) const
191{
192 if (!d->setup())
193 return {};
194 return d->m_collectionHandler->filterData(filterName);
195}
196
197/*!
198 Changes the existing filter details of the filter identified by
199 \a filterName to \a filterData. If the filter does not exist, a
200 new filter is created.
201
202 Returns \c true if setting the filter succeeded, otherwise returns \c false.
203*/
204bool QHelpFilterEngine::setFilterData(const QString &filterName, const QHelpFilterData &filterData)
205{
206 if (!d->setup())
207 return false;
208 return d->m_collectionHandler->setFilterData(filterName, filterData);
209}
210
211/*!
212 Removes the filter identified by \a filterName.
213
214 Returns \c true if removing the filter succeeded, otherwise returns
215 \c false.
216*/
217bool QHelpFilterEngine::removeFilter(const QString &filterName)
218{
219 if (!d->setup())
220 return false;
221 return d->m_collectionHandler->removeFilter(filterName);
222}
223
224/*!
225 Returns the name of the currently active filter.
226*/
227QString QHelpFilterEngine::activeFilter() const
228{
229 if (!d->setup())
230 return {};
231 return d->m_currentFilter;
232}
233
234/*!
235 Changes the currently active filter to \a filterName.
236
237 Returns \c true if changing the filter succeeded, otherwise
238 returns \c false.
239*/
240bool QHelpFilterEngine::setActiveFilter(const QString &filterName)
241{
242 if (!d->setup())
243 return false;
244
245 if (filterName == d->m_currentFilter)
246 return true;
247
248 if (!filterName.isEmpty() && !d->m_collectionHandler->filters().contains(filterName))
249 return false;
250
251 d->m_currentFilter = filterName;
252 d->m_collectionHandler->setCustomValue(QLatin1StringView(ActiveFilter), d->m_currentFilter);
253 emit filterActivated(d->m_currentFilter);
254 return true;
255}
256
257/*!
258 Returns the list of all registered documentation namespaces that match
259 the filter identified by \a filterName.
260*/
261QStringList QHelpFilterEngine::namespacesForFilter(const QString &filterName) const
262{
263 if (!d->setup())
264 return {};
265 return d->m_collectionHandler->namespacesForFilter(filterName);
266}
267
268/*!
269 \since 5.15
270
271 Returns a sorted list of available indices.
272 The returned list contents depend on the active filter, and therefore only
273 the indices registered for the active filter will be returned.
274*/
275QStringList QHelpFilterEngine::indices() const
276{
277 return indices(activeFilter());
278}
279
280/*!
281 \since 5.15
282
283 Returns a sorted list of available indices, filtered by \a filterName.
284 The returned list contents depend on the passed filter, and therefore only
285 the indices registered for this filter will be returned.
286 If you want to get all available indices unfiltered,
287 pass empty string as \a filterName.
288*/
289QStringList QHelpFilterEngine::indices(const QString &filterName) const
290{
291 if (!d->setup())
292 return {};
293 return d->m_collectionHandler->indicesForFilter(filterName);
294}
295
296QT_END_NAMESPACE
QHelpCollectionHandler * m_collectionHandler
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE const char ActiveFilter[]