searchresulttreeitems.cpp 3.93 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32 33
#include "searchresulttreeitems.h"

using namespace Find::Internal;

34 35 36 37 38 39 40
SearchResultTreeItem::SearchResultTreeItem(const SearchResultItem &item,
                                           const SearchResultTreeItem *parent)
  : item(item),
  m_parent(parent),
  m_isUserCheckable(false),
  m_checkState(Qt::Unchecked),
  m_isGenerated(false)
con's avatar
con committed
41 42 43 44 45 46 47 48
{
}

SearchResultTreeItem::~SearchResultTreeItem()
{
    clearChildren();
}

49 50 51 52 53
bool SearchResultTreeItem::isLeaf() const
{
    return childrenCount() == 0 && parent() != 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
bool SearchResultTreeItem::isUserCheckable() const
{
    return m_isUserCheckable;
}

void SearchResultTreeItem::setIsUserCheckable(bool isUserCheckable)
{
    m_isUserCheckable = isUserCheckable;
}

Qt::CheckState SearchResultTreeItem::checkState() const
{
    return m_checkState;
}

void SearchResultTreeItem::setCheckState(Qt::CheckState checkState)
{
    m_checkState = checkState;
}

Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
74
void SearchResultTreeItem::clearChildren()
con's avatar
con committed
75 76 77 78 79
{
    qDeleteAll(m_children);
    m_children.clear();
}

Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
80
int SearchResultTreeItem::childrenCount() const
con's avatar
con committed
81 82 83 84
{
    return m_children.count();
}

Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
85
int SearchResultTreeItem::rowOfItem() const
con's avatar
con committed
86
{
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
87
    return (m_parent ? m_parent->m_children.indexOf(const_cast<SearchResultTreeItem*>(this)):0);
con's avatar
con committed
88 89
}

Roberto Raggi's avatar
Roberto Raggi committed
90
SearchResultTreeItem* SearchResultTreeItem::childAt(int index) const
con's avatar
con committed
91 92 93 94
{
    return m_children.at(index);
}

Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
95
const SearchResultTreeItem *SearchResultTreeItem::parent() const
con's avatar
con committed
96 97 98 99
{
    return m_parent;
}

100
static bool lessThanByText(SearchResultTreeItem *a, const QString &b)
con's avatar
con committed
101
{
102
    return a->item.text < b;
con's avatar
con committed
103 104
}

105
int SearchResultTreeItem::insertionIndex(const QString &text, SearchResultTreeItem **existingItem) const
con's avatar
con committed
106
{
107 108 109 110 111 112 113 114 115 116
    QList<SearchResultTreeItem *>::const_iterator insertionPosition =
            qLowerBound(m_children.begin(), m_children.end(), text, lessThanByText);
    if (existingItem) {
        if (insertionPosition != m_children.end() && (*insertionPosition)->item.text == text) {
            (*existingItem) = (*insertionPosition);
        } else {
            *existingItem = 0;
        }
    }
    return insertionPosition - m_children.begin();
con's avatar
con committed
117 118
}

119
int SearchResultTreeItem::insertionIndex(const SearchResultItem &item, SearchResultTreeItem **existingItem) const
con's avatar
con committed
120
{
121
    return insertionIndex(item.text, existingItem);
con's avatar
con committed
122 123
}

124
void SearchResultTreeItem::insertChild(int index, SearchResultTreeItem *child)
con's avatar
con committed
125
{
126
    m_children.insert(index, child);
con's avatar
con committed
127 128
}

129
void SearchResultTreeItem::insertChild(int index, const SearchResultItem &item)
con's avatar
con committed
130
{
131
    SearchResultTreeItem *child = new SearchResultTreeItem(item, this);
Roberto Raggi's avatar
Roberto Raggi committed
132 133 134 135
    if (isUserCheckable()) {
        child->setIsUserCheckable(true);
        child->setCheckState(Qt::Checked);
    }
136 137 138 139 140 141
    insertChild(index, child);
}

void SearchResultTreeItem::appendChild(const SearchResultItem &item)
{
    insertChild(m_children.count(), item);
con's avatar
con committed
142
}