Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcompleter_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QCOMPLETER_P_H
6#define QCOMPLETER_P_H
7
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtWidgets/private/qtwidgetsglobal_p.h>
21#include "private/qobject_p.h"
22
23#include "QtWidgets/qabstractitemview.h"
24#include "QtCore/qabstractproxymodel.h"
25#include "QtCore/qmap.h"
26#include "qcompleter.h"
28#include "QtGui/qpainter.h"
29
30#include "private/qabstractproxymodel_p.h"
31#include <QtCore/qpointer.h>
32
34
35QT_BEGIN_NAMESPACE
36
37class QCompletionModel;
38
40{
41 Q_DECLARE_PUBLIC(QCompleter)
42
43public:
45 ~QCompleterPrivate() { delete popup; }
46 void init(QAbstractItemModel *model = nullptr);
47
50 QAbstractItemView *popup;
53
56 int role;
57 int column;
60 bool wrap;
61
65
66 void showPopup(const QRect&);
67 void _q_complete(QModelIndex, bool = false);
68 void _q_completionSelected(const QItemSelection&);
69 void _q_autoResizePopup();
70 void _q_fileSystemModelDirectoryLoaded(const QString &path);
71 void setCurrentIndex(QModelIndex, bool = true);
72
73 static QCompleterPrivate *get(QCompleter *o) { return o->d_func(); }
74 static const QCompleterPrivate *get(const QCompleter *o) { return o->d_func(); }
75};
76
78{
79public:
80 QIndexMapper() : v(false), f(0), t(-1) { }
81 QIndexMapper(int f, int t) : v(false), f(f), t(t) { }
82 QIndexMapper(const QList<int> &vec) : v(true), vector(vec), f(-1), t(-1) { }
83
84 inline int count() const { return v ? vector.size() : t - f + 1; }
85 inline int operator[] (int index) const { return v ? vector[index] : f + index; }
86 inline int indexOf(int x) const { return v ? vector.indexOf(x) : ((t < f) ? -1 : x - f); }
87 inline bool isValid() const { return !isEmpty(); }
88 inline bool isEmpty() const { return v ? vector.isEmpty() : (t < f); }
89 inline void append(int x) { Q_ASSERT(v); vector.append(x); }
90 inline int first() const { return v ? vector.first() : f; }
91 inline int last() const { return v ? vector.last() : t; }
92 inline int from() const { Q_ASSERT(!v); return f; }
93 inline int to() const { Q_ASSERT(!v); return t; }
94 inline int cost() const { return vector.size()+2; }
95
96private:
97 bool v;
98 QList<int> vector;
99 int f, t;
100};
101
104 QMatchData(const QIndexMapper& indices, int em, bool p) :
107 inline bool isValid() const { return indices.isValid(); }
110};
111
113{
114public:
117
119 virtual ~QCompletionEngine() { }
120
121 void filter(const QStringList &parts);
122
124 bool matchHint(const QString &part, const QModelIndex &parent, QMatchData *m) const;
125
126 void saveInCache(QString, const QModelIndex&, const QMatchData&);
127 bool lookupCache(const QString &part, const QModelIndex &parent, QMatchData *m) const;
128
129 virtual void filterOnDemand(int) { }
130 virtual QMatchData filter(const QString&, const QModelIndex&, int) = 0;
131
132 int matchCount() const { return curMatch.indices.count() + historyMatch.indices.count(); }
133
139
141 int cost;
142};
143
145{
146public:
148 QMatchData filter(const QString&, const QModelIndex&, int) override;
149 QIndexMapper indexHint(QString, const QModelIndex&, Qt::SortOrder);
150 Qt::SortOrder sortOrder(const QModelIndex&) const;
151};
152
154{
155public:
157
158 void filterOnDemand(int) override;
159 QMatchData filter(const QString&, const QModelIndex&, int) override;
160private:
161 int buildIndices(const QString& str, const QModelIndex& parent, int n,
162 const QIndexMapper& iv, QMatchData* m);
163};
164
166{
167public:
168 QCompleterItemDelegate(QAbstractItemView *view)
169 : QStyledItemDelegate(view), view(view) { }
170 void paint(QPainter *p, const QStyleOptionViewItem& opt, const QModelIndex& idx) const override {
171 QStyleOptionViewItem optCopy = opt;
172 optCopy.showDecorationSelected = true;
173 if (view->currentIndex() == idx)
174 optCopy.state |= QStyle::State_HasFocus;
175 QStyledItemDelegate::paint(p, optCopy, idx);
176 }
177
178private:
179 QAbstractItemView *view;
180};
181
183
185{
187
188public:
190
191 void createEngine();
192 void setFiltered(bool);
193 void filter(const QStringList& parts);
194 int completionCount() const;
195 int currentRow() const { return engine->curRow; }
196 bool setCurrentRow(int row);
197 QModelIndex currentIndex(bool) const;
198
199 QModelIndex index(int row, int column, const QModelIndex & = QModelIndex()) const override;
200 int rowCount(const QModelIndex &index = QModelIndex()) const override;
201 int columnCount(const QModelIndex &index = QModelIndex()) const override;
202 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
203 QModelIndex parent(const QModelIndex & = QModelIndex()) const override { return QModelIndex(); }
204 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
205
206 void setSourceModel(QAbstractItemModel *sourceModel) override;
207 QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
208 QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
209
213
215
216signals:
217 void rowsAdded();
218
219public Q_SLOTS:
220 void invalidate();
221 void rowsInserted();
222 void modelDestroyed();
223};
224
226{
227 Q_DECLARE_PUBLIC(QCompletionModel)
228};
229
230QT_END_NAMESPACE
231
232#endif // QCOMPLETER_P_H
QCompleterItemDelegate(QAbstractItemView *view)
void paint(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &idx) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
Qt::CaseSensitivity cs
void setCurrentIndex(QModelIndex, bool=true)
static const QCompleterPrivate * get(const QCompleter *o)
void _q_fileSystemModelDirectoryLoaded(const QString &path)
void _q_completionSelected(const QItemSelection &)
Qt::MatchFlags filterMode
QPointer< QWidget > widget
void init(QAbstractItemModel *model=nullptr)
QAbstractItemView * popup
void _q_complete(QModelIndex, bool=false)
QCompletionModel * proxy
static QCompleterPrivate * get(QCompleter *o)
void showPopup(const QRect &)
QMatchData historyMatch
QMatchData curMatch
void saveInCache(QString, const QModelIndex &, const QMatchData &)
virtual QMatchData filter(const QString &, const QModelIndex &, int)=0
QModelIndex curParent
void filter(const QStringList &parts)
virtual void filterOnDemand(int)
QMap< QString, QMatchData > CacheItem
int matchCount() const
bool matchHint(const QString &part, const QModelIndex &parent, QMatchData *m) const
QCompletionEngine(QCompleterPrivate *c)
bool lookupCache(const QString &part, const QModelIndex &parent, QMatchData *m) const
virtual ~QCompletionEngine()
QStringList curParts
QMap< QModelIndex, CacheItem > Cache
QCompleterPrivate * c
QMatchData filterHistory()
QModelIndex index(int row, int column, const QModelIndex &=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
int currentRow() const
int rowCount(const QModelIndex &index=QModelIndex()) const override
Returns the number of rows under the given parent.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
\reimp
void setFiltered(bool)
int completionCount() const
int columnCount(const QModelIndex &index=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QCompleterPrivate * c
QScopedPointer< QCompletionEngine > engine
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Reimplement this function to return the model index in the proxy model that corresponds to the source...
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
\reimp
QModelIndex parent(const QModelIndex &=QModelIndex()) const override
QModelIndex currentIndex(bool) const
void filter(const QStringList &parts)
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Reimplement this function to return the model index in the source model that corresponds to the proxy...
void setSourceModel(QAbstractItemModel *sourceModel) override
Sets the given sourceModel to be processed by the proxy model.
bool setCurrentRow(int row)
int cost() const
bool isValid() const
int from() const
bool isEmpty() const
int indexOf(int x) const
int first() const
void append(int x)
int count() const
int last() const
int operator[](int index) const
QIndexMapper(const QList< int > &vec)
QIndexMapper(int f, int t)
int to() const
QSortedModelEngine(QCompleterPrivate *c)
QMatchData filter(const QString &, const QModelIndex &, int) override
Qt::SortOrder sortOrder(const QModelIndex &) const
QIndexMapper indexHint(QString, const QModelIndex &, Qt::SortOrder)
QUnsortedModelEngine(QCompleterPrivate *c)
void filterOnDemand(int) override
QMatchData filter(const QString &, const QModelIndex &, int) override
QT_REQUIRE_CONFIG(animation)
QCompleter * completer
[0]
QMatchData(const QIndexMapper &indices, int em, bool p)
int exactMatchIndex
bool isValid() const
QIndexMapper indices