aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/find/searchresulttreemodel.cpp
blob: b602e1b020f05d284fa9d30ca6f49acefd69713c (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "searchresulttreemodel.h"
#include "searchresulttreeitems.h"
#include "searchresulttreeitemroles.h"
#include "../icore.h"

#include <utils/algorithm.h>
#include <utils/searchresultitem.h>

#include <QApplication>
#include <QFont>
#include <QFontMetrics>
#include <QDebug>

using namespace Utils;

namespace Core {
namespace Internal {

class SearchResultTreeModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    SearchResultTreeModel(QObject *parent = nullptr);
    ~SearchResultTreeModel() override;

    void setShowReplaceUI(bool show);
    void setRelativePaths(bool relative);
    void setTextEditorFont(const QFont &font, const SearchResultColors &colors);

    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &child) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;

    QModelIndex next(const QModelIndex &idx, bool includeGenerated = false, bool *wrapped = nullptr) const;
    QModelIndex prev(const QModelIndex &idx, bool includeGenerated = false, bool *wrapped = nullptr) const;

    QList<QModelIndex> addResults(const SearchResultItems &items, SearchResult::AddMode mode);

    static SearchResultTreeItem *treeItemAtIndex(const QModelIndex &idx);

signals:
    void jumpToSearchResult(const QString &fileName, int lineNumber,
                            int searchTermStart, int searchTermLength);

public slots:
    void clear();

private:
    QModelIndex index(SearchResultTreeItem *item) const;
    void addResultsToCurrentParent(const SearchResultItems &items, SearchResult::AddMode mode);
    QSet<SearchResultTreeItem *> addPath(const QStringList &path);
    QVariant data(const SearchResultTreeItem *row, int role) const;
    bool setCheckState(const QModelIndex &idx, Qt::CheckState checkState, bool firstCall = true);
    void updateCheckStateFromChildren(const QModelIndex &idx, SearchResultTreeItem *item);
    QModelIndex nextIndex(const QModelIndex &idx, bool *wrapped = nullptr) const;
    QModelIndex prevIndex(const QModelIndex &idx, bool *wrapped = nullptr) const;

    SearchResultTreeItem *m_rootItem;
    SearchResultTreeItem *m_currentParent;
    SearchResultColors m_colors;
    QModelIndex m_currentIndex;
    QStringList m_currentPath; // the path that belongs to the current parent
    QFont m_textEditorFont;
    bool m_showReplaceUI;
    bool m_relativePaths;
    bool m_editorFontIsUsed;
};

SearchResultTreeModel::SearchResultTreeModel(QObject *parent)
    : QAbstractItemModel(parent)
    , m_currentParent(nullptr)
    , m_showReplaceUI(false)
    , m_relativePaths(false)
    , m_editorFontIsUsed(false)
{
    m_rootItem = new SearchResultTreeItem;
    m_textEditorFont = QFont(QLatin1String("Courier"));
}

SearchResultTreeModel::~SearchResultTreeModel()
{
    delete m_rootItem;
}

void SearchResultTreeModel::setShowReplaceUI(bool show)
{
    m_showReplaceUI = show;
    // We cannot send dataChanged for the whole hierarchy in one go,
    // because all items in a dataChanged must have the same parent.
    // Send dataChanged for each parent of children individually...
    QList<QModelIndex> changeQueue;
    changeQueue.append(QModelIndex());
    while (!changeQueue.isEmpty()) {
        const QModelIndex current = changeQueue.takeFirst();
        int childCount = rowCount(current);
        if (childCount > 0) {
            emit dataChanged(index(0, 0, current), index(childCount - 1, 0, current));
            for (int r = 0; r < childCount; ++r)
                changeQueue.append(index(r, 0, current));
        }
    }
}

void SearchResultTreeModel::setRelativePaths(bool relative)
{
    m_relativePaths = relative;
    emit dataChanged(index(0, 0), index(rowCount() - 1, 0));
}

void SearchResultTreeModel::setTextEditorFont(const QFont &font, const SearchResultColors &colors)
{
    emit layoutAboutToBeChanged();
    m_textEditorFont = font;
    m_colors = colors;
    emit layoutChanged();
}

Qt::ItemFlags SearchResultTreeModel::flags(const QModelIndex &idx) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(idx);

    if (idx.isValid()) {
        if (m_showReplaceUI)
            flags |= Qt::ItemIsUserCheckable;
    }

    return flags;
}

QModelIndex SearchResultTreeModel::index(int row, int column,
                                         const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    const SearchResultTreeItem *parentItem;

    if (!parent.isValid())
        parentItem = m_rootItem;
    else
        parentItem = treeItemAtIndex(parent);

    const SearchResultTreeItem *childItem = parentItem->childAt(row);
    if (childItem)
        return createIndex(row, column, const_cast<SearchResultTreeItem *>(childItem));
    else
        return QModelIndex();
}

QModelIndex SearchResultTreeModel::index(SearchResultTreeItem *item) const
{
    return createIndex(item->rowOfItem(), 0, item);
}

QModelIndex SearchResultTreeModel::parent(const QModelIndex &idx) const
{
    if (!idx.isValid())
        return QModelIndex();

    const SearchResultTreeItem *childItem = treeItemAtIndex(idx);
    const SearchResultTreeItem *parentItem = childItem->parent();

    if (parentItem == m_rootItem)
        return QModelIndex();

    return createIndex(parentItem->rowOfItem(), 0, const_cast<SearchResultTreeItem *>(parentItem));
}

int SearchResultTreeModel::rowCount(const QModelIndex &parent) const
{
    if (parent.column() > 0)
        return 0;

    const SearchResultTreeItem *parentItem;

    if (!parent.isValid())
        parentItem = m_rootItem;
    else
        parentItem = treeItemAtIndex(parent);

    return parentItem->childrenCount();
}

int SearchResultTreeModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 1;
}

SearchResultTreeItem *SearchResultTreeModel::treeItemAtIndex(const QModelIndex &idx)
{
    return static_cast<SearchResultTreeItem*>(idx.internalPointer());
}

QVariant SearchResultTreeModel::data(const QModelIndex &idx, int role) const
{
    if (!idx.isValid())
        return QVariant();

    QVariant result;

    if (role == Qt::SizeHintRole) {
        int height = QApplication::fontMetrics().height();
        if (m_editorFontIsUsed) {
            const int editorFontHeight = QFontMetrics(m_textEditorFont).height();
            height = qMax(height, editorFontHeight);
        }
        result = QSize(0, height);
    } else {
        result = data(treeItemAtIndex(idx), role);
    }

    return result;
}

bool SearchResultTreeModel::setData(const QModelIndex &idx, const QVariant &value, int role)
{
    if (role == Qt::CheckStateRole) {
        auto checkState = static_cast<Qt::CheckState>(value.toInt());
        return setCheckState(idx, checkState);
    }
    return QAbstractItemModel::setData(idx, value, role);
}

bool SearchResultTreeModel::setCheckState(const QModelIndex &idx, Qt::CheckState checkState, bool firstCall)
{
    SearchResultTreeItem *item = treeItemAtIndex(idx);
    if (item->checkState() == checkState)
        return false;
    item->setCheckState(checkState);
    if (firstCall) {
        emit dataChanged(idx, idx);
        updateCheckStateFromChildren(idx.parent(), item->parent());
    }
    // check children
    if (int children = item->childrenCount()) {
        for (int i = 0; i < children; ++i)
            setCheckState(index(i, 0, idx), checkState, false);
        emit dataChanged(index(0, 0, idx), index(children-1, 0, idx));
    }
    return true;
}

void SearchResultTreeModel::updateCheckStateFromChildren(const QModelIndex &idx,
                                                         SearchResultTreeItem *item)
{
    if (!item || item == m_rootItem)
        return;

    bool hasChecked = false;
    bool hasUnchecked = false;
    for (int i = 0; i < item->childrenCount(); ++i) {
        SearchResultTreeItem *child = item->childAt(i);
        if (child->checkState() == Qt::Checked)
            hasChecked = true;
        else if (child->checkState() == Qt::Unchecked)
            hasUnchecked = true;
        else if (child->checkState() == Qt::PartiallyChecked)
            hasChecked = hasUnchecked = true;
    }
    if (hasChecked && hasUnchecked)
        item->setCheckState(Qt::PartiallyChecked);
    else if (hasChecked)
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);
    emit dataChanged(idx, idx);

    updateCheckStateFromChildren(idx.parent(), item->parent());
}

void setDataInternal(const QModelIndex &index, const QVariant &value, int role);

QVariant SearchResultTreeModel::data(const SearchResultTreeItem *row, int role) const
{
    QVariant result;

    switch (role)
    {
    case Qt::CheckStateRole:
        result = row->checkState();
        break;
    case Qt::ToolTipRole:
        result = row->item.lineText().trimmed();
        break;
    case Qt::FontRole:
        if (row->item.useTextEditorFont())
            result = m_textEditorFont;
        else
            result = QVariant();
        break;
    case Qt::ForegroundRole:
        result = m_colors.value(row->item.style()).textForeground;
        break;
    case Qt::BackgroundRole:
        result = m_colors.value(row->item.style()).textBackground;
        break;
    case ItemDataRoles::ResultLineRole:
        result = row->item.lineText();
        break;
    case Qt::DisplayRole:
        if (m_relativePaths && row->isGenerated()) {
            result = ICore::pathRelativeToActiveProject(FilePath::fromUserInput(row->item.lineText())).toUserOutput();
        } else {
            result = row->item.lineText();
        }
        break;
    case ItemDataRoles::ResultItemRole:
        result = QVariant::fromValue(row->item);
        break;
    case ItemDataRoles::ResultBeginLineNumberRole:
        result = row->item.mainRange().begin.line;
        break;
    case ItemDataRoles::ResultIconRole:
        result = row->item.icon();
        break;
    case ItemDataRoles::ResultHighlightBackgroundColor:
        result = m_colors.value(row->item.style()).highlightBackground;
        break;
    case ItemDataRoles::ResultHighlightForegroundColor:
        result = m_colors.value(row->item.style()).highlightForeground;
        break;
    case ItemDataRoles::FunctionHighlightBackgroundColor:
        result = m_colors.value(row->item.style()).containingFunctionBackground;
        break;
    case ItemDataRoles::FunctionHighlightForegroundColor:
        result = m_colors.value(row->item.style()).containingFunctionForeground;
        break;
    case ItemDataRoles::ResultBeginColumnNumberRole:
        result = row->item.mainRange().begin.column;
        break;
    case ItemDataRoles::SearchTermLengthRole:{
        Text::Range range = row->item.mainRange();
        range.end.line -= range.begin.line - 1;
        range.begin.line = 1;
        result = range.length(row->item.lineText());
        break;
    }
    case ItemDataRoles::ContainingFunctionNameRole:
        result = row->item.containingFunctionName().value_or(QString{});
        break;
    case ItemDataRoles::IsGeneratedRole:
        result = row->isGenerated();
        break;
    default:
        result = QVariant();
        break;
    }

    return result;
}

QVariant SearchResultTreeModel::headerData(int section, Qt::Orientation orientation,
                                           int role) const
{
    Q_UNUSED(section)
    Q_UNUSED(orientation)
    Q_UNUSED(role)
    return QVariant();
}

/**
 * Makes sure that the nodes for a specific path exist and sets
 * m_currentParent to the last final
 */
QSet<SearchResultTreeItem *> SearchResultTreeModel::addPath(const QStringList &path)
{
    QSet<SearchResultTreeItem *> pathNodes;
    SearchResultTreeItem *currentItem = m_rootItem;
    QModelIndex currentItemIndex = QModelIndex();
    SearchResultTreeItem *partItem = nullptr;
    QStringList currentPath;
    for (const QString &part : path) {
        const int insertionIndex = currentItem->insertionIndex(part, &partItem);
        if (!partItem) {
            SearchResultItem item;
            item.setPath(currentPath);
            item.setLineText(part);
            partItem = new SearchResultTreeItem(item, currentItem);
            if (m_showReplaceUI)
                partItem->setCheckState(Qt::Checked);
            partItem->setGenerated(true);
            beginInsertRows(currentItemIndex, insertionIndex, insertionIndex);
            currentItem->insertChild(insertionIndex, partItem);
            endInsertRows();
        }
        pathNodes << partItem;
        currentItemIndex = index(insertionIndex, 0, currentItemIndex);
        currentItem = partItem;
        currentPath << part;
    }

    m_currentParent = currentItem;
    m_currentPath = currentPath;
    m_currentIndex = currentItemIndex;
    return pathNodes;
}

void SearchResultTreeModel::addResultsToCurrentParent(const SearchResultItems &items,
                                                      SearchResult::AddMode mode)
{
    if (!m_currentParent)
        return;

    if (mode == SearchResult::AddOrdered) {
        // this is the mode for e.g. text search
        beginInsertRows(m_currentIndex, m_currentParent->childrenCount(), m_currentParent->childrenCount() + items.count());
        for (const SearchResultItem &item : items) {
            m_currentParent->appendChild(item);
        }
        endInsertRows();
    } else {
        for (const SearchResultItem &item : items) {
            SearchResultTreeItem *existingItem;
            const int insertionIndex = m_currentParent->insertionIndex(item, &existingItem, mode);
            if (existingItem) {
                existingItem->setGenerated(false);
                existingItem->item = item;
                QModelIndex itemIndex = index(insertionIndex, 0, m_currentIndex);
                emit dataChanged(itemIndex, itemIndex);
            } else {
                beginInsertRows(m_currentIndex, insertionIndex, insertionIndex);
                m_currentParent->insertChild(insertionIndex, item);
                endInsertRows();
            }
        }
    }
    updateCheckStateFromChildren(index(m_currentParent), m_currentParent);
    emit dataChanged(m_currentIndex, m_currentIndex); // Make sure that the number after the file name gets updated
}

static bool lessThanByPath(const SearchResultItem &a, const SearchResultItem &b)
{
    if (a.path().size() < b.path().size())
        return true;
    if (a.path().size() > b.path().size())
        return false;
    for (int i = 0; i < a.path().size(); ++i) {
        if (a.path().at(i) < b.path().at(i))
            return true;
        if (a.path().at(i) > b.path().at(i))
            return false;
    }
    return false;
}

/**
 * Adds the search result to the list of results, creating nodes for the path when
 * necessary.
 */
QList<QModelIndex> SearchResultTreeModel::addResults(const SearchResultItems &items, SearchResult::AddMode mode)
{
    QSet<SearchResultTreeItem *> pathNodes;
    SearchResultItems sortedItems = items;
    std::stable_sort(sortedItems.begin(), sortedItems.end(), lessThanByPath);
    SearchResultItems itemSet;
    for (const SearchResultItem &item : sortedItems) {
        m_editorFontIsUsed |= item.useTextEditorFont();
        if (!m_currentParent || (m_currentPath != item.path())) {
            // first add all the items from before
            if (!itemSet.isEmpty()) {
                addResultsToCurrentParent(itemSet, mode);
                itemSet.clear();
            }
            // switch parent
            pathNodes += addPath(item.path());
        }
        itemSet << item;
    }
    if (!itemSet.isEmpty()) {
        addResultsToCurrentParent(itemSet, mode);
        itemSet.clear();
    }
    QList<QModelIndex> pathIndices;
    for (SearchResultTreeItem *item : std::as_const(pathNodes))
        pathIndices << index(item);
    return pathIndices;
}

void SearchResultTreeModel::clear()
{
    beginResetModel();
    m_currentParent = nullptr;
    m_rootItem->clearChildren();
    m_editorFontIsUsed = false;
    endResetModel();
}

QModelIndex SearchResultTreeModel::nextIndex(const QModelIndex &idx, bool *wrapped) const
{
    // pathological
    if (!idx.isValid())
        return index(0, 0);

    if (rowCount(idx) > 0) {
        // node with children
        return index(0, 0, idx);
    }
    // leaf node
    QModelIndex nextIndex;
    QModelIndex current = idx;
    while (!nextIndex.isValid()) {
        int row = current.row();
        current = current.parent();
        if (row + 1 < rowCount(current)) {
            // Same parent has another child
            nextIndex = index(row + 1, 0, current);
        } else {
            // go up one parent
            if (!current.isValid()) {
                // we start from the beginning
                if (wrapped)
                    *wrapped = true;
                nextIndex = index(0, 0);
            }
        }
    }
    return nextIndex;
}

QModelIndex SearchResultTreeModel::next(const QModelIndex &idx, bool includeGenerated, bool *wrapped) const
{
    QModelIndex value = idx;
    do {
        value = nextIndex(value, wrapped);
    } while (value != idx && !includeGenerated && treeItemAtIndex(value)->isGenerated());
    return value;
}

QModelIndex SearchResultTreeModel::prevIndex(const QModelIndex &idx, bool *wrapped) const
{
    QModelIndex current = idx;
    bool checkForChildren = true;
    if (current.isValid()) {
        int row = current.row();
        if (row > 0) {
            current = index(row - 1, 0, current.parent());
        } else {
            current = current.parent();
            checkForChildren = !current.isValid();
            if (checkForChildren && wrapped) {
                // we start from the end
                *wrapped = true;
            }
        }
    }
    if (checkForChildren) {
        // traverse down the hierarchy
        while (int rc = rowCount(current)) {
            current = index(rc - 1, 0, current);
        }
    }
    return current;
}

QModelIndex SearchResultTreeModel::prev(const QModelIndex &idx, bool includeGenerated, bool *wrapped) const
{
    QModelIndex value = idx;
    do {
        value = prevIndex(value, wrapped);
    } while (value != idx && !includeGenerated && treeItemAtIndex(value)->isGenerated());
    return value;
}

SearchResultFilterModel::SearchResultFilterModel(QObject *parent) : QSortFilterProxyModel(parent)
{
    setSourceModel(new SearchResultTreeModel(this));
}

void SearchResultFilterModel::setFilter(SearchResultFilter *filter)
{
    if (m_filter)
        m_filter->disconnect(this);
    m_filter = filter;
    if (m_filter) {
        connect(m_filter, &SearchResultFilter::filterChanged,
                this, [this] {
            invalidateFilter();
            emit filterInvalidated();
        });
    }
    invalidateFilter();
}

void SearchResultFilterModel::setShowReplaceUI(bool show)
{
    sourceModel()->setShowReplaceUI(show);
}

void SearchResultFilterModel::setRelativePaths(bool relative)
{
    sourceModel()->setRelativePaths(relative);
}

void SearchResultFilterModel::setTextEditorFont(const QFont &font, const SearchResultColors &colors)
{
    sourceModel()->setTextEditorFont(font, colors);
}

QList<QModelIndex> SearchResultFilterModel::addResults(const SearchResultItems &items,
                                                       SearchResult::AddMode mode)
{
    QList<QModelIndex> sourceIndexes = sourceModel()->addResults(items, mode);
    sourceIndexes = Utils::filtered(sourceIndexes, [this](const QModelIndex &idx) {
        return filterAcceptsRow(idx.row(), idx.parent());
    });
    return Utils::transform(sourceIndexes,
                            [this](const QModelIndex &idx) { return mapFromSource(idx); });
}

void SearchResultFilterModel::clear()
{
    sourceModel()->clear();
}

QModelIndex SearchResultFilterModel::nextOrPrev(const QModelIndex &idx, bool *wrapped,
        const std::function<QModelIndex (const QModelIndex &)> &func) const
{
    if (wrapped)
        *wrapped = false;
    const QModelIndex sourceIndex = mapToSource(idx);
    QModelIndex nextOrPrevSourceIndex = func(sourceIndex);
    while (nextOrPrevSourceIndex != sourceIndex
           && !filterAcceptsRow(nextOrPrevSourceIndex.row(), nextOrPrevSourceIndex.parent())) {
        nextOrPrevSourceIndex = func(nextOrPrevSourceIndex);
    }
    return mapFromSource(nextOrPrevSourceIndex);
}

QModelIndex SearchResultFilterModel::next(const QModelIndex &idx, bool includeGenerated,
                                          bool *wrapped) const
{
    return nextOrPrev(idx, wrapped, [this, includeGenerated, wrapped](const QModelIndex &index) {
        return sourceModel()->next(index, includeGenerated, wrapped); });
}

QModelIndex SearchResultFilterModel::prev(const QModelIndex &idx, bool includeGenerated,
                                          bool *wrapped) const
{
    return nextOrPrev(idx, wrapped, [this, includeGenerated, wrapped](const QModelIndex &index) {
        return sourceModel()->prev(index, includeGenerated, wrapped); });
}

SearchResultTreeItem *SearchResultFilterModel::itemForIndex(const QModelIndex &index) const
{
    return static_cast<SearchResultTreeItem *>(mapToSource(index).internalPointer());
}

bool SearchResultFilterModel::filterAcceptsRow(int source_row,
                                               const QModelIndex &source_parent) const
{
    const QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);
    const SearchResultTreeItem * const item = SearchResultTreeModel::treeItemAtIndex(idx);
    if (!item)
        return false;
    if (!m_filter)
        return true;
    if (item->item.userData().isValid())
        return m_filter->matches(item->item);
    const int childCount = sourceModel()->rowCount(idx);
    for (int i = 0; i < childCount; ++i) {
        if (filterAcceptsRow(i, idx))
            return true;
    }
    return false;
}

SearchResultTreeModel *SearchResultFilterModel::sourceModel() const
{
    return static_cast<SearchResultTreeModel *>(QSortFilterProxyModel::sourceModel());
}

} // namespace Internal
} // namespace Core

#include <searchresulttreemodel.moc>