Qt 4.8
qcompleter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
145 #include "qcompleter_p.h"
146 
147 #ifndef QT_NO_COMPLETER
148 
149 #include "QtGui/qscrollbar.h"
150 #include "QtGui/qstringlistmodel.h"
151 #include "QtGui/qdirmodel.h"
152 #include "QtGui/qfilesystemmodel.h"
153 #include "QtGui/qheaderview.h"
154 #include "QtGui/qlistview.h"
155 #include "QtGui/qapplication.h"
156 #include "QtGui/qevent.h"
157 #include "QtGui/qheaderview.h"
158 #include "QtGui/qdesktopwidget.h"
159 #include "QtGui/qlineedit.h"
160 
162 
165  c(c), showAll(false)
166 {
167  createEngine();
168 }
169 
171 {
172  Q_D(const QCompletionModel);
173  return d->model->columnCount();
174 }
175 
177 {
178  bool hadModel = (sourceModel() != 0);
179 
180  if (hadModel)
181  QObject::disconnect(sourceModel(), 0, this, 0);
182 
184 
185  if (source) {
186  // TODO: Optimize updates in the source model
187  connect(source, SIGNAL(modelReset()), this, SLOT(invalidate()));
188  connect(source, SIGNAL(destroyed()), this, SLOT(modelDestroyed()));
189  connect(source, SIGNAL(layoutChanged()), this, SLOT(invalidate()));
190  connect(source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted()));
191  connect(source, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(invalidate()));
192  connect(source, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(invalidate()));
193  connect(source, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(invalidate()));
195  }
196 
197  invalidate();
198 }
199 
201 {
202  bool sortedEngine = false;
203  switch (c->sorting) {
205  sortedEngine = false;
206  break;
208  sortedEngine = c->cs == Qt::CaseSensitive;
209  break;
211  sortedEngine = c->cs == Qt::CaseInsensitive;
212  break;
213  }
214 
215  if (sortedEngine)
217  else
219 }
220 
222 {
223  Q_D(const QCompletionModel);
224  if (!index.isValid())
225  return engine->curParent;
226 
227  int row;
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 
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(); // source parent and our parent don't match
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 
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()) // invalid row
293  return false;
294 
295  engine->curRow = row;
296  return true;
297 }
298 
300 {
301  if (!engine->matchCount())
302  return QModelIndex();
303 
304  int row = engine->curRow;
305  if (showAll)
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 
339 {
340  if (!engine->matchCount())
341  return 0;
342 
344  return engine->matchCount();
345 }
346 
348 {
349  Q_D(const QCompletionModel);
350  if (parent.isValid())
351  return 0;
352 
353  if (showAll) {
354  // Show all items below current parent, even if we have no valid matches
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 
365 {
366  if (showAll == !filtered)
367  return;
368  showAll = !filtered;
369  resetModel();
370 }
371 
373 {
374  Q_D(const QCompletionModel);
375  if (parent.isValid())
376  return false;
377 
378  if (showAll)
379  return d->model->hasChildren(mapToSource(parent));
380 
381  if (!engine->matchCount())
382  return false;
383 
384  return true;
385 }
386 
388 {
389  Q_D(const QCompletionModel);
390  return d->model->data(mapToSource(index), role);
391 }
392 
394 {
395  QAbstractProxyModel::setSourceModel(0); // switch to static empty model
396  invalidate();
397 }
398 
400 {
401  invalidate();
402  emit rowsAdded();
403 }
404 
406 {
407  engine->cache.clear();
409 }
410 
412 {
414  engine->filter(parts);
415  resetModel();
416 
417  if (d->model->canFetchMore(engine->curParent))
418  d->model->fetchMore(engine->curParent);
419 }
420 
422 {
423  if (rowCount() == 0) {
424  reset();
425  return;
426  }
427 
430  QModelIndexList empty;
431  for (int i = 0; i < piList.size(); i++)
432  empty.append(QModelIndex());
433  changePersistentIndexList(piList, empty);
435 }
436 
439 {
440  const QAbstractItemModel *model = c->proxy->sourceModel();
441  curParts = parts;
442  if (curParts.isEmpty())
443  curParts.append(QString());
444 
445  curRow = -1;
446  curParent = QModelIndex();
447  curMatch = QMatchData();
448  historyMatch = filterHistory();
449 
450  if (!model)
451  return;
452 
454  for (int i = 0; i < curParts.count() - 1; i++) {
455  QString part = curParts[i];
456  int emi = filter(part, parent, -1).exactMatchIndex;
457  if (emi == -1)
458  return;
459  parent = model->index(emi, c->column, parent);
460  }
461 
462  // Note that we set the curParent to a valid parent, even if we have no matches
463  // When filtering is disabled, we show all the items under this parent
464  curParent = parent;
465  if (curParts.last().isEmpty())
466  curMatch = QMatchData(QIndexMapper(0, model->rowCount(curParent) - 1), -1, false);
467  else
468  curMatch = filter(curParts.last(), curParent, 1); // build at least one
469  curRow = curMatch.isValid() ? 0 : -1;
470 }
471 
473 {
474  QAbstractItemModel *source = c->proxy->sourceModel();
475  if (curParts.count() <= 1 || c->proxy->showAll || !source)
476  return QMatchData();
477  bool isDirModel = false;
478  bool isFsModel = false;
479  Q_UNUSED(isDirModel)
480  Q_UNUSED(isFsModel)
481 #ifndef QT_NO_DIRMODEL
482  isDirModel = (qobject_cast<QDirModel *>(source) != 0);
483 #endif
484 #ifndef QT_NO_FILESYSTEMMODEL
485  isFsModel = (qobject_cast<QFileSystemModel *>(source) != 0);
486 #endif
487  QVector<int> v;
488  QIndexMapper im(v);
489  QMatchData m(im, -1, true);
490 
491  for (int i = 0; i < source->rowCount(); i++) {
492  QString str = source->index(i, c->column).data().toString();
493  if (str.startsWith(c->prefix, c->cs)
494 #if (!defined(Q_OS_WIN) || defined(Q_OS_WINCE)) && !defined(Q_OS_SYMBIAN)
495  && ((!isFsModel && !isDirModel) || QDir::toNativeSeparators(str) != QDir::separator())
496 #endif
497  )
498  m.indices.append(i);
499  }
500  return m;
501 }
502 
503 // Returns a match hint from the cache by chopping the search string
505 {
506  if (c->cs == Qt::CaseInsensitive)
507  part = part.toLower();
508 
509  const CacheItem& map = cache[parent];
510 
511  QString key = part;
512  while (!key.isEmpty()) {
513  key.chop(1);
514  if (map.contains(key)) {
515  *hint = map[key];
516  return true;
517  }
518  }
519 
520  return false;
521 }
522 
524 {
525  if (c->cs == Qt::CaseInsensitive)
526  part = part.toLower();
527  const CacheItem& map = cache[parent];
528  if (!map.contains(part))
529  return false;
530  *m = map[part];
531  return true;
532 }
533 
534 // When the cache size exceeds 1MB, it clears out about 1/2 of the cache.
536 {
537  QMatchData old = cache[parent].take(part);
538  cost = cost + m.indices.cost() - old.indices.cost();
539  if (cost * sizeof(int) > 1024 * 1024) {
541  while (it1 != cache.end()) {
542  CacheItem& ci = it1.value();
543  int sz = ci.count()/2;
545  int i = 0;
546  while (it2 != ci.end() && i < sz) {
547  cost -= it2.value().indices.cost();
548  it2 = ci.erase(it2);
549  i++;
550  }
551  if (ci.count() == 0) {
552  it1 = cache.erase(it1);
553  } else {
554  ++it1;
555  }
556  }
557  }
558 
559  if (c->cs == Qt::CaseInsensitive)
560  part = part.toLower();
561  cache[parent][part] = m;
562 }
563 
566 {
567  const QAbstractItemModel *model = c->proxy->sourceModel();
568 
569  if (c->cs == Qt::CaseInsensitive)
570  part = part.toLower();
571 
572  const CacheItem& map = cache[parent];
573 
574  // Try to find a lower and upper bound for the search from previous results
575  int to = model->rowCount(parent) - 1;
576  int from = 0;
577  const CacheItem::const_iterator it = map.lowerBound(part);
578 
579  // look backward for first valid hint
580  for(CacheItem::const_iterator it1 = it; it1-- != map.constBegin();) {
581  const QMatchData& value = it1.value();
582  if (value.isValid()) {
583  if (order == Qt::AscendingOrder) {
584  from = value.indices.last() + 1;
585  } else {
586  to = value.indices.first() - 1;
587  }
588  break;
589  }
590  }
591 
592  // look forward for first valid hint
593  for(CacheItem::const_iterator it2 = it; it2 != map.constEnd(); ++it2) {
594  const QMatchData& value = it2.value();
595  if (value.isValid() && !it2.key().startsWith(part)) {
596  if (order == Qt::AscendingOrder) {
597  to = value.indices.first() - 1;
598  } else {
599  from = value.indices.first() + 1;
600  }
601  break;
602  }
603  }
604 
605  return QIndexMapper(from, to);
606 }
607 
609 {
610  const QAbstractItemModel *model = c->proxy->sourceModel();
611 
612  int rowCount = model->rowCount(parent);
613  if (rowCount < 2)
614  return Qt::AscendingOrder;
615  QString first = model->data(model->index(0, c->column, parent), c->role).toString();
616  QString last = model->data(model->index(rowCount - 1, c->column, parent), c->role).toString();
617  return QString::compare(first, last, c->cs) <= 0 ? Qt::AscendingOrder : Qt::DescendingOrder;
618 }
619 
621 {
622  const QAbstractItemModel *model = c->proxy->sourceModel();
623 
624  QMatchData hint;
625  if (lookupCache(part, parent, &hint))
626  return hint;
627 
628  QIndexMapper indices;
629  Qt::SortOrder order = sortOrder(parent);
630 
631  if (matchHint(part, parent, &hint)) {
632  if (!hint.isValid())
633  return QMatchData();
634  indices = hint.indices;
635  } else {
636  indices = indexHint(part, parent, order);
637  }
638 
639  // binary search the model within 'indices' for 'part' under 'parent'
640  int high = indices.to() + 1;
641  int low = indices.from() - 1;
642  int probe;
643  QModelIndex probeIndex;
644  QString probeData;
645 
646  while (high - low > 1)
647  {
648  probe = (high + low) / 2;
649  probeIndex = model->index(probe, c->column, parent);
650  probeData = model->data(probeIndex, c->role).toString();
651  const int cmp = QString::compare(probeData, part, c->cs);
652  if ((order == Qt::AscendingOrder && cmp >= 0)
653  || (order == Qt::DescendingOrder && cmp < 0)) {
654  high = probe;
655  } else {
656  low = probe;
657  }
658  }
659 
660  if ((order == Qt::AscendingOrder && low == indices.to())
661  || (order == Qt::DescendingOrder && high == indices.from())) { // not found
662  saveInCache(part, parent, QMatchData());
663  return QMatchData();
664  }
665 
666  probeIndex = model->index(order == Qt::AscendingOrder ? low+1 : high-1, c->column, parent);
667  probeData = model->data(probeIndex, c->role).toString();
668  if (!probeData.startsWith(part, c->cs)) {
669  saveInCache(part, parent, QMatchData());
670  return QMatchData();
671  }
672 
673  const bool exactMatch = QString::compare(probeData, part, c->cs) == 0;
674  int emi = exactMatch ? (order == Qt::AscendingOrder ? low+1 : high-1) : -1;
675 
676  int from = 0;
677  int to = 0;
678  if (order == Qt::AscendingOrder) {
679  from = low + 1;
680  high = indices.to() + 1;
681  low = from;
682  } else {
683  to = high - 1;
684  low = indices.from() - 1;
685  high = to;
686  }
687 
688  while (high - low > 1)
689  {
690  probe = (high + low) / 2;
691  probeIndex = model->index(probe, c->column, parent);
692  probeData = model->data(probeIndex, c->role).toString();
693  const bool startsWith = probeData.startsWith(part, c->cs);
694  if ((order == Qt::AscendingOrder && startsWith)
695  || (order == Qt::DescendingOrder && !startsWith)) {
696  low = probe;
697  } else {
698  high = probe;
699  }
700  }
701 
702  QMatchData m(order == Qt::AscendingOrder ? QIndexMapper(from, high - 1) : QIndexMapper(low+1, to), emi, false);
703  saveInCache(part, parent, m);
704  return m;
705 }
706 
709  const QIndexMapper& indices, QMatchData* m)
710 {
711  Q_ASSERT(m->partial);
712  Q_ASSERT(n != -1 || m->exactMatchIndex == -1);
713  const QAbstractItemModel *model = c->proxy->sourceModel();
714  int i, count = 0;
715 
716  for (i = 0; i < indices.count() && count != n; ++i) {
717  QModelIndex idx = model->index(indices[i], c->column, parent);
718  QString data = model->data(idx, c->role).toString();
719  if (!data.startsWith(str, c->cs) || !(model->flags(idx) & Qt::ItemIsSelectable))
720  continue;
721  m->indices.append(indices[i]);
722  ++count;
723  if (m->exactMatchIndex == -1 && QString::compare(data, str, c->cs) == 0) {
724  m->exactMatchIndex = indices[i];
725  if (n == -1)
726  return indices[i];
727  }
728  }
729  return indices[i-1];
730 }
731 
733 {
734  Q_ASSERT(matchCount());
735  if (!curMatch.partial)
736  return;
737  Q_ASSERT(n >= -1);
738  const QAbstractItemModel *model = c->proxy->sourceModel();
739  int lastRow = model->rowCount(curParent) - 1;
740  QIndexMapper im(curMatch.indices.last() + 1, lastRow);
741  int lastIndex = buildIndices(curParts.last(), curParent, n, im, &curMatch);
742  curMatch.partial = (lastRow != lastIndex);
743  saveInCache(curParts.last(), curParent, curMatch);
744 }
745 
747 {
748  QMatchData hint;
749 
750  QVector<int> v;
751  QIndexMapper im(v);
752  QMatchData m(im, -1, true);
753 
754  const QAbstractItemModel *model = c->proxy->sourceModel();
755  bool foundInCache = lookupCache(part, parent, &m);
756 
757  if (!foundInCache) {
758  if (matchHint(part, parent, &hint) && !hint.isValid())
759  return QMatchData();
760  }
761 
762  if (!foundInCache && !hint.isValid()) {
763  const int lastRow = model->rowCount(parent) - 1;
764  QIndexMapper all(0, lastRow);
765  int lastIndex = buildIndices(part, parent, n, all, &m);
766  m.partial = (lastIndex != lastRow);
767  } else {
768  if (!foundInCache) { // build from hint as much as we can
769  buildIndices(part, parent, INT_MAX, hint.indices, &m);
770  m.partial = hint.partial;
771  }
772  if (m.partial && ((n == -1 && m.exactMatchIndex == -1) || (m.indices.count() < n))) {
773  // need more and have more
774  const int lastRow = model->rowCount(parent) - 1;
775  QIndexMapper rest(hint.indices.last() + 1, lastRow);
776  int want = n == -1 ? -1 : n - m.indices.count();
777  int lastIndex = buildIndices(part, parent, want, rest, &m);
778  m.partial = (lastRow != lastIndex);
779  }
780  }
781 
782  saveInCache(part, parent, m);
783  return m;
784 }
785 
788 : widget(0), proxy(0), popup(0), cs(Qt::CaseSensitive), role(Qt::EditRole), column(0),
789  maxVisibleItems(7), sorting(QCompleter::UnsortedModel), wrap(true), eatFocusOut(true),
790  hiddenBecauseNoMatch(false)
791 {
792 }
793 
795 {
796  Q_Q(QCompleter);
797  proxy = new QCompletionModel(this, q);
798  QObject::connect(proxy, SIGNAL(rowsAdded()), q, SLOT(_q_autoResizePopup()));
799  q->setModel(m);
800 #ifdef QT_NO_LISTVIEW
801  q->setCompletionMode(QCompleter::InlineCompletion);
802 #else
803  q->setCompletionMode(QCompleter::PopupCompletion);
804 #endif // QT_NO_LISTVIEW
805 }
806 
808 {
809  Q_Q(QCompleter);
810  if (!q->popup())
811  return;
812  if (!select) {
814  } else {
815  if (!index.isValid())
816  popup->selectionModel()->clear();
817  else
820  }
821  index = popup->selectionModel()->currentIndex();
822  if (!index.isValid())
823  popup->scrollToTop();
824  else
826 }
827 
829 {
831  if (!selection.indexes().isEmpty())
832  index = selection.indexes().first();
833 
834  _q_complete(index, true);
835 }
836 
838 {
839  Q_Q(QCompleter);
840  QString completion;
841 
842  if (!index.isValid() || (!proxy->showAll && (index.row() >= proxy->engine->matchCount()))) {
843  completion = prefix;
844  } else {
845  if (!(index.flags() & Qt::ItemIsEnabled))
846  return;
847  QModelIndex si = proxy->mapToSource(index);
848  si = si.sibling(si.row(), column); // for clicked()
849  completion = q->pathFromIndex(si);
850 #ifndef QT_NO_DIRMODEL
851  // add a trailing separator in inline
853  if (qobject_cast<QDirModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir())
854  completion += QDir::separator();
855  }
856 #endif
857 #ifndef QT_NO_FILESYSTEMMODEL
858  // add a trailing separator in inline
860  if (qobject_cast<QFileSystemModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir())
861  completion += QDir::separator();
862  }
863 #endif
864  }
865 
866  if (highlighted) {
867  emit q->highlighted(index);
868  emit q->highlighted(completion);
869  } else {
870  emit q->activated(index);
871  emit q->activated(completion);
872  }
873 }
874 
876 {
877  if (!popup || !popup->isVisible())
878  return;
880 }
881 
883 {
886  QPoint pos;
887  int rh, w;
888  int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3;
890  if (hsb && hsb->isVisible())
892 
893  if (rect.isValid()) {
894  rh = rect.height();
895  w = rect.width();
896  pos = widget->mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft());
897  } else {
898  rh = widget->height();
899  pos = widget->mapToGlobal(QPoint(0, widget->height() - 2));
900  w = widget->width();
901  }
902 
903  if (w > screen.width())
904  w = screen.width();
905  if ((pos.x() + w) > (screen.x() + screen.width()))
906  pos.setX(screen.x() + screen.width() - w);
907  if (pos.x() < screen.x())
908  pos.setX(screen.x());
909 
910  int top = pos.y() - rh - screen.top() + 2;
911  int bottom = screen.bottom() - pos.y();
912  h = qMax(h, popup->minimumHeight());
913  if (h > bottom) {
914  h = qMin(qMax(top, bottom), h);
915 
916  if (top > bottom)
917  pos.setY(pos.y() - h - rh + 2);
918  }
919 
920  popup->setGeometry(pos.x(), pos.y(), w, h);
921 
922  if (!popup->isVisible())
923  popup->show();
924 }
925 
927 {
928  Q_Q(QCompleter);
929  // Slot called when QFileSystemModel has finished loading.
930  // If we hide the popup because there was no match because the model was not loaded yet,
931  // we re-start the completion when we get the results
933  && prefix.startsWith(path) && prefix != (path + QLatin1Char('/'))
934  && widget) {
935  q->complete();
936  }
937 }
938 
943 : QObject(*new QCompleterPrivate(), parent)
944 {
945  Q_D(QCompleter);
946  d->init();
947 }
948 
954  : QObject(*new QCompleterPrivate(), parent)
955 {
956  Q_D(QCompleter);
957  d->init(model);
958 }
959 
960 #ifndef QT_NO_STRINGLISTMODEL
961 
966 : QObject(*new QCompleterPrivate(), parent)
967 {
968  Q_D(QCompleter);
969  d->init(new QStringListModel(list, this));
970 }
971 #endif // QT_NO_STRINGLISTMODEL
972 
977 {
978 }
979 
990 {
991  Q_D(QCompleter);
992  if (d->widget)
993  d->widget->removeEventFilter(this);
994  d->widget = widget;
995  if (d->widget)
996  d->widget->installEventFilter(this);
997  if (d->popup) {
998  d->popup->hide();
999  d->popup->setFocusProxy(d->widget);
1000  }
1001 }
1002 
1009 {
1010  Q_D(const QCompleter);
1011  return d->widget;
1012 }
1013 
1026 {
1027  Q_D(QCompleter);
1028  QAbstractItemModel *oldModel = d->proxy->sourceModel();
1029  d->proxy->setSourceModel(model);
1030  if (d->popup)
1031  setPopup(d->popup); // set the model and make new connections
1032  if (oldModel && oldModel->QObject::parent() == this)
1033  delete oldModel;
1034 #ifndef QT_NO_DIRMODEL
1035  if (qobject_cast<QDirModel *>(model)) {
1036 #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN)
1038 #else
1040 #endif
1041  }
1042 #endif // QT_NO_DIRMODEL
1043 #ifndef QT_NO_FILESYSTEMMODEL
1045  if (fsModel) {
1046 #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN)
1048 #else
1050 #endif
1052  connect(fsModel, SIGNAL(directoryLoaded(QString)), this, SLOT(_q_fileSystemModelDirectoryLoaded(QString)));
1053  }
1054 #endif // QT_NO_FILESYSTEMMODEL
1055 }
1056 
1063 {
1064  Q_D(const QCompleter);
1065  return d->proxy->sourceModel();
1066 }
1067 
1093 {
1094  Q_D(QCompleter);
1095  d->mode = mode;
1096  d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion);
1097 
1098  if (mode == QCompleter::InlineCompletion) {
1099  if (d->widget)
1100  d->widget->removeEventFilter(this);
1101  if (d->popup) {
1102  d->popup->deleteLater();
1103  d->popup = 0;
1104  }
1105  } else {
1106  if (d->widget)
1107  d->widget->installEventFilter(this);
1108  }
1109 }
1110 
1112 {
1113  Q_D(const QCompleter);
1114  return d->mode;
1115 }
1116 
1133 {
1134  Q_D(QCompleter);
1135  Q_ASSERT(popup != 0);
1136  if (d->popup) {
1137  QObject::disconnect(d->popup->selectionModel(), 0, this, 0);
1138  QObject::disconnect(d->popup, 0, this, 0);
1139  }
1140  if (d->popup != popup)
1141  delete d->popup;
1142  if (popup->model() != d->proxy)
1143  popup->setModel(d->proxy);
1144 #if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA)
1145  popup->show();
1146 #else
1147  popup->hide();
1148 #endif
1149 
1150  Qt::FocusPolicy origPolicy = Qt::NoFocus;
1151  if (d->widget)
1152  origPolicy = d->widget->focusPolicy();
1153  popup->setParent(0, Qt::Popup);
1154  popup->setFocusPolicy(Qt::NoFocus);
1155  if (d->widget)
1156  d->widget->setFocusPolicy(origPolicy);
1157 
1158  popup->setFocusProxy(d->widget);
1159  popup->installEventFilter(this);
1160  popup->setItemDelegate(new QCompleterItemDelegate(popup));
1161 #ifndef QT_NO_LISTVIEW
1162  if (QListView *listView = qobject_cast<QListView *>(popup)) {
1163  listView->setModelColumn(d->column);
1164  }
1165 #endif
1166 
1167  QObject::connect(popup, SIGNAL(clicked(QModelIndex)),
1168  this, SLOT(_q_complete(QModelIndex)));
1170  popup, SLOT(hide()));
1171 
1173  this, SLOT(_q_completionSelected(QItemSelection)));
1174  d->popup = popup;
1175 }
1176 
1183 {
1184  Q_D(const QCompleter);
1185 #ifndef QT_NO_LISTVIEW
1186  if (!d->popup && completionMode() != QCompleter::InlineCompletion) {
1187  QListView *listView = new QListView;
1192  listView->setModelColumn(d->column);
1193  QCompleter *that = const_cast<QCompleter*>(this);
1194  that->setPopup(listView);
1195  }
1196 #endif // QT_NO_LISTVIEW
1197  return d->popup;
1198 }
1199 
1204 {
1205  return QObject::event(ev);
1206 }
1207 
1212 {
1213  Q_D(QCompleter);
1214 
1215  if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) {
1216  d->hiddenBecauseNoMatch = false;
1217  if (d->popup && d->popup->isVisible())
1218  return true;
1219  }
1220 
1221  if (o != d->popup)
1222  return QObject::eventFilter(o, e);
1223 
1224  switch (e->type()) {
1225  case QEvent::KeyPress: {
1226  QKeyEvent *ke = static_cast<QKeyEvent *>(e);
1227 
1228  QModelIndex curIndex = d->popup->currentIndex();
1229  QModelIndexList selList = d->popup->selectionModel()->selectedIndexes();
1230 
1231  const int key = ke->key();
1232  // In UnFilteredPopup mode, select the current item
1233  if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid()
1235  d->setCurrentIndex(curIndex);
1236  return true;
1237  }
1238 
1239  // Handle popup navigation keys. These are hardcoded because up/down might make the
1240  // widget do something else (lineedit cursor moves to home/end on mac, for instance)
1241  switch (key) {
1242  case Qt::Key_End:
1243  case Qt::Key_Home:
1244  if (ke->modifiers() & Qt::ControlModifier)
1245  return false;
1246  break;
1247 
1248  case Qt::Key_Up:
1249  if (!curIndex.isValid()) {
1250  int rowCount = d->proxy->rowCount();
1251  QModelIndex lastIndex = d->proxy->index(rowCount - 1, d->column);
1252  d->setCurrentIndex(lastIndex);
1253  return true;
1254  } else if (curIndex.row() == 0) {
1255  if (d->wrap)
1256  d->setCurrentIndex(QModelIndex());
1257  return true;
1258  }
1259  return false;
1260 
1261  case Qt::Key_Down:
1262  if (!curIndex.isValid()) {
1263  QModelIndex firstIndex = d->proxy->index(0, d->column);
1264  d->setCurrentIndex(firstIndex);
1265  return true;
1266  } else if (curIndex.row() == d->proxy->rowCount() - 1) {
1267  if (d->wrap)
1268  d->setCurrentIndex(QModelIndex());
1269  return true;
1270  }
1271  return false;
1272 
1273  case Qt::Key_PageUp:
1274  case Qt::Key_PageDown:
1275  return false;
1276  }
1277 
1278  // Send the event to the widget. If the widget accepted the event, do nothing
1279  // If the widget did not accept the event, provide a default implementation
1280  d->eatFocusOut = false;
1281  (static_cast<QObject *>(d->widget))->event(ke);
1282  d->eatFocusOut = true;
1283  if (!d->widget || e->isAccepted() || !d->popup->isVisible()) {
1284  // widget lost focus, hide the popup
1285  if (d->widget && (!d->widget->hasFocus()
1286 #ifdef QT_KEYPAD_NAVIGATION
1287  || (QApplication::keypadNavigationEnabled() && !d->widget->hasEditFocus())
1288 #endif
1289  ))
1290  d->popup->hide();
1291  if (e->isAccepted())
1292  return true;
1293  }
1294 
1295  // default implementation for keys not handled by the widget when popup is open
1296  switch (key) {
1297 #ifdef QT_KEYPAD_NAVIGATION
1298  case Qt::Key_Select:
1299  if (!QApplication::keypadNavigationEnabled())
1300  break;
1301 #endif
1302  case Qt::Key_Return:
1303  case Qt::Key_Enter:
1304  case Qt::Key_Tab:
1305  d->popup->hide();
1306  if (curIndex.isValid())
1307  d->_q_complete(curIndex);
1308  break;
1309 
1310  case Qt::Key_F4:
1311  if (ke->modifiers() & Qt::AltModifier)
1312  d->popup->hide();
1313  break;
1314 
1315  case Qt::Key_Backtab:
1316  case Qt::Key_Escape:
1317  d->popup->hide();
1318  break;
1319 
1320  default:
1321  break;
1322  }
1323 
1324  return true;
1325  }
1326 
1327 #ifdef QT_KEYPAD_NAVIGATION
1328  case QEvent::KeyRelease: {
1329  QKeyEvent *ke = static_cast<QKeyEvent *>(e);
1330  if (QApplication::keypadNavigationEnabled() && ke->key() == Qt::Key_Back) {
1331  // Send the event to the 'widget'. This is what we did for KeyPress, so we need
1332  // to do the same for KeyRelease, in case the widget's KeyPress event set
1333  // up something (such as a timer) that is relying on also receiving the
1334  // key release. I see this as a bug in Qt, and should really set it up for all
1335  // the affected keys. However, it is difficult to tell how this will affect
1336  // existing code, and I can't test for every combination!
1337  d->eatFocusOut = false;
1338  static_cast<QObject *>(d->widget)->event(ke);
1339  d->eatFocusOut = true;
1340  }
1341  break;
1342  }
1343 #endif
1344 
1345  case QEvent::MouseButtonPress: {
1346 #ifdef QT_KEYPAD_NAVIGATION
1347  if (QApplication::keypadNavigationEnabled()) {
1348  // if we've clicked in the widget (or its descendant), let it handle the click
1349  QWidget *source = qobject_cast<QWidget *>(o);
1350  if (source) {
1351  QPoint pos = source->mapToGlobal((static_cast<QMouseEvent *>(e))->pos());
1352  QWidget *target = QApplication::widgetAt(pos);
1353  if (target && (d->widget->isAncestorOf(target) ||
1354  target == d->widget)) {
1355  d->eatFocusOut = false;
1356  static_cast<QObject *>(target)->event(e);
1357  d->eatFocusOut = true;
1358  return true;
1359  }
1360  }
1361  }
1362 #endif
1363  if (!d->popup->underMouse()) {
1364  d->popup->hide();
1365  return true;
1366  }
1367  }
1368  return false;
1369 
1370  case QEvent::InputMethod:
1372  QApplication::sendEvent(d->widget, e);
1373  break;
1374 
1375  default:
1376  return false;
1377  }
1378  return false;
1379 }
1380 
1391 void QCompleter::complete(const QRect& rect)
1392 {
1393  Q_D(QCompleter);
1394  QModelIndex idx = d->proxy->currentIndex(false);
1395  d->hiddenBecauseNoMatch = false;
1396  if (d->mode == QCompleter::InlineCompletion) {
1397  if (idx.isValid())
1398  d->_q_complete(idx, true);
1399  return;
1400  }
1401 
1402  Q_ASSERT(d->widget != 0);
1403  if ((d->mode == QCompleter::PopupCompletion && !idx.isValid())
1404  || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) {
1405  if (d->popup)
1406  d->popup->hide(); // no suggestion, hide
1407  d->hiddenBecauseNoMatch = true;
1408  return;
1409  }
1410 
1411  popup();
1413  d->setCurrentIndex(idx, false);
1414 
1415  d->showPopup(rect);
1416  d->popupRect = rect;
1417 }
1418 
1429 {
1430  Q_D(QCompleter);
1431  return d->proxy->setCurrentRow(row);
1432 }
1433 
1440 {
1441  Q_D(const QCompleter);
1442  return d->proxy->currentRow();
1443 }
1444 
1451 {
1452  Q_D(const QCompleter);
1453  return d->proxy->completionCount();
1454 }
1455 
1497 {
1498  Q_D(QCompleter);
1499  if (d->sorting == sorting)
1500  return;
1501  d->sorting = sorting;
1502  d->proxy->createEngine();
1503  d->proxy->invalidate();
1504 }
1505 
1507 {
1508  Q_D(const QCompleter);
1509  return d->sorting;
1510 }
1511 
1527 {
1528  Q_D(QCompleter);
1529  if (d->column == column)
1530  return;
1531 #ifndef QT_NO_LISTVIEW
1532  if (QListView *listView = qobject_cast<QListView *>(d->popup))
1533  listView->setModelColumn(column);
1534 #endif
1535  d->column = column;
1536  d->proxy->invalidate();
1537 }
1538 
1539 int QCompleter::completionColumn() const
1540 {
1541  Q_D(const QCompleter);
1542  return d->column;
1543 }
1544 
1557 {
1558  Q_D(QCompleter);
1559  if (d->role == role)
1560  return;
1561  d->role = role;
1562  d->proxy->invalidate();
1563 }
1564 
1565 int QCompleter::completionRole() const
1566 {
1567  Q_D(const QCompleter);
1568  return d->role;
1569 }
1570 
1582 {
1583  Q_D(QCompleter);
1584  if (d->wrap == wrap)
1585  return;
1586  d->wrap = wrap;
1587 }
1588 
1589 bool QCompleter::wrapAround() const
1590 {
1591  Q_D(const QCompleter);
1592  return d->wrap;
1593 }
1594 
1605 int QCompleter::maxVisibleItems() const
1606 {
1607  Q_D(const QCompleter);
1608  return d->maxVisibleItems;
1609 }
1610 
1612 {
1613  Q_D(QCompleter);
1614  if (maxItems < 0) {
1615  qWarning("QCompleter::setMaxVisibleItems: "
1616  "Invalid max visible items (%d) must be >= 0", maxItems);
1617  return;
1618  }
1619  d->maxVisibleItems = maxItems;
1620 }
1621 
1634 {
1635  Q_D(QCompleter);
1636  if (d->cs == cs)
1637  return;
1638  d->cs = cs;
1639  d->proxy->createEngine();
1640  d->proxy->invalidate();
1641 }
1642 
1644 {
1645  Q_D(const QCompleter);
1646  return d->cs;
1647 }
1648 
1660 {
1661  Q_D(QCompleter);
1662  d->prefix = prefix;
1663  d->proxy->filter(splitPath(prefix));
1664 }
1665 
1667 {
1668  Q_D(const QCompleter);
1669  return d->prefix;
1670 }
1671 
1678 {
1679  Q_D(const QCompleter);
1680  return d->proxy->currentIndex(false);
1681 }
1682 
1691 {
1692  Q_D(const QCompleter);
1693  return pathFromIndex(d->proxy->currentIndex(true));
1694 }
1695 
1708 {
1709  Q_D(const QCompleter);
1710  return d->proxy;
1711 }
1712 
1725 {
1726  Q_D(const QCompleter);
1727  if (!index.isValid())
1728  return QString();
1729 
1730  QAbstractItemModel *sourceModel = d->proxy->sourceModel();
1731  if (!sourceModel)
1732  return QString();
1733  bool isDirModel = false;
1734  bool isFsModel = false;
1735 #ifndef QT_NO_DIRMODEL
1736  isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
1737 #endif
1738 #ifndef QT_NO_FILESYSTEMMODEL
1739  isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0;
1740 #endif
1741  if (!isDirModel && !isFsModel)
1742  return sourceModel->data(index, d->role).toString();
1743 
1744  QModelIndex idx = index;
1745  QStringList list;
1746  do {
1747  QString t;
1748  if (isDirModel)
1749  t = sourceModel->data(idx, Qt::EditRole).toString();
1750 #ifndef QT_NO_FILESYSTEMMODEL
1751  else
1752  t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString();
1753 #endif
1754  list.prepend(t);
1755  QModelIndex parent = idx.parent();
1756  idx = parent.sibling(parent.row(), index.column());
1757  } while (idx.isValid());
1758 
1759 #if (!defined(Q_OS_WIN) || defined(Q_OS_WINCE)) && !defined(Q_OS_SYMBIAN)
1760  if (list.count() == 1) // only the separator or some other text
1761  return list[0];
1762  list[0].clear() ; // the join below will provide the separator
1763 #endif
1764 
1765  return list.join(QDir::separator());
1766 }
1767 
1781 {
1782  bool isDirModel = false;
1783  bool isFsModel = false;
1784 #ifndef QT_NO_DIRMODEL
1785  Q_D(const QCompleter);
1786  isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
1787 #endif
1788 #ifndef QT_NO_FILESYSTEMMODEL
1789 #ifdef QT_NO_DIRMODEL
1790  Q_D(const QCompleter);
1791 #endif
1792  isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0;
1793 #endif
1794 
1795  if ((!isDirModel && !isFsModel) || path.isEmpty())
1796  return QStringList(completionPrefix());
1797 
1798  QString pathCopy = QDir::toNativeSeparators(path);
1799  QString sep = QDir::separator();
1800 #if defined(Q_OS_SYMBIAN)
1801  if (pathCopy == QLatin1String("\\"))
1802  return QStringList(pathCopy);
1803 #elif defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
1804  if (pathCopy == QLatin1String("\\") || pathCopy == QLatin1String("\\\\"))
1805  return QStringList(pathCopy);
1806  QString doubleSlash(QLatin1String("\\\\"));
1807  if (pathCopy.startsWith(doubleSlash))
1808  pathCopy = pathCopy.mid(2);
1809  else
1810  doubleSlash.clear();
1811 #endif
1812 
1813  QRegExp re(QLatin1Char('[') + QRegExp::escape(sep) + QLatin1Char(']'));
1814  QStringList parts = pathCopy.split(re);
1815 
1816 #if defined(Q_OS_SYMBIAN)
1817  // Do nothing
1818 #elif defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
1819  if (!doubleSlash.isEmpty())
1820  parts[0].prepend(doubleSlash);
1821 #else
1822  if (pathCopy[0] == sep[0]) // readd the "/" at the beginning as the split removed it
1823  parts[0] = QDir::fromNativeSeparators(QString(sep[0]));
1824 #endif
1825 
1826  return parts;
1827 }
1828 
1876 
1877 #include "moc_qcompleter.cpp"
1878 
1879 #endif // QT_NO_COMPLETER
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
ModelSorting modelSorting() const
CompletionMode
This enum specifies how completions are provided to the user.
Definition: qcompleter.h:77
int minimumHeight() const
QModelIndex curParent
Definition: qcompleter_p.h:166
void setSelectionMode(QAbstractItemView::SelectionMode mode)
The QKeyEvent class describes a key event.
Definition: qevent.h:224
The QFileSystemModel class provides a data model for the local filesystem.
virtual void clear()
Clears the selection model.
unsigned char c[8]
Definition: qnumeric_p.h:62
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setMaxVisibleItems(int maxItems)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
void setPopup(QAbstractItemView *popup)
Sets the popup used to display completions to popup.
QPointer< QWidget > widget
void append(int x)
Definition: qcompleter_p.h:118
void setCompletionColumn(int column)
void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
Definition: qcompleter.cpp:176
int width
the width of the widget excluding any window frame
Definition: qwidget.h:166
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
~QCompleter()
Destroys the completer object.
Definition: qcompleter.cpp:976
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
QAbstractItemModel * completionModel() const
Returns the completion model.
void setCurrentIndex(QModelIndex, bool=true)
Definition: qcompleter.cpp:807
#define it(className, varName)
QCompleterPrivate * c
Definition: qcompleter_p.h:240
int first() const
Definition: qcompleter_p.h:119
QString currentCompletion() const
Returns the current completion string.
bool isVisible() const
Definition: qwidget.h:1005
Qt::CaseSensitivity caseSensitivity() const
QString completionPrefix() const
QCompleter::CompletionMode mode
Definition: qcompleter_p.h:84
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
QPointer< QWidget > widget
Definition: qcompleter_p.h:81
virtual void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setEditTriggers(EditTriggers triggers)
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
int count(const Key &key) const
Returns the number of items associated with key key.
Definition: qmap.h:539
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void columnsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been inserted into the model.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
QStringList curParts
Definition: qcompleter_p.h:165
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QStringListModel class provides a model that supplies strings to views.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
QMatchData filterHistory()
Definition: qcompleter.cpp:472
void filter(const QStringList &parts)
Definition: qcompleter.cpp:438
#define Q_D(Class)
Definition: qglobal.h:2482
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
bool matchHint(QString, const QModelIndex &, QMatchData *)
Definition: qcompleter.cpp:504
void _q_fileSystemModelDirectoryLoaded(const QString &path)
Definition: qcompleter.cpp:926
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
virtual QString pathFromIndex(const QModelIndex &index) const
Returns the path for the given index.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
Reimplement this function to return the model index in the source model that corresponds to the proxy...
Definition: qcompleter.cpp:221
int maxVisibleItems() const
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
bool isValid() const
Definition: qcompleter_p.h:136
#define Q_Q(Class)
Definition: qglobal.h:2483
QIndexMapper indexHint(QString, const QModelIndex &, Qt::SortOrder)
Definition: qcompleter.cpp:565
bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
Definition: qcompleter.cpp:372
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qmap.h:251
void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Sets the model item index to be the current item, and emits currentChanged().
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
virtual void setModel(QAbstractItemModel *model)
Sets the model for the view to present.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
static QWidget * widgetAt(const QPoint &p)
Returns the widget at global screen position point, or 0 if there is no Qt widget there...
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition: qevent.cpp:999
ModelSorting
This enum specifies how the items in the model are sorted.
Definition: qcompleter.h:83
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
#define SIGNAL(a)
Definition: qobjectdefs.h:227
SortOrder
Definition: qnamespace.h:189
static QString toString(Register *reg, int type, bool *ok=0)
The QScrollBar widget provides a vertical or horizontal scroll bar.
Definition: qscrollbar.h:59
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool setCurrentRow(int row)
Sets the current row to the row specified.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int columnCount(const QModelIndex &index=QModelIndex()) const
Returns the number of columns for the children of the given parent.
Definition: qcompleter.cpp:170
bool isAccepted() const
Definition: qcoreevent.h:307
QModelIndex index(int row, int column, const QModelIndex &=QModelIndex()) const
Returns the index of the item in the model specified by the given row, column and parent index...
Definition: qcompleter.cpp:314
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
QPoint bottomRight() const
Returns the position of the rectangle&#39;s bottom-right corner.
Definition: qrect.h:291
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise ret...
Definition: qfileinfo.cpp:990
virtual QStringList splitPath(const QString &path) const
Splits the given path into strings that are used to match at each level in the model().
int completionColumn() const
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
int height
the height of the widget excluding any window frame
Definition: qwidget.h:167
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
virtual void filterOnDemand(int)
Definition: qcompleter_p.h:158
QMatchData curMatch
Definition: qcompleter_p.h:163
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QItemSelectionModel * selectionModel() const
Returns the current selection model.
virtual void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible)=0
Scrolls the view if necessary to ensure that the item at index is visible.
int row() const
Returns the row this model index refers to.
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
void _q_complete(QModelIndex, bool=false)
Definition: qcompleter.cpp:837
#define emit
Definition: qobjectdefs.h:76
int indexOf(int x) const
Definition: qcompleter_p.h:115
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
Definition: qcompleter.cpp:387
LayoutDirection
Definition: qnamespace.h:1580
QAbstractItemModel * model() const
Returns the model that provides completion strings.
Q_CORE_EXPORT void qWarning(const char *,...)
void setCompletionPrefix(const QString &prefix)
QMatchData historyMatch
Definition: qcompleter_p.h:163
int currentRow() const
Returns the current row.
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
int to() const
Definition: qcompleter_p.h:122
void init(QAbstractItemModel *model=0)
Definition: qcompleter.cpp:794
QAbstractItemView * popup
Definition: qcompleter_p.h:83
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
void setModelSorting(ModelSorting sorting)
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
Reimplement this function to return the model index in the proxy model that corresponds to the source...
Definition: qcompleter.cpp:247
void clear()
Removes all items from the list.
Definition: qlist.h:764
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
QModelIndex currentIndex() const
Returns the model item index for the current item, or an invalid index if there is no current item...
static int cmp(const ushort *s1, const ushort *s2, size_t len)
bool wrapAround() const
void setCompletionMode(CompletionMode mode)
void show()
Shows the widget and its child widgets.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
void setModelColumn(int column)
Definition: qlistview.cpp:1586
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QModelIndex currentIndex(bool) const
Definition: qcompleter.cpp:299
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
void hide()
Hides the widget.
Definition: qwidget.h:501
void scrollToTop()
Scrolls the view to the top.
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
The QAbstractItemModel class provides the abstract interface for item model classes.
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting...
int exactMatchIndex
Definition: qcompleter_p.h:137
Qt::ItemFlags flags() const
Returns the flags for the item referred to by the index.
QCompletionModel(QCompleterPrivate *c, QObject *parent)
Definition: qcompleter.cpp:163
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
Qt::LayoutDirection layoutDirection
the layout direction for this widget
Definition: qwidget.h:216
static QDesktopWidget * desktop()
Returns the desktop widget (also called the root window).
int from() const
Definition: qcompleter_p.h:121
CompletionMode completionMode() const
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QString join(const QString &sep) const
Joins all the string list&#39;s strings into a single string with each element separated by the given sep...
Definition: qstringlist.h:162
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
CaseSensitivity
Definition: qnamespace.h:1451
The QAbstractItemView class provides the basic functionality for item view classes.
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
void saveInCache(QString, const QModelIndex &, const QMatchData &)
Definition: qcompleter.cpp:535
void setFocusProxy(QWidget *)
Sets the widget&#39;s focus proxy to widget w.
Definition: qwidget.cpp:6537
void setY(int y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qpoint.h:137
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
The QItemSelection class manages information about selected items in a model.
bool eventFilter(QObject *o, QEvent *e)
Reimplemented Function
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57
int compare(const QString &s) const
Definition: qstring.cpp:5037
QPoint bottomLeft() const
Returns the position of the rectangle&#39;s bottom-left corner.
Definition: qrect.h:297
QSize sizeHint() const
Reimplemented Function
Definition: qscrollbar.cpp:490
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
Definition: qmap.h:233
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
void activated(const QString &text)
This signal is sent when an item in the popup() is activated by the user (by clicking or pressing ret...
void setWidget(QWidget *widget)
Sets the widget for which completion are provided for to widget.
Definition: qcompleter.cpp:989
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
void complete(const QRect &rect=QRect())
For QCompleter::PopupCompletion and QCompletion::UnfilteredPopupCompletion modes, calling this functi...
The QModelIndex class is used to locate data in a data model.
QScrollBar * horizontalScrollBar() const
Returns the horizontal scroll bar.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
void showPopup(const QRect &)
Definition: qcompleter.cpp:882
FocusPolicy
Definition: qnamespace.h:181
void modelReset()
This signal is emitted when reset() is called, after the model&#39;s internal state (e.
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
int height() const
Returns the height.
Definition: qsize.h:129
if(void) toggleToolbarShown
int last() const
Definition: qcompleter_p.h:120
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
bool contains(const Key &key) const
Returns true if the map contains an item with key key; otherwise returns false.
Definition: qmap.h:553
int completionCount() const
Returns the number of completions for the current prefix.
bool event(QEvent *)
Reimplemented Function
void setModel(QAbstractItemModel *c)
Sets the model which provides completions to model.
QCompletionModel * proxy
Definition: qcompleter_p.h:82
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
quint16 index
QObject * parent
Definition: qobject.h:92
QMatchData filter(const QString &, const QModelIndex &, int)
Definition: qcompleter.cpp:620
void setFiltered(bool)
Definition: qcompleter.cpp:364
iterator erase(iterator it)
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to...
Definition: qmap.h:717
void filter(const QStringList &parts)
Definition: qcompleter.cpp:411
int buildIndices(const QString &str, const QModelIndex &parent, int n, const QIndexMapper &iv, QMatchData *m)
Definition: qcompleter.cpp:708
Qt::SortOrder sortOrder(const QModelIndex &) const
Definition: qcompleter.cpp:608
void _q_autoResizePopup()
Definition: qcompleter.cpp:875
QCompleter(QObject *parent=0)
Constructs a completer object with the given parent.
Definition: qcompleter.cpp:942
QFuture< T > filtered(const Sequence &sequence, FilterFunction filterFunction)
int rowCount(const QModelIndex &index=QModelIndex()) const
Returns the number of rows under the given parent.
Definition: qcompleter.cpp:347
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
void setItemDelegate(QAbstractItemDelegate *delegate)
Sets the item delegate for this view and its model to delegate.
QScopedPointer< QCompletionEngine > engine
Definition: qcompleter_p.h:241
void setCompletionRole(int role)
iterator lowerBound(const Key &key)
Returns an iterator pointing to the first item with key key in the map.
Definition: qmap.h:899
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
int completionCount() const
Definition: qcompleter.cpp:338
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848
QModelIndexList indexes() const
Returns a list of model indexes that correspond to the selected items.
const QRect availableGeometry(int screen=-1) const
void setWrapAround(bool wrap)
int matchCount() const
Definition: qcompleter_p.h:161
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
void _q_completionSelected(const QItemSelection &)
Definition: qcompleter.cpp:828
QIndexMapper indices
Definition: qcompleter_p.h:135
QCompleter::ModelSorting sorting
Definition: qcompleter_p.h:91
bool setCurrentRow(int row)
Definition: qcompleter.cpp:284
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
int completionRole() const
QAbstractItemModel * model() const
Returns the model that this view is presenting.
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
int cost() const
Definition: qcompleter_p.h:123
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void setX(int x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qpoint.h:134
void rowsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been removed from the model.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QDirModel class provides a data model for the local filesystem.
Definition: qdirmodel.h:59
void columnsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been removed from the model.
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QWidget * widget() const
Returns the widget for which the completer object is providing completions.
#define INT_MAX
virtual int sizeHintForRow(int row) const
Returns the height size hint for the specified row or -1 if there is no model.
void filterOnDemand(int)
Definition: qcompleter.cpp:732
void clear()
Removes all items from the map.
Definition: qmap.h:444
QAbstractItemView * popup() const
Returns the popup used to display completions.
int count() const
Definition: qcompleter_p.h:113
QModelIndex currentIndex() const
Returns the model index of the current completion in the completionModel().
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
void reset()
Resets the model to its original state in any attached views.
int column() const
Returns the column this model index refers to.
void setFocusPolicy(Qt::FocusPolicy policy)
Definition: qwidget.cpp:7631
static QString escape(const QString &str)
Returns the string str with every regexp special character escaped with a backslash.
Definition: qregexp.cpp:4392
QMatchData filter(const QString &, const QModelIndex &, int)
Definition: qcompleter.cpp:746
bool lookupCache(QString part, const QModelIndex &parent, QMatchData *m)
Definition: qcompleter.cpp:523