Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | | - |
41 | | - |
42 | | - |
43 | | - |
44 | | - |
45 | | - |
46 | | - |
47 | | - |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
52 | | - |
53 | | - |
54 | | - |
55 | | - |
56 | | - |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | | - |
64 | | - |
65 | | - |
66 | | - |
67 | | - |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | | - |
73 | | - |
74 | | - |
75 | | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | | - |
89 | | - |
90 | | - |
91 | | - |
92 | | - |
93 | | - |
94 | | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | | - |
103 | | - |
104 | | - |
105 | | - |
106 | | - |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
117 | | - |
118 | | - |
119 | | - |
120 | | - |
121 | | - |
122 | | - |
123 | | - |
124 | | - |
125 | | - |
126 | | - |
127 | | - |
128 | | - |
129 | | - |
130 | | - |
131 | | - |
132 | | - |
133 | | - |
134 | | - |
135 | | - |
136 | | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | | - |
144 | #include "qcompleter_p.h" | - |
145 | | - |
146 | #ifndef QT_NO_COMPLETER | - |
147 | | - |
148 | #include "QtWidgets/qscrollbar.h" | - |
149 | #include "QtCore/qstringlistmodel.h" | - |
150 | #include "QtWidgets/qdirmodel.h" | - |
151 | #include "QtWidgets/qfilesystemmodel.h" | - |
152 | #include "QtWidgets/qheaderview.h" | - |
153 | #include "QtWidgets/qlistview.h" | - |
154 | #include "QtWidgets/qapplication.h" | - |
155 | #include "QtGui/qevent.h" | - |
156 | #include "QtWidgets/qdesktopwidget.h" | - |
157 | #include "QtWidgets/qlineedit.h" | - |
158 | | - |
159 | QT_BEGIN_NAMESPACE | - |
160 | | - |
161 | QCompletionModel::QCompletionModel(QCompleterPrivate *c, QObject *parent) | - |
162 | : QAbstractProxyModel(*new QCompletionModelPrivate, parent), | - |
163 | c(c), showAll(false) | - |
164 | { | - |
165 | createEngine(); | - |
166 | } | - |
167 | | - |
168 | int QCompletionModel::columnCount(const QModelIndex &) const | - |
169 | { | - |
170 | Q_D(const QCompletionModel); | - |
171 | return d->model->columnCount(); | - |
172 | } | - |
173 | | - |
174 | void QCompletionModel::setSourceModel(QAbstractItemModel *source) | - |
175 | { | - |
176 | bool hadModel = (sourceModel() != 0); | - |
177 | | - |
178 | if (hadModel) | - |
179 | QObject::disconnect(sourceModel(), 0, this, 0); | - |
180 | | - |
181 | QAbstractProxyModel::setSourceModel(source); | - |
182 | | - |
183 | if (source) { | - |
184 | | - |
185 | connect(source, SIGNAL(modelReset()), this, SLOT(invalidate())); | - |
186 | connect(source, SIGNAL(destroyed()), this, SLOT(modelDestroyed())); | - |
187 | connect(source, SIGNAL(layoutChanged()), this, SLOT(invalidate())); | - |
188 | connect(source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted())); | - |
189 | connect(source, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(invalidate())); | - |
190 | connect(source, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(invalidate())); | - |
191 | connect(source, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(invalidate())); | - |
192 | connect(source, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(invalidate())); | - |
193 | } | - |
194 | | - |
195 | invalidate(); | - |
196 | } | - |
197 | | - |
198 | void QCompletionModel::createEngine() | - |
199 | { | - |
200 | bool sortedEngine = false; | - |
201 | if (c->filterMode == Qt::MatchStartsWith) { | - |
202 | switch (c->sorting) { | - |
203 | case QCompleter::UnsortedModel: | - |
204 | sortedEngine = false; | - |
205 | break; | - |
206 | case QCompleter::CaseSensitivelySortedModel: | - |
207 | sortedEngine = c->cs == Qt::CaseSensitive; | - |
208 | break; | - |
209 | case QCompleter::CaseInsensitivelySortedModel: | - |
210 | sortedEngine = c->cs == Qt::CaseInsensitive; | - |
211 | break; | - |
212 | } | - |
213 | } | - |
214 | | - |
215 | if (sortedEngine) | - |
216 | engine.reset(new QSortedModelEngine(c)); | - |
217 | else | - |
218 | engine.reset(new QUnsortedModelEngine(c)); | - |
219 | } | - |
220 | | - |
221 | QModelIndex QCompletionModel::mapToSource(const QModelIndex& index) const | - |
222 | { | - |
223 | Q_D(const QCompletionModel); | - |
224 | if (!index.isValid()) | - |
225 | return engine->curParent; | - |
226 | | - |
227 | int row; | - |
228 | QModelIndex parent = engine->curParent; | - |
229 | if (!showAll) { | - |
230 | if (!engine->matchCount()) | - |
231 | return QModelIndex(); | - |
232 | Q_ASSERT(index.row() < engine->matchCount()); | - |
233 | QIndexMapper& rootIndices = engine->historyMatch.indices; | - |
234 | if (index.row() < rootIndices.count()) { | - |
235 | row = rootIndices[index.row()]; | - |
236 | parent = QModelIndex(); | - |
237 | } else { | - |
238 | row = engine->curMatch.indices[index.row() - rootIndices.count()]; | - |
239 | } | - |
240 | } else { | - |
241 | row = index.row(); | - |
242 | } | - |
243 | | - |
244 | return d->model->index(row, index.column(), parent); | - |
245 | } | - |
246 | | - |
247 | QModelIndex QCompletionModel::mapFromSource(const QModelIndex& idx) const | - |
248 | { | - |
249 | if (!idx.isValid()) | - |
250 | return QModelIndex(); | - |
251 | | - |
252 | int row = -1; | - |
253 | if (!showAll) { | - |
254 | if (!engine->matchCount()) | - |
255 | return QModelIndex(); | - |
256 | | - |
257 | QIndexMapper& rootIndices = engine->historyMatch.indices; | - |
258 | if (idx.parent().isValid()) { | - |
259 | if (idx.parent() != engine->curParent) | - |
260 | return QModelIndex(); | - |
261 | } else { | - |
262 | row = rootIndices.indexOf(idx.row()); | - |
263 | if (row == -1 && engine->curParent.isValid()) | - |
264 | return QModelIndex(); | - |
265 | } | - |
266 | | - |
267 | if (row == -1) { | - |
268 | QIndexMapper& indices = engine->curMatch.indices; | - |
269 | engine->filterOnDemand(idx.row() - indices.last()); | - |
270 | row = indices.indexOf(idx.row()) + rootIndices.count(); | - |
271 | } | - |
272 | | - |
273 | if (row == -1) | - |
274 | return QModelIndex(); | - |
275 | } else { | - |
276 | if (idx.parent() != engine->curParent) | - |
277 | return QModelIndex(); | - |
278 | row = idx.row(); | - |
279 | } | - |
280 | | - |
281 | return createIndex(row, idx.column()); | - |
282 | } | - |
283 | | - |
284 | bool QCompletionModel::setCurrentRow(int row) | - |
285 | { | - |
286 | if (row < 0 || !engine->matchCount()) | - |
287 | return false; | - |
288 | | - |
289 | if (row >= engine->matchCount()) | - |
290 | engine->filterOnDemand(row + 1 - engine->matchCount()); | - |
291 | | - |
292 | if (row >= engine->matchCount()) | - |
293 | return false; | - |
294 | | - |
295 | engine->curRow = row; | - |
296 | return true; | - |
297 | } | - |
298 | | - |
299 | QModelIndex QCompletionModel::currentIndex(bool sourceIndex) const | - |
300 | { | - |
301 | if (!engine->matchCount()) | - |
302 | return QModelIndex(); | - |
303 | | - |
304 | int row = engine->curRow; | - |
305 | if (showAll) | - |
306 | row = engine->curMatch.indices[engine->curRow]; | - |
307 | | - |
308 | QModelIndex idx = createIndex(row, c->column); | - |
309 | if (!sourceIndex) | - |
310 | return idx; | - |
311 | return mapToSource(idx); | - |
312 | } | - |
313 | | - |
314 | QModelIndex QCompletionModel::index(int row, int column, const QModelIndex& parent) const | - |
315 | { | - |
316 | Q_D(const QCompletionModel); | - |
317 | if (row < 0 || column < 0 || column >= columnCount(parent) || parent.isValid()) | - |
318 | return QModelIndex(); | - |
319 | | - |
320 | if (!showAll) { | - |
321 | if (!engine->matchCount()) | - |
322 | return QModelIndex(); | - |
323 | if (row >= engine->historyMatch.indices.count()) { | - |
324 | int want = row + 1 - engine->matchCount(); | - |
325 | if (want > 0) | - |
326 | engine->filterOnDemand(want); | - |
327 | if (row >= engine->matchCount()) | - |
328 | return QModelIndex(); | - |
329 | } | - |
330 | } else { | - |
331 | if (row >= d->model->rowCount(engine->curParent)) | - |
332 | return QModelIndex(); | - |
333 | } | - |
334 | | - |
335 | return createIndex(row, column); | - |
336 | } | - |
337 | | - |
338 | int QCompletionModel::completionCount() const | - |
339 | { | - |
340 | if (!engine->matchCount()) | - |
341 | return 0; | - |
342 | | - |
343 | engine->filterOnDemand(INT_MAX); | - |
344 | return engine->matchCount(); | - |
345 | } | - |
346 | | - |
347 | int QCompletionModel::rowCount(const QModelIndex &parent) const | - |
348 | { | - |
349 | Q_D(const QCompletionModel); | - |
350 | if (parent.isValid()) | - |
351 | return 0; | - |
352 | | - |
353 | if (showAll) { | - |
354 | | - |
355 | if (engine->curParts.count() != 1 && !engine->matchCount() | - |
356 | && !engine->curParent.isValid()) | - |
357 | return 0; | - |
358 | return d->model->rowCount(engine->curParent); | - |
359 | } | - |
360 | | - |
361 | return completionCount(); | - |
362 | } | - |
363 | | - |
364 | void QCompletionModel::setFiltered(bool filtered) | - |
365 | { | - |
366 | if (showAll == !filtered) | - |
367 | return; | - |
368 | beginResetModel(); | - |
369 | showAll = !filtered; | - |
370 | endResetModel(); | - |
371 | } | - |
372 | | - |
373 | bool QCompletionModel::hasChildren(const QModelIndex &parent) const | - |
374 | { | - |
375 | Q_D(const QCompletionModel); | - |
376 | if (parent.isValid()) | - |
377 | return false; | - |
378 | | - |
379 | if (showAll) | - |
380 | return d->model->hasChildren(mapToSource(parent)); | - |
381 | | - |
382 | if (!engine->matchCount()) | - |
383 | return false; | - |
384 | | - |
385 | return true; | - |
386 | } | - |
387 | | - |
388 | QVariant QCompletionModel::data(const QModelIndex& index, int role) const | - |
389 | { | - |
390 | Q_D(const QCompletionModel); | - |
391 | return d->model->data(mapToSource(index), role); | - |
392 | } | - |
393 | | - |
394 | void QCompletionModel::modelDestroyed() | - |
395 | { | - |
396 | QAbstractProxyModel::setSourceModel(0); | - |
397 | invalidate(); | - |
398 | } | - |
399 | | - |
400 | void QCompletionModel::rowsInserted() | - |
401 | { | - |
402 | invalidate(); | - |
403 | emit rowsAdded(); | - |
404 | } | - |
405 | | - |
406 | void QCompletionModel::invalidate() | - |
407 | { | - |
408 | engine->cache.clear(); | - |
409 | filter(engine->curParts); | - |
410 | } | - |
411 | | - |
412 | void QCompletionModel::filter(const QStringList& parts) | - |
413 | { | - |
414 | Q_D(QCompletionModel); | - |
415 | beginResetModel(); | - |
416 | engine->filter(parts); | - |
417 | endResetModel(); | - |
418 | | - |
419 | if (d->model->canFetchMore(engine->curParent)) | - |
420 | d->model->fetchMore(engine->curParent); | - |
421 | } | - |
422 | | - |
423 | | - |
424 | void QCompletionEngine::filter(const QStringList& parts) | - |
425 | { | - |
426 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
427 | curParts = parts; | - |
428 | if (curParts.isEmpty()) | - |
429 | curParts.append(QString()); | - |
430 | | - |
431 | curRow = -1; | - |
432 | curParent = QModelIndex(); | - |
433 | curMatch = QMatchData(); | - |
434 | historyMatch = filterHistory(); | - |
435 | | - |
436 | if (!model) | - |
437 | return; | - |
438 | | - |
439 | QModelIndex parent; | - |
440 | for (int i = 0; i < curParts.count() - 1; i++) { | - |
441 | QString part = curParts.at(i); | - |
442 | int emi = filter(part, parent, -1).exactMatchIndex; | - |
443 | if (emi == -1) | - |
444 | return; | - |
445 | parent = model->index(emi, c->column, parent); | - |
446 | } | - |
447 | | - |
448 | | - |
449 | | - |
450 | curParent = parent; | - |
451 | if (curParts.constLast().isEmpty()) | - |
452 | curMatch = QMatchData(QIndexMapper(0, model->rowCount(curParent) - 1), -1, false); | - |
453 | else | - |
454 | curMatch = filter(curParts.constLast(), curParent, 1); | - |
455 | curRow = curMatch.isValid() ? 0 : -1; | - |
456 | } | - |
457 | | - |
458 | QMatchData QCompletionEngine::filterHistory() | - |
459 | { | - |
460 | QAbstractItemModel *source = c->proxy->sourceModel(); | - |
461 | if (curParts.count() <= 1 || c->proxy->showAll || !source) | - |
462 | return QMatchData(); | - |
463 | | - |
464 | #ifndef QT_NO_DIRMODEL | - |
465 | const bool isDirModel = (qobject_cast<QDirModel *>(source) != 0); | - |
466 | #else | - |
467 | const bool isDirModel = false; | - |
468 | #endif | - |
469 | Q_UNUSED(isDirModel) | - |
470 | #ifndef QT_NO_FILESYSTEMMODEL | - |
471 | const bool isFsModel = (qobject_cast<QFileSystemModel *>(source) != 0); | - |
472 | #else | - |
473 | const bool isFsModel = false; | - |
474 | #endif | - |
475 | Q_UNUSED(isFsModel) | - |
476 | QVector<int> v; | - |
477 | QIndexMapper im(v); | - |
478 | QMatchData m(im, -1, true); | - |
479 | | - |
480 | for (int i = 0; i < source->rowCount(); i++) { | - |
481 | QString str = source->index(i, c->column).data().toString(); | - |
482 | if (str.startsWith(c->prefix, c->cs) | - |
483 | #if !defined(Q_OS_WIN) || defined(Q_OS_WINCE) | - |
484 | && ((!isFsModel && !isDirModel) || QDir::toNativeSeparators(str) != QDir::separator()) | - |
485 | #endif | - |
486 | ) | - |
487 | m.indices.append(i); | - |
488 | } | - |
489 | return m; | - |
490 | } | - |
491 | | - |
492 | | - |
493 | bool QCompletionEngine::matchHint(QString part, const QModelIndex& parent, QMatchData *hint) | - |
494 | { | - |
495 | if (c->cs == Qt::CaseInsensitive) | - |
496 | part = part.toLower(); | - |
497 | | - |
498 | const CacheItem& map = cache[parent]; | - |
499 | | - |
500 | QString key = part; | - |
501 | while (!key.isEmpty()) { | - |
502 | key.chop(1); | - |
503 | if (map.contains(key)) { | - |
504 | *hint = map[key]; | - |
505 | return true; | - |
506 | } | - |
507 | } | - |
508 | | - |
509 | return false; | - |
510 | } | - |
511 | | - |
512 | bool QCompletionEngine::lookupCache(QString part, const QModelIndex& parent, QMatchData *m) | - |
513 | { | - |
514 | if (c->cs == Qt::CaseInsensitive) | - |
515 | part = part.toLower(); | - |
516 | const CacheItem& map = cache[parent]; | - |
517 | if (!map.contains(part)) | - |
518 | return false; | - |
519 | *m = map[part]; | - |
520 | return true; | - |
521 | } | - |
522 | | - |
523 | | - |
524 | void QCompletionEngine::saveInCache(QString part, const QModelIndex& parent, const QMatchData& m) | - |
525 | { | - |
526 | if (c->filterMode == Qt::MatchEndsWith) | - |
527 | return; | - |
528 | QMatchData old = cache[parent].take(part); | - |
529 | cost = cost + m.indices.cost() - old.indices.cost(); | - |
530 | if (cost * sizeof(int) > 1024 * 1024) { | - |
531 | QMap<QModelIndex, CacheItem>::iterator it1 = cache.begin(); | - |
532 | while (it1 != cache.end()) { | - |
533 | CacheItem& ci = it1.value(); | - |
534 | int sz = ci.count()/2; | - |
535 | QMap<QString, QMatchData>::iterator it2 = ci.begin(); | - |
536 | int i = 0; | - |
537 | while (it2 != ci.end() && i < sz) { | - |
538 | cost -= it2.value().indices.cost(); | - |
539 | it2 = ci.erase(it2); | - |
540 | i++; | - |
541 | } | - |
542 | if (ci.count() == 0) { | - |
543 | it1 = cache.erase(it1); | - |
544 | } else { | - |
545 | ++it1; | - |
546 | } | - |
547 | } | - |
548 | } | - |
549 | | - |
550 | if (c->cs == Qt::CaseInsensitive) | - |
551 | part = part.toLower(); | - |
552 | cache[parent][part] = m; | - |
553 | } | - |
554 | | - |
555 | | - |
556 | QIndexMapper QSortedModelEngine::indexHint(QString part, const QModelIndex& parent, Qt::SortOrder order) | - |
557 | { | - |
558 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
559 | | - |
560 | if (c->cs == Qt::CaseInsensitive) | - |
561 | part = part.toLower(); | - |
562 | | - |
563 | const CacheItem& map = cache[parent]; | - |
564 | | - |
565 | | - |
566 | int to = model->rowCount(parent) - 1; | - |
567 | int from = 0; | - |
568 | const CacheItem::const_iterator it = map.lowerBound(part); | - |
569 | | - |
570 | | - |
571 | for(CacheItem::const_iterator it1 = it; it1-- != map.constBegin();) { | - |
572 | const QMatchData& value = it1.value(); | - |
573 | if (value.isValid()) { | - |
574 | if (order == Qt::AscendingOrder) { | - |
575 | from = value.indices.last() + 1; | - |
576 | } else { | - |
577 | to = value.indices.first() - 1; | - |
578 | } | - |
579 | break; | - |
580 | } | - |
581 | } | - |
582 | | - |
583 | | - |
584 | for(CacheItem::const_iterator it2 = it; it2 != map.constEnd(); ++it2) { | - |
585 | const QMatchData& value = it2.value(); | - |
586 | if (value.isValid() && !it2.key().startsWith(part)) { | - |
587 | if (order == Qt::AscendingOrder) { | - |
588 | to = value.indices.first() - 1; | - |
589 | } else { | - |
590 | from = value.indices.first() + 1; | - |
591 | } | - |
592 | break; | - |
593 | } | - |
594 | } | - |
595 | | - |
596 | return QIndexMapper(from, to); | - |
597 | } | - |
598 | | - |
599 | Qt::SortOrder QSortedModelEngine::sortOrder(const QModelIndex &parent) const | - |
600 | { | - |
601 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
602 | | - |
603 | int rowCount = model->rowCount(parent); | - |
604 | if (rowCount < 2) | - |
605 | return Qt::AscendingOrder; | - |
606 | QString first = model->data(model->index(0, c->column, parent), c->role).toString(); | - |
607 | QString last = model->data(model->index(rowCount - 1, c->column, parent), c->role).toString(); | - |
608 | return QString::compare(first, last, c->cs) <= 0 ? Qt::AscendingOrder : Qt::DescendingOrder; | - |
609 | } | - |
610 | | - |
611 | QMatchData QSortedModelEngine::filter(const QString& part, const QModelIndex& parent, int) | - |
612 | { | - |
613 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
614 | | - |
615 | QMatchData hint; | - |
616 | if (lookupCache(part, parent, &hint)) | - |
617 | return hint; | - |
618 | | - |
619 | QIndexMapper indices; | - |
620 | Qt::SortOrder order = sortOrder(parent); | - |
621 | | - |
622 | if (matchHint(part, parent, &hint)) { | - |
623 | if (!hint.isValid()) | - |
624 | return QMatchData(); | - |
625 | indices = hint.indices; | - |
626 | } else { | - |
627 | indices = indexHint(part, parent, order); | - |
628 | } | - |
629 | | - |
630 | | - |
631 | int high = indices.to() + 1; | - |
632 | int low = indices.from() - 1; | - |
633 | int probe; | - |
634 | QModelIndex probeIndex; | - |
635 | QString probeData; | - |
636 | | - |
637 | while (high - low > 1) | - |
638 | { | - |
639 | probe = (high + low) / 2; | - |
640 | probeIndex = model->index(probe, c->column, parent); | - |
641 | probeData = model->data(probeIndex, c->role).toString(); | - |
642 | const int cmp = QString::compare(probeData, part, c->cs); | - |
643 | if ((order == Qt::AscendingOrder && cmp >= 0) | - |
644 | || (order == Qt::DescendingOrder && cmp < 0)) { | - |
645 | high = probe; | - |
646 | } else { | - |
647 | low = probe; | - |
648 | } | - |
649 | } | - |
650 | | - |
651 | if ((order == Qt::AscendingOrder && low == indices.to()) | - |
652 | || (order == Qt::DescendingOrder && high == indices.from())) { | - |
653 | saveInCache(part, parent, QMatchData()); | - |
654 | return QMatchData(); | - |
655 | } | - |
656 | | - |
657 | probeIndex = model->index(order == Qt::AscendingOrder ? low+1 : high-1, c->column, parent); | - |
658 | probeData = model->data(probeIndex, c->role).toString(); | - |
659 | if (!probeData.startsWith(part, c->cs)) { | - |
660 | saveInCache(part, parent, QMatchData()); | - |
661 | return QMatchData(); | - |
662 | } | - |
663 | | - |
664 | const bool exactMatch = QString::compare(probeData, part, c->cs) == 0; | - |
665 | int emi = exactMatch ? (order == Qt::AscendingOrder ? low+1 : high-1) : -1; | - |
666 | | - |
667 | int from = 0; | - |
668 | int to = 0; | - |
669 | if (order == Qt::AscendingOrder) { | - |
670 | from = low + 1; | - |
671 | high = indices.to() + 1; | - |
672 | low = from; | - |
673 | } else { | - |
674 | to = high - 1; | - |
675 | low = indices.from() - 1; | - |
676 | high = to; | - |
677 | } | - |
678 | | - |
679 | while (high - low > 1) | - |
680 | { | - |
681 | probe = (high + low) / 2; | - |
682 | probeIndex = model->index(probe, c->column, parent); | - |
683 | probeData = model->data(probeIndex, c->role).toString(); | - |
684 | const bool startsWith = probeData.startsWith(part, c->cs); | - |
685 | if ((order == Qt::AscendingOrder && startsWith) | - |
686 | || (order == Qt::DescendingOrder && !startsWith)) { | - |
687 | low = probe; | - |
688 | } else { | - |
689 | high = probe; | - |
690 | } | - |
691 | } | - |
692 | | - |
693 | QMatchData m(order == Qt::AscendingOrder ? QIndexMapper(from, high - 1) : QIndexMapper(low+1, to), emi, false); | - |
694 | saveInCache(part, parent, m); | - |
695 | return m; | - |
696 | } | - |
697 | | - |
698 | | - |
699 | int QUnsortedModelEngine::buildIndices(const QString& str, const QModelIndex& parent, int n, | - |
700 | const QIndexMapper& indices, QMatchData* m) | - |
701 | { | - |
702 | Q_ASSERT(m->partial); | - |
703 | Q_ASSERT(n != -1 || m->exactMatchIndex == -1); | - |
704 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
705 | int i, count = 0; | - |
706 | | - |
707 | for (i = 0; i < indices.count() && count != n; ++i) { | - |
708 | QModelIndex idx = model->index(indices[i], c->column, parent); | - |
709 | | - |
710 | if (!(model->flags(idx) & Qt::ItemIsSelectable)) | - |
711 | continue; | - |
712 | | - |
713 | QString data = model->data(idx, c->role).toString(); | - |
714 | | - |
715 | switch (c->filterMode) { | - |
716 | case Qt::MatchStartsWith: | - |
717 | if (!data.startsWith(str, c->cs)) | - |
718 | continue; | - |
719 | break; | - |
720 | case Qt::MatchContains: | - |
721 | if (!data.contains(str, c->cs)) | - |
722 | continue; | - |
723 | break; | - |
724 | case Qt::MatchEndsWith: | - |
725 | if (!data.endsWith(str, c->cs)) | - |
726 | continue; | - |
727 | break; | - |
728 | case Qt::MatchExactly: | - |
729 | case Qt::MatchFixedString: | - |
730 | case Qt::MatchCaseSensitive: | - |
731 | case Qt::MatchRegExp: | - |
732 | case Qt::MatchWildcard: | - |
733 | case Qt::MatchWrap: | - |
734 | case Qt::MatchRecursive: | - |
735 | Q_UNREACHABLE(); | - |
736 | break; | - |
737 | } | - |
738 | m->indices.append(indices[i]); | - |
739 | ++count; | - |
740 | if (m->exactMatchIndex == -1 && QString::compare(data, str, c->cs) == 0) { | - |
741 | m->exactMatchIndex = indices[i]; | - |
742 | if (n == -1) | - |
743 | return indices[i]; | - |
744 | } | - |
745 | } | - |
746 | return indices[i-1]; | - |
747 | } | - |
748 | | - |
749 | void QUnsortedModelEngine::filterOnDemand(int n) | - |
750 | { | - |
751 | Q_ASSERT(matchCount()); | - |
752 | if (!curMatch.partial)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
753 | return; never executed: return; | 0 |
754 | Q_ASSERT(n >= -1); | - |
755 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
756 | int lastRow = model->rowCount(curParent) - 1; | - |
757 | QIndexMapper im(curMatch.indices.last() + 1, lastRow); | - |
758 | int lastIndex = buildIndices(curParts.lastconstLast(), curParent, n, im, &curMatch); | - |
759 | curMatch.partial = (lastRow != lastIndex); | - |
760 | saveInCache(curParts.lastconstLast(), curParent, curMatch); | - |
761 | } never executed: end of block | 0 |
762 | | - |
763 | QMatchData QUnsortedModelEngine::filter(const QString& part, const QModelIndex& parent, int n) | - |
764 | { | - |
765 | QMatchData hint; | - |
766 | | - |
767 | QVector<int> v; | - |
768 | QIndexMapper im(v); | - |
769 | QMatchData m(im, -1, true); | - |
770 | | - |
771 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
772 | bool foundInCache = lookupCache(part, parent, &m); | - |
773 | | - |
774 | if (!foundInCache) { | - |
775 | if (matchHint(part, parent, &hint) && !hint.isValid()) | - |
776 | return QMatchData(); | - |
777 | } | - |
778 | | - |
779 | if (!foundInCache && !hint.isValid()) { | - |
780 | const int lastRow = model->rowCount(parent) - 1; | - |
781 | QIndexMapper all(0, lastRow); | - |
782 | int lastIndex = buildIndices(part, parent, n, all, &m); | - |
783 | m.partial = (lastIndex != lastRow); | - |
784 | } else { | - |
785 | if (!foundInCache) { | - |
786 | buildIndices(part, parent, INT_MAX, hint.indices, &m); | - |
787 | m.partial = hint.partial; | - |
788 | } | - |
789 | if (m.partial && ((n == -1 && m.exactMatchIndex == -1) || (m.indices.count() < n))) { | - |
790 | | - |
791 | const int lastRow = model->rowCount(parent) - 1; | - |
792 | QIndexMapper rest(hint.indices.last() + 1, lastRow); | - |
793 | int want = n == -1 ? -1 : n - m.indices.count(); | - |
794 | int lastIndex = buildIndices(part, parent, want, rest, &m); | - |
795 | m.partial = (lastRow != lastIndex); | - |
796 | } | - |
797 | } | - |
798 | | - |
799 | saveInCache(part, parent, m); | - |
800 | return m; | - |
801 | } | - |
802 | | - |
803 | | - |
804 | QCompleterPrivate::QCompleterPrivate() | - |
805 | : widget(0), proxy(0), popup(0), filterMode(Qt::MatchStartsWith), cs(Qt::CaseSensitive), | - |
806 | role(Qt::EditRole), column(0), maxVisibleItems(7), sorting(QCompleter::UnsortedModel), | - |
807 | wrap(true), eatFocusOut(true), hiddenBecauseNoMatch(false) | - |
808 | { | - |
809 | } | - |
810 | | - |
811 | void QCompleterPrivate::init(QAbstractItemModel *m) | - |
812 | { | - |
813 | Q_Q(QCompleter); | - |
814 | proxy = new QCompletionModel(this, q); | - |
815 | QObject::connect(proxy, SIGNAL(rowsAdded()), q, SLOT(_q_autoResizePopup())); | - |
816 | q->setModel(m); | - |
817 | #ifdef QT_NO_LISTVIEW | - |
818 | q->setCompletionMode(QCompleter::InlineCompletion); | - |
819 | #else | - |
820 | q->setCompletionMode(QCompleter::PopupCompletion); | - |
821 | #endif // QT_NO_LISTVIEW | - |
822 | } | - |
823 | | - |
824 | void QCompleterPrivate::setCurrentIndex(QModelIndex index, bool select) | - |
825 | { | - |
826 | Q_Q(QCompleter); | - |
827 | if (!q->popup()) | - |
828 | return; | - |
829 | if (!select) { | - |
830 | popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); | - |
831 | } else { | - |
832 | if (!index.isValid()) | - |
833 | popup->selectionModel()->clear(); | - |
834 | else | - |
835 | popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | - |
836 | | QItemSelectionModel::Rows); | - |
837 | } | - |
838 | index = popup->selectionModel()->currentIndex(); | - |
839 | if (!index.isValid()) | - |
840 | popup->scrollToTop(); | - |
841 | else | - |
842 | popup->scrollTo(index, QAbstractItemView::PositionAtTop); | - |
843 | } | - |
844 | | - |
845 | void QCompleterPrivate::_q_completionSelected(const QItemSelection& selection) | - |
846 | { | - |
847 | QModelIndex index; | - |
848 | if (!selection.indexes().isEmpty()) | - |
849 | index = selection.indexes().first(); | - |
850 | | - |
851 | _q_complete(index, true); | - |
852 | } | - |
853 | | - |
854 | void QCompleterPrivate::_q_complete(QModelIndex index, bool highlighted) | - |
855 | { | - |
856 | Q_Q(QCompleter); | - |
857 | QString completion; | - |
858 | | - |
859 | if (!index.isValid() || (!proxy->showAll && (index.row() >= proxy->engine->matchCount()))) { | - |
860 | completion = prefix; | - |
861 | index = QModelIndex(); | - |
862 | } else { | - |
863 | if (!(index.flags() & Qt::ItemIsEnabled)) | - |
864 | return; | - |
865 | QModelIndex si = proxy->mapToSource(index); | - |
866 | si = si.sibling(si.row(), column); | - |
867 | completion = q->pathFromIndex(si); | - |
868 | #ifndef QT_NO_DIRMODEL | - |
869 | | - |
870 | if (mode == QCompleter::InlineCompletion) { | - |
871 | if (qobject_cast<QDirModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir()) | - |
872 | completion += QDir::separator(); | - |
873 | } | - |
874 | #endif | - |
875 | #ifndef QT_NO_FILESYSTEMMODEL | - |
876 | | - |
877 | if (mode == QCompleter::InlineCompletion) { | - |
878 | if (qobject_cast<QFileSystemModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir()) | - |
879 | completion += QDir::separator(); | - |
880 | } | - |
881 | #endif | - |
882 | } | - |
883 | | - |
884 | if (highlighted) { | - |
885 | emit q->highlighted(index); | - |
886 | emit q->highlighted(completion); | - |
887 | } else { | - |
888 | emit q->activated(index); | - |
889 | emit q->activated(completion); | - |
890 | } | - |
891 | } | - |
892 | | - |
893 | void QCompleterPrivate::_q_autoResizePopup() | - |
894 | { | - |
895 | if (!popup || !popup->isVisible()) | - |
896 | return; | - |
897 | showPopup(popupRect); | - |
898 | } | - |
899 | | - |
900 | void QCompleterPrivate::showPopup(const QRect& rect) | - |
901 | { | - |
902 | const QRect screen = QApplication::desktop()->availableGeometry(widget); | - |
903 | Qt::LayoutDirection dir = widget->layoutDirection(); | - |
904 | QPoint pos; | - |
905 | int rh, w; | - |
906 | int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3; | - |
907 | QScrollBar *hsb = popup->horizontalScrollBar(); | - |
908 | if (hsb && hsb->isVisible()) | - |
909 | h += popup->horizontalScrollBar()->sizeHint().height(); | - |
910 | | - |
911 | if (rect.isValid()) { | - |
912 | rh = rect.height(); | - |
913 | w = rect.width(); | - |
914 | pos = widget->mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft()); | - |
915 | } else { | - |
916 | rh = widget->height(); | - |
917 | pos = widget->mapToGlobal(QPoint(0, widget->height() - 2)); | - |
918 | w = widget->width(); | - |
919 | } | - |
920 | | - |
921 | if (w > screen.width()) | - |
922 | w = screen.width(); | - |
923 | if ((pos.x() + w) > (screen.x() + screen.width())) | - |
924 | pos.setX(screen.x() + screen.width() - w); | - |
925 | if (pos.x() < screen.x()) | - |
926 | pos.setX(screen.x()); | - |
927 | | - |
928 | int top = pos.y() - rh - screen.top() + 2; | - |
929 | int bottom = screen.bottom() - pos.y(); | - |
930 | h = qMax(h, popup->minimumHeight()); | - |
931 | if (h > bottom) { | - |
932 | h = qMin(qMax(top, bottom), h); | - |
933 | | - |
934 | if (top > bottom) | - |
935 | pos.setY(pos.y() - h - rh + 2); | - |
936 | } | - |
937 | | - |
938 | popup->setGeometry(pos.x(), pos.y(), w, h); | - |
939 | | - |
940 | if (!popup->isVisible()) | - |
941 | popup->show(); | - |
942 | } | - |
943 | | - |
944 | void QCompleterPrivate::_q_fileSystemModelDirectoryLoaded(const QString &path) | - |
945 | { | - |
946 | Q_Q(QCompleter); | - |
947 | | - |
948 | | - |
949 | | - |
950 | if (hiddenBecauseNoMatch | - |
951 | && prefix.startsWith(path) && prefix != (path + QLatin1Char('/')) | - |
952 | && widget) { | - |
953 | q->complete(); | - |
954 | } | - |
955 | } | - |
956 | | - |
957 | | - |
958 | | - |
959 | | - |
960 | QCompleter::QCompleter(QObject *parent) | - |
961 | : QObject(*new QCompleterPrivate(), parent) | - |
962 | { | - |
963 | Q_D(QCompleter); | - |
964 | d->init(); | - |
965 | } | - |
966 | | - |
967 | | - |
968 | | - |
969 | | - |
970 | | - |
971 | QCompleter::QCompleter(QAbstractItemModel *model, QObject *parent) | - |
972 | : QObject(*new QCompleterPrivate(), parent) | - |
973 | { | - |
974 | Q_D(QCompleter); | - |
975 | d->init(model); | - |
976 | } | - |
977 | | - |
978 | #ifndef QT_NO_STRINGLISTMODEL | - |
979 | | - |
980 | | - |
981 | | - |
982 | | - |
983 | QCompleter::QCompleter(const QStringList& list, QObject *parent) | - |
984 | : QObject(*new QCompleterPrivate(), parent) | - |
985 | { | - |
986 | Q_D(QCompleter); | - |
987 | d->init(new QStringListModel(list, this)); | - |
988 | } | - |
989 | #endif // QT_NO_STRINGLISTMODEL | - |
990 | | - |
991 | | - |
992 | | - |
993 | | - |
994 | QCompleter::~QCompleter() | - |
995 | { | - |
996 | } | - |
997 | | - |
998 | | - |
999 | | - |
1000 | | - |
1001 | | - |
1002 | | - |
1003 | | - |
1004 | | - |
1005 | | - |
1006 | | - |
1007 | void QCompleter::setWidget(QWidget *widget) | - |
1008 | { | - |
1009 | Q_D(QCompleter); | - |
1010 | if (d->widget) | - |
1011 | d->widget->removeEventFilter(this); | - |
1012 | d->widget = widget; | - |
1013 | if (d->widget) | - |
1014 | d->widget->installEventFilter(this); | - |
1015 | if (d->popup) { | - |
1016 | d->popup->hide(); | - |
1017 | d->popup->setFocusProxy(d->widget); | - |
1018 | } | - |
1019 | } | - |
1020 | | - |
1021 | | - |
1022 | | - |
1023 | | - |
1024 | | - |
1025 | | - |
1026 | QWidget *QCompleter::widget() const | - |
1027 | { | - |
1028 | Q_D(const QCompleter); | - |
1029 | return d->widget; | - |
1030 | } | - |
1031 | | - |
1032 | | - |
1033 | | - |
1034 | | - |
1035 | | - |
1036 | | - |
1037 | | - |
1038 | | - |
1039 | | - |
1040 | | - |
1041 | | - |
1042 | | - |
1043 | void QCompleter::setModel(QAbstractItemModel *model) | - |
1044 | { | - |
1045 | Q_D(QCompleter); | - |
1046 | QAbstractItemModel *oldModel = d->proxy->sourceModel(); | - |
1047 | #ifndef QT_NO_FILESYSTEMMODEL | - |
1048 | if (qobject_cast<const QFileSystemModel *>(oldModel)) | - |
1049 | setCompletionRole(Qt::EditRole); | - |
1050 | #endif | - |
1051 | d->proxy->setSourceModel(model); | - |
1052 | if (d->popup) | - |
1053 | setPopup(d->popup); | - |
1054 | if (oldModel && oldModel->QObject::parent() == this) | - |
1055 | delete oldModel; | - |
1056 | #ifndef QT_NO_DIRMODEL | - |
1057 | if (qobject_cast<QDirModel *>(model)) { | - |
1058 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
1059 | setCaseSensitivity(Qt::CaseInsensitive); | - |
1060 | #else | - |
1061 | setCaseSensitivity(Qt::CaseSensitive); | - |
1062 | #endif | - |
1063 | } | - |
1064 | #endif // QT_NO_DIRMODEL | - |
1065 | #ifndef QT_NO_FILESYSTEMMODEL | - |
1066 | QFileSystemModel *fsModel = qobject_cast<QFileSystemModel *>(model); | - |
1067 | if (fsModel) { | - |
1068 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
1069 | setCaseSensitivity(Qt::CaseInsensitive); | - |
1070 | #else | - |
1071 | setCaseSensitivity(Qt::CaseSensitive); | - |
1072 | #endif | - |
1073 | setCompletionRole(QFileSystemModel::FileNameRole); | - |
1074 | connect(fsModel, SIGNAL(directoryLoaded(QString)), this, SLOT(_q_fileSystemModelDirectoryLoaded(QString))); | - |
1075 | } | - |
1076 | #endif // QT_NO_FILESYSTEMMODEL | - |
1077 | } | - |
1078 | | - |
1079 | | - |
1080 | | - |
1081 | | - |
1082 | | - |
1083 | | - |
1084 | QAbstractItemModel *QCompleter::model() const | - |
1085 | { | - |
1086 | Q_D(const QCompleter); | - |
1087 | return d->proxy->sourceModel(); | - |
1088 | } | - |
1089 | | - |
1090 | | - |
1091 | | - |
1092 | | - |
1093 | | - |
1094 | | - |
1095 | | - |
1096 | | - |
1097 | | - |
1098 | | - |
1099 | | - |
1100 | | - |
1101 | | - |
1102 | | - |
1103 | | - |
1104 | | - |
1105 | | - |
1106 | | - |
1107 | | - |
1108 | void QCompleter::setCompletionMode(QCompleter::CompletionMode mode) | - |
1109 | { | - |
1110 | Q_D(QCompleter); | - |
1111 | d->mode = mode; | - |
1112 | d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion); | - |
1113 | | - |
1114 | if (mode == QCompleter::InlineCompletion) { | - |
1115 | if (d->widget) | - |
1116 | d->widget->removeEventFilter(this); | - |
1117 | if (d->popup) { | - |
1118 | d->popup->deleteLater(); | - |
1119 | d->popup = 0; | - |
1120 | } | - |
1121 | } else { | - |
1122 | if (d->widget) | - |
1123 | d->widget->installEventFilter(this); | - |
1124 | } | - |
1125 | } | - |
1126 | | - |
1127 | QCompleter::CompletionMode QCompleter::completionMode() const | - |
1128 | { | - |
1129 | Q_D(const QCompleter); | - |
1130 | return d->mode; | - |
1131 | } | - |
1132 | | - |
1133 | | - |
1134 | | - |
1135 | | - |
1136 | | - |
1137 | | - |
1138 | | - |
1139 | | - |
1140 | | - |
1141 | | - |
1142 | | - |
1143 | | - |
1144 | | - |
1145 | | - |
1146 | | - |
1147 | | - |
1148 | | - |
1149 | | - |
1150 | void QCompleter::setFilterMode(Qt::MatchFlags filterMode) | - |
1151 | { | - |
1152 | Q_D(QCompleter); | - |
1153 | | - |
1154 | if (d->filterMode == filterMode)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
1155 | return; never executed: return; | 0 |
1156 | | - |
1157 | if (Q_UNLIKELY(filterMode != Qt::MatchStartsWith &&TRUE | never evaluated | FALSE | never evaluated |
| 0 |
1158 | filterMode != Qt::MatchContains && | - |
1159 | filterMode != Qt::MatchEndsWith))) { | - |
1160 | qWarning("Unhandled QCompleter::filterMode flag is used."); | - |
1161 | return; never executed: return; | 0 |
1162 | } | - |
1163 | | - |
1164 | d->filterMode = filterMode; | - |
1165 | d->proxy->createEngine(); | - |
1166 | d->proxy->invalidate(); | - |
1167 | } never executed: end of block | 0 |
1168 | | - |
1169 | Qt::MatchFlags QCompleter::filterMode() const | - |
1170 | { | - |
1171 | Q_D(const QCompleter); | - |
1172 | return d->filterMode; | - |
1173 | } | - |
1174 | | - |
1175 | | - |
1176 | | - |
1177 | | - |
1178 | | - |
1179 | | - |
1180 | | - |
1181 | | - |
1182 | | - |
1183 | | - |
1184 | | - |
1185 | | - |
1186 | | - |
1187 | | - |
1188 | | - |
1189 | | - |
1190 | void QCompleter::setPopup(QAbstractItemView *popup) | - |
1191 | { | - |
1192 | Q_D(QCompleter); | - |
1193 | Q_ASSERT(popup != 0); | - |
1194 | if (d->popup) { | - |
1195 | QObject::disconnect(d->popup->selectionModel(), 0, this, 0); | - |
1196 | QObject::disconnect(d->popup, 0, this, 0); | - |
1197 | } | - |
1198 | if (d->popup != popup) | - |
1199 | delete d->popup; | - |
1200 | if (popup->model() != d->proxy) | - |
1201 | popup->setModel(d->proxy); | - |
1202 | popup->hide(); | - |
1203 | | - |
1204 | Qt::FocusPolicy origPolicy = Qt::NoFocus; | - |
1205 | if (d->widget) | - |
1206 | origPolicy = d->widget->focusPolicy(); | - |
1207 | popup->setParent(0, Qt::Popup); | - |
1208 | popup->setFocusPolicy(Qt::NoFocus); | - |
1209 | if (d->widget) | - |
1210 | d->widget->setFocusPolicy(origPolicy); | - |
1211 | | - |
1212 | popup->setFocusProxy(d->widget); | - |
1213 | popup->installEventFilter(this); | - |
1214 | popup->setItemDelegate(new QCompleterItemDelegate(popup)); | - |
1215 | #ifndef QT_NO_LISTVIEW | - |
1216 | if (QListView *listView = qobject_cast<QListView *>(popup)) { | - |
1217 | listView->setModelColumn(d->column); | - |
1218 | } | - |
1219 | #endif | - |
1220 | | - |
1221 | QObject::connect(popup, SIGNAL(clicked(QModelIndex)), | - |
1222 | this, SLOT(_q_complete(QModelIndex))); | - |
1223 | QObject::connect(this, SIGNAL(activated(QModelIndex)), | - |
1224 | popup, SLOT(hide())); | - |
1225 | | - |
1226 | QObject::connect(popup->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), | - |
1227 | this, SLOT(_q_completionSelected(QItemSelection))); | - |
1228 | d->popup = popup; | - |
1229 | } | - |
1230 | | - |
1231 | | - |
1232 | | - |
1233 | | - |
1234 | | - |
1235 | | - |
1236 | QAbstractItemView *QCompleter::popup() const | - |
1237 | { | - |
1238 | Q_D(const QCompleter); | - |
1239 | #ifndef QT_NO_LISTVIEW | - |
1240 | if (!d->popup && completionMode() != QCompleter::InlineCompletion) { | - |
1241 | QListView *listView = new QListView; | - |
1242 | listView->setEditTriggers(QAbstractItemView::NoEditTriggers); | - |
1243 | listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | - |
1244 | listView->setSelectionBehavior(QAbstractItemView::SelectRows); | - |
1245 | listView->setSelectionMode(QAbstractItemView::SingleSelection); | - |
1246 | listView->setModelColumn(d->column); | - |
1247 | QCompleter *that = const_cast<QCompleter*>(this); | - |
1248 | that->setPopup(listView); | - |
1249 | } | - |
1250 | #endif // QT_NO_LISTVIEW | - |
1251 | return d->popup; | - |
1252 | } | - |
1253 | | - |
1254 | | - |
1255 | | - |
1256 | | - |
1257 | bool QCompleter::event(QEvent *ev) | - |
1258 | { | - |
1259 | return QObject::event(ev); | - |
1260 | } | - |
1261 | | - |
1262 | | - |
1263 | | - |
1264 | | - |
1265 | bool QCompleter::eventFilter(QObject *o, QEvent *e) | - |
1266 | { | - |
1267 | Q_D(QCompleter); | - |
1268 | | - |
1269 | if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) { | - |
1270 | d->hiddenBecauseNoMatch = false; | - |
1271 | if (d->popup && d->popup->isVisible()) | - |
1272 | return true; | - |
1273 | } | - |
1274 | | - |
1275 | if (o != d->popup) | - |
1276 | return QObject::eventFilter(o, e); | - |
1277 | | - |
1278 | switch (e->type()) { | - |
1279 | case QEvent::KeyPress: { | - |
1280 | QKeyEvent *ke = static_cast<QKeyEvent *>(e); | - |
1281 | | - |
1282 | QModelIndex curIndex = d->popup->currentIndex(); | - |
1283 | QModelIndexList selList = d->popup->selectionModel()->selectedIndexes(); | - |
1284 | | - |
1285 | const int key = ke->key(); | - |
1286 | | - |
1287 | if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid() | - |
1288 | && d->mode == QCompleter::UnfilteredPopupCompletion) { | - |
1289 | d->setCurrentIndex(curIndex); | - |
1290 | return true; | - |
1291 | } | - |
1292 | | - |
1293 | | - |
1294 | | - |
1295 | switch (key) { | - |
1296 | case Qt::Key_End: | - |
1297 | case Qt::Key_Home: | - |
1298 | if (ke->modifiers() & Qt::ControlModifier) | - |
1299 | return false; | - |
1300 | break; | - |
1301 | | - |
1302 | case Qt::Key_Up: | - |
1303 | if (!curIndex.isValid()) { | - |
1304 | int rowCount = d->proxy->rowCount(); | - |
1305 | QModelIndex lastIndex = d->proxy->index(rowCount - 1, d->column); | - |
1306 | d->setCurrentIndex(lastIndex); | - |
1307 | return true; | - |
1308 | } else if (curIndex.row() == 0) { | - |
1309 | if (d->wrap) | - |
1310 | d->setCurrentIndex(QModelIndex()); | - |
1311 | return true; | - |
1312 | } | - |
1313 | return false; | - |
1314 | | - |
1315 | case Qt::Key_Down: | - |
1316 | if (!curIndex.isValid()) { | - |
1317 | QModelIndex firstIndex = d->proxy->index(0, d->column); | - |
1318 | d->setCurrentIndex(firstIndex); | - |
1319 | return true; | - |
1320 | } else if (curIndex.row() == d->proxy->rowCount() - 1) { | - |
1321 | if (d->wrap) | - |
1322 | d->setCurrentIndex(QModelIndex()); | - |
1323 | return true; | - |
1324 | } | - |
1325 | return false; | - |
1326 | | - |
1327 | case Qt::Key_PageUp: | - |
1328 | case Qt::Key_PageDown: | - |
1329 | return false; | - |
1330 | } | - |
1331 | | - |
1332 | | - |
1333 | | - |
1334 | d->eatFocusOut = false; | - |
1335 | (static_cast<QObject *>(d->widget))->event(ke); | - |
1336 | d->eatFocusOut = true; | - |
1337 | if (!d->widget || e->isAccepted() || !d->popup->isVisible()) { | - |
1338 | | - |
1339 | if (d->widget && (!d->widget->hasFocus() | - |
1340 | #ifdef QT_KEYPAD_NAVIGATION | - |
1341 | || (QApplication::keypadNavigationEnabled() && !d->widget->hasEditFocus()) | - |
1342 | #endif | - |
1343 | )) | - |
1344 | d->popup->hide(); | - |
1345 | if (e->isAccepted()) | - |
1346 | return true; | - |
1347 | } | - |
1348 | | - |
1349 | | - |
1350 | if (ke->matches(QKeySequence::Cancel)) { | - |
1351 | d->popup->hide(); | - |
1352 | return true; | - |
1353 | } | - |
1354 | | - |
1355 | switch (key) { | - |
1356 | #ifdef QT_KEYPAD_NAVIGATION | - |
1357 | case Qt::Key_Select: | - |
1358 | if (!QApplication::keypadNavigationEnabled()) | - |
1359 | break; | - |
1360 | #endif | - |
1361 | case Qt::Key_Return: | - |
1362 | case Qt::Key_Enter: | - |
1363 | case Qt::Key_Tab: | - |
1364 | d->popup->hide(); | - |
1365 | if (curIndex.isValid()) | - |
1366 | d->_q_complete(curIndex); | - |
1367 | break; | - |
1368 | | - |
1369 | case Qt::Key_F4: | - |
1370 | if (ke->modifiers() & Qt::AltModifier) | - |
1371 | d->popup->hide(); | - |
1372 | break; | - |
1373 | | - |
1374 | case Qt::Key_Backtab: | - |
1375 | d->popup->hide(); | - |
1376 | break; | - |
1377 | | - |
1378 | default: | - |
1379 | break; | - |
1380 | } | - |
1381 | | - |
1382 | return true; | - |
1383 | } | - |
1384 | | - |
1385 | #ifdef QT_KEYPAD_NAVIGATION | - |
1386 | case QEvent::KeyRelease: { | - |
1387 | QKeyEvent *ke = static_cast<QKeyEvent *>(e); | - |
1388 | if (QApplication::keypadNavigationEnabled() && ke->key() == Qt::Key_Back) { | - |
1389 | | - |
1390 | | - |
1391 | | - |
1392 | | - |
1393 | | - |
1394 | | - |
1395 | d->eatFocusOut = false; | - |
1396 | static_cast<QObject *>(d->widget)->event(ke); | - |
1397 | d->eatFocusOut = true; | - |
1398 | } | - |
1399 | break; | - |
1400 | } | - |
1401 | #endif | - |
1402 | | - |
1403 | case QEvent::MouseButtonPress: { | - |
1404 | #ifdef QT_KEYPAD_NAVIGATION | - |
1405 | if (QApplication::keypadNavigationEnabled()) { | - |
1406 | | - |
1407 | QWidget *source = qobject_cast<QWidget *>(o); | - |
1408 | if (source) { | - |
1409 | QPoint pos = source->mapToGlobal((static_cast<QMouseEvent *>(e))->pos()); | - |
1410 | QWidget *target = QApplication::widgetAt(pos); | - |
1411 | if (target && (d->widget->isAncestorOf(target) || | - |
1412 | target == d->widget)) { | - |
1413 | d->eatFocusOut = false; | - |
1414 | static_cast<QObject *>(target)->event(e); | - |
1415 | d->eatFocusOut = true; | - |
1416 | return true; | - |
1417 | } | - |
1418 | } | - |
1419 | } | - |
1420 | #endif | - |
1421 | if (!d->popup->underMouse()) { | - |
1422 | d->popup->hide(); | - |
1423 | return true; | - |
1424 | } | - |
1425 | } | - |
1426 | return false; | - |
1427 | | - |
1428 | case QEvent::InputMethod: | - |
1429 | case QEvent::ShortcutOverride: | - |
1430 | QApplication::sendEvent(d->widget, e); | - |
1431 | break; | - |
1432 | | - |
1433 | default: | - |
1434 | return false; | - |
1435 | } | - |
1436 | return false; | - |
1437 | } | - |
1438 | | - |
1439 | | - |
1440 | | - |
1441 | | - |
1442 | | - |
1443 | | - |
1444 | | - |
1445 | | - |
1446 | | - |
1447 | | - |
1448 | | - |
1449 | void QCompleter::complete(const QRect& rect) | - |
1450 | { | - |
1451 | Q_D(QCompleter); | - |
1452 | QModelIndex idx = d->proxy->currentIndex(false); | - |
1453 | d->hiddenBecauseNoMatch = false; | - |
1454 | if (d->mode == QCompleter::InlineCompletion) { | - |
1455 | if (idx.isValid()) | - |
1456 | d->_q_complete(idx, true); | - |
1457 | return; | - |
1458 | } | - |
1459 | | - |
1460 | Q_ASSERT(d->widget != 0); | - |
1461 | if ((d->mode == QCompleter::PopupCompletion && !idx.isValid()) | - |
1462 | || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) { | - |
1463 | if (d->popup) | - |
1464 | d->popup->hide(); | - |
1465 | d->hiddenBecauseNoMatch = true; | - |
1466 | return; | - |
1467 | } | - |
1468 | | - |
1469 | popup(); | - |
1470 | if (d->mode == QCompleter::UnfilteredPopupCompletion) | - |
1471 | d->setCurrentIndex(idx, false); | - |
1472 | | - |
1473 | d->showPopup(rect); | - |
1474 | d->popupRect = rect; | - |
1475 | } | - |
1476 | | - |
1477 | | - |
1478 | | - |
1479 | | - |
1480 | | - |
1481 | | - |
1482 | | - |
1483 | | - |
1484 | | - |
1485 | | - |
1486 | bool QCompleter::setCurrentRow(int row) | - |
1487 | { | - |
1488 | Q_D(QCompleter); | - |
1489 | return d->proxy->setCurrentRow(row); | - |
1490 | } | - |
1491 | | - |
1492 | | - |
1493 | | - |
1494 | | - |
1495 | | - |
1496 | | - |
1497 | int QCompleter::currentRow() const | - |
1498 | { | - |
1499 | Q_D(const QCompleter); | - |
1500 | return d->proxy->currentRow(); | - |
1501 | } | - |
1502 | | - |
1503 | | - |
1504 | | - |
1505 | | - |
1506 | | - |
1507 | | - |
1508 | int QCompleter::completionCount() const | - |
1509 | { | - |
1510 | Q_D(const QCompleter); | - |
1511 | return d->proxy->completionCount(); | - |
1512 | } | - |
1513 | | - |
1514 | | - |
1515 | | - |
1516 | | - |
1517 | | - |
1518 | | - |
1519 | | - |
1520 | | - |
1521 | | - |
1522 | | - |
1523 | | - |
1524 | | - |
1525 | | - |
1526 | | - |
1527 | | - |
1528 | | - |
1529 | | - |
1530 | | - |
1531 | | - |
1532 | | - |
1533 | | - |
1534 | | - |
1535 | | - |
1536 | | - |
1537 | | - |
1538 | | - |
1539 | | - |
1540 | | - |
1541 | | - |
1542 | | - |
1543 | | - |
1544 | | - |
1545 | | - |
1546 | | - |
1547 | | - |
1548 | void QCompleter::setModelSorting(QCompleter::ModelSorting sorting) | - |
1549 | { | - |
1550 | Q_D(QCompleter); | - |
1551 | if (d->sorting == sorting) | - |
1552 | return; | - |
1553 | d->sorting = sorting; | - |
1554 | d->proxy->createEngine(); | - |
1555 | d->proxy->invalidate(); | - |
1556 | } | - |
1557 | | - |
1558 | QCompleter::ModelSorting QCompleter::modelSorting() const | - |
1559 | { | - |
1560 | Q_D(const QCompleter); | - |
1561 | return d->sorting; | - |
1562 | } | - |
1563 | | - |
1564 | | - |
1565 | | - |
1566 | | - |
1567 | | - |
1568 | | - |
1569 | | - |
1570 | | - |
1571 | | - |
1572 | | - |
1573 | | - |
1574 | | - |
1575 | void QCompleter::setCompletionColumn(int column) | - |
1576 | { | - |
1577 | Q_D(QCompleter); | - |
1578 | if (d->column == column) | - |
1579 | return; | - |
1580 | #ifndef QT_NO_LISTVIEW | - |
1581 | if (QListView *listView = qobject_cast<QListView *>(d->popup)) | - |
1582 | listView->setModelColumn(column); | - |
1583 | #endif | - |
1584 | d->column = column; | - |
1585 | d->proxy->invalidate(); | - |
1586 | } | - |
1587 | | - |
1588 | int QCompleter::completionColumn() const | - |
1589 | { | - |
1590 | Q_D(const QCompleter); | - |
1591 | return d->column; | - |
1592 | } | - |
1593 | | - |
1594 | | - |
1595 | | - |
1596 | | - |
1597 | | - |
1598 | | - |
1599 | | - |
1600 | | - |
1601 | | - |
1602 | void QCompleter::setCompletionRole(int role) | - |
1603 | { | - |
1604 | Q_D(QCompleter); | - |
1605 | if (d->role == role) | - |
1606 | return; | - |
1607 | d->role = role; | - |
1608 | d->proxy->invalidate(); | - |
1609 | } | - |
1610 | | - |
1611 | int QCompleter::completionRole() const | - |
1612 | { | - |
1613 | Q_D(const QCompleter); | - |
1614 | return d->role; | - |
1615 | } | - |
1616 | | - |
1617 | | - |
1618 | | - |
1619 | | - |
1620 | | - |
1621 | | - |
1622 | | - |
1623 | | - |
1624 | void QCompleter::setWrapAround(bool wrap) | - |
1625 | { | - |
1626 | Q_D(QCompleter); | - |
1627 | if (d->wrap == wrap) | - |
1628 | return; | - |
1629 | d->wrap = wrap; | - |
1630 | } | - |
1631 | | - |
1632 | bool QCompleter::wrapAround() const | - |
1633 | { | - |
1634 | Q_D(const QCompleter); | - |
1635 | return d->wrap; | - |
1636 | } | - |
1637 | | - |
1638 | | - |
1639 | | - |
1640 | | - |
1641 | | - |
1642 | | - |
1643 | | - |
1644 | | - |
1645 | int QCompleter::maxVisibleItems() const | - |
1646 | { | - |
1647 | Q_D(const QCompleter); | - |
1648 | return d->maxVisibleItems; | - |
1649 | } | - |
1650 | | - |
1651 | void QCompleter::setMaxVisibleItems(int maxItems) | - |
1652 | { | - |
1653 | Q_D(QCompleter); | - |
1654 | if (Q_UNLIKELY(maxItems < 0))) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
1655 | qWarning("QCompleter::setMaxVisibleItems: " | - |
1656 | "Invalid max visible items (%d) must be >= 0", maxItems); | - |
1657 | return; never executed: return; | 0 |
1658 | } | - |
1659 | d->maxVisibleItems = maxItems; | - |
1660 | } never executed: end of block | 0 |
1661 | | - |
1662 | | - |
1663 | | - |
1664 | | - |
1665 | | - |
1666 | | - |
1667 | | - |
1668 | | - |
1669 | | - |
1670 | void QCompleter::setCaseSensitivity(Qt::CaseSensitivity cs) | - |
1671 | { | - |
1672 | Q_D(QCompleter); | - |
1673 | if (d->cs == cs) | - |
1674 | return; | - |
1675 | d->cs = cs; | - |
1676 | d->proxy->createEngine(); | - |
1677 | d->proxy->invalidate(); | - |
1678 | } | - |
1679 | | - |
1680 | Qt::CaseSensitivity QCompleter::caseSensitivity() const | - |
1681 | { | - |
1682 | Q_D(const QCompleter); | - |
1683 | return d->cs; | - |
1684 | } | - |
1685 | | - |
1686 | | - |
1687 | | - |
1688 | | - |
1689 | | - |
1690 | | - |
1691 | | - |
1692 | | - |
1693 | void QCompleter::setCompletionPrefix(const QString &prefix) | - |
1694 | { | - |
1695 | Q_D(QCompleter); | - |
1696 | d->prefix = prefix; | - |
1697 | d->proxy->filter(splitPath(prefix)); | - |
1698 | } | - |
1699 | | - |
1700 | QString QCompleter::completionPrefix() const | - |
1701 | { | - |
1702 | Q_D(const QCompleter); | - |
1703 | return d->prefix; | - |
1704 | } | - |
1705 | | - |
1706 | | - |
1707 | | - |
1708 | | - |
1709 | | - |
1710 | | - |
1711 | QModelIndex QCompleter::currentIndex() const | - |
1712 | { | - |
1713 | Q_D(const QCompleter); | - |
1714 | return d->proxy->currentIndex(false); | - |
1715 | } | - |
1716 | | - |
1717 | | - |
1718 | | - |
1719 | | - |
1720 | | - |
1721 | | - |
1722 | | - |
1723 | | - |
1724 | QString QCompleter::currentCompletion() const | - |
1725 | { | - |
1726 | Q_D(const QCompleter); | - |
1727 | return pathFromIndex(d->proxy->currentIndex(true)); | - |
1728 | } | - |
1729 | | - |
1730 | | - |
1731 | | - |
1732 | | - |
1733 | | - |
1734 | | - |
1735 | | - |
1736 | | - |
1737 | | - |
1738 | | - |
1739 | | - |
1740 | | - |
1741 | QAbstractItemModel *QCompleter::completionModel() const | - |
1742 | { | - |
1743 | Q_D(const QCompleter); | - |
1744 | return d->proxy; | - |
1745 | } | - |
1746 | | - |
1747 | | - |
1748 | | - |
1749 | | - |
1750 | | - |
1751 | | - |
1752 | | - |
1753 | | - |
1754 | | - |
1755 | | - |
1756 | | - |
1757 | | - |
1758 | QString QCompleter::pathFromIndex(const QModelIndex& index) const | - |
1759 | { | - |
1760 | Q_D(const QCompleter); | - |
1761 | if (!index.isValid()) | - |
1762 | return QString(); | - |
1763 | | - |
1764 | QAbstractItemModel *sourceModel = d->proxy->sourceModel(); | - |
1765 | if (!sourceModel) | - |
1766 | return QString(); | - |
1767 | bool isDirModel = false; | - |
1768 | bool isFsModel = false; | - |
1769 | #ifndef QT_NO_DIRMODEL | - |
1770 | isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; | - |
1771 | #endif | - |
1772 | #ifndef QT_NO_FILESYSTEMMODEL | - |
1773 | isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; | - |
1774 | #endif | - |
1775 | if (!isDirModel && !isFsModel) | - |
1776 | return sourceModel->data(index, d->role).toString(); | - |
1777 | | - |
1778 | QModelIndex idx = index; | - |
1779 | QStringList list; | - |
1780 | do { | - |
1781 | QString t; | - |
1782 | if (isDirModel) | - |
1783 | t = sourceModel->data(idx, Qt::EditRole).toString(); | - |
1784 | #ifndef QT_NO_FILESYSTEMMODEL | - |
1785 | else | - |
1786 | t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString(); | - |
1787 | #endif | - |
1788 | list.prepend(t); | - |
1789 | QModelIndex parent = idx.parent(); | - |
1790 | idx = parent.sibling(parent.row(), index.column()); | - |
1791 | } while (idx.isValid()); | - |
1792 | | - |
1793 | #if !defined(Q_OS_WIN) || defined(Q_OS_WINCE) | - |
1794 | if (list.count() == 1) | - |
1795 | return list[0]; | - |
1796 | list[0].clear() ; | - |
1797 | #endif | - |
1798 | | - |
1799 | return list.join(QDir::separator()); | - |
1800 | } | - |
1801 | | - |
1802 | | - |
1803 | | - |
1804 | | - |
1805 | | - |
1806 | | - |
1807 | | - |
1808 | | - |
1809 | | - |
1810 | | - |
1811 | | - |
1812 | | - |
1813 | | - |
1814 | QStringList QCompleter::splitPath(const QString& path) const | - |
1815 | { | - |
1816 | bool isDirModel = false; | - |
1817 | bool isFsModel = false; | - |
1818 | #ifndef QT_NO_DIRMODEL | - |
1819 | Q_D(const QCompleter); | - |
1820 | isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; | - |
1821 | #endif | - |
1822 | #ifndef QT_NO_FILESYSTEMMODEL | - |
1823 | #ifdef QT_NO_DIRMODEL | - |
1824 | Q_D(const QCompleter); | - |
1825 | #endif | - |
1826 | isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; | - |
1827 | #endif | - |
1828 | | - |
1829 | if ((!isDirModel && !isFsModel) || path.isEmpty()) | - |
1830 | return QStringList(completionPrefix()); | - |
1831 | | - |
1832 | QString pathCopy = QDir::toNativeSeparators(path); | - |
1833 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
1834 | if (pathCopy == QLatin1String("\\") || pathCopy == QLatin1String("\\\\")) | - |
1835 | return QStringList(pathCopy); | - |
1836 | const bool startsWithDoubleSlash = pathCopy.startsWith(QLatin1String("\\\\")); | - |
1837 | if (startsWithDoubleSlash) | - |
1838 | pathCopy = pathCopy.mid(2); | - |
1839 | #endif | - |
1840 | | - |
1841 | const QChar sep = QDir::separator(); | - |
1842 | QStringList parts = pathCopy.split(sep); | - |
1843 | | - |
1844 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - |
1845 | if (startsWithDoubleSlash) | - |
1846 | parts[0].prepend(QLatin1String("\\\\")); | - |
1847 | #else | - |
1848 | if (pathCopy[0] == sep) | - |
1849 | parts[0] = QLatin1Char('/'); | - |
1850 | #endif | - |
1851 | | - |
1852 | return parts; | - |
1853 | } | - |
1854 | | - |
1855 | | - |
1856 | | - |
1857 | | - |
1858 | | - |
1859 | | - |
1860 | | - |
1861 | | - |
1862 | | - |
1863 | | - |
1864 | | - |
1865 | | - |
1866 | | - |
1867 | | - |
1868 | | - |
1869 | | - |
1870 | | - |
1871 | | - |
1872 | | - |
1873 | | - |
1874 | | - |
1875 | | - |
1876 | | - |
1877 | | - |
1878 | | - |
1879 | | - |
1880 | | - |
1881 | | - |
1882 | | - |
1883 | | - |
1884 | | - |
1885 | | - |
1886 | | - |
1887 | | - |
1888 | | - |
1889 | QT_END_NAMESPACE | - |
1890 | | - |
1891 | #include "moc_qcompleter.cpp" | - |
1892 | | - |
1893 | #include "moc_qcompleter_p.cpp" | - |
1894 | | - |
1895 | #endif // QT_NO_COMPLETER | - |
| | |