Qt 4.8
Public Functions | List of all members
QSortedModelEngine Class Reference

#include <qcompleter_p.h>

Inheritance diagram for QSortedModelEngine:
QCompletionEngine

Public Functions

QMatchData filter (const QString &, const QModelIndex &, int)
 
QIndexMapper indexHint (QString, const QModelIndex &, Qt::SortOrder)
 
 QSortedModelEngine (QCompleterPrivate *c)
 
Qt::SortOrder sortOrder (const QModelIndex &) const
 
- Public Functions inherited from QCompletionEngine
void filter (const QStringList &parts)
 
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 ()
 

Additional Inherited Members

- Public Types inherited from QCompletionEngine
typedef QMap< QModelIndex, CacheItemCache
 
typedef QMap< QString, QMatchDataCacheItem
 
- Public Variables inherited from QCompletionEngine
QCompleterPrivatec
 
Cache cache
 
int cost
 
QMatchData curMatch
 
QModelIndex curParent
 
QStringList curParts
 
int curRow
 
QMatchData historyMatch
 

Detailed Description

Definition at line 173 of file qcompleter_p.h.

Constructors and Destructors

◆ QSortedModelEngine()

QSortedModelEngine::QSortedModelEngine ( QCompleterPrivate c)
inline

Definition at line 176 of file qcompleter_p.h.

176 : QCompletionEngine(c) { }
QCompletionEngine(QCompleterPrivate *c)
Definition: qcompleter_p.h:147

Functions

◆ filter()

QMatchData QSortedModelEngine::filter ( const QString part,
const QModelIndex parent,
int   
)
virtual

Implements QCompletionEngine.

Definition at line 620 of file qcompleter.cpp.

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 }
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
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
bool matchHint(QString, const QModelIndex &, QMatchData *)
Definition: qcompleter.cpp:504
bool isValid() const
Definition: qcompleter_p.h:136
QIndexMapper indexHint(QString, const QModelIndex &, Qt::SortOrder)
Definition: qcompleter.cpp:565
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...
SortOrder
Definition: qnamespace.h:189
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
int to() const
Definition: qcompleter_p.h:122
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.
static int cmp(const ushort *s1, const ushort *s2, size_t len)
The QAbstractItemModel class provides the abstract interface for item model classes.
int from() const
Definition: qcompleter_p.h:121
void saveInCache(QString, const QModelIndex &, const QMatchData &)
Definition: qcompleter.cpp:535
int compare(const QString &s) const
Definition: qstring.cpp:5037
The QModelIndex class is used to locate data in a data model.
QCompletionModel * proxy
Definition: qcompleter_p.h:82
Qt::SortOrder sortOrder(const QModelIndex &) const
Definition: qcompleter.cpp:608
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
QIndexMapper indices
Definition: qcompleter_p.h:135
bool lookupCache(QString part, const QModelIndex &parent, QMatchData *m)
Definition: qcompleter.cpp:523

◆ indexHint()

QIndexMapper QSortedModelEngine::indexHint ( QString  part,
const QModelIndex parent,
Qt::SortOrder  order 
)

Definition at line 565 of file qcompleter.cpp.

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 }
#define it(className, varName)
int first() const
Definition: qcompleter_p.h:119
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
QCompleterPrivate * c
Definition: qcompleter_p.h:164
bool isValid() const
Definition: qcompleter_p.h:136
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
QMap< QString, QMatchData > CacheItem
Definition: qcompleter_p.h:144
The QAbstractItemModel class provides the abstract interface for item model classes.
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
int last() const
Definition: qcompleter_p.h:120
QCompletionModel * proxy
Definition: qcompleter_p.h:82
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
QIndexMapper indices
Definition: qcompleter_p.h:135
friend class const_iterator
Definition: qmap.h:369

◆ sortOrder()

Qt::SortOrder QSortedModelEngine::sortOrder ( const QModelIndex parent) const

Definition at line 608 of file qcompleter.cpp.

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 }
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
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 QString toString(Register *reg, int type, bool *ok=0)
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
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.
The QAbstractItemModel class provides the abstract interface for item model classes.
int compare(const QString &s) const
Definition: qstring.cpp:5037
QCompletionModel * proxy
Definition: qcompleter_p.h:82
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87

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