src/gui/util/qcompleter_p.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QCOMPLETER_P_H
       
    43 #define QCOMPLETER_P_H
       
    44 
       
    45 
       
    46 //
       
    47 //  W A R N I N G
       
    48 //  -------------
       
    49 //
       
    50 // This file is not part of the Qt API.  It exists purely as an
       
    51 // implementation detail.  This header file may change from version to
       
    52 // version without notice, or even be removed.
       
    53 //
       
    54 // We mean it.
       
    55 //
       
    56 
       
    57 #include "private/qobject_p.h"
       
    58 
       
    59 #ifndef QT_NO_COMPLETER
       
    60 
       
    61 #include "QtGui/qtreeview.h"
       
    62 #include "QtGui/qabstractproxymodel.h"
       
    63 #include "qcompleter.h"
       
    64 #include "QtGui/qitemdelegate.h"
       
    65 #include "QtGui/qpainter.h"
       
    66 #include "private/qabstractproxymodel_p.h"
       
    67 
       
    68 QT_BEGIN_NAMESPACE
       
    69 
       
    70 class QCompletionModel;
       
    71 
       
    72 class QCompleterPrivate : public QObjectPrivate
       
    73 {
       
    74     Q_DECLARE_PUBLIC(QCompleter)
       
    75 
       
    76 public:
       
    77     QCompleterPrivate();
       
    78     ~QCompleterPrivate() { delete popup; }
       
    79     void init(QAbstractItemModel *model = 0);
       
    80 
       
    81     QPointer<QWidget> widget;
       
    82     QCompletionModel *proxy;
       
    83     QAbstractItemView *popup;
       
    84     QCompleter::CompletionMode mode;
       
    85 
       
    86     QString prefix;
       
    87     Qt::CaseSensitivity cs;
       
    88     int role;
       
    89     int column;
       
    90     int maxVisibleItems;
       
    91     QCompleter::ModelSorting sorting;
       
    92     bool wrap;
       
    93 
       
    94     bool eatFocusOut;
       
    95     QRect popupRect;
       
    96 
       
    97     void showPopup(const QRect&);
       
    98     void _q_complete(QModelIndex, bool = false);
       
    99     void _q_completionSelected(const QItemSelection&);
       
   100     void _q_autoResizePopup();
       
   101     void setCurrentIndex(QModelIndex, bool = true);
       
   102 };
       
   103 
       
   104 class QIndexMapper
       
   105 {
       
   106 public:
       
   107     QIndexMapper() : v(false), f(0), t(-1) { }
       
   108     QIndexMapper(int f, int t) : v(false), f(f), t(t) { }
       
   109     QIndexMapper(QVector<int> vec) : v(true), vector(vec), f(-1), t(-1) { }
       
   110 
       
   111     inline int count() const { return v ? vector.count() : t - f + 1; }
       
   112     inline int operator[] (int index) const { return v ? vector[index] : f + index; }
       
   113     inline int indexOf(int x) const { return v ? vector.indexOf(x) : ((t < f) ? -1 : x - f); }
       
   114     inline bool isValid() const { return !isEmpty(); }
       
   115     inline bool isEmpty() const { return v ? vector.isEmpty() : (t < f); }
       
   116     inline void append(int x) { Q_ASSERT(v); vector.append(x); }
       
   117     inline int first() const { return v ? vector.first() : f; }
       
   118     inline int last() const { return v ? vector.last() : t; }
       
   119     inline int from() const { Q_ASSERT(!v); return f; }
       
   120     inline int to() const { Q_ASSERT(!v); return t; }
       
   121     inline int cost() const { return vector.count()+2; }
       
   122 
       
   123 private:
       
   124     bool v;
       
   125     QVector<int> vector;
       
   126     int f, t;
       
   127 };
       
   128 
       
   129 struct QMatchData {
       
   130     QMatchData() : exactMatchIndex(-1) { }
       
   131     QMatchData(const QIndexMapper& indices, int em, bool p) :
       
   132         indices(indices), exactMatchIndex(em), partial(p) { }
       
   133     QIndexMapper indices;
       
   134     inline bool isValid() const { return indices.isValid(); }
       
   135     int  exactMatchIndex;
       
   136     bool partial;
       
   137 };
       
   138 
       
   139 class QCompletionEngine
       
   140 {
       
   141 public:
       
   142     typedef QMap<QString, QMatchData> CacheItem;
       
   143     typedef QMap<QModelIndex, CacheItem> Cache;
       
   144 
       
   145     QCompletionEngine(QCompleterPrivate *c) : c(c), curRow(-1), cost(0) { }
       
   146     virtual ~QCompletionEngine() { }
       
   147 
       
   148     void filter(const QStringList &parts);
       
   149 
       
   150     QMatchData filterHistory();
       
   151     bool matchHint(QString, const QModelIndex&, QMatchData*);
       
   152 
       
   153     void saveInCache(QString, const QModelIndex&, const QMatchData&);
       
   154     bool lookupCache(QString part, const QModelIndex& parent, QMatchData *m);
       
   155 
       
   156     virtual void filterOnDemand(int) { }
       
   157     virtual QMatchData filter(const QString&, const QModelIndex&, int) = 0;
       
   158 
       
   159     int matchCount() const { return curMatch.indices.count() + historyMatch.indices.count(); }
       
   160 
       
   161     QMatchData curMatch, historyMatch;
       
   162     QCompleterPrivate *c;
       
   163     QStringList curParts;
       
   164     QModelIndex curParent;
       
   165     int curRow;
       
   166 
       
   167     Cache cache;
       
   168     int cost;
       
   169 };
       
   170 
       
   171 class QSortedModelEngine : public QCompletionEngine
       
   172 {
       
   173 public:
       
   174     QSortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
       
   175     QMatchData filter(const QString&, const QModelIndex&, int);
       
   176     QIndexMapper indexHint(QString, const QModelIndex&, Qt::SortOrder);
       
   177     Qt::SortOrder sortOrder(const QModelIndex&) const;
       
   178 };
       
   179 
       
   180 class QUnsortedModelEngine : public QCompletionEngine
       
   181 {
       
   182 public:
       
   183     QUnsortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
       
   184 
       
   185     void filterOnDemand(int);
       
   186     QMatchData filter(const QString&, const QModelIndex&, int);
       
   187 private:
       
   188     int buildIndices(const QString& str, const QModelIndex& parent, int n,
       
   189                      const QIndexMapper& iv, QMatchData* m);
       
   190 };
       
   191 
       
   192 class QCompleterItemDelegate : public QItemDelegate
       
   193 {
       
   194 public:
       
   195     QCompleterItemDelegate(QAbstractItemView *view)
       
   196         : QItemDelegate(view), view(view) { }
       
   197     void paint(QPainter *p, const QStyleOptionViewItem& opt, const QModelIndex& idx) const {
       
   198         QStyleOptionViewItem optCopy = opt;
       
   199         optCopy.showDecorationSelected = true;
       
   200         if (view->currentIndex() == idx)
       
   201             optCopy.state |= QStyle::State_HasFocus;
       
   202         QItemDelegate::paint(p, optCopy, idx);
       
   203     }
       
   204 
       
   205 private:
       
   206     QAbstractItemView *view;
       
   207 };
       
   208 
       
   209 class QCompletionModelPrivate;
       
   210 
       
   211 class QCompletionModel : public QAbstractProxyModel
       
   212 {
       
   213     Q_OBJECT
       
   214 
       
   215 public:
       
   216     QCompletionModel(QCompleterPrivate *c, QObject *parent);
       
   217 
       
   218     void createEngine();
       
   219     void setFiltered(bool);
       
   220     void filter(const QStringList& parts);
       
   221     int completionCount() const;
       
   222     int currentRow() const { return engine->curRow; }
       
   223     bool setCurrentRow(int row);
       
   224     QModelIndex currentIndex(bool) const;
       
   225     void resetModel();
       
   226 
       
   227     QModelIndex index(int row, int column, const QModelIndex & = QModelIndex()) const;
       
   228     int rowCount(const QModelIndex &index = QModelIndex()) const;
       
   229     int columnCount(const QModelIndex &index = QModelIndex()) const;
       
   230     bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
       
   231     QModelIndex parent(const QModelIndex & = QModelIndex()) const { return QModelIndex(); }
       
   232     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
       
   233 
       
   234     void setSourceModel(QAbstractItemModel *sourceModel);
       
   235     QModelIndex mapToSource(const QModelIndex& proxyIndex) const;
       
   236     QModelIndex mapFromSource(const QModelIndex& sourceIndex) const;
       
   237 
       
   238     QCompleterPrivate *c;
       
   239     QScopedPointer<QCompletionEngine> engine;
       
   240     bool showAll;
       
   241 
       
   242     Q_DECLARE_PRIVATE(QCompletionModel)
       
   243 
       
   244 signals:
       
   245     void rowsAdded();
       
   246 
       
   247 public Q_SLOTS:
       
   248     void invalidate();
       
   249     void rowsInserted();
       
   250     void modelDestroyed();
       
   251 };
       
   252 
       
   253 class QCompletionModelPrivate : public QAbstractProxyModelPrivate
       
   254 {
       
   255     Q_DECLARE_PUBLIC(QCompletionModel)
       
   256 };
       
   257 
       
   258 QT_END_NAMESPACE
       
   259 
       
   260 #endif // QT_NO_COMPLETER
       
   261 
       
   262 #endif // QCOMPLETER_P_H