1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
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 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 | QCompleter::ModelSorting sorting;
|
---|
91 | bool wrap;
|
---|
92 |
|
---|
93 | bool eatFocusOut;
|
---|
94 | QRect popupRect;
|
---|
95 |
|
---|
96 | void showPopup(const QRect&);
|
---|
97 | void _q_complete(QModelIndex, bool = false);
|
---|
98 | void _q_completionSelected(const QItemSelection&);
|
---|
99 | void _q_autoResizePopup();
|
---|
100 | void setCurrentIndex(QModelIndex, bool = true);
|
---|
101 | };
|
---|
102 |
|
---|
103 | class QIndexMapper
|
---|
104 | {
|
---|
105 | public:
|
---|
106 | QIndexMapper() : v(false), f(0), t(-1) { }
|
---|
107 | QIndexMapper(int f, int t) : v(false), f(f), t(t) { }
|
---|
108 | QIndexMapper(QVector<int> vec) : v(true), vector(vec), f(-1), t(-1) { }
|
---|
109 |
|
---|
110 | inline int count() const { return v ? vector.count() : t - f + 1; }
|
---|
111 | inline int operator[] (int index) const { return v ? vector[index] : f + index; }
|
---|
112 | inline int indexOf(int x) const { return v ? vector.indexOf(x) : ((t < f) ? -1 : x - f); }
|
---|
113 | inline bool isValid() const { return !isEmpty(); }
|
---|
114 | inline bool isEmpty() const { return v ? vector.isEmpty() : (t < f); }
|
---|
115 | inline void append(int x) { Q_ASSERT(v); vector.append(x); }
|
---|
116 | inline int first() const { return v ? vector.first() : f; }
|
---|
117 | inline int last() const { return v ? vector.last() : t; }
|
---|
118 | inline int from() const { Q_ASSERT(!v); return f; }
|
---|
119 | inline int to() const { Q_ASSERT(!v); return t; }
|
---|
120 | inline int cost() const { return vector.count()+2; }
|
---|
121 |
|
---|
122 | private:
|
---|
123 | bool v;
|
---|
124 | QVector<int> vector;
|
---|
125 | int f, t;
|
---|
126 | };
|
---|
127 |
|
---|
128 | struct QMatchData {
|
---|
129 | QMatchData() : exactMatchIndex(-1) { }
|
---|
130 | QMatchData(const QIndexMapper& indices, int em, bool p) :
|
---|
131 | indices(indices), exactMatchIndex(em), partial(p) { }
|
---|
132 | QIndexMapper indices;
|
---|
133 | inline bool isValid() const { return indices.isValid(); }
|
---|
134 | int exactMatchIndex;
|
---|
135 | bool partial;
|
---|
136 | };
|
---|
137 |
|
---|
138 | class QCompletionEngine
|
---|
139 | {
|
---|
140 | public:
|
---|
141 | typedef QMap<QString, QMatchData> CacheItem;
|
---|
142 | typedef QMap<QModelIndex, CacheItem> Cache;
|
---|
143 |
|
---|
144 | QCompletionEngine(QCompleterPrivate *c) : c(c), curRow(-1), cost(0) { }
|
---|
145 | virtual ~QCompletionEngine() { }
|
---|
146 |
|
---|
147 | void filter(const QStringList &parts);
|
---|
148 |
|
---|
149 | QMatchData filterHistory();
|
---|
150 | bool matchHint(QString, const QModelIndex&, QMatchData*);
|
---|
151 |
|
---|
152 | void saveInCache(QString, const QModelIndex&, const QMatchData&);
|
---|
153 | bool lookupCache(QString part, const QModelIndex& parent, QMatchData *m);
|
---|
154 |
|
---|
155 | virtual void filterOnDemand(int) { }
|
---|
156 | virtual QMatchData filter(const QString&, const QModelIndex&, int) = 0;
|
---|
157 |
|
---|
158 | int matchCount() const { return curMatch.indices.count() + historyMatch.indices.count(); }
|
---|
159 |
|
---|
160 | QMatchData curMatch, historyMatch;
|
---|
161 | QCompleterPrivate *c;
|
---|
162 | QStringList curParts;
|
---|
163 | QModelIndex curParent;
|
---|
164 | int curRow;
|
---|
165 |
|
---|
166 | Cache cache;
|
---|
167 | int cost;
|
---|
168 | };
|
---|
169 |
|
---|
170 | class QSortedModelEngine : public QCompletionEngine
|
---|
171 | {
|
---|
172 | public:
|
---|
173 | QSortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
|
---|
174 | QMatchData filter(const QString&, const QModelIndex&, int);
|
---|
175 | QIndexMapper indexHint(QString, const QModelIndex&, Qt::SortOrder);
|
---|
176 | Qt::SortOrder sortOrder(const QModelIndex&) const;
|
---|
177 | };
|
---|
178 |
|
---|
179 | class QUnsortedModelEngine : public QCompletionEngine
|
---|
180 | {
|
---|
181 | public:
|
---|
182 | QUnsortedModelEngine(QCompleterPrivate *c) : QCompletionEngine(c) { }
|
---|
183 |
|
---|
184 | void filterOnDemand(int);
|
---|
185 | QMatchData filter(const QString&, const QModelIndex&, int);
|
---|
186 | private:
|
---|
187 | int buildIndices(const QString& str, const QModelIndex& parent, int n,
|
---|
188 | const QIndexMapper& iv, QMatchData* m);
|
---|
189 | };
|
---|
190 |
|
---|
191 | class QCompleterItemDelegate : public QItemDelegate
|
---|
192 | {
|
---|
193 | public:
|
---|
194 | QCompleterItemDelegate(QAbstractItemView *view)
|
---|
195 | : QItemDelegate(view), view(view) { }
|
---|
196 | void paint(QPainter *p, const QStyleOptionViewItem& opt, const QModelIndex& idx) const {
|
---|
197 | QStyleOptionViewItem optCopy = opt;
|
---|
198 | optCopy.showDecorationSelected = true;
|
---|
199 | if (view->currentIndex() == idx)
|
---|
200 | optCopy.state |= QStyle::State_HasFocus;
|
---|
201 | QItemDelegate::paint(p, optCopy, idx);
|
---|
202 | }
|
---|
203 |
|
---|
204 | private:
|
---|
205 | QAbstractItemView *view;
|
---|
206 | };
|
---|
207 |
|
---|
208 | class QCompletionModelPrivate;
|
---|
209 |
|
---|
210 | class QCompletionModel : public QAbstractProxyModel
|
---|
211 | {
|
---|
212 | Q_OBJECT
|
---|
213 |
|
---|
214 | public:
|
---|
215 | QCompletionModel(QCompleterPrivate *c, QObject *parent);
|
---|
216 | ~QCompletionModel() { delete engine; }
|
---|
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 | 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
|
---|