9#include <QtCore/qfuturewatcher.h>
44void QHelpIndexModelPrivate::createIndex(
const FutureProvider &futureProvider)
46 const bool wasRunning =
bool(watcher);
47 watcher.reset(
new QFutureWatcher<QStringList>);
48 QObject::connect(watcher.get(), &QFutureWatcherBase::finished, q, [
this] {
49 if (!watcher->isCanceled()) {
50 indices = watcher->result();
53 watcher.release()->deleteLater();
54 emit q->indexCreated();
56 watcher->setFuture(futureProvider());
63 emit q->indexCreationStarted();
68
69
70
71
72
73
76
77
78
79
80
81
82
83
86
87
88
89
91QHelpIndexModel::QHelpIndexModel(QHelpEngineCore *helpEngine)
92 : QStringListModel(helpEngine)
93 , d(
new QHelpIndexModelPrivate{
this, helpEngine})
96QHelpIndexModel::~QHelpIndexModel()
102
103
104
105
106void QHelpIndexModel::createIndexForCurrentFilter()
109 d->createIndex([
this] {
return d->helpEngine->requestIndexForCurrentFilter(); });
114
115
116
117void QHelpIndexModel::createIndex(
const QString &filter)
120 d->createIndex([
this, filter] {
return d->helpEngine->requestIndex(filter); });
125void QHelpIndexModel::insertIndices()
129
130
131
132bool QHelpIndexModel::isCreatingIndex()
const
135 return bool(d->watcher);
142
143
144
145
146QHelpEngineCore *QHelpIndexModel::helpEngine()
const
148 return d->helpEngine;
152
153
154
155
156
157
158
159
160
161QModelIndex QHelpIndexModel::filter(
const QString &filter,
const QString &wildcard)
163 if (filter.isEmpty()) {
164 setStringList(d->indices);
165 return index(-1, 0, {});
168 using Checker = std::function<
bool(
const QString &)>;
169 const auto checkIndices = [
this, filter](
const Checker &checker) {
170 QStringList filteredList;
172 int perfectMatch = -1;
173 for (
const QString &index : std::as_const(d->indices)) {
174 if (checker(index)) {
175 filteredList.append(index);
176 if (perfectMatch == -1 && index.startsWith(filter, Qt::CaseInsensitive)) {
178 goodMatch = filteredList.size() - 1;
179 if (filter.size() == index.size())
180 perfectMatch = filteredList.size() - 1;
181 }
else if (perfectMatch > -1 && index == filter) {
182 perfectMatch = filteredList.size() - 1;
186 setStringList(filteredList);
187 return perfectMatch >= 0 ? perfectMatch : qMax(0, goodMatch);
190 int perfectMatch = -1;
191 if (!wildcard.isEmpty()) {
192 const auto re = QRegularExpression::wildcardToRegularExpression(wildcard,
193 QRegularExpression::UnanchoredWildcardConversion);
194 const QRegularExpression regExp(re, QRegularExpression::CaseInsensitiveOption);
195 perfectMatch = checkIndices([regExp](
const QString &index) {
196 return index.contains(regExp);
199 perfectMatch = checkIndices([filter](
const QString &index) {
200 return index.contains(filter, Qt::CaseInsensitive);
203 return index(perfectMatch, 0, {});
207
208
209
210
211
212
215
216
217
218
219
220
221
222
223
224
225
228
229
230
231
232
233
234
235
236
239
240
241
242
243
244
245
246
247
249QHelpIndexWidget::QHelpIndexWidget()
251 setEditTriggers(QAbstractItemView::NoEditTriggers);
252 setUniformItemSizes(
true);
253 connect(
this, &QAbstractItemView::activated,
this, &QHelpIndexWidget::showLink);
256void QHelpIndexWidget::showLink(
const QModelIndex &index)
258 if (!index.isValid())
261 QHelpIndexModel *indexModel = qobject_cast<QHelpIndexModel*>(model());
265 const QVariant &v = indexModel->data(index, Qt::DisplayRole);
266 const QString name = v.isValid() ? v.toString() : QString();
268 const QList<QHelpLink> &docs = indexModel->helpEngine()->documentsForKeyword(name);
269 if (docs.size() > 1) {
270 emit documentsActivated(docs, name);
271#if QT_DEPRECATED_SINCE(5
, 15
)
273 QT_WARNING_DISABLE_DEPRECATED
274 QMultiMap<QString, QUrl> links;
275 for (
const auto &doc : docs)
276 links.insert(doc.title, doc.url);
277 emit linksActivated(links, name);
280 }
else if (!docs.isEmpty()) {
281 emit documentActivated(docs.first(), name);
282#if QT_DEPRECATED_SINCE(5
, 15
)
284 QT_WARNING_DISABLE_DEPRECATED
285 emit linkActivated(docs.first().url, name);
292
293
294
295
296void QHelpIndexWidget::activateCurrentItem()
298 showLink(currentIndex());
302
303
304
305
306
307void QHelpIndexWidget::filterIndices(
const QString &filter,
const QString &wildcard)
309 QHelpIndexModel *indexModel = qobject_cast<QHelpIndexModel *>(model());
312 const QModelIndex &idx = indexModel->filter(filter, wildcard);
314 setCurrentIndex(idx);
QHelpEngineCore * helpEngine
Combined button and popup list for selecting options.