10#include <QtCore/qfuturewatcher.h>
45void QHelpIndexModelPrivate::createIndex(
const FutureProvider &futureProvider)
47 const bool wasRunning =
bool(watcher);
48 watcher.reset(
new QFutureWatcher<QStringList>);
49 QObject::connect(watcher.get(), &QFutureWatcherBase::finished, q, [
this] {
50 if (!watcher->isCanceled()) {
51 indices = watcher->result();
54 watcher.release()->deleteLater();
55 emit q->indexCreated();
57 watcher->setFuture(futureProvider());
64 emit q->indexCreationStarted();
69
70
71
72
73
74
77
78
79
80
81
82
83
84
87
88
89
90
92QHelpIndexModel::QHelpIndexModel(QHelpEngineCore *helpEngine)
93 : QStringListModel(helpEngine)
94 , d(
new QHelpIndexModelPrivate{
this, helpEngine})
97QHelpIndexModel::~QHelpIndexModel()
103
104
105
106
107void QHelpIndexModel::createIndexForCurrentFilter()
110 d->createIndex([
this] {
return d->helpEngine->requestIndexForCurrentFilter(); });
115
116
117
118void QHelpIndexModel::createIndex(
const QString &filter)
121 d->createIndex([
this, filter] {
return d->helpEngine->requestIndex(filter); });
126void QHelpIndexModel::insertIndices()
130
131
132
133bool QHelpIndexModel::isCreatingIndex()
const
136 return bool(d->watcher);
143
144
145
146
147QHelpEngineCore *QHelpIndexModel::helpEngine()
const
149 return d->helpEngine;
153
154
155
156
157
158
159
160
161
162QModelIndex QHelpIndexModel::filter(
const QString &filter,
const QString &wildcard)
164 if (filter.isEmpty()) {
165 setStringList(d->indices);
166 return index(-1, 0, {});
169 using Checker = std::function<
bool(
const QString &)>;
170 const auto checkIndices = [
this, filter](
const Checker &checker) {
171 QStringList filteredList;
173 int perfectMatch = -1;
174 for (
const QString &index : std::as_const(d->indices)) {
175 if (checker(index)) {
176 filteredList.append(index);
177 if (perfectMatch == -1 && index.startsWith(filter, Qt::CaseInsensitive)) {
179 goodMatch = filteredList.size() - 1;
180 if (filter.size() == index.size())
181 perfectMatch = filteredList.size() - 1;
182 }
else if (perfectMatch > -1 && index == filter) {
183 perfectMatch = filteredList.size() - 1;
187 setStringList(filteredList);
188 return perfectMatch >= 0 ? perfectMatch : qMax(0, goodMatch);
191 int perfectMatch = -1;
192 if (!wildcard.isEmpty()) {
193 const auto re = QRegularExpression::wildcardToRegularExpression(wildcard,
194 QRegularExpression::UnanchoredWildcardConversion);
195 const QRegularExpression regExp(re, QRegularExpression::CaseInsensitiveOption);
196 perfectMatch = checkIndices([regExp](
const QString &index) {
197 return index.contains(regExp);
200 perfectMatch = checkIndices([filter](
const QString &index) {
201 return index.contains(filter, Qt::CaseInsensitive);
204 return index(perfectMatch, 0, {});
208
209
210
211
212
213
216
217
218
219
220
221
222
223
224
225
226
229
230
231
232
233
234
235
236
237
240
241
242
243
244
245
246
247
248
250QHelpIndexWidget::QHelpIndexWidget()
252 setEditTriggers(QAbstractItemView::NoEditTriggers);
253 setUniformItemSizes(
true);
254 connect(
this, &QAbstractItemView::activated,
this, &QHelpIndexWidget::showLink);
257void QHelpIndexWidget::showLink(
const QModelIndex &index)
259 if (!index.isValid())
262 QHelpIndexModel *indexModel = qobject_cast<QHelpIndexModel*>(model());
266 const QVariant &v = indexModel->data(index, Qt::DisplayRole);
267 const QString name = v.isValid() ? v.toString() : QString();
269 const QList<QHelpLink> &docs = indexModel->helpEngine()->documentsForKeyword(name);
270 if (docs.size() > 1) {
271 emit documentsActivated(docs, name);
272#if QT_DEPRECATED_SINCE(5
, 15
)
274 QT_WARNING_DISABLE_DEPRECATED
275 QMultiMap<QString, QUrl> links;
276 for (
const auto &doc : docs)
277 links.insert(doc.title, doc.url);
278 emit linksActivated(links, name);
281 }
else if (!docs.isEmpty()) {
282 emit documentActivated(docs.first(), name);
283#if QT_DEPRECATED_SINCE(5
, 15
)
285 QT_WARNING_DISABLE_DEPRECATED
286 emit linkActivated(docs.first().url, name);
293
294
295
296
297void QHelpIndexWidget::activateCurrentItem()
299 showLink(currentIndex());
303
304
305
306
307
308void QHelpIndexWidget::filterIndices(
const QString &filter,
const QString &wildcard)
310 QHelpIndexModel *indexModel = qobject_cast<QHelpIndexModel *>(model());
313 const QModelIndex &idx = indexModel->filter(filter, wildcard);
315 setCurrentIndex(idx);
QHelpEngineCore * helpEngine
Combined button and popup list for selecting options.