1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
/*! \class ItemViewFindWidget
\brief A search bar that is commonly added below the searchable item view.
\internal
This widget implements a search bar which becomes visible when the user
wants to start searching. It is a modern replacement for the commonly used
search dialog. It is usually placed below a QAbstractItemView using a QVBoxLayout.
The QAbstractItemView instance will need to be associated with this class using
setItemView().
The search is incremental and can be set to case sensitive or whole words
using buttons available on the search bar.
The item traversal order should fit QTreeView, QTableView and QListView alike.
More complex tree structures will work as well, assuming the branch structure
is painted left to the items, without crossing lines.
\sa QAbstractItemView
*/
#include "itemviewfindwidget.h"
#include <QtGui/QAbstractItemView>
#include <QtGui/QCheckBox>
#include <QtGui/QTreeView>
QT_BEGIN_NAMESPACE
/*!
Constructs a ItemViewFindWidget.
\a flags is passed to the AbstractFindWidget constructor.
\a parent is passed to the QWidget constructor.
*/
ItemViewFindWidget::ItemViewFindWidget(FindFlags flags, QWidget *parent)
: AbstractFindWidget(flags, parent)
, m_itemView(0)
{
}
/*!
Associates a QAbstractItemView with this find widget. Searches done using this find
widget will then apply to the given QAbstractItemView.
An event filter is set on the QAbstractItemView which intercepts the ESC key while
the find widget is active, and uses it to deactivate the find widget.
If the find widget is already associated with a QAbstractItemView, the event filter
is removed from this QAbstractItemView first.
\a itemView may be NULL.
*/
void ItemViewFindWidget::setItemView(QAbstractItemView *itemView)
{
if (m_itemView)
m_itemView->removeEventFilter(this);
m_itemView = itemView;
if (m_itemView)
m_itemView->installEventFilter(this);
}
/*!
\reimp
*/
void ItemViewFindWidget::deactivate()
{
if (m_itemView)
m_itemView->setFocus();
AbstractFindWidget::deactivate();
}
// Sorting is needed to find the start/end of the selection.
// This is utter black magic. And it is damn slow.
static bool indexLessThan(const QModelIndex &a, const QModelIndex &b)
{
// First determine the nesting of each index in the tree.
QModelIndex aa = a;
int aDepth = 0;
while (aa.parent() != QModelIndex()) {
// As a side effect, check if one of the items is the parent of the other.
// Children are always displayed below their parents, so sort them further down.
if (aa.parent() == b)
return true;
aa = aa.parent();
aDepth++;
}
QModelIndex ba = b;
int bDepth = 0;
while (ba.parent() != QModelIndex()) {
if (ba.parent() == a)
return false;
ba = ba.parent();
bDepth++;
}
// Now find indices at comparable depth.
for (aa = a; aDepth > bDepth; aDepth--)
aa = aa.parent();
for (ba = b; aDepth < bDepth; bDepth--)
ba = ba.parent();
// If they have the same parent, sort them within a top-to-bottom, left-to-right rectangle.
if (aa.parent() == ba.parent()) {
if (aa.row() < ba.row())
return true;
if (aa.row() > ba.row())
return false;
return aa.column() < ba.column();
}
// Now try to find indices that have the same grandparent. This ends latest at the root node.
while (aa.parent().parent() != ba.parent().parent()) {
aa = aa.parent();
ba = ba.parent();
}
// A bigger row is always displayed further down.
if (aa.parent().row() < ba.parent().row())
return true;
if (aa.parent().row() > ba.parent().row())
return false;
// Here's the trick: a child spawned from a bigger column is displayed further *up*.
// That's because the tree lines are on the left and are supposed not to cross each other.
// This case is mostly academical, as "all" models spawn children from the first column.
return aa.parent().column() > ba.parent().column();
}
/*!
\reimp
*/
void ItemViewFindWidget::find(const QString &ttf, bool skipCurrent, bool backward, bool *found, bool *wrapped)
{
if (!m_itemView || !m_itemView->model()->hasChildren())
return;
QModelIndex idx;
if (skipCurrent && m_itemView->selectionModel()->hasSelection()) {
QModelIndexList il = m_itemView->selectionModel()->selectedIndexes();
qSort(il.begin(), il.end(), indexLessThan);
idx = backward ? il.first() : il.last();
} else {
idx = m_itemView->currentIndex();
}
*found = true;
QModelIndex newIdx = idx;
if (!ttf.isEmpty()) {
if (newIdx.isValid()) {
int column = newIdx.column();
if (skipCurrent)
if (QTreeView *tv = qobject_cast<QTreeView *>(m_itemView))
if (tv->allColumnsShowFocus())
column = backward ? 0 : m_itemView->model()->columnCount(newIdx.parent()) - 1;
newIdx = findHelper(ttf, skipCurrent, backward,
newIdx.parent(), newIdx.row(), column);
}
if (!newIdx.isValid()) {
int row = backward ? m_itemView->model()->rowCount() : 0;
int column = backward ? 0 : -1;
newIdx = findHelper(ttf, true, backward, m_itemView->rootIndex(), row, column);
if (!newIdx.isValid()) {
*found = false;
newIdx = idx;
} else {
*wrapped = true;
}
}
}
if (!isVisible())
show();
m_itemView->setCurrentIndex(newIdx);
}
// You are not expected to understand the following two functions.
// The traversal order is described in the indexLessThan() comments above.
static inline bool skipForward(const QAbstractItemModel *model, QModelIndex &parent, int &row, int &column)
{
forever {
column++;
if (column < model->columnCount(parent))
return true;
forever {
while (--column >= 0) {
QModelIndex nIdx = model->index(row, column, parent);
if (nIdx.isValid()) {
if (model->hasChildren(nIdx)) {
row = 0;
column = 0;
parent = nIdx;
return true;
}
}
}
if (++row < model->rowCount(parent))
break;
if (!parent.isValid())
return false;
row = parent.row();
column = parent.column();
parent = parent.parent();
}
}
}
static inline bool skipBackward(const QAbstractItemModel *model, QModelIndex &parent, int &row, int &column)
{
column--;
if (column == -1) {
if (--row < 0) {
if (!parent.isValid())
return false;
row = parent.row();
column = parent.column();
parent = parent.parent();
}
while (++column < model->columnCount(parent)) {
QModelIndex nIdx = model->index(row, column, parent);
if (nIdx.isValid()) {
if (model->hasChildren(nIdx)) {
row = model->rowCount(nIdx) - 1;
column = -1;
parent = nIdx;
}
}
}
column--;
}
return true;
}
// QAbstractItemModel::match() does not support backwards searching. Still using it would
// be just a bit inefficient (not much worse than when no match is found).
// The bigger problem is that QAbstractItemView does not provide a method to sort a
// set of indices in traversal order (to find the start and end of the selection).
// Consequently, we do everything by ourselves to be consistent. Of course, this puts
// constraints on the allowable visualizations.
QModelIndex ItemViewFindWidget::findHelper(const QString &textToFind, bool skipCurrent, bool backward,
QModelIndex parent, int row, int column)
{
const QAbstractItemModel *model = m_itemView->model();
forever {
if (skipCurrent) {
if (backward) {
if (!skipBackward(model, parent, row, column))
return QModelIndex();
} else {
if (!skipForward(model, parent, row, column))
return QModelIndex();
}
}
QModelIndex idx = model->index(row, column, parent);
if (idx.isValid()) {
Qt::CaseSensitivity cs = caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if (wholeWords()) {
QString rx = QLatin1String("\\b") + QRegExp::escape(textToFind) + QLatin1String("\\b");
if (idx.data().toString().indexOf(QRegExp(rx, cs)) >= 0)
return idx;
} else {
if (idx.data().toString().indexOf(textToFind, 0, cs) >= 0)
return idx;
}
}
skipCurrent = true;
}
}
QT_END_NAMESPACE
|