Qt 4.8
Public Types | Public Functions | Public Variables | List of all members
QCompletionEngine Class Referenceabstract

#include <qcompleter_p.h>

Inheritance diagram for QCompletionEngine:
QSortedModelEngine QUnsortedModelEngine

Public Types

typedef QMap< QModelIndex, CacheItemCache
 
typedef QMap< QString, QMatchDataCacheItem
 

Public Functions

void filter (const QStringList &parts)
 
virtual QMatchData filter (const QString &, const QModelIndex &, int)=0
 
QMatchData filterHistory ()
 
virtual void filterOnDemand (int)
 
bool lookupCache (QString part, const QModelIndex &parent, QMatchData *m)
 
int matchCount () const
 
bool matchHint (QString, const QModelIndex &, QMatchData *)
 
 QCompletionEngine (QCompleterPrivate *c)
 
void saveInCache (QString, const QModelIndex &, const QMatchData &)
 
virtual ~QCompletionEngine ()
 

Public Variables

QCompleterPrivatec
 
Cache cache
 
int cost
 
QMatchData curMatch
 
QModelIndex curParent
 
QStringList curParts
 
int curRow
 
QMatchData historyMatch
 

Detailed Description

Definition at line 141 of file qcompleter_p.h.

Typedefs

◆ Cache

Definition at line 145 of file qcompleter_p.h.

◆ CacheItem

Definition at line 144 of file qcompleter_p.h.

Constructors and Destructors

◆ QCompletionEngine()

QCompletionEngine::QCompletionEngine ( QCompleterPrivate c)
inline

Definition at line 147 of file qcompleter_p.h.

147 : c(c), curRow(-1), cost(0) { }
QCompleterPrivate * c
Definition: qcompleter_p.h:164

◆ ~QCompletionEngine()

virtual QCompletionEngine::~QCompletionEngine ( )
inlinevirtual

Definition at line 148 of file qcompleter_p.h.

148 { }

Functions

◆ filter() [1/2]

void QCompletionEngine::filter ( const QStringList parts)

Definition at line 438 of file qcompleter.cpp.

Referenced by QCompletionModel::filter().

439 {
440  const QAbstractItemModel *model = c->proxy->sourceModel();
441  curParts = parts;
442  if (curParts.isEmpty())
444 
445  curRow = -1;
447  curMatch = QMatchData();
449 
450  if (!model)
451  return;
452 
453  QModelIndex parent;
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 }
QModelIndex curParent
Definition: qcompleter_p.h:166
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
QStringList curParts
Definition: qcompleter_p.h:165
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
QMatchData filterHistory()
Definition: qcompleter.cpp:472
void filter(const QStringList &parts)
Definition: qcompleter.cpp:438
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
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...
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
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
QMatchData historyMatch
Definition: qcompleter_p.h:163
The QAbstractItemModel class provides the abstract interface for item model classes.
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
The QModelIndex class is used to locate data in a data model.
QCompletionModel * proxy
Definition: qcompleter_p.h:82

◆ filter() [2/2]

virtual QMatchData QCompletionEngine::filter ( const QString ,
const QModelIndex ,
int   
)
pure virtual

◆ filterHistory()

QMatchData QCompletionEngine::filterHistory ( )

Definition at line 472 of file qcompleter.cpp.

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 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
The QFileSystemModel class provides a data model for the local filesystem.
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
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
QStringList curParts
Definition: qcompleter_p.h:165
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
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...
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
The QAbstractItemModel class provides the abstract interface for item model classes.
QString & append(QChar c)
Definition: qstring.cpp:1777
QCompletionModel * proxy
Definition: qcompleter_p.h:82
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
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
#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

◆ filterOnDemand()

virtual void QCompletionEngine::filterOnDemand ( int  )
inlinevirtual

◆ lookupCache()

bool QCompletionEngine::lookupCache ( QString  part,
const QModelIndex parent,
QMatchData m 
)

Definition at line 523 of file qcompleter.cpp.

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 }
QCompleterPrivate * c
Definition: qcompleter_p.h:164
QMap< QString, QMatchData > CacheItem
Definition: qcompleter_p.h:144
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87

◆ matchCount()

int QCompletionEngine::matchCount ( ) const
inline

◆ matchHint()

bool QCompletionEngine::matchHint ( QString  part,
const QModelIndex parent,
QMatchData hint 
)

Definition at line 504 of file qcompleter.cpp.

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 }
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QMap< QString, QMatchData > CacheItem
Definition: qcompleter_p.h:144
int key
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87

◆ saveInCache()

void QCompletionEngine::saveInCache ( QString  part,
const QModelIndex parent,
const QMatchData m 
)

Definition at line 535 of file qcompleter.cpp.

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;
544  QMap<QString, QMatchData>::iterator it2 = ci.begin();
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 }
int count(const Key &key) const
Returns the number of items associated with key key.
Definition: qmap.h:539
QCompleterPrivate * c
Definition: qcompleter_p.h:164
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qmap.h:251
QMap< QString, QMatchData > CacheItem
Definition: qcompleter_p.h:144
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
Definition: qmap.h:233
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
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
T take(const Key &key)
Removes the item with the key key from the map and returns the value associated with it...
Definition: qmap.h:692
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
QIndexMapper indices
Definition: qcompleter_p.h:135
int cost() const
Definition: qcompleter_p.h:123
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

Properties

◆ c

QCompleterPrivate* QCompletionEngine::c

Definition at line 164 of file qcompleter_p.h.

◆ cache

Cache QCompletionEngine::cache

Definition at line 169 of file qcompleter_p.h.

Referenced by QCompletionModel::invalidate().

◆ cost

int QCompletionEngine::cost

Definition at line 170 of file qcompleter_p.h.

◆ curMatch

QMatchData QCompletionEngine::curMatch

◆ curParent

QModelIndex QCompletionEngine::curParent

◆ curParts

QStringList QCompletionEngine::curParts

Definition at line 165 of file qcompleter_p.h.

Referenced by QCompletionModel::invalidate(), and QCompletionModel::rowCount().

◆ curRow

int QCompletionEngine::curRow

◆ historyMatch

QMatchData QCompletionEngine::historyMatch

The documentation for this class was generated from the following files: