| Line | Source | Count |
| 1 | | - |
| 2 | | - |
| 3 | | - |
| 4 | | - |
| 5 | | - |
| 6 | | - |
| 7 | QCompletionModel::QCompletionModel(QCompleterPrivate *c, QObject *parent) | - |
| 8 | : QAbstractProxyModel(*new QCompletionModelPrivate, parent), | - |
| 9 | c(c), showAll(false) | - |
| 10 | { | - |
| 11 | createEngine(); | - |
| 12 | } | - |
| 13 | | - |
| 14 | int QCompletionModel::columnCount(const QModelIndex &) const | - |
| 15 | { | - |
| 16 | const QCompletionModelPrivate * const d = d_func(); | - |
| 17 | return d->model->columnCount(); | - |
| 18 | } | - |
| 19 | | - |
| 20 | void QCompletionModel::setSourceModel(QAbstractItemModel *source) | - |
| 21 | { | - |
| 22 | bool hadModel = (sourceModel() != 0); | - |
| 23 | | - |
| 24 | if (hadModel) | - |
| 25 | QObject::disconnect(sourceModel(), 0, this, 0); | - |
| 26 | | - |
| 27 | QAbstractProxyModel::setSourceModel(source); | - |
| 28 | | - |
| 29 | if (source) { | - |
| 30 | | - |
| 31 | connect(source, qFlagLocation("2""modelReset()" "\0" __FILE__ ":" "179""185"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "179""185")); | - |
| 32 | connect(source, qFlagLocation("2""destroyed()" "\0" __FILE__ ":" "180""186"), this, qFlagLocation("1""modelDestroyed()" "\0" __FILE__ ":" "180""186")); | - |
| 33 | connect(source, qFlagLocation("2""layoutChanged()" "\0" __FILE__ ":" "181""187"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "181""187")); | - |
| 34 | connect(source, qFlagLocation("2""rowsInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "182""188"), this, qFlagLocation("1""rowsInserted()" "\0" __FILE__ ":" "182""188")); | - |
| 35 | connect(source, qFlagLocation("2""rowsRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "183""189"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "183""189")); | - |
| 36 | connect(source, qFlagLocation("2""columnsInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "184""190"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "184""190")); | - |
| 37 | connect(source, qFlagLocation("2""columnsRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "185""191"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "185""191")); | - |
| 38 | connect(source, qFlagLocation("2""dataChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "186""192"), this, qFlagLocation("1""invalidate()" "\0" __FILE__ ":" "186""192")); | - |
| 39 | } | - |
| 40 | | - |
| 41 | invalidate(); | - |
| 42 | } | - |
| 43 | | - |
| 44 | void QCompletionModel::createEngine() | - |
| 45 | { | - |
| 46 | bool sortedEngine = false; | - |
| 47 | if (c->filterMode == Qt::MatchStartsWith) { | - |
| 48 | switch (c->sorting) { | - |
| 49 | case QCompleter::UnsortedModel: | - |
| 50 | sortedEngine = false; | - |
| 51 | break; | - |
| 52 | case QCompleter::CaseSensitivelySortedModel: | - |
| 53 | sortedEngine = c->cs == Qt::CaseSensitive; | - |
| 54 | break; | - |
| 55 | case QCompleter::CaseInsensitivelySortedModel: | - |
| 56 | sortedEngine = c->cs == Qt::CaseInsensitive; | - |
| 57 | break; | - |
| 58 | } | - |
| 59 | } | - |
| 60 | | - |
| 61 | if (sortedEngine) | - |
| 62 | engine.reset(new QSortedModelEngine(c)); | - |
| 63 | else | - |
| 64 | engine.reset(new QUnsortedModelEngine(c)); | - |
| 65 | } | - |
| 66 | | - |
| 67 | QModelIndex QCompletionModel::mapToSource(const QModelIndex& index) const | - |
| 68 | { | - |
| 69 | const QCompletionModelPrivate * const d = d_func(); | - |
| 70 | if (!index.isValid()) | - |
| 71 | return engine->curParent; | - |
| 72 | | - |
| 73 | int row; | - |
| 74 | QModelIndex parent = engine->curParent; | - |
| 75 | if (!showAll) { | - |
| 76 | if (!engine->matchCount()) | - |
| 77 | return QModelIndex(); | - |
| 78 | ((!(index.row() < engine->matchCount())) ? qt_assert("index.row() < engine->matchCount()",__FILE__,226232) : qt_noop()); | - |
| 79 | QIndexMapper& rootIndices = engine->historyMatch.indices; | - |
| 80 | if (index.row() < rootIndices.count()) { | - |
| 81 | row = rootIndices[index.row()]; | - |
| 82 | parent = QModelIndex(); | - |
| 83 | } else { | - |
| 84 | row = engine->curMatch.indices[index.row() - rootIndices.count()]; | - |
| 85 | } | - |
| 86 | } else { | - |
| 87 | row = index.row(); | - |
| 88 | } | - |
| 89 | | - |
| 90 | return d->model->index(row, index.column(), parent); | - |
| 91 | } | - |
| 92 | | - |
| 93 | QModelIndex QCompletionModel::mapFromSource(const QModelIndex& idx) const | - |
| 94 | { | - |
| 95 | if (!idx.isValid()) | - |
| 96 | return QModelIndex(); | - |
| 97 | | - |
| 98 | int row = -1; | - |
| 99 | if (!showAll) { | - |
| 100 | if (!engine->matchCount()) | - |
| 101 | return QModelIndex(); | - |
| 102 | | - |
| 103 | QIndexMapper& rootIndices = engine->historyMatch.indices; | - |
| 104 | if (idx.parent().isValid()) { | - |
| 105 | if (idx.parent() != engine->curParent) | - |
| 106 | return QModelIndex(); | - |
| 107 | } else { | - |
| 108 | row = rootIndices.indexOf(idx.row()); | - |
| 109 | if (row == -1 && engine->curParent.isValid()) | - |
| 110 | return QModelIndex(); | - |
| 111 | } | - |
| 112 | | - |
| 113 | if (row == -1) { | - |
| 114 | QIndexMapper& indices = engine->curMatch.indices; | - |
| 115 | engine->filterOnDemand(idx.row() - indices.last()); | - |
| 116 | row = indices.indexOf(idx.row()) + rootIndices.count(); | - |
| 117 | } | - |
| 118 | | - |
| 119 | if (row == -1) | - |
| 120 | return QModelIndex(); | - |
| 121 | } else { | - |
| 122 | if (idx.parent() != engine->curParent) | - |
| 123 | return QModelIndex(); | - |
| 124 | row = idx.row(); | - |
| 125 | } | - |
| 126 | | - |
| 127 | return createIndex(row, idx.column()); | - |
| 128 | } | - |
| 129 | | - |
| 130 | bool QCompletionModel::setCurrentRow(int row) | - |
| 131 | { | - |
| 132 | if (row < 0 || !engine->matchCount()) | - |
| 133 | return false; | - |
| 134 | | - |
| 135 | if (row >= engine->matchCount()) | - |
| 136 | engine->filterOnDemand(row + 1 - engine->matchCount()); | - |
| 137 | | - |
| 138 | if (row >= engine->matchCount()) | - |
| 139 | return false; | - |
| 140 | | - |
| 141 | engine->curRow = row; | - |
| 142 | return true; | - |
| 143 | } | - |
| 144 | | - |
| 145 | QModelIndex QCompletionModel::currentIndex(bool sourceIndex) const | - |
| 146 | { | - |
| 147 | if (!engine->matchCount()) | - |
| 148 | return QModelIndex(); | - |
| 149 | | - |
| 150 | int row = engine->curRow; | - |
| 151 | if (showAll) | - |
| 152 | row = engine->curMatch.indices[engine->curRow]; | - |
| 153 | | - |
| 154 | QModelIndex idx = createIndex(row, c->column); | - |
| 155 | if (!sourceIndex) | - |
| 156 | return idx; | - |
| 157 | return mapToSource(idx); | - |
| 158 | } | - |
| 159 | | - |
| 160 | QModelIndex QCompletionModel::index(int row, int column, const QModelIndex& parent) const | - |
| 161 | { | - |
| 162 | const QCompletionModelPrivate * const d = d_func(); | - |
| 163 | if (row < 0 || column < 0 || column >= columnCount(parent) || parent.isValid()) | - |
| 164 | return QModelIndex(); | - |
| 165 | | - |
| 166 | if (!showAll) { | - |
| 167 | if (!engine->matchCount()) | - |
| 168 | return QModelIndex(); | - |
| 169 | if (row >= engine->historyMatch.indices.count()) { | - |
| 170 | int want = row + 1 - engine->matchCount(); | - |
| 171 | if (want > 0) | - |
| 172 | engine->filterOnDemand(want); | - |
| 173 | if (row >= engine->matchCount()) | - |
| 174 | return QModelIndex(); | - |
| 175 | } | - |
| 176 | } else { | - |
| 177 | if (row >= d->model->rowCount(engine->curParent)) | - |
| 178 | return QModelIndex(); | - |
| 179 | } | - |
| 180 | | - |
| 181 | return createIndex(row, column); | - |
| 182 | } | - |
| 183 | | - |
| 184 | int QCompletionModel::completionCount() const | - |
| 185 | { | - |
| 186 | if (!engine->matchCount()) | - |
| 187 | return 0; | - |
| 188 | | - |
| 189 | engine->filterOnDemand(2147483647); | - |
| 190 | return engine->matchCount(); | - |
| 191 | } | - |
| 192 | | - |
| 193 | int QCompletionModel::rowCount(const QModelIndex &parent) const | - |
| 194 | { | - |
| 195 | const QCompletionModelPrivate * const d = d_func(); | - |
| 196 | if (parent.isValid()) | - |
| 197 | return 0; | - |
| 198 | | - |
| 199 | if (showAll) { | - |
| 200 | | - |
| 201 | if (engine->curParts.count() != 1 && !engine->matchCount() | - |
| 202 | && !engine->curParent.isValid()) | - |
| 203 | return 0; | - |
| 204 | return d->model->rowCount(engine->curParent); | - |
| 205 | } | - |
| 206 | | - |
| 207 | return completionCount(); | - |
| 208 | } | - |
| 209 | | - |
| 210 | void QCompletionModel::setFiltered(bool filtered) | - |
| 211 | { | - |
| 212 | if (showAll == !filtered) | - |
| 213 | return; | - |
| 214 | beginResetModel(); | - |
| 215 | showAll = !filtered; | - |
| 216 | endResetModel(); | - |
| 217 | } | - |
| 218 | | - |
| 219 | bool QCompletionModel::hasChildren(const QModelIndex &parent) const | - |
| 220 | { | - |
| 221 | const QCompletionModelPrivate * const d = d_func(); | - |
| 222 | if (parent.isValid()) | - |
| 223 | return false; | - |
| 224 | | - |
| 225 | if (showAll) | - |
| 226 | return d->model->hasChildren(mapToSource(parent)); | - |
| 227 | | - |
| 228 | if (!engine->matchCount()) | - |
| 229 | return false; | - |
| 230 | | - |
| 231 | return true; | - |
| 232 | } | - |
| 233 | | - |
| 234 | QVariant QCompletionModel::data(const QModelIndex& index, int role) const | - |
| 235 | { | - |
| 236 | const QCompletionModelPrivate * const d = d_func(); | - |
| 237 | return d->model->data(mapToSource(index), role); | - |
| 238 | } | - |
| 239 | | - |
| 240 | void QCompletionModel::modelDestroyed() | - |
| 241 | { | - |
| 242 | QAbstractProxyModel::setSourceModel(0); | - |
| 243 | invalidate(); | - |
| 244 | } | - |
| 245 | | - |
| 246 | void QCompletionModel::rowsInserted() | - |
| 247 | { | - |
| 248 | invalidate(); | - |
| 249 | rowsAdded(); | - |
| 250 | } | - |
| 251 | | - |
| 252 | void QCompletionModel::invalidate() | - |
| 253 | { | - |
| 254 | engine->cache.clear(); | - |
| 255 | filter(engine->curParts); | - |
| 256 | } | - |
| 257 | | - |
| 258 | void QCompletionModel::filter(const QStringList& parts) | - |
| 259 | { | - |
| 260 | QCompletionModelPrivate * const d = d_func(); | - |
| 261 | beginResetModel(); | - |
| 262 | engine->filter(parts); | - |
| 263 | endResetModel(); | - |
| 264 | | - |
| 265 | if (d->model->canFetchMore(engine->curParent)) | - |
| 266 | d->model->fetchMore(engine->curParent); | - |
| 267 | } | - |
| 268 | | - |
| 269 | | - |
| 270 | void QCompletionEngine::filter(const QStringList& parts) | - |
| 271 | { | - |
| 272 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 273 | curParts = parts; | - |
| 274 | if (curParts.isEmpty()) | - |
| 275 | curParts.append(QString()); | - |
| 276 | | - |
| 277 | curRow = -1; | - |
| 278 | curParent = QModelIndex(); | - |
| 279 | curMatch = QMatchData(); | - |
| 280 | historyMatch = filterHistory(); | - |
| 281 | | - |
| 282 | if (!model) | - |
| 283 | return; | - |
| 284 | | - |
| 285 | QModelIndex parent; | - |
| 286 | for (int i = 0; i < curParts.count() - 1; i++) { | - |
| 287 | QString part = curParts.at(i); | - |
| 288 | int emi = filter(part, parent, -1).exactMatchIndex; | - |
| 289 | if (emi == -1) | - |
| 290 | return; | - |
| 291 | parent = model->index(emi, c->column, parent); | - |
| 292 | } | - |
| 293 | | - |
| 294 | | - |
| 295 | | - |
| 296 | curParent = parent; | - |
| 297 | if (curParts.constLast().isEmpty()) | - |
| 298 | curMatch = QMatchData(QIndexMapper(0, model->rowCount(curParent) - 1), -1, false); | - |
| 299 | else | - |
| 300 | curMatch = filter(curParts.constLast(), curParent, 1); | - |
| 301 | curRow = curMatch.isValid() ? 0 : -1; | - |
| 302 | } | - |
| 303 | | - |
| 304 | QMatchData QCompletionEngine::filterHistory() | - |
| 305 | { | - |
| 306 | QAbstractItemModel *source = c->proxy->sourceModel(); | - |
| 307 | if (curParts.count() <= 1 || c->proxy->showAll || !source) | - |
| 308 | return QMatchData(); | - |
| 309 | | - |
| 310 | | - |
| 311 | const bool isDirModel = (qobject_cast<QDirModel *>(source) != 0); | - |
| 312 | | - |
| 313 | | - |
| 314 | | - |
| 315 | (void)isDirModel; | - |
| 316 | | - |
| 317 | const bool isFsModel = (qobject_cast<QFileSystemModel *>(source) != 0); | - |
| 318 | | - |
| 319 | | - |
| 320 | | - |
| 321 | (void)isFsModel; | - |
| 322 | QVector<int> v; | - |
| 323 | QIndexMapper im(v); | - |
| 324 | QMatchData m(im, -1, true); | - |
| 325 | | - |
| 326 | for (int i = 0; i < source->rowCount(); i++) { | - |
| 327 | QString str = source->index(i, c->column).data().toString(); | - |
| 328 | if (str.startsWith(c->prefix, c->cs) | - |
| 329 | | - |
| 330 | && ((!isFsModel && !isDirModel) || QDir::toNativeSeparators(str) != QDir::separator()) | - |
| 331 | | - |
| 332 | ) | - |
| 333 | m.indices.append(i); | - |
| 334 | } | - |
| 335 | return m; | - |
| 336 | } | - |
| 337 | | - |
| 338 | | - |
| 339 | bool QCompletionEngine::matchHint(QString part, const QModelIndex& parent, QMatchData *hint) | - |
| 340 | { | - |
| 341 | if (c->cs == Qt::CaseInsensitive) | - |
| 342 | part = part.toLower(); | - |
| 343 | | - |
| 344 | const CacheItem& map = cache[parent]; | - |
| 345 | | - |
| 346 | QString key = part; | - |
| 347 | while (!key.isEmpty()) { | - |
| 348 | key.chop(1); | - |
| 349 | if (map.contains(key)) { | - |
| 350 | *hint = map[key]; | - |
| 351 | return true; | - |
| 352 | } | - |
| 353 | } | - |
| 354 | | - |
| 355 | return false; | - |
| 356 | } | - |
| 357 | | - |
| 358 | bool QCompletionEngine::lookupCache(QString part, const QModelIndex& parent, QMatchData *m) | - |
| 359 | { | - |
| 360 | if (c->cs == Qt::CaseInsensitive) | - |
| 361 | part = part.toLower(); | - |
| 362 | const CacheItem& map = cache[parent]; | - |
| 363 | if (!map.contains(part)) | - |
| 364 | return false; | - |
| 365 | *m = map[part]; | - |
| 366 | return true; | - |
| 367 | } | - |
| 368 | | - |
| 369 | | - |
| 370 | void QCompletionEngine::saveInCache(QString part, const QModelIndex& parent, const QMatchData& m) | - |
| 371 | { | - |
| 372 | if (c->filterMode == Qt::MatchEndsWith) | - |
| 373 | return; | - |
| 374 | QMatchData old = cache[parent].take(part); | - |
| 375 | cost = cost + m.indices.cost() - old.indices.cost(); | - |
| 376 | if (cost * sizeof(int) > 1024 * 1024) { | - |
| 377 | QMap<QModelIndex, CacheItem>::iterator it1 = cache.begin(); | - |
| 378 | while (it1 != cache.end()) { | - |
| 379 | CacheItem& ci = it1.value(); | - |
| 380 | int sz = ci.count()/2; | - |
| 381 | QMap<QString, QMatchData>::iterator it2 = ci.begin(); | - |
| 382 | int i = 0; | - |
| 383 | while (it2 != ci.end() && i < sz) { | - |
| 384 | cost -= it2.value().indices.cost(); | - |
| 385 | it2 = ci.erase(it2); | - |
| 386 | i++; | - |
| 387 | } | - |
| 388 | if (ci.count() == 0) { | - |
| 389 | it1 = cache.erase(it1); | - |
| 390 | } else { | - |
| 391 | ++it1; | - |
| 392 | } | - |
| 393 | } | - |
| 394 | } | - |
| 395 | | - |
| 396 | if (c->cs == Qt::CaseInsensitive) | - |
| 397 | part = part.toLower(); | - |
| 398 | cache[parent][part] = m; | - |
| 399 | } | - |
| 400 | | - |
| 401 | | - |
| 402 | QIndexMapper QSortedModelEngine::indexHint(QString part, const QModelIndex& parent, Qt::SortOrder order) | - |
| 403 | { | - |
| 404 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 405 | | - |
| 406 | if (c->cs == Qt::CaseInsensitive) | - |
| 407 | part = part.toLower(); | - |
| 408 | | - |
| 409 | const CacheItem& map = cache[parent]; | - |
| 410 | | - |
| 411 | | - |
| 412 | int to = model->rowCount(parent) - 1; | - |
| 413 | int from = 0; | - |
| 414 | const CacheItem::const_iterator it = map.lowerBound(part); | - |
| 415 | | - |
| 416 | | - |
| 417 | for(CacheItem::const_iterator it1 = it; it1-- != map.constBegin();) { | - |
| 418 | const QMatchData& value = it1.value(); | - |
| 419 | if (value.isValid()) { | - |
| 420 | if (order == Qt::AscendingOrder) { | - |
| 421 | from = value.indices.last() + 1; | - |
| 422 | } else { | - |
| 423 | to = value.indices.first() - 1; | - |
| 424 | } | - |
| 425 | break; | - |
| 426 | } | - |
| 427 | } | - |
| 428 | | - |
| 429 | | - |
| 430 | for(CacheItem::const_iterator it2 = it; it2 != map.constEnd(); ++it2) { | - |
| 431 | const QMatchData& value = it2.value(); | - |
| 432 | if (value.isValid() && !it2.key().startsWith(part)) { | - |
| 433 | if (order == Qt::AscendingOrder) { | - |
| 434 | to = value.indices.first() - 1; | - |
| 435 | } else { | - |
| 436 | from = value.indices.first() + 1; | - |
| 437 | } | - |
| 438 | break; | - |
| 439 | } | - |
| 440 | } | - |
| 441 | | - |
| 442 | return QIndexMapper(from, to); | - |
| 443 | } | - |
| 444 | | - |
| 445 | Qt::SortOrder QSortedModelEngine::sortOrder(const QModelIndex &parent) const | - |
| 446 | { | - |
| 447 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 448 | | - |
| 449 | int rowCount = model->rowCount(parent); | - |
| 450 | if (rowCount < 2) | - |
| 451 | return Qt::AscendingOrder; | - |
| 452 | QString first = model->data(model->index(0, c->column, parent), c->role).toString(); | - |
| 453 | QString last = model->data(model->index(rowCount - 1, c->column, parent), c->role).toString(); | - |
| 454 | return QString::compare(first, last, c->cs) <= 0 ? Qt::AscendingOrder : Qt::DescendingOrder; | - |
| 455 | } | - |
| 456 | | - |
| 457 | QMatchData QSortedModelEngine::filter(const QString& part, const QModelIndex& parent, int) | - |
| 458 | { | - |
| 459 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 460 | | - |
| 461 | QMatchData hint; | - |
| 462 | if (lookupCache(part, parent, &hint)) | - |
| 463 | return hint; | - |
| 464 | | - |
| 465 | QIndexMapper indices; | - |
| 466 | Qt::SortOrder order = sortOrder(parent); | - |
| 467 | | - |
| 468 | if (matchHint(part, parent, &hint)) { | - |
| 469 | if (!hint.isValid()) | - |
| 470 | return QMatchData(); | - |
| 471 | indices = hint.indices; | - |
| 472 | } else { | - |
| 473 | indices = indexHint(part, parent, order); | - |
| 474 | } | - |
| 475 | | - |
| 476 | | - |
| 477 | int high = indices.to() + 1; | - |
| 478 | int low = indices.from() - 1; | - |
| 479 | int probe; | - |
| 480 | QModelIndex probeIndex; | - |
| 481 | QString probeData; | - |
| 482 | | - |
| 483 | while (high - low > 1) | - |
| 484 | { | - |
| 485 | probe = (high + low) / 2; | - |
| 486 | probeIndex = model->index(probe, c->column, parent); | - |
| 487 | probeData = model->data(probeIndex, c->role).toString(); | - |
| 488 | const int cmp = QString::compare(probeData, part, c->cs); | - |
| 489 | if ((order == Qt::AscendingOrder && cmp >= 0) | - |
| 490 | || (order == Qt::DescendingOrder && cmp < 0)) { | - |
| 491 | high = probe; | - |
| 492 | } else { | - |
| 493 | low = probe; | - |
| 494 | } | - |
| 495 | } | - |
| 496 | | - |
| 497 | if ((order == Qt::AscendingOrder && low == indices.to()) | - |
| 498 | || (order == Qt::DescendingOrder && high == indices.from())) { | - |
| 499 | saveInCache(part, parent, QMatchData()); | - |
| 500 | return QMatchData(); | - |
| 501 | } | - |
| 502 | | - |
| 503 | probeIndex = model->index(order == Qt::AscendingOrder ? low+1 : high-1, c->column, parent); | - |
| 504 | probeData = model->data(probeIndex, c->role).toString(); | - |
| 505 | if (!probeData.startsWith(part, c->cs)) { | - |
| 506 | saveInCache(part, parent, QMatchData()); | - |
| 507 | return QMatchData(); | - |
| 508 | } | - |
| 509 | | - |
| 510 | const bool exactMatch = QString::compare(probeData, part, c->cs) == 0; | - |
| 511 | int emi = exactMatch ? (order == Qt::AscendingOrder ? low+1 : high-1) : -1; | - |
| 512 | | - |
| 513 | int from = 0; | - |
| 514 | int to = 0; | - |
| 515 | if (order == Qt::AscendingOrder) { | - |
| 516 | from = low + 1; | - |
| 517 | high = indices.to() + 1; | - |
| 518 | low = from; | - |
| 519 | } else { | - |
| 520 | to = high - 1; | - |
| 521 | low = indices.from() - 1; | - |
| 522 | high = to; | - |
| 523 | } | - |
| 524 | | - |
| 525 | while (high - low > 1) | - |
| 526 | { | - |
| 527 | probe = (high + low) / 2; | - |
| 528 | probeIndex = model->index(probe, c->column, parent); | - |
| 529 | probeData = model->data(probeIndex, c->role).toString(); | - |
| 530 | const bool startsWith = probeData.startsWith(part, c->cs); | - |
| 531 | if ((order == Qt::AscendingOrder && startsWith) | - |
| 532 | || (order == Qt::DescendingOrder && !startsWith)) { | - |
| 533 | low = probe; | - |
| 534 | } else { | - |
| 535 | high = probe; | - |
| 536 | } | - |
| 537 | } | - |
| 538 | | - |
| 539 | QMatchData m(order == Qt::AscendingOrder ? QIndexMapper(from, high - 1) : QIndexMapper(low+1, to), emi, false); | - |
| 540 | saveInCache(part, parent, m); | - |
| 541 | return m; | - |
| 542 | } | - |
| 543 | | - |
| 544 | | - |
| 545 | int QUnsortedModelEngine::buildIndices(const QString& str, const QModelIndex& parent, int n, | - |
| 546 | const QIndexMapper& indices, QMatchData* m) | - |
| 547 | { | - |
| 548 | ((!(m->partial)) ? qt_assert("m->partial",__FILE__,696702) : qt_noop()); | - |
| 549 | ((!(n != -1 || m->exactMatchIndex == -1)) ? qt_assert("n != -1 || m->exactMatchIndex == -1",__FILE__,697703) : qt_noop()); | - |
| 550 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 551 | int i, count = 0; | - |
| 552 | | - |
| 553 | for (i = 0; i < indices.count() && count != n; ++i) { | - |
| 554 | QModelIndex idx = model->index(indices[i], c->column, parent); | - |
| 555 | | - |
| 556 | if (!(model->flags(idx) & Qt::ItemIsSelectable)) | - |
| 557 | continue; | - |
| 558 | | - |
| 559 | QString data = model->data(idx, c->role).toString(); | - |
| 560 | | - |
| 561 | switch (c->filterMode) { | - |
| 562 | case Qt::MatchStartsWith: | - |
| 563 | if (!data.startsWith(str, c->cs)) | - |
| 564 | continue; | - |
| 565 | break; | - |
| 566 | case Qt::MatchContains: | - |
| 567 | if (!data.contains(str, c->cs)) | - |
| 568 | continue; | - |
| 569 | break; | - |
| 570 | case Qt::MatchEndsWith: | - |
| 571 | if (!data.endsWith(str, c->cs)) | - |
| 572 | continue; | - |
| 573 | break; | - |
| 574 | case Qt::MatchExactly: | - |
| 575 | case Qt::MatchFixedString: | - |
| 576 | case Qt::MatchCaseSensitive: | - |
| 577 | case Qt::MatchRegExp: | - |
| 578 | case Qt::MatchWildcard: | - |
| 579 | case Qt::MatchWrap: | - |
| 580 | case Qt::MatchRecursive: | - |
| 581 | do { ((!(false)) ? qt_assert_x("Q_UNREACHABLE()", "Q_UNREACHABLE was reached",__FILE__,729735) : qt_noop()); __builtin_unreachable(); } while (0); | - |
| 582 | break; | - |
| 583 | } | - |
| 584 | m->indices.append(indices[i]); | - |
| 585 | ++count; | - |
| 586 | if (m->exactMatchIndex == -1 && QString::compare(data, str, c->cs) == 0) { | - |
| 587 | m->exactMatchIndex = indices[i]; | - |
| 588 | if (n == -1) | - |
| 589 | return indices[i]; | - |
| 590 | } | - |
| 591 | } | - |
| 592 | return indices[i-1]; | - |
| 593 | } | - |
| 594 | | - |
| 595 | void QUnsortedModelEngine::filterOnDemand(int n) | - |
| 596 | { | - |
| 597 | ((!(matchCount())) ? qt_assert("matchCount()",__FILE__,745751) : qt_noop()); | - |
| 598 | if (!curMatch.partial| TRUE | never evaluated | | FALSE | never evaluated |
) | 0 |
| 599 | return; never executed: return; | 0 |
| 600 | ((!(n >= -1)) ? qt_assert("n >= -1",__FILE__,748754) : qt_noop()); | - |
| 601 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 602 | int lastRow = model->rowCount(curParent) - 1; | - |
| 603 | QIndexMapper im(curMatch.indices.last() + 1, lastRow); | - |
| 604 | int lastIndex = buildIndices(curParts.lastconstLast(), curParent, n, im, &curMatch); | - |
| 605 | curMatch.partial = (lastRow != lastIndex); | - |
| 606 | saveInCache(curParts.lastconstLast(), curParent, curMatch); | - |
| 607 | } never executed: end of block | 0 |
| 608 | | - |
| 609 | QMatchData QUnsortedModelEngine::filter(const QString& part, const QModelIndex& parent, int n) | - |
| 610 | { | - |
| 611 | QMatchData hint; | - |
| 612 | | - |
| 613 | QVector<int> v; | - |
| 614 | QIndexMapper im(v); | - |
| 615 | QMatchData m(im, -1, true); | - |
| 616 | | - |
| 617 | const QAbstractItemModel *model = c->proxy->sourceModel(); | - |
| 618 | bool foundInCache = lookupCache(part, parent, &m); | - |
| 619 | | - |
| 620 | if (!foundInCache) { | - |
| 621 | if (matchHint(part, parent, &hint) && !hint.isValid()) | - |
| 622 | return QMatchData(); | - |
| 623 | } | - |
| 624 | | - |
| 625 | if (!foundInCache && !hint.isValid()) { | - |
| 626 | const int lastRow = model->rowCount(parent) - 1; | - |
| 627 | QIndexMapper all(0, lastRow); | - |
| 628 | int lastIndex = buildIndices(part, parent, n, all, &m); | - |
| 629 | m.partial = (lastIndex != lastRow); | - |
| 630 | } else { | - |
| 631 | if (!foundInCache) { | - |
| 632 | buildIndices(part, parent, 2147483647, hint.indices, &m); | - |
| 633 | m.partial = hint.partial; | - |
| 634 | } | - |
| 635 | if (m.partial && ((n == -1 && m.exactMatchIndex == -1) || (m.indices.count() < n))) { | - |
| 636 | | - |
| 637 | const int lastRow = model->rowCount(parent) - 1; | - |
| 638 | QIndexMapper rest(hint.indices.last() + 1, lastRow); | - |
| 639 | int want = n == -1 ? -1 : n - m.indices.count(); | - |
| 640 | int lastIndex = buildIndices(part, parent, want, rest, &m); | - |
| 641 | m.partial = (lastRow != lastIndex); | - |
| 642 | } | - |
| 643 | } | - |
| 644 | | - |
| 645 | saveInCache(part, parent, m); | - |
| 646 | return m; | - |
| 647 | } | - |
| 648 | | - |
| 649 | | - |
| 650 | QCompleterPrivate::QCompleterPrivate() | - |
| 651 | : widget(0), proxy(0), popup(0), filterMode(Qt::MatchStartsWith), cs(Qt::CaseSensitive), | - |
| 652 | role(Qt::EditRole), column(0), maxVisibleItems(7), sorting(QCompleter::UnsortedModel), | - |
| 653 | wrap(true), eatFocusOut(true), hiddenBecauseNoMatch(false) | - |
| 654 | { | - |
| 655 | } | - |
| 656 | | - |
| 657 | void QCompleterPrivate::init(QAbstractItemModel *m) | - |
| 658 | { | - |
| 659 | QCompleter * const q = q_func(); | - |
| 660 | proxy = new QCompletionModel(this, q); | - |
| 661 | QObject::connect(proxy, qFlagLocation("2""rowsAdded()" "\0" __FILE__ ":" "809""815"), q, qFlagLocation("1""_q_autoResizePopup()" "\0" __FILE__ ":" "809""815")); | - |
| 662 | q->setModel(m); | - |
| 663 | | - |
| 664 | | - |
| 665 | | - |
| 666 | q->setCompletionMode(QCompleter::PopupCompletion); | - |
| 667 | | - |
| 668 | } | - |
| 669 | | - |
| 670 | void QCompleterPrivate::setCurrentIndex(QModelIndex index, bool select) | - |
| 671 | { | - |
| 672 | QCompleter * const q = q_func(); | - |
| 673 | if (!q->popup()) | - |
| 674 | return; | - |
| 675 | if (!select) { | - |
| 676 | popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); | - |
| 677 | } else { | - |
| 678 | if (!index.isValid()) | - |
| 679 | popup->selectionModel()->clear(); | - |
| 680 | else | - |
| 681 | popup->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | - |
| 682 | | QItemSelectionModel::Rows); | - |
| 683 | } | - |
| 684 | index = popup->selectionModel()->currentIndex(); | - |
| 685 | if (!index.isValid()) | - |
| 686 | popup->scrollToTop(); | - |
| 687 | else | - |
| 688 | popup->scrollTo(index, QAbstractItemView::PositionAtTop); | - |
| 689 | } | - |
| 690 | | - |
| 691 | void QCompleterPrivate::_q_completionSelected(const QItemSelection& selection) | - |
| 692 | { | - |
| 693 | QModelIndex index; | - |
| 694 | if (!selection.indexes().isEmpty()) | - |
| 695 | index = selection.indexes().first(); | - |
| 696 | | - |
| 697 | _q_complete(index, true); | - |
| 698 | } | - |
| 699 | | - |
| 700 | void QCompleterPrivate::_q_complete(QModelIndex index, bool highlighted) | - |
| 701 | { | - |
| 702 | QCompleter * const q = q_func(); | - |
| 703 | QString completion; | - |
| 704 | | - |
| 705 | if (!index.isValid() || (!proxy->showAll && (index.row() >= proxy->engine->matchCount()))) { | - |
| 706 | completion = prefix; | - |
| 707 | index = QModelIndex(); | - |
| 708 | } else { | - |
| 709 | if (!(index.flags() & Qt::ItemIsEnabled)) | - |
| 710 | return; | - |
| 711 | QModelIndex si = proxy->mapToSource(index); | - |
| 712 | si = si.sibling(si.row(), column); | - |
| 713 | completion = q->pathFromIndex(si); | - |
| 714 | | - |
| 715 | | - |
| 716 | if (mode == QCompleter::InlineCompletion) { | - |
| 717 | if (qobject_cast<QDirModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir()) | - |
| 718 | completion += QDir::separator(); | - |
| 719 | } | - |
| 720 | | - |
| 721 | | - |
| 722 | | - |
| 723 | if (mode == QCompleter::InlineCompletion) { | - |
| 724 | if (qobject_cast<QFileSystemModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir()) | - |
| 725 | completion += QDir::separator(); | - |
| 726 | } | - |
| 727 | | - |
| 728 | } | - |
| 729 | | - |
| 730 | if (highlighted) { | - |
| 731 | q->highlighted(index); | - |
| 732 | q->highlighted(completion); | - |
| 733 | } else { | - |
| 734 | q->activated(index); | - |
| 735 | q->activated(completion); | - |
| 736 | } | - |
| 737 | } | - |
| 738 | | - |
| 739 | void QCompleterPrivate::_q_autoResizePopup() | - |
| 740 | { | - |
| 741 | if (!popup || !popup->isVisible()) | - |
| 742 | return; | - |
| 743 | showPopup(popupRect); | - |
| 744 | } | - |
| 745 | | - |
| 746 | void QCompleterPrivate::showPopup(const QRect& rect) | - |
| 747 | { | - |
| 748 | const QRect screen = QApplication::desktop()->availableGeometry(widget); | - |
| 749 | Qt::LayoutDirection dir = widget->layoutDirection(); | - |
| 750 | QPoint pos; | - |
| 751 | int rh, w; | - |
| 752 | int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3; | - |
| 753 | QScrollBar *hsb = popup->horizontalScrollBar(); | - |
| 754 | if (hsb && hsb->isVisible()) | - |
| 755 | h += popup->horizontalScrollBar()->sizeHint().height(); | - |
| 756 | | - |
| 757 | if (rect.isValid()) { | - |
| 758 | rh = rect.height(); | - |
| 759 | w = rect.width(); | - |
| 760 | pos = widget->mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft()); | - |
| 761 | } else { | - |
| 762 | rh = widget->height(); | - |
| 763 | pos = widget->mapToGlobal(QPoint(0, widget->height() - 2)); | - |
| 764 | w = widget->width(); | - |
| 765 | } | - |
| 766 | | - |
| 767 | if (w > screen.width()) | - |
| 768 | w = screen.width(); | - |
| 769 | if ((pos.x() + w) > (screen.x() + screen.width())) | - |
| 770 | pos.setX(screen.x() + screen.width() - w); | - |
| 771 | if (pos.x() < screen.x()) | - |
| 772 | pos.setX(screen.x()); | - |
| 773 | | - |
| 774 | int top = pos.y() - rh - screen.top() + 2; | - |
| 775 | int bottom = screen.bottom() - pos.y(); | - |
| 776 | h = qMax(h, popup->minimumHeight()); | - |
| 777 | if (h > bottom) { | - |
| 778 | h = qMin(qMax(top, bottom), h); | - |
| 779 | | - |
| 780 | if (top > bottom) | - |
| 781 | pos.setY(pos.y() - h - rh + 2); | - |
| 782 | } | - |
| 783 | | - |
| 784 | popup->setGeometry(pos.x(), pos.y(), w, h); | - |
| 785 | | - |
| 786 | if (!popup->isVisible()) | - |
| 787 | popup->show(); | - |
| 788 | } | - |
| 789 | | - |
| 790 | void QCompleterPrivate::_q_fileSystemModelDirectoryLoaded(const QString &path) | - |
| 791 | { | - |
| 792 | QCompleter * const q = q_func(); | - |
| 793 | | - |
| 794 | | - |
| 795 | | - |
| 796 | if (hiddenBecauseNoMatch | - |
| 797 | && prefix.startsWith(path) && prefix != (path + QLatin1Char('/')) | - |
| 798 | && widget) { | - |
| 799 | q->complete(); | - |
| 800 | } | - |
| 801 | } | - |
| 802 | | - |
| 803 | | - |
| 804 | | - |
| 805 | | - |
| 806 | QCompleter::QCompleter(QObject *parent) | - |
| 807 | : QObject(*new QCompleterPrivate(), parent) | - |
| 808 | { | - |
| 809 | QCompleterPrivate * const d = d_func(); | - |
| 810 | d->init(); | - |
| 811 | } | - |
| 812 | | - |
| 813 | | - |
| 814 | | - |
| 815 | | - |
| 816 | | - |
| 817 | QCompleter::QCompleter(QAbstractItemModel *model, QObject *parent) | - |
| 818 | : QObject(*new QCompleterPrivate(), parent) | - |
| 819 | { | - |
| 820 | QCompleterPrivate * const d = d_func(); | - |
| 821 | d->init(model); | - |
| 822 | } | - |
| 823 | | - |
| 824 | | - |
| 825 | | - |
| 826 | | - |
| 827 | | - |
| 828 | | - |
| 829 | QCompleter::QCompleter(const QStringList& list, QObject *parent) | - |
| 830 | : QObject(*new QCompleterPrivate(), parent) | - |
| 831 | { | - |
| 832 | QCompleterPrivate * const d = d_func(); | - |
| 833 | d->init(new QStringListModel(list, this)); | - |
| 834 | } | - |
| 835 | | - |
| 836 | | - |
| 837 | | - |
| 838 | | - |
| 839 | | - |
| 840 | QCompleter::~QCompleter() | - |
| 841 | { | - |
| 842 | } | - |
| 843 | void QCompleter::setWidget(QWidget *widget) | - |
| 844 | { | - |
| 845 | QCompleterPrivate * const d = d_func(); | - |
| 846 | if (d->widget) | - |
| 847 | d->widget->removeEventFilter(this); | - |
| 848 | d->widget = widget; | - |
| 849 | if (d->widget) | - |
| 850 | d->widget->installEventFilter(this); | - |
| 851 | if (d->popup) { | - |
| 852 | d->popup->hide(); | - |
| 853 | d->popup->setFocusProxy(d->widget); | - |
| 854 | } | - |
| 855 | } | - |
| 856 | | - |
| 857 | | - |
| 858 | | - |
| 859 | | - |
| 860 | | - |
| 861 | | - |
| 862 | QWidget *QCompleter::widget() const | - |
| 863 | { | - |
| 864 | const QCompleterPrivate * const d = d_func(); | - |
| 865 | return d->widget; | - |
| 866 | } | - |
| 867 | void QCompleter::setModel(QAbstractItemModel *model) | - |
| 868 | { | - |
| 869 | QCompleterPrivate * const d = d_func(); | - |
| 870 | QAbstractItemModel *oldModel = d->proxy->sourceModel(); | - |
| 871 | | - |
| 872 | if (qobject_cast<const QFileSystemModel *>(oldModel)) | - |
| 873 | setCompletionRole(Qt::EditRole); | - |
| 874 | | - |
| 875 | d->proxy->setSourceModel(model); | - |
| 876 | if (d->popup) | - |
| 877 | setPopup(d->popup); | - |
| 878 | if (oldModel && oldModel->QObject::parent() == this) | - |
| 879 | delete oldModel; | - |
| 880 | | - |
| 881 | if (qobject_cast<QDirModel *>(model)) { | - |
| 882 | | - |
| 883 | | - |
| 884 | | - |
| 885 | setCaseSensitivity(Qt::CaseSensitive); | - |
| 886 | | - |
| 887 | } | - |
| 888 | | - |
| 889 | | - |
| 890 | QFileSystemModel *fsModel = qobject_cast<QFileSystemModel *>(model); | - |
| 891 | if (fsModel) { | - |
| 892 | | - |
| 893 | | - |
| 894 | | - |
| 895 | setCaseSensitivity(Qt::CaseSensitive); | - |
| 896 | | - |
| 897 | setCompletionRole(QFileSystemModel::FileNameRole); | - |
| 898 | connect(fsModel, qFlagLocation("2""directoryLoaded(QString)" "\0" __FILE__ ":" "1068""1074"), this, qFlagLocation("1""_q_fileSystemModelDirectoryLoaded(QString)" "\0" __FILE__ ":" "1068""1074")); | - |
| 899 | } | - |
| 900 | | - |
| 901 | } | - |
| 902 | | - |
| 903 | | - |
| 904 | | - |
| 905 | | - |
| 906 | | - |
| 907 | | - |
| 908 | QAbstractItemModel *QCompleter::model() const | - |
| 909 | { | - |
| 910 | const QCompleterPrivate * const d = d_func(); | - |
| 911 | return d->proxy->sourceModel(); | - |
| 912 | } | - |
| 913 | void QCompleter::setCompletionMode(QCompleter::CompletionMode mode) | - |
| 914 | { | - |
| 915 | QCompleterPrivate * const d = d_func(); | - |
| 916 | d->mode = mode; | - |
| 917 | d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion); | - |
| 918 | | - |
| 919 | if (mode == QCompleter::InlineCompletion) { | - |
| 920 | if (d->widget) | - |
| 921 | d->widget->removeEventFilter(this); | - |
| 922 | if (d->popup) { | - |
| 923 | d->popup->deleteLater(); | - |
| 924 | d->popup = 0; | - |
| 925 | } | - |
| 926 | } else { | - |
| 927 | if (d->widget) | - |
| 928 | d->widget->installEventFilter(this); | - |
| 929 | } | - |
| 930 | } | - |
| 931 | | - |
| 932 | QCompleter::CompletionMode QCompleter::completionMode() const | - |
| 933 | { | - |
| 934 | const QCompleterPrivate * const d = d_func(); | - |
| 935 | return d->mode; | - |
| 936 | } | - |
| 937 | void QCompleter::setFilterMode(Qt::MatchFlags filterMode) | - |
| 938 | { | - |
| 939 | QCompleterPrivate * const d = d_func(); | - |
| 940 | | - |
| 941 | if (d->filterMode == filterMode| TRUE | never evaluated | | FALSE | never evaluated |
) | 0 |
| 942 | return; never executed: return; | 0 |
| 943 | | - |
| 944 | if (__builtin_expect(!!(| TRUE | never evaluated | | FALSE | never evaluated |
filterMode != Qt::MatchStartsWith && filterMode != Qt::MatchContains && filterMode != Qt::MatchEndsWith), false)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 945 | | - |
| 946 | ) { | - |
| 947 | QMessageLogger(__FILE__, 11541160, __PRETTY_FUNCTION__).warning("Unhandled QCompleter::filterMode flag is used."); | - |
| 948 | return; never executed: return; | 0 |
| 949 | } | - |
| 950 | | - |
| 951 | d->filterMode = filterMode; | - |
| 952 | d->proxy->createEngine(); | - |
| 953 | d->proxy->invalidate(); | - |
| 954 | } never executed: end of block | 0 |
| 955 | | - |
| 956 | Qt::MatchFlags QCompleter::filterMode() const | - |
| 957 | { | - |
| 958 | const QCompleterPrivate * const d = d_func(); | - |
| 959 | return d->filterMode; | - |
| 960 | } | - |
| 961 | void QCompleter::setPopup(QAbstractItemView *popup) | - |
| 962 | { | - |
| 963 | QCompleterPrivate * const d = d_func(); | - |
| 964 | ((!(popup != 0)) ? qt_assert("popup != 0",__FILE__,11871193) : qt_noop()); | - |
| 965 | if (d->popup) { | - |
| 966 | QObject::disconnect(d->popup->selectionModel(), 0, this, 0); | - |
| 967 | QObject::disconnect(d->popup, 0, this, 0); | - |
| 968 | } | - |
| 969 | if (d->popup != popup) | - |
| 970 | delete d->popup; | - |
| 971 | if (popup->model() != d->proxy) | - |
| 972 | popup->setModel(d->proxy); | - |
| 973 | popup->hide(); | - |
| 974 | | - |
| 975 | Qt::FocusPolicy origPolicy = Qt::NoFocus; | - |
| 976 | if (d->widget) | - |
| 977 | origPolicy = d->widget->focusPolicy(); | - |
| 978 | popup->setParent(0, Qt::Popup); | - |
| 979 | popup->setFocusPolicy(Qt::NoFocus); | - |
| 980 | if (d->widget) | - |
| 981 | d->widget->setFocusPolicy(origPolicy); | - |
| 982 | | - |
| 983 | popup->setFocusProxy(d->widget); | - |
| 984 | popup->installEventFilter(this); | - |
| 985 | popup->setItemDelegate(new QCompleterItemDelegate(popup)); | - |
| 986 | | - |
| 987 | if (QListView *listView = qobject_cast<QListView *>(popup)) { | - |
| 988 | listView->setModelColumn(d->column); | - |
| 989 | } | - |
| 990 | | - |
| 991 | | - |
| 992 | QObject::connect(popup, qFlagLocation("2""clicked(QModelIndex)" "\0" __FILE__ ":" "1215""1221"), | - |
| 993 | this, qFlagLocation("1""_q_complete(QModelIndex)" "\0" __FILE__ ":" "1216""1222")); | - |
| 994 | QObject::connect(this, qFlagLocation("2""activated(QModelIndex)" "\0" __FILE__ ":" "1217""1223"), | - |
| 995 | popup, qFlagLocation("1""hide()" "\0" __FILE__ ":" "1218""1224")); | - |
| 996 | | - |
| 997 | QObject::connect(popup->selectionModel(), qFlagLocation("2""selectionChanged(QItemSelection,QItemSelection)" "\0" __FILE__ ":" "1220""1226"), | - |
| 998 | this, qFlagLocation("1""_q_completionSelected(QItemSelection)" "\0" __FILE__ ":" "1221""1227")); | - |
| 999 | d->popup = popup; | - |
| 1000 | } | - |
| 1001 | | - |
| 1002 | | - |
| 1003 | | - |
| 1004 | | - |
| 1005 | | - |
| 1006 | | - |
| 1007 | QAbstractItemView *QCompleter::popup() const | - |
| 1008 | { | - |
| 1009 | const QCompleterPrivate * const d = d_func(); | - |
| 1010 | | - |
| 1011 | if (!d->popup && completionMode() != QCompleter::InlineCompletion) { | - |
| 1012 | QListView *listView = new QListView; | - |
| 1013 | listView->setEditTriggers(QAbstractItemView::NoEditTriggers); | - |
| 1014 | listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | - |
| 1015 | listView->setSelectionBehavior(QAbstractItemView::SelectRows); | - |
| 1016 | listView->setSelectionMode(QAbstractItemView::SingleSelection); | - |
| 1017 | listView->setModelColumn(d->column); | - |
| 1018 | QCompleter *that = const_cast<QCompleter*>(this); | - |
| 1019 | that->setPopup(listView); | - |
| 1020 | } | - |
| 1021 | | - |
| 1022 | return d->popup; | - |
| 1023 | } | - |
| 1024 | | - |
| 1025 | | - |
| 1026 | | - |
| 1027 | | - |
| 1028 | bool QCompleter::event(QEvent *ev) | - |
| 1029 | { | - |
| 1030 | return QObject::event(ev); | - |
| 1031 | } | - |
| 1032 | | - |
| 1033 | | - |
| 1034 | | - |
| 1035 | | - |
| 1036 | bool QCompleter::eventFilter(QObject *o, QEvent *e) | - |
| 1037 | { | - |
| 1038 | QCompleterPrivate * const d = d_func(); | - |
| 1039 | | - |
| 1040 | if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) { | - |
| 1041 | d->hiddenBecauseNoMatch = false; | - |
| 1042 | if (d->popup && d->popup->isVisible()) | - |
| 1043 | return true; | - |
| 1044 | } | - |
| 1045 | | - |
| 1046 | if (o != d->popup) | - |
| 1047 | return QObject::eventFilter(o, e); | - |
| 1048 | | - |
| 1049 | switch (e->type()) { | - |
| 1050 | case QEvent::KeyPress: { | - |
| 1051 | QKeyEvent *ke = static_cast<QKeyEvent *>(e); | - |
| 1052 | | - |
| 1053 | QModelIndex curIndex = d->popup->currentIndex(); | - |
| 1054 | QModelIndexList selList = d->popup->selectionModel()->selectedIndexes(); | - |
| 1055 | | - |
| 1056 | const int key = ke->key(); | - |
| 1057 | | - |
| 1058 | if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid() | - |
| 1059 | && d->mode == QCompleter::UnfilteredPopupCompletion) { | - |
| 1060 | d->setCurrentIndex(curIndex); | - |
| 1061 | return true; | - |
| 1062 | } | - |
| 1063 | | - |
| 1064 | | - |
| 1065 | | - |
| 1066 | switch (key) { | - |
| 1067 | case Qt::Key_End: | - |
| 1068 | case Qt::Key_Home: | - |
| 1069 | if (ke->modifiers() & Qt::ControlModifier) | - |
| 1070 | return false; | - |
| 1071 | break; | - |
| 1072 | | - |
| 1073 | case Qt::Key_Up: | - |
| 1074 | if (!curIndex.isValid()) { | - |
| 1075 | int rowCount = d->proxy->rowCount(); | - |
| 1076 | QModelIndex lastIndex = d->proxy->index(rowCount - 1, d->column); | - |
| 1077 | d->setCurrentIndex(lastIndex); | - |
| 1078 | return true; | - |
| 1079 | } else if (curIndex.row() == 0) { | - |
| 1080 | if (d->wrap) | - |
| 1081 | d->setCurrentIndex(QModelIndex()); | - |
| 1082 | return true; | - |
| 1083 | } | - |
| 1084 | return false; | - |
| 1085 | | - |
| 1086 | case Qt::Key_Down: | - |
| 1087 | if (!curIndex.isValid()) { | - |
| 1088 | QModelIndex firstIndex = d->proxy->index(0, d->column); | - |
| 1089 | d->setCurrentIndex(firstIndex); | - |
| 1090 | return true; | - |
| 1091 | } else if (curIndex.row() == d->proxy->rowCount() - 1) { | - |
| 1092 | if (d->wrap) | - |
| 1093 | d->setCurrentIndex(QModelIndex()); | - |
| 1094 | return true; | - |
| 1095 | } | - |
| 1096 | return false; | - |
| 1097 | | - |
| 1098 | case Qt::Key_PageUp: | - |
| 1099 | case Qt::Key_PageDown: | - |
| 1100 | return false; | - |
| 1101 | } | - |
| 1102 | | - |
| 1103 | | - |
| 1104 | | - |
| 1105 | d->eatFocusOut = false; | - |
| 1106 | (static_cast<QObject *>(d->widget))->event(ke); | - |
| 1107 | d->eatFocusOut = true; | - |
| 1108 | if (!d->widget || e->isAccepted() || !d->popup->isVisible()) { | - |
| 1109 | | - |
| 1110 | if (d->widget && (!d->widget->hasFocus() | - |
| 1111 | | - |
| 1112 | | - |
| 1113 | | - |
| 1114 | )) | - |
| 1115 | d->popup->hide(); | - |
| 1116 | if (e->isAccepted()) | - |
| 1117 | return true; | - |
| 1118 | } | - |
| 1119 | | - |
| 1120 | | - |
| 1121 | if (ke->matches(QKeySequence::Cancel)) { | - |
| 1122 | d->popup->hide(); | - |
| 1123 | return true; | - |
| 1124 | } | - |
| 1125 | | - |
| 1126 | switch (key) { | - |
| 1127 | | - |
| 1128 | | - |
| 1129 | | - |
| 1130 | | - |
| 1131 | | - |
| 1132 | case Qt::Key_Return: | - |
| 1133 | case Qt::Key_Enter: | - |
| 1134 | case Qt::Key_Tab: | - |
| 1135 | d->popup->hide(); | - |
| 1136 | if (curIndex.isValid()) | - |
| 1137 | d->_q_complete(curIndex); | - |
| 1138 | break; | - |
| 1139 | | - |
| 1140 | case Qt::Key_F4: | - |
| 1141 | if (ke->modifiers() & Qt::AltModifier) | - |
| 1142 | d->popup->hide(); | - |
| 1143 | break; | - |
| 1144 | | - |
| 1145 | case Qt::Key_Backtab: | - |
| 1146 | d->popup->hide(); | - |
| 1147 | break; | - |
| 1148 | | - |
| 1149 | default: | - |
| 1150 | break; | - |
| 1151 | } | - |
| 1152 | | - |
| 1153 | return true; | - |
| 1154 | } | - |
| 1155 | case QEvent::MouseButtonPress: { | - |
| 1156 | if (!d->popup->underMouse()) { | - |
| 1157 | d->popup->hide(); | - |
| 1158 | return true; | - |
| 1159 | } | - |
| 1160 | } | - |
| 1161 | return false; | - |
| 1162 | | - |
| 1163 | case QEvent::InputMethod: | - |
| 1164 | case QEvent::ShortcutOverride: | - |
| 1165 | QApplication::sendEvent(d->widget, e); | - |
| 1166 | break; | - |
| 1167 | | - |
| 1168 | default: | - |
| 1169 | return false; | - |
| 1170 | } | - |
| 1171 | return false; | - |
| 1172 | } | - |
| 1173 | void QCompleter::complete(const QRect& rect) | - |
| 1174 | { | - |
| 1175 | QCompleterPrivate * const d = d_func(); | - |
| 1176 | QModelIndex idx = d->proxy->currentIndex(false); | - |
| 1177 | d->hiddenBecauseNoMatch = false; | - |
| 1178 | if (d->mode == QCompleter::InlineCompletion) { | - |
| 1179 | if (idx.isValid()) | - |
| 1180 | d->_q_complete(idx, true); | - |
| 1181 | return; | - |
| 1182 | } | - |
| 1183 | | - |
| 1184 | ((!(d->widget != 0)) ? qt_assert("d->widget != 0",__FILE__,14541460) : qt_noop()); | - |
| 1185 | if ((d->mode == QCompleter::PopupCompletion && !idx.isValid()) | - |
| 1186 | || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) { | - |
| 1187 | if (d->popup) | - |
| 1188 | d->popup->hide(); | - |
| 1189 | d->hiddenBecauseNoMatch = true; | - |
| 1190 | return; | - |
| 1191 | } | - |
| 1192 | | - |
| 1193 | popup(); | - |
| 1194 | if (d->mode == QCompleter::UnfilteredPopupCompletion) | - |
| 1195 | d->setCurrentIndex(idx, false); | - |
| 1196 | | - |
| 1197 | d->showPopup(rect); | - |
| 1198 | d->popupRect = rect; | - |
| 1199 | } | - |
| 1200 | bool QCompleter::setCurrentRow(int row) | - |
| 1201 | { | - |
| 1202 | QCompleterPrivate * const d = d_func(); | - |
| 1203 | return d->proxy->setCurrentRow(row); | - |
| 1204 | } | - |
| 1205 | | - |
| 1206 | | - |
| 1207 | | - |
| 1208 | | - |
| 1209 | | - |
| 1210 | | - |
| 1211 | int QCompleter::currentRow() const | - |
| 1212 | { | - |
| 1213 | const QCompleterPrivate * const d = d_func(); | - |
| 1214 | return d->proxy->currentRow(); | - |
| 1215 | } | - |
| 1216 | | - |
| 1217 | | - |
| 1218 | | - |
| 1219 | | - |
| 1220 | | - |
| 1221 | | - |
| 1222 | int QCompleter::completionCount() const | - |
| 1223 | { | - |
| 1224 | const QCompleterPrivate * const d = d_func(); | - |
| 1225 | return d->proxy->completionCount(); | - |
| 1226 | } | - |
| 1227 | void QCompleter::setModelSorting(QCompleter::ModelSorting sorting) | - |
| 1228 | { | - |
| 1229 | QCompleterPrivate * const d = d_func(); | - |
| 1230 | if (d->sorting == sorting) | - |
| 1231 | return; | - |
| 1232 | d->sorting = sorting; | - |
| 1233 | d->proxy->createEngine(); | - |
| 1234 | d->proxy->invalidate(); | - |
| 1235 | } | - |
| 1236 | | - |
| 1237 | QCompleter::ModelSorting QCompleter::modelSorting() const | - |
| 1238 | { | - |
| 1239 | const QCompleterPrivate * const d = d_func(); | - |
| 1240 | return d->sorting; | - |
| 1241 | } | - |
| 1242 | void QCompleter::setCompletionColumn(int column) | - |
| 1243 | { | - |
| 1244 | QCompleterPrivate * const d = d_func(); | - |
| 1245 | if (d->column == column) | - |
| 1246 | return; | - |
| 1247 | | - |
| 1248 | if (QListView *listView = qobject_cast<QListView *>(d->popup)) | - |
| 1249 | listView->setModelColumn(column); | - |
| 1250 | | - |
| 1251 | d->column = column; | - |
| 1252 | d->proxy->invalidate(); | - |
| 1253 | } | - |
| 1254 | | - |
| 1255 | int QCompleter::completionColumn() const | - |
| 1256 | { | - |
| 1257 | const QCompleterPrivate * const d = d_func(); | - |
| 1258 | return d->column; | - |
| 1259 | } | - |
| 1260 | void QCompleter::setCompletionRole(int role) | - |
| 1261 | { | - |
| 1262 | QCompleterPrivate * const d = d_func(); | - |
| 1263 | if (d->role == role) | - |
| 1264 | return; | - |
| 1265 | d->role = role; | - |
| 1266 | d->proxy->invalidate(); | - |
| 1267 | } | - |
| 1268 | | - |
| 1269 | int QCompleter::completionRole() const | - |
| 1270 | { | - |
| 1271 | const QCompleterPrivate * const d = d_func(); | - |
| 1272 | return d->role; | - |
| 1273 | } | - |
| 1274 | void QCompleter::setWrapAround(bool wrap) | - |
| 1275 | { | - |
| 1276 | QCompleterPrivate * const d = d_func(); | - |
| 1277 | if (d->wrap == wrap) | - |
| 1278 | return; | - |
| 1279 | d->wrap = wrap; | - |
| 1280 | } | - |
| 1281 | | - |
| 1282 | bool QCompleter::wrapAround() const | - |
| 1283 | { | - |
| 1284 | const QCompleterPrivate * const d = d_func(); | - |
| 1285 | return d->wrap; | - |
| 1286 | } | - |
| 1287 | int QCompleter::maxVisibleItems() const | - |
| 1288 | { | - |
| 1289 | const QCompleterPrivate * const d = d_func(); | - |
| 1290 | return d->maxVisibleItems; | - |
| 1291 | } | - |
| 1292 | | - |
| 1293 | void QCompleter::setMaxVisibleItems(int maxItems) | - |
| 1294 | { | - |
| 1295 | QCompleterPrivate * const d = d_func(); | - |
| 1296 | if (__builtin_expect(!!(| TRUE | never evaluated | | FALSE | never evaluated |
maxItems < 0)), false)| TRUE | never evaluated | | FALSE | never evaluated |
) { | 0 |
| 1297 | QMessageLogger(__FILE__, 16491655, __PRETTY_FUNCTION__).warning("QCompleter::setMaxVisibleItems: " | - |
| 1298 | "Invalid max visible items (%d) must be >= 0", maxItems); | - |
| 1299 | return; never executed: return; | 0 |
| 1300 | } | - |
| 1301 | d->maxVisibleItems = maxItems; | - |
| 1302 | } never executed: end of block | 0 |
| 1303 | void QCompleter::setCaseSensitivity(Qt::CaseSensitivity cs) | - |
| 1304 | { | - |
| 1305 | QCompleterPrivate * const d = d_func(); | - |
| 1306 | if (d->cs == cs) | - |
| 1307 | return; | - |
| 1308 | d->cs = cs; | - |
| 1309 | d->proxy->createEngine(); | - |
| 1310 | d->proxy->invalidate(); | - |
| 1311 | } | - |
| 1312 | | - |
| 1313 | Qt::CaseSensitivity QCompleter::caseSensitivity() const | - |
| 1314 | { | - |
| 1315 | const QCompleterPrivate * const d = d_func(); | - |
| 1316 | return d->cs; | - |
| 1317 | } | - |
| 1318 | void QCompleter::setCompletionPrefix(const QString &prefix) | - |
| 1319 | { | - |
| 1320 | QCompleterPrivate * const d = d_func(); | - |
| 1321 | d->prefix = prefix; | - |
| 1322 | d->proxy->filter(splitPath(prefix)); | - |
| 1323 | } | - |
| 1324 | | - |
| 1325 | QString QCompleter::completionPrefix() const | - |
| 1326 | { | - |
| 1327 | const QCompleterPrivate * const d = d_func(); | - |
| 1328 | return d->prefix; | - |
| 1329 | } | - |
| 1330 | | - |
| 1331 | | - |
| 1332 | | - |
| 1333 | | - |
| 1334 | | - |
| 1335 | | - |
| 1336 | QModelIndex QCompleter::currentIndex() const | - |
| 1337 | { | - |
| 1338 | const QCompleterPrivate * const d = d_func(); | - |
| 1339 | return d->proxy->currentIndex(false); | - |
| 1340 | } | - |
| 1341 | QString QCompleter::currentCompletion() const | - |
| 1342 | { | - |
| 1343 | const QCompleterPrivate * const d = d_func(); | - |
| 1344 | return pathFromIndex(d->proxy->currentIndex(true)); | - |
| 1345 | } | - |
| 1346 | QAbstractItemModel *QCompleter::completionModel() const | - |
| 1347 | { | - |
| 1348 | const QCompleterPrivate * const d = d_func(); | - |
| 1349 | return d->proxy; | - |
| 1350 | } | - |
| 1351 | QString QCompleter::pathFromIndex(const QModelIndex& index) const | - |
| 1352 | { | - |
| 1353 | const QCompleterPrivate * const d = d_func(); | - |
| 1354 | if (!index.isValid()) | - |
| 1355 | return QString(); | - |
| 1356 | | - |
| 1357 | QAbstractItemModel *sourceModel = d->proxy->sourceModel(); | - |
| 1358 | if (!sourceModel) | - |
| 1359 | return QString(); | - |
| 1360 | bool isDirModel = false; | - |
| 1361 | bool isFsModel = false; | - |
| 1362 | | - |
| 1363 | isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; | - |
| 1364 | | - |
| 1365 | | - |
| 1366 | isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; | - |
| 1367 | | - |
| 1368 | if (!isDirModel && !isFsModel) | - |
| 1369 | return sourceModel->data(index, d->role).toString(); | - |
| 1370 | | - |
| 1371 | QModelIndex idx = index; | - |
| 1372 | QStringList list; | - |
| 1373 | do { | - |
| 1374 | QString t; | - |
| 1375 | if (isDirModel) | - |
| 1376 | t = sourceModel->data(idx, Qt::EditRole).toString(); | - |
| 1377 | | - |
| 1378 | else | - |
| 1379 | t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString(); | - |
| 1380 | | - |
| 1381 | list.prepend(t); | - |
| 1382 | QModelIndex parent = idx.parent(); | - |
| 1383 | idx = parent.sibling(parent.row(), index.column()); | - |
| 1384 | } while (idx.isValid()); | - |
| 1385 | | - |
| 1386 | | - |
| 1387 | if (list.count() == 1) | - |
| 1388 | return list[0]; | - |
| 1389 | list[0].clear() ; | - |
| 1390 | | - |
| 1391 | | - |
| 1392 | return list.join(QDir::separator()); | - |
| 1393 | } | - |
| 1394 | QStringList QCompleter::splitPath(const QString& path) const | - |
| 1395 | { | - |
| 1396 | bool isDirModel = false; | - |
| 1397 | bool isFsModel = false; | - |
| 1398 | | - |
| 1399 | const QCompleterPrivate * const d = d_func(); | - |
| 1400 | isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; | - |
| 1401 | | - |
| 1402 | | - |
| 1403 | | - |
| 1404 | | - |
| 1405 | | - |
| 1406 | isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; | - |
| 1407 | | - |
| 1408 | | - |
| 1409 | if ((!isDirModel && !isFsModel) || path.isEmpty()) | - |
| 1410 | return QStringList(completionPrefix()); | - |
| 1411 | | - |
| 1412 | QString pathCopy = QDir::toNativeSeparators(path); | - |
| 1413 | const QChar sep = QDir::separator(); | - |
| 1414 | QStringList parts = pathCopy.split(sep); | - |
| 1415 | | - |
| 1416 | | - |
| 1417 | | - |
| 1418 | | - |
| 1419 | | - |
| 1420 | if (pathCopy[0] == sep) | - |
| 1421 | parts[0] = QLatin1Char('/'); | - |
| 1422 | | - |
| 1423 | | - |
| 1424 | return parts; | - |
| 1425 | } | - |
| 1426 | | - |
| 1427 | | - |
| 1428 | | - |
| | |