[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | **
|
---|
[561] | 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.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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;
|
---|
[561] | 90 | int maxVisibleItems;
|
---|
[2] | 91 | QCompleter::ModelSorting sorting;
|
---|
| 92 | bool wrap;
|
---|
| 93 |
|
---|
| 94 | bool eatFocusOut;
|
---|
| 95 | QRect popupRect;
|
---|
[846] | 96 | bool hiddenBecauseNoMatch;
|
---|
[2] | 97 |
|
---|
| 98 | void showPopup(const QRect&);
|
---|
| 99 | void _q_complete(QModelIndex, bool = false);
|
---|
| 100 | void _q_completionSelected(const QItemSelection&);
|
---|
| 101 | void _q_autoResizePopup();
|
---|
[846] | 102 | void _q_fileSystemModelDirectoryLoaded(const QString &path);
|
---|
[2] | 103 | void setCurrentIndex(QModelIndex, bool = true);
|
---|
| 104 | };
|
---|
| 105 |
|
---|
| 106 | class QIndexMapper
|
---|
| 107 | {
|
---|
| 108 | public:
|
---|
| 109 | QIndexMapper() : v(false), f(0), t(-1) { }
|
---|
| 110 | QIndexMapper(int f, int t) : v(false), f(f), t(t) { }
|
---|
| 111 | QIndexMapper(QVector<int> vec) : v(true), vector(vec), f(-1), t(-1) { }
|
---|
| 112 |
|
---|
| 113 | inline int count() const { return v ? vector.count() : t - f + 1; }
|
---|
| 114 | inline int operator[] (int index) const { return v ? vector[index] : f + index; }
|
---|
| 115 | inline int indexOf(int x) const { return v ? vector.indexOf(x) : ((t < f) ? -1 : x - f); }
|
---|
| 116 | inline bool isValid() const { return !isEmpty(); }
|
---|
| 117 | inline bool isEmpty() const { return v ? vector.isEmpty() : (t < f); }
|
---|
| 118 | inline void append(int x) { Q_ASSERT(v); vector.append(x); }
|
---|
| 119 | inline int first() const { return v ? vector.first() : f; }
|
---|
| 120 | inline int last() const { return v ? vector.last() : t; }
|
---|
| 121 | inline int from() const { Q_ASSERT(!v); return f; }
|
---|
| 122 | inline int to() const { Q_ASSERT(!v); return t; }
|
---|
| 123 | inline int cost() const { return vector.count()+2; }
|
---|
| 124 |
|
---|
| 125 | private:
|
---|
| 126 | bool v;
|
---|
| 127 | QVector<int> vector;
|
---|
| 128 | int f, t;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | struct QMatchData {
|
---|
| 132 | QMatchData() : exactMatchIndex(-1) { }
|
---|
| 133 | QMatchData(const QIndexMapper& indices, int em, bool p) :
|
---|
| 134 | indices(indices), exactMatchIndex(em), partial(p) { }
|
---|
| 135 | QIndexMapper indices;
|
---|
| 136 | inline bool isValid() const { return indices.isValid(); }
|
---|
| 137 | int exactMatchIndex;
|
---|
| 138 | bool partial;
|
---|
| 139 | };
|
---|
| 140 |
|
---|
| 141 | class QCompletionEngine
|
---|
| 142 | {
|
---|
| 143 | public:
|
---|
| 144 | typedef QMap<QString, QMatchData> CacheItem;
|
---|
| 145 | typedef QMap<QModelIndex, CacheItem> Cache;
|
---|
| 146 |
|
---|
| 147 | QCompletionEngine(QCompleterPrivate *c) : c(c), curRow(-1), cost(0) { }
|
---|
| 148 | virtual ~QCompletionEngine() { }
|
---|
| 149 |
|
---|
| 150 | void filter(const QStringList &parts);
|
---|
| 151 |
|
---|
| 152 | QMatchData filterHistory();
|
---|
| 153 | bool matchHint(QString, const QModelIndex&, QMatchData*);
|
---|
| 154 |
|
---|
| 155 | void saveInCache(QString, const QModelIndex&, const QMatchData&);
|
---|
| 156 | bool lookupCache(QString part, const QModelIndex& parent, QMatchData *m);
|
---|
| 157 |
|
---|
| 158 | virtual void filterOnDemand(int) { }
|
---|
| 159 | virtual QMatchData filter(const QString&, const QModelIndex&, int) = 0;
|
---|
| 160 |
|
---|
| 161 | int matchCount() const { return curMatch.indices.count() + historyMatch.indices.count(); }
|
---|
| 162 |
|
---|
| 163 | QMatchData curMatch, historyMatch;
|
---|
| 164 | QCompleterPrivate *c;
|
---|
| 165 | QStringList curParts;
|
---|
| 166 | QModelIndex curParent;
|
---|
| 167 | int curRow;
|
---|
| 168 |
|
---|
| 169 | Cache cache;
|
---|
| 170 | int cost;
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 | class QSortedModelEngine : public QCompletionEngine
|
---|
| 174 | {
|
---|
| 175 | public:
|
---|
| 176 | QSortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
|
---|
| 177 | QMatchData filter(const QString&, const QModelIndex&, int);
|
---|
| 178 | QIndexMapper indexHint(QString, const QModelIndex&, Qt::SortOrder);
|
---|
| 179 | Qt::SortOrder sortOrder(const QModelIndex&) const;
|
---|
| 180 | };
|
---|
| 181 |
|
---|
| 182 | class QUnsortedModelEngine : public QCompletionEngine
|
---|
| 183 | {
|
---|
| 184 | public:
|
---|
| 185 | QUnsortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
|
---|
| 186 |
|
---|
| 187 | void filterOnDemand(int);
|
---|
| 188 | QMatchData filter(const QString&, const QModelIndex&, int);
|
---|
| 189 | private:
|
---|
| 190 | int buildIndices(const QString& str, const QModelIndex& parent, int n,
|
---|
| 191 | const QIndexMapper& iv, QMatchData* m);
|
---|
| 192 | };
|
---|
| 193 |
|
---|
| 194 | class QCompleterItemDelegate : public QItemDelegate
|
---|
| 195 | {
|
---|
| 196 | public:
|
---|
| 197 | QCompleterItemDelegate(QAbstractItemView *view)
|
---|
| 198 | : QItemDelegate(view), view(view) { }
|
---|
| 199 | void paint(QPainter *p, const QStyleOptionViewItem& opt, const QModelIndex& idx) const {
|
---|
| 200 | QStyleOptionViewItem optCopy = opt;
|
---|
| 201 | optCopy.showDecorationSelected = true;
|
---|
| 202 | if (view->currentIndex() == idx)
|
---|
| 203 | optCopy.state |= QStyle::State_HasFocus;
|
---|
| 204 | QItemDelegate::paint(p, optCopy, idx);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | private:
|
---|
| 208 | QAbstractItemView *view;
|
---|
| 209 | };
|
---|
| 210 |
|
---|
| 211 | class QCompletionModelPrivate;
|
---|
| 212 |
|
---|
| 213 | class QCompletionModel : public QAbstractProxyModel
|
---|
| 214 | {
|
---|
| 215 | Q_OBJECT
|
---|
| 216 |
|
---|
| 217 | public:
|
---|
| 218 | QCompletionModel(QCompleterPrivate *c, QObject *parent);
|
---|
| 219 |
|
---|
| 220 | void createEngine();
|
---|
| 221 | void setFiltered(bool);
|
---|
| 222 | void filter(const QStringList& parts);
|
---|
| 223 | int completionCount() const;
|
---|
| 224 | int currentRow() const { return engine->curRow; }
|
---|
| 225 | bool setCurrentRow(int row);
|
---|
| 226 | QModelIndex currentIndex(bool) const;
|
---|
| 227 | void resetModel();
|
---|
| 228 |
|
---|
| 229 | QModelIndex index(int row, int column, const QModelIndex & = QModelIndex()) const;
|
---|
| 230 | int rowCount(const QModelIndex &index = QModelIndex()) const;
|
---|
| 231 | int columnCount(const QModelIndex &index = QModelIndex()) const;
|
---|
| 232 | bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
|
---|
| 233 | QModelIndex parent(const QModelIndex & = QModelIndex()) const { return QModelIndex(); }
|
---|
| 234 | QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
---|
| 235 |
|
---|
| 236 | void setSourceModel(QAbstractItemModel *sourceModel);
|
---|
| 237 | QModelIndex mapToSource(const QModelIndex& proxyIndex) const;
|
---|
| 238 | QModelIndex mapFromSource(const QModelIndex& sourceIndex) const;
|
---|
| 239 |
|
---|
| 240 | QCompleterPrivate *c;
|
---|
[561] | 241 | QScopedPointer<QCompletionEngine> engine;
|
---|
[2] | 242 | bool showAll;
|
---|
| 243 |
|
---|
| 244 | Q_DECLARE_PRIVATE(QCompletionModel)
|
---|
| 245 |
|
---|
| 246 | signals:
|
---|
| 247 | void rowsAdded();
|
---|
| 248 |
|
---|
| 249 | public Q_SLOTS:
|
---|
| 250 | void invalidate();
|
---|
| 251 | void rowsInserted();
|
---|
| 252 | void modelDestroyed();
|
---|
| 253 | };
|
---|
| 254 |
|
---|
| 255 | class QCompletionModelPrivate : public QAbstractProxyModelPrivate
|
---|
| 256 | {
|
---|
| 257 | Q_DECLARE_PUBLIC(QCompletionModel)
|
---|
| 258 | };
|
---|
| 259 |
|
---|
| 260 | QT_END_NAMESPACE
|
---|
| 261 |
|
---|
| 262 | #endif // QT_NO_COMPLETER
|
---|
| 263 |
|
---|
| 264 | #endif // QCOMPLETER_P_H
|
---|