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
109#include "QtWidgets/qscrollbar.h"
110#include "QtCore/qdir.h"
111#if QT_CONFIG(stringlistmodel)
112#include "QtCore/qstringlistmodel.h"
114#if QT_CONFIG(filesystemmodel)
115#include "QtGui/qfilesystemmodel.h"
117#include "QtWidgets/qheaderview.h"
118#if QT_CONFIG(listview)
119#include "QtWidgets/qlistview.h"
121#include "QtWidgets/qapplication.h"
122#include "QtGui/qevent.h"
123#include <private/qapplication_p.h>
124#include <private/qwidget_p.h>
125#include <qpa/qplatformwindow.h>
126#include <qpa/qplatformwindow_p.h>
127#if QT_CONFIG(lineedit)
128#include "QtWidgets/qlineedit.h"
130#include "QtCore/qdir.h"
134using namespace Qt::StringLiterals;
137 : QAbstractProxyModel(*
new QCompletionModelPrivate, parent),
145 Q_D(
const QCompletionModel);
146 return d->model->columnCount();
151 bool hadModel = (sourceModel() !=
nullptr);
154 QObject::disconnect(sourceModel(),
nullptr,
this,
nullptr);
156 QAbstractProxyModel::setSourceModel(source);
160 connect(source, SIGNAL(modelReset()),
this, SLOT(invalidate()));
162 connect(source, SIGNAL(layoutChanged()),
this, SLOT(invalidate()));
164 connect(source, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
this, SLOT(invalidate()));
165 connect(source, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
this, SLOT(invalidate()));
166 connect(source, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
this, SLOT(invalidate()));
167 connect(source, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(invalidate()));
175 bool sortedEngine =
false;
176 if (c->filterMode == Qt::MatchStartsWith) {
177 switch (
c->sorting) {
178 case QCompleter::UnsortedModel:
179 sortedEngine =
false;
181 case QCompleter::CaseSensitivelySortedModel:
182 sortedEngine =
c->cs == Qt::CaseSensitive;
184 case QCompleter::CaseInsensitivelySortedModel:
185 sortedEngine =
c->cs == Qt::CaseInsensitive;
191 engine.reset(
new QSortedModelEngine(c));
193 engine.reset(
new QUnsortedModelEngine(c));
198 Q_D(
const QCompletionModel);
199 if (!index.isValid())
200 return engine->curParent;
203 QModelIndex parent = engine->curParent;
205 if (!engine->matchCount())
206 return QModelIndex();
207 Q_ASSERT(index.row() < engine->matchCount());
208 QIndexMapper& rootIndices = engine->historyMatch.indices;
209 if (index.row() < rootIndices
.count()) {
210 row = rootIndices[index.row()];
211 parent = QModelIndex();
213 row = engine->curMatch.indices[index.row() - rootIndices.count()];
219 return d->model->index(row, index.column(), parent);
225 return QModelIndex();
229 if (!engine->matchCount())
230 return QModelIndex();
232 QIndexMapper& rootIndices = engine->historyMatch.indices;
233 if (idx.parent().isValid()) {
234 if (idx.parent() != engine->curParent)
235 return QModelIndex();
238 if (row == -1 && engine->curParent.isValid())
239 return QModelIndex();
244 engine->filterOnDemand(idx.row() - indices.last());
249 return QModelIndex();
251 if (idx.parent() != engine->curParent)
252 return QModelIndex();
256 return createIndex(row, idx.column());
261 if (row < 0 || !engine->matchCount())
264 if (row >= engine->matchCount())
265 engine->filterOnDemand(row + 1 - engine->matchCount());
267 if (row >= engine->matchCount())
270 engine->curRow = row;
276 if (!engine->matchCount())
277 return QModelIndex();
279 int row = engine->curRow;
281 row = engine->curMatch.indices[engine->curRow];
283 QModelIndex idx = createIndex(row,
c->column);
286 return mapToSource(idx);
291 Q_D(
const QCompletionModel);
292 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.isValid())
293 return QModelIndex();
296 if (!engine->matchCount())
297 return QModelIndex();
298 if (row >= engine->historyMatch.indices.count()) {
299 int want = row + 1 - engine->matchCount();
301 engine->filterOnDemand(want);
302 if (row >= engine->matchCount())
303 return QModelIndex();
306 if (row >= d->model->rowCount(engine->curParent))
307 return QModelIndex();
310 return createIndex(row, column);
315 if (!engine->matchCount())
318 engine->filterOnDemand(INT_MAX);
319 return engine->matchCount();
324 Q_D(
const QCompletionModel);
325 if (parent.isValid())
330 if (engine->curParts.size() != 1 && !engine->matchCount()
331 && !engine->curParent.isValid())
333 return d->model->rowCount(engine->curParent);
350 Q_D(
const QCompletionModel);
351 if (parent.isValid())
355 return d->model->hasChildren(mapToSource(parent));
357 if (!engine->matchCount())
365 Q_D(
const QCompletionModel);
366 return d->model->data(mapToSource(index), role);
371 if (!index.isValid())
372 return Qt::NoItemFlags;
373 return QAbstractProxyModel::flags(index);
378 QAbstractProxyModel::setSourceModel(
nullptr);
390 engine->cache.clear();
391 filter(engine->curParts);
396 Q_D(QCompletionModel);
398 engine->filter(parts);
401 if (d->model->canFetchMore(engine->curParent))
402 d->model->fetchMore(engine->curParent);
404#if QT_CONFIG(filesystemmodel)
408 if (engine->matchCount() == 0 && parts.size() > 1) {
409 if (
auto *fsModel = qobject_cast<QFileSystemModel *>(d->model)) {
410 const QModelIndex dirIndex = fsModel->index(QFileInfo(c->prefix).path());
411 if (dirIndex.isValid() && fsModel->canFetchMore(dirIndex))
412 fsModel->fetchMore(dirIndex);
421 const QAbstractItemModel *model =
c->proxy->sourceModel();
423 if (curParts.isEmpty())
424 curParts.append(QString());
427 curParent = QModelIndex();
428 curMatch = QMatchData();
429 historyMatch = filterHistory();
435 for (
int i = 0; i < curParts.size() - 1; i++) {
436 QString part = curParts.at(i);
437 int emi = filter(part, parent, -1).exactMatchIndex;
440 parent = model->index(emi,
c->column, parent);
446 if (curParts.constLast().isEmpty())
447 curMatch = QMatchData(QIndexMapper(0, model->rowCount(curParent) - 1), -1,
false);
449 curMatch = filter(curParts.constLast(), curParent, 1);
450 curRow = curMatch.isValid() ? 0 : -1;
455 QAbstractItemModel *source =
c->proxy->sourceModel();
456 if (curParts.size() <= 1 || c->proxy->showAll || !source)
459#if QT_CONFIG(filesystemmodel)
460 const bool isFsModel = (qobject_cast<QFileSystemModel *>(source) !=
nullptr);
462 const bool isFsModel =
false;
469 for (
int i = 0; i < source->rowCount(); i++) {
470 QString str = source->index(i,
c->column).data().toString();
471 if (str.startsWith(c->prefix, c->cs)
472#if !defined(Q_OS_WIN)
473 && (!isFsModel || QDir::toNativeSeparators(str) != QDir::separator())
487 const auto cit =
cache.find(parent);
488 if (cit ==
cache.end())
492 const auto mapEnd = map.end();
494 QString key = c->cs == Qt::CaseInsensitive ? part.toLower() : part;
496 while (!key.isEmpty()) {
498 const auto it = map.find(key);
513 const auto cit =
cache.find(parent);
514 if (cit ==
cache.end())
519 const QString key = c->cs == Qt::CaseInsensitive ? part.toLower() : part;
521 const auto it = map.find(key);
532 if (c->filterMode == Qt::MatchEndsWith)
535 cost =
cost + m.indices.cost() - old.indices.cost();
536 if (
cost *
sizeof(
int) > 1024 * 1024) {
537 QMap<QModelIndex, CacheItem>::iterator it1 =
cache.begin();
538 while (it1 !=
cache.end()) {
540 int sz = ci.size()/2;
541 QMap<QString, QMatchData>::iterator it2 = ci.begin();
543 while (it2 != ci.end() && i < sz) {
544 cost -= it2.value().indices.cost();
548 if (ci.size() == 0) {
549 it1 =
cache.erase(it1);
556 if (c->cs == Qt::CaseInsensitive)
557 part = std::move(part).toLower();
558 cache[parent][part] = m;
564 const QAbstractItemModel *model = c->proxy->sourceModel();
566 if (c->cs == Qt::CaseInsensitive)
567 part = std::move(part).toLower();
572 int to = model->rowCount(parent) - 1;
574 const CacheItem::const_iterator it = map.lowerBound(part);
577 for (CacheItem::const_iterator it1 = it; it1 != map.constBegin();) {
581 if (order == Qt::AscendingOrder) {
582 from = value.indices.last() + 1;
584 to = value.indices.first() - 1;
591 for(CacheItem::const_iterator it2 = it; it2 != map.constEnd(); ++it2) {
593 if (value
.isValid() && !it2.key().startsWith(part)) {
594 if (order == Qt::AscendingOrder) {
595 to = value.indices.first() - 1;
597 from = value.indices.first() + 1;
608 const QAbstractItemModel *model = c->proxy->sourceModel();
610 int rowCount = model->rowCount(parent);
612 return Qt::AscendingOrder;
613 QString first = model->data(model->index(0, c->column, parent), c->role).toString();
614 QString last = model->data(model->index(rowCount - 1, c->column, parent), c->role).toString();
615 return QString::compare(first, last, c->cs) <= 0 ? Qt::AscendingOrder : Qt::DescendingOrder;
620 const QAbstractItemModel *model = c->proxy->sourceModel();
623 if (lookupCache(part, parent, &hint))
627 Qt::SortOrder order = sortOrder(parent);
629 if (matchHint(part, parent, &hint)) {
632 indices = hint.indices;
634 indices = indexHint(part, parent, order);
638 int high = indices
.to() + 1;
641 QModelIndex probeIndex;
644 while (high - low > 1)
646 probe = (high + low) / 2;
647 probeIndex = model->index(probe, c->column, parent);
648 probeData = model->data(probeIndex, c->role).toString();
649 const int cmp = QString::compare(probeData, part, c->cs);
650 if ((order == Qt::AscendingOrder && cmp >= 0)
651 || (order == Qt::DescendingOrder && cmp < 0)) {
658 if ((order == Qt::AscendingOrder && low == indices.to())
659 || (order == Qt::DescendingOrder && high == indices.from())) {
660 saveInCache(part, parent, QMatchData());
664 probeIndex = model->index(order == Qt::AscendingOrder ? low+1 : high-1, c->column, parent);
665 probeData = model->data(probeIndex, c->role).toString();
666 if (!probeData.startsWith(part, c->cs)) {
667 saveInCache(part, parent, QMatchData());
671 const bool exactMatch = QString::compare(probeData, part, c->cs) == 0;
672 int emi = exactMatch ? (order == Qt::AscendingOrder ? low+1 : high-1) : -1;
676 if (order == Qt::AscendingOrder) {
678 high = indices
.to() + 1;
686 while (high - low > 1)
688 probe = (high + low) / 2;
689 probeIndex = model->index(probe, c->column, parent);
690 probeData = model->data(probeIndex, c->role).toString();
691 const bool startsWith = probeData.startsWith(part, c->cs);
692 if ((order == Qt::AscendingOrder && startsWith)
693 || (order == Qt::DescendingOrder && !startsWith)) {
700 QMatchData m(order == Qt::AscendingOrder ? QIndexMapper(from, high - 1) : QIndexMapper(low+1, to), emi,
false);
701 saveInCache(part, parent, m);
711 const QAbstractItemModel *model = c->proxy->sourceModel();
714 for (i = 0; i < indices
.count() && count != n; ++i) {
715 QModelIndex idx = model->index(indices
[i
], c->column, parent);
717 if (!(model->flags(idx) & Qt::ItemIsSelectable))
720 QString data = model->data(idx, c->role).toString();
722 switch (c->filterMode) {
723 case Qt::MatchStartsWith:
724 if (!data.startsWith(str, c->cs))
727 case Qt::MatchContains:
728 if (!data.contains(str, c->cs))
731 case Qt::MatchEndsWith:
732 if (!data.endsWith(str, c->cs))
735 case Qt::MatchExactly:
736 case Qt::MatchFixedString:
737 case Qt::MatchCaseSensitive:
738 case Qt::MatchRegularExpression:
739 case Qt::MatchWildcard:
741 case Qt::MatchRecursive:
745 m->indices.append(indices
[i
]);
747 if (m->exactMatchIndex == -1 && QString::compare(data, str, c->cs) == 0) {
759 if (!curMatch.partial)
762 const QAbstractItemModel *model = c->proxy->sourceModel();
763 int lastRow = model->rowCount(curParent) - 1;
765 int lastIndex = buildIndices(curParts.constLast(), curParent, n, im, &curMatch);
766 curMatch.partial = (lastRow != lastIndex);
767 saveInCache(curParts.constLast(), curParent, curMatch);
778 const QAbstractItemModel *model = c->proxy->sourceModel();
779 bool foundInCache = lookupCache(part, parent, &m);
782 if (matchHint(part, parent, &hint) && !hint.isValid())
787 const int lastRow = model->rowCount(parent) - 1;
789 int lastIndex = buildIndices(part, parent, n, all, &m);
793 buildIndices(part, parent, INT_MAX, hint.indices, &m);
798 const int lastRow = model->rowCount(parent) - 1;
800 int want = n == -1 ? -1 : n - m.indices.count();
801 int lastIndex = buildIndices(part, parent, want, rest, &m);
806 saveInCache(part, parent, m);
815 filterMode(Qt::MatchStartsWith),
816 cs(Qt::CaseSensitive),
820 sorting(QCompleter::UnsortedModel),
830 proxy =
new QCompletionModel(
this, q);
831 QObject::connect(proxy, SIGNAL(rowsAdded()), q, SLOT(_q_autoResizePopup()));
833#if !QT_CONFIG(listview)
834 q->setCompletionMode(QCompleter::InlineCompletion);
836 q->setCompletionMode(QCompleter::PopupCompletion);
846 popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
848 if (!index.isValid())
849 popup->selectionModel()->clear();
851 popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select
852 | QItemSelectionModel::Rows);
854 index =
popup->selectionModel()->currentIndex();
855 if (!index.isValid())
856 popup->scrollToTop();
858 popup->scrollTo(index, QAbstractItemView::PositionAtTop);
864 if (
const auto indexes = selection.indexes(); !indexes.isEmpty())
865 index = indexes.first();
867 _q_complete(index,
true);
875 if (!index.isValid() || (!
proxy->showAll && (index.row() >=
proxy->engine->matchCount()))) {
877 index = QModelIndex();
879 if (!(index.flags() & Qt::ItemIsEnabled))
881 QModelIndex si =
proxy->mapToSource(index);
882 si = si.sibling(si.row(),
column);
883 completion = q->pathFromIndex(si);
884#if QT_CONFIG(filesystemmodel)
886 if (mode == QCompleter::InlineCompletion) {
887 if (qobject_cast<QFileSystemModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir())
888 completion += QDir::separator();
894 emit q->highlighted(index);
895 emit q->highlighted(completion);
897 emit q->activated(index);
898 emit q->activated(completion);
906 showPopup(popupRect);
911 const QRect screen = widget->screen()->availableGeometry();
915 QScrollBar *hsb =
popup->horizontalScrollBar();
916 if (hsb && hsb->isVisible())
917 h +=
popup->horizontalScrollBar()->sizeHint().height();
919 if (rect.isValid()) {
922 pos = widget->mapToGlobal(rect.bottomLeft());
924 rh = widget->height();
925 pos = widget->mapToGlobal(QPoint(0, widget->height() - 2));
929 if (w > screen.width())
931 if ((pos.x() + w) > (screen.x() + screen.width()))
932 pos.setX(screen.x() + screen.width() - w);
933 if (pos.x() < screen.x())
934 pos.setX(screen.x());
936 int top = pos.y() - rh - screen.top() + 2;
937 int bottom = screen.bottom() - pos.y();
938 h = qMax(h,
popup->minimumHeight());
940 h = qMin(qMax(top, bottom), h);
943 pos.setY(pos.y() - h - rh + 2);
946 popup->setGeometry(pos.x(), pos.y(), w, h);
948 if (!
popup->isVisible()) {
949#if QT_CONFIG(wayland)
950 popup->createWinId();
951 if (
auto waylandWindow =
dynamic_cast<QNativeInterface::Private::QWaylandWindow*>(popup->windowHandle()->handle())) {
952 popup->windowHandle()->setTransientParent(widget->window()->windowHandle());
953 if (!rect.isValid()) {
954 const QRect controlGeometry = QRect(
955 widget->mapTo(widget->topLevelWidget(), QPoint(0, 0)), widget->size());
956 waylandWindow->setParentControlGeometry(controlGeometry);
957 waylandWindow->setExtendedWindowType(
958 QNativeInterface::Private::QWaylandWindow::ComboBox);
966#if QT_CONFIG(filesystemmodel)
967static bool isRoot(
const QFileSystemModel *model,
const QString &path)
969 const auto index = model->index(path);
970 return index.isValid() && model->fileInfo(index).isRoot();
973static bool completeOnLoaded(
const QFileSystemModel *model,
974 const QString &nativePrefix,
976 Qt::CaseSensitivity caseSensitivity)
978 const auto pathSize = path.size();
979 const auto prefixSize = nativePrefix.size();
980 if (prefixSize < pathSize)
982 const QString prefix = QDir::fromNativeSeparators(nativePrefix);
983 if (prefixSize == pathSize)
984 return path.compare(prefix, caseSensitivity) == 0 && isRoot(model, path);
986 const auto separator = u'/';
987 return prefix.startsWith(path, caseSensitivity) && prefix.at(pathSize) == separator
988 && !QStringView{prefix}.right(prefixSize - pathSize - 1).contains(separator);
991void QCompleterPrivate::_q_fileSystemModelDirectoryLoaded(
const QString &path)
998 if (hiddenBecauseNoMatch && widget) {
999 if (
auto model = qobject_cast<
const QFileSystemModel *>(proxy->sourceModel())) {
1000 if (completeOnLoaded(model, prefix, path, cs))
1010
1011
1012QCompleter::QCompleter(QObject *parent)
1013: QObject(*
new QCompleterPrivate(), parent)
1020
1021
1022
1023QCompleter::QCompleter(QAbstractItemModel *model, QObject *parent)
1024 : QObject(*
new QCompleterPrivate(), parent)
1030#if QT_CONFIG(stringlistmodel)
1032
1033
1034
1035QCompleter::QCompleter(
const QStringList& list, QObject *parent)
1036: QObject(*
new QCompleterPrivate(), parent)
1039 d->init(
new QStringListModel(list,
this));
1044
1045
1046QCompleter::~QCompleter()
1051
1052
1053
1054
1055
1056
1057
1058
1059void QCompleter::setWidget(QWidget *widget)
1062 if (widget == d->widget)
1066 d->widget->removeEventFilter(
this);
1069 d->widget->installEventFilter(
this);
1073 d->popup->setFocusProxy(d->widget);
1078
1079
1080
1081
1082QWidget *QCompleter::widget()
const
1084 Q_D(
const QCompleter);
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099void QCompleter::setModel(QAbstractItemModel *model)
1102 QAbstractItemModel *oldModel = d->proxy->sourceModel();
1103 if (oldModel == model)
1105#if QT_CONFIG(filesystemmodel)
1106 if (qobject_cast<
const QFileSystemModel *>(oldModel))
1107 setCompletionRole(Qt::EditRole);
1109 d->proxy->setSourceModel(model);
1112 if (oldModel && oldModel->QObject::parent() ==
this)
1114#if QT_CONFIG(filesystemmodel)
1115 QFileSystemModel *fsModel = qobject_cast<QFileSystemModel *>(model);
1117#if defined(Q_OS_WIN)
1118 setCaseSensitivity(Qt::CaseInsensitive);
1120 setCaseSensitivity(Qt::CaseSensitive);
1122 setCompletionRole(QFileSystemModel::FileNameRole);
1123 connect(fsModel, SIGNAL(directoryLoaded(QString)),
this, SLOT(_q_fileSystemModelDirectoryLoaded(QString)));
1129
1130
1131
1132
1133QAbstractItemModel *QCompleter::model()
const
1135 Q_D(
const QCompleter);
1136 return d->proxy->sourceModel();
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1152
1153
1154
1155
1156
1157void QCompleter::setCompletionMode(QCompleter::CompletionMode mode)
1161 d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion);
1163 if (mode == QCompleter::InlineCompletion) {
1165 d->widget->removeEventFilter(
this);
1167 d->popup->deleteLater();
1172 d->widget->installEventFilter(
this);
1176QCompleter::CompletionMode QCompleter::completionMode()
const
1178 Q_D(
const QCompleter);
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1202void QCompleter::setFilterMode(Qt::MatchFlags filterMode)
1206 if (d->filterMode == filterMode)
1209 if (Q_UNLIKELY(filterMode != Qt::MatchStartsWith &&
1210 filterMode != Qt::MatchContains &&
1211 filterMode != Qt::MatchEndsWith)) {
1212 qWarning(
"Unhandled QCompleter::filterMode flag is used.");
1216 d->filterMode = filterMode;
1217 d->proxy->createEngine();
1218 d->proxy->invalidate();
1221Qt::MatchFlags QCompleter::filterMode()
const
1223 Q_D(
const QCompleter);
1224 return d->filterMode;
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242void QCompleter::setPopup(QAbstractItemView *popup)
1246 if (popup == d->popup)
1250 const Qt::FocusPolicy origPolicy = d->widget ? d->widget->focusPolicy()
1255 QObject::disconnect(d->popup->selectionModel(),
nullptr,
this,
nullptr);
1256 QObject::disconnect(d->popup,
nullptr,
this,
nullptr);
1262 if (d->popup->model() != d->proxy)
1263 d->popup->setModel(d->proxy);
1271 d->popup->setParent(
nullptr);
1272 d->popup->setWindowFlag(Qt::Popup);
1273 d->popup->setFocusPolicy(Qt::NoFocus);
1275 d->widget->setFocusPolicy(origPolicy);
1277 d->popup->setFocusProxy(d->widget);
1278 d->popup->installEventFilter(
this);
1279 d->popup->setItemDelegate(
new QCompleterItemDelegate(d->popup));
1280#if QT_CONFIG(listview)
1281 if (QListView *listView = qobject_cast<QListView *>(d->popup)) {
1282 listView->setModelColumn(d->column);
1286 QObject::connect(d->popup, SIGNAL(clicked(QModelIndex)),
1287 this, SLOT(_q_complete(QModelIndex)));
1288 QObject::connect(
this, SIGNAL(activated(QModelIndex)),
1289 d->popup, SLOT(hide()));
1291 QObject::connect(d->popup->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
1292 this, SLOT(_q_completionSelected(QItemSelection)));
1296
1297
1298
1299
1300QAbstractItemView *QCompleter::popup()
const
1302 Q_D(
const QCompleter);
1303#if QT_CONFIG(listview)
1304 if (!d->popup && completionMode() != QCompleter::InlineCompletion) {
1305 QListView *listView =
new QListView;
1306 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1307 listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1308 listView->setSelectionBehavior(QAbstractItemView::SelectRows);
1309 listView->setSelectionMode(QAbstractItemView::SingleSelection);
1310 listView->setModelColumn(d->column);
1311 QCompleter *that =
const_cast<QCompleter*>(
this);
1312 that->setPopup(listView);
1319
1320
1321bool QCompleter::event(QEvent *ev)
1323 return QObject::event(ev);
1327
1328
1329bool QCompleter::eventFilter(QObject *o, QEvent *e)
1333 if (o == d->widget) {
1334 switch (e->type()) {
1335 case QEvent::FocusOut:
1336 if (d->eatFocusOut) {
1337 d->hiddenBecauseNoMatch =
false;
1338 if (d->popup && d->popup->isVisible())
1352 return QObject::eventFilter(o, e);
1355 switch (e->type()) {
1356 case QEvent::KeyPress: {
1357 QKeyEvent *ke =
static_cast<QKeyEvent *>(e);
1359 QModelIndex curIndex = d->popup->currentIndex();
1360 QModelIndexList selList = d->popup->selectionModel()->selectedIndexes();
1362 const int key = ke->key();
1364 if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid()
1365 && d->mode == QCompleter::UnfilteredPopupCompletion) {
1366 d->setCurrentIndex(curIndex);
1375 if (ke->modifiers() & Qt::ControlModifier)
1380 if (!curIndex.isValid()) {
1381 int rowCount = d->proxy->rowCount();
1382 QModelIndex lastIndex = d->proxy->index(rowCount - 1, d->column);
1383 d->setCurrentIndex(lastIndex);
1385 }
else if (curIndex.row() == 0) {
1387 d->setCurrentIndex(QModelIndex());
1393 if (!curIndex.isValid()) {
1394 QModelIndex firstIndex = d->proxy->index(0, d->column);
1395 d->setCurrentIndex(firstIndex);
1397 }
else if (curIndex.row() == d->proxy->rowCount() - 1) {
1399 d->setCurrentIndex(QModelIndex());
1404 case Qt::Key_PageUp:
1405 case Qt::Key_PageDown:
1412 d->eatFocusOut =
false;
1413 (
static_cast<QObject *>(d->widget))->event(ke);
1414 d->eatFocusOut =
true;
1416 if (!d->widget || e->isAccepted() || !d->popup->isVisible()) {
1418 if (d->widget && !d->widget->hasFocus())
1420 if (e->isAccepted())
1425#if QT_CONFIG(shortcut)
1426 if (ke->matches(QKeySequence::Cancel)) {
1432 case Qt::Key_Return:
1436 if (curIndex.isValid())
1437 d->_q_complete(curIndex);
1441 if (ke->modifiers() & Qt::AltModifier)
1445 case Qt::Key_Backtab:
1456 case QEvent::MouseButtonPress:
1457 if (!d->popup->underMouse()) {
1458 if (!QGuiApplicationPrivate::maybeForwardEventToVirtualKeyboard(e))
1464 case QEvent::MouseButtonRelease:
1465 QGuiApplicationPrivate::maybeForwardEventToVirtualKeyboard(e);
1467 case QEvent::InputMethod:
1468 case QEvent::ShortcutOverride:
1470 QCoreApplication::sendEvent(d->widget, e);
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489void QCompleter::complete(
const QRect& rect)
1492 QModelIndex idx = d->proxy->currentIndex(
false);
1493 d->hiddenBecauseNoMatch =
false;
1494 if (d->mode == QCompleter::InlineCompletion) {
1496 d->_q_complete(idx,
true);
1500 Q_ASSERT(d->widget);
1501 if ((d->mode == QCompleter::PopupCompletion && !idx.isValid())
1502 || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) {
1505 d->hiddenBecauseNoMatch =
true;
1510 if (d->mode == QCompleter::UnfilteredPopupCompletion)
1511 d->setCurrentIndex(idx,
false);
1514 d->popupRect = rect;
1518
1519
1520
1521
1522
1523
1524
1525
1526bool QCompleter::setCurrentRow(
int row)
1529 return d->proxy->setCurrentRow(row);
1533
1534
1535
1536
1537int QCompleter::currentRow()
const
1539 Q_D(
const QCompleter);
1540 return d->proxy->currentRow();
1544
1545
1546
1547
1548int QCompleter::completionCount()
const
1550 Q_D(
const QCompleter);
1551 return d->proxy->completionCount();
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588void QCompleter::setModelSorting(QCompleter::ModelSorting sorting)
1591 if (d->sorting == sorting)
1593 d->sorting = sorting;
1594 d->proxy->createEngine();
1595 d->proxy->invalidate();
1598QCompleter::ModelSorting QCompleter::modelSorting()
const
1600 Q_D(
const QCompleter);
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615void QCompleter::setCompletionColumn(
int column)
1618 if (d->column == column)
1620#if QT_CONFIG(listview)
1621 if (QListView *listView = qobject_cast<QListView *>(d->popup))
1622 listView->setModelColumn(column);
1625 d->proxy->invalidate();
1628int QCompleter::completionColumn()
const
1630 Q_D(
const QCompleter);
1635
1636
1637
1638
1639
1640
1641
1642void QCompleter::setCompletionRole(
int role)
1645 if (d->role == role)
1648 d->proxy->invalidate();
1651int QCompleter::completionRole()
const
1653 Q_D(
const QCompleter);
1658
1659
1660
1661
1662
1663
1664void QCompleter::setWrapAround(
bool wrap)
1667 if (d->wrap == wrap)
1672bool QCompleter::wrapAround()
const
1674 Q_D(
const QCompleter);
1679
1680
1681
1682
1683
1684
1685int QCompleter::maxVisibleItems()
const
1687 Q_D(
const QCompleter);
1688 return d->maxVisibleItems;
1691void QCompleter::setMaxVisibleItems(
int maxItems)
1694 if (Q_UNLIKELY(maxItems < 0)) {
1695 qWarning(
"QCompleter::setMaxVisibleItems: "
1696 "Invalid max visible items (%d) must be >= 0", maxItems);
1699 d->maxVisibleItems = maxItems;
1703
1704
1705
1706
1707
1708
1709
1710void QCompleter::setCaseSensitivity(Qt::CaseSensitivity cs)
1716 d->proxy->createEngine();
1717 d->proxy->invalidate();
1720Qt::CaseSensitivity QCompleter::caseSensitivity()
const
1722 Q_D(
const QCompleter);
1727
1728
1729
1730
1731
1732
1733void QCompleter::setCompletionPrefix(
const QString &prefix)
1737 d->proxy->filter(splitPath(prefix));
1740QString QCompleter::completionPrefix()
const
1742 Q_D(
const QCompleter);
1747
1748
1749
1750
1751QModelIndex QCompleter::currentIndex()
const
1753 Q_D(
const QCompleter);
1754 return d->proxy->currentIndex(
false);
1758
1759
1760
1761
1762
1763
1764QString QCompleter::currentCompletion()
const
1766 Q_D(
const QCompleter);
1767 return pathFromIndex(d->proxy->currentIndex(
true));
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781QAbstractItemModel *QCompleter::completionModel()
const
1783 Q_D(
const QCompleter);
1788
1789
1790
1791
1792
1793
1794
1795
1796
1798QString QCompleter::pathFromIndex(
const QModelIndex& index)
const
1800 Q_D(
const QCompleter);
1801 if (!index.isValid())
1804 QAbstractItemModel *sourceModel = d->proxy->sourceModel();
1807 bool isFsModel =
false;
1808#if QT_CONFIG(filesystemmodel)
1809 isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) !=
nullptr;
1812 return sourceModel->data(index, d->role).toString();
1814 QModelIndex idx = index;
1818#if QT_CONFIG(filesystemmodel)
1819 t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString();
1822 QModelIndex parent = idx.parent();
1823 idx = parent.sibling(parent.row(), index.column());
1824 }
while (idx.isValid());
1826#if !defined(Q_OS_WIN)
1827 if (list.size() == 1)
1832 return list.join(QDir::separator());
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847QStringList QCompleter::splitPath(
const QString& path)
const
1849 bool isFsModel =
false;
1850#if QT_CONFIG(filesystemmodel)
1851 Q_D(
const QCompleter);
1852 isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) !=
nullptr;
1855 if (!isFsModel || path.isEmpty())
1856 return QStringList(completionPrefix());
1858 QString pathCopy = QDir::toNativeSeparators(path);
1859#if defined(Q_OS_WIN)
1860 if (pathCopy ==
"\\"_L1 || pathCopy ==
"\\\\"_L1)
1861 return QStringList(pathCopy);
1862 const bool startsWithDoubleSlash = pathCopy.startsWith(
"\\\\"_L1);
1863 if (startsWithDoubleSlash)
1864 pathCopy = pathCopy.mid(2);
1867 const QChar sep = QDir::separator();
1868 QStringList parts = pathCopy.split(sep);
1870#if defined(Q_OS_WIN)
1871 if (startsWithDoubleSlash)
1872 parts[0].prepend(
"\\\\"_L1);
1874 if (pathCopy[0] == sep)
1882
1883
1884
1885
1886
1887
1888
1891
1892
1893
1894
1895
1896
1899
1900
1901
1902
1903
1904
1905
1908
1909
1910
1911
1912
1913
1917#include "moc_qcompleter.cpp"
1919#include "moc_qcompleter_p.cpp"
void setCurrentIndex(QModelIndex, bool=true)
void _q_fileSystemModelDirectoryLoaded(const QString &path)
bool hiddenBecauseNoMatch
void _q_completionSelected(const QItemSelection &)
void _q_autoResizePopup()
void init(QAbstractItemModel *model=nullptr)
QAbstractItemView * popup
void _q_complete(QModelIndex, bool=false)
void showPopup(const QRect &)
void saveInCache(QString, const QModelIndex &, const QMatchData &)
void filter(const QStringList &parts)
QMap< QString, QMatchData > CacheItem
bool matchHint(const QString &part, const QModelIndex &parent, QMatchData *m) const
bool lookupCache(const QString &part, const QModelIndex &parent, QMatchData *m) const
QMatchData filterHistory()
QModelIndex index(int row, int column, const QModelIndex &=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
int rowCount(const QModelIndex &index=QModelIndex()) const override
Returns the number of rows under the given parent.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
\reimp
int completionCount() const
int columnCount(const QModelIndex &index=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Reimplement this function to return the model index in the proxy model that corresponds to the source...
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
\reimp
QModelIndex currentIndex(bool) const
void filter(const QStringList &parts)
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Reimplement this function to return the model index in the source model that corresponds to the proxy...
void setSourceModel(QAbstractItemModel *sourceModel) override
Sets the given sourceModel to be processed by the proxy model.
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
bool setCurrentRow(int row)
int operator[](int index) const
QIndexMapper(int f, int t)
QMatchData filter(const QString &, const QModelIndex &, int) override
Qt::SortOrder sortOrder(const QModelIndex &) const
QIndexMapper indexHint(QString, const QModelIndex &, Qt::SortOrder)
void filterOnDemand(int) override
QMatchData filter(const QString &, const QModelIndex &, int) override
Combined button and popup list for selecting options.
QMatchData(const QIndexMapper &indices, int em, bool p)