Qt 4.8
qcompleter_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
69 
70 class QCompletionModel;
71 
73 {
75 
76 public:
78  ~QCompleterPrivate() { delete popup; }
79  void init(QAbstractItemModel *model = 0);
80 
85 
88  int role;
89  int column;
92  bool wrap;
93 
97 
98  void showPopup(const QRect&);
99  void _q_complete(QModelIndex, bool = false);
101  void _q_autoResizePopup();
102  void _q_fileSystemModelDirectoryLoaded(const QString &path);
103  void setCurrentIndex(QModelIndex, bool = true);
104 };
105 
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;
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) { }
136  inline bool isValid() const { return indices.isValid(); }
138  bool partial;
139 };
140 
142 {
143 public:
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 
167  int curRow;
168 
169  Cache cache;
170  int cost;
171 };
172 
174 {
175 public:
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 
183 {
184 public:
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 
195 {
196 public:
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:
209 };
210 
212 
214 {
215  Q_OBJECT
216 
217 public:
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 
242  bool showAll;
243 
245 
246 signals:
247  void rowsAdded();
248 
249 public Q_SLOTS:
250  void invalidate();
251  void rowsInserted();
252  void modelDestroyed();
253 };
254 
256 {
258 };
259 
261 
262 #endif // QT_NO_COMPLETER
263 
264 #endif // QCOMPLETER_P_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QMatchData(const QIndexMapper &indices, int em, bool p)
Definition: qcompleter_p.h:133
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
CompletionMode
This enum specifies how completions are provided to the user.
Definition: qcompleter.h:77
QModelIndex curParent
Definition: qcompleter_p.h:166
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QIndexMapper(QVector< int > vec)
Definition: qcompleter_p.h:111
void append(int x)
Definition: qcompleter_p.h:118
void setCurrentIndex(QModelIndex, bool=true)
Definition: qcompleter.cpp:807
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
QCompleterPrivate * c
Definition: qcompleter_p.h:240
int first() const
Definition: qcompleter_p.h:119
QCompleter::CompletionMode mode
Definition: qcompleter_p.h:84
QPointer< QWidget > widget
Definition: qcompleter_p.h:81
virtual ~QCompletionEngine()
Definition: qcompleter_p.h:148
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
void paint(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &idx) const
Renders the delegate using the given painter and style option for the item specified by index...
Definition: qcompleter_p.h:199
QStringList curParts
Definition: qcompleter_p.h:165
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
QCompleterPrivate * c
Definition: qcompleter_p.h:164
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QAbstractItemView * view
Definition: qcompleter_p.h:208
void _q_fileSystemModelDirectoryLoaded(const QString &path)
Definition: qcompleter.cpp:926
bool isEmpty() const
Definition: qcompleter_p.h:117
bool isValid() const
Definition: qcompleter_p.h:136
ModelSorting
This enum specifies how the items in the model are sorted.
Definition: qcompleter.h:83
QCompletionEngine(QCompleterPrivate *c)
Definition: qcompleter_p.h:147
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool isEmpty(const char *str)
virtual void filterOnDemand(int)
Definition: qcompleter_p.h:158
QMap< QString, QMatchData > CacheItem
Definition: qcompleter_p.h:144
void _q_complete(QModelIndex, bool=false)
Definition: qcompleter.cpp:837
int indexOf(int x) const
Definition: qcompleter_p.h:115
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
bool showDecorationSelected
whether the decoration should be highlighted on selected items
Definition: qstyleoption.h:553
static const char * data(const QByteArray &arr)
QMatchData historyMatch
Definition: qcompleter_p.h:163
int to() const
Definition: qcompleter_p.h:122
void init(QAbstractItemModel *model=0)
Definition: qcompleter.cpp:794
QAbstractItemView * popup
Definition: qcompleter_p.h:83
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAbstractItemModel class provides the abstract interface for item model classes.
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting...
int exactMatchIndex
Definition: qcompleter_p.h:137
int from() const
Definition: qcompleter_p.h:121
QIndexMapper(int f, int t)
Definition: qcompleter_p.h:110
CaseSensitivity
Definition: qnamespace.h:1451
The QAbstractItemView class provides the basic functionality for item view classes.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QItemSelection class manages information about selected items in a model.
QMap< QModelIndex, CacheItem > Cache
Definition: qcompleter_p.h:145
QVector< int > vector
Definition: qcompleter_p.h:127
QCompleterItemDelegate(QAbstractItemView *view)
Definition: qcompleter_p.h:197
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QModelIndex class is used to locate data in a data model.
void showPopup(const QRect &)
Definition: qcompleter.cpp:882
int last() const
Definition: qcompleter_p.h:120
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QFuture< void > filter(Sequence &sequence, FilterFunction filterFunction)
QCompletionModel * proxy
Definition: qcompleter_p.h:82
quint16 index
QObject * parent
Definition: qobject.h:92
void _q_autoResizePopup()
Definition: qcompleter.cpp:875
QUnsortedModelEngine(QCompleterPrivate *c)
Definition: qcompleter_p.h:185
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QScopedPointer< QCompletionEngine > engine
Definition: qcompleter_p.h:241
int matchCount() const
Definition: qcompleter_p.h:161
Qt::CaseSensitivity cs
Definition: qcompleter_p.h:87
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
int currentRow() const
Definition: qcompleter_p.h:224
void _q_completionSelected(const QItemSelection &)
Definition: qcompleter.cpp:828
QIndexMapper indices
Definition: qcompleter_p.h:135
QCompleter::ModelSorting sorting
Definition: qcompleter_p.h:91
#define signals
Definition: qobjectdefs.h:69
int cost() const
Definition: qcompleter_p.h:123
QModelIndex parent(const QModelIndex &=QModelIndex()) const
Returns the parent of the model item with the given index.
Definition: qcompleter_p.h:233
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Renders the delegate using the given painter and style option for the item specified by index...
bool isValid() const
Definition: qcompleter_p.h:116
int count() const
Definition: qcompleter_p.h:113
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
QSortedModelEngine(QCompleterPrivate *c)
Definition: qcompleter_p.h:176