summaryrefslogtreecommitdiffstats
path: root/src/qtreeselectionmanager.cpp
blob: 96f4a374837ec27d94a1d7fae69b32129b83154b (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** GNU Lesser General Public License Usage
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qtreeselectionmanager.h"
#include "qtreeselectionmanager_p.h"

QT_BEGIN_NAMESPACE

QtTreeSelectionManagerPrivate::QtTreeSelectionManagerPrivate()
    : q_ptr(0), model(0), mode(QtTreeSelectionManager::Select)
{
}

QtTreeSelectionManagerPrivate::~QtTreeSelectionManagerPrivate()
{
}

void QtTreeSelectionManagerPrivate::_q_modelDestroyed()
{
}

void QtTreeSelectionManagerPrivate::_q_itemsRemoved(const QtTreeModelBase::iterator_base &it, int count)
{
    QtTreeModelBase::iterator_base it2 = it;
    for (int i = 0; i < count && it.isValid(); ++i) {
        selections.remove(it2);
        it2.nextSibling();
    }
}

void QtTreeSelectionManagerPrivate::_q_reset()
{
}

/*!
  \class QtTreeSelectionManager
  \brief A tree selection manager
*/

QtTreeSelectionManager::QtTreeSelectionManager(QObject *parent)
    : QObject(parent), d_ptr(new QtTreeSelectionManagerPrivate)
{
    d_ptr->q_ptr = this;
}

QtTreeSelectionManager::QtTreeSelectionManager(QtTreeSelectionManagerPrivate &dd, QObject *parent)
    : QObject(parent), d_ptr(&dd)
{
    d_ptr->q_ptr = this;
}

QtTreeSelectionManager::~QtTreeSelectionManager()
{
    delete d_ptr;
}

void QtTreeSelectionManager::setCurrentItem(const QtTreeModelIterator &it)
{
    Q_D(QtTreeSelectionManager);
    QtTreeModelIterator previous = d->currentItem;
    d->currentItem = it;
    emit currentChanged(it, previous);
}

QtTreeModelIterator QtTreeSelectionManager::currentItem() const
{
    Q_D(const QtTreeSelectionManager);
    return d->currentItem;
}

void QtTreeSelectionManager::setAnchorItem(const QtTreeModelIterator &it)
{
    Q_D(QtTreeSelectionManager);
    QtTreeModelIterator previous = d->anchorItem;
    d->anchorItem = it;
    emit anchorChanged(it, previous);
}

QtTreeModelIterator QtTreeSelectionManager::anchorItem() const
{
    Q_D(const QtTreeSelectionManager);
    return d->anchorItem;
}

QtTreeSelectionManager::SelectionMode QtTreeSelectionManager::anchorSelectionMode() const
{
    Q_D(const QtTreeSelectionManager);
    return d->mode;
}

void QtTreeSelectionManager::setAnchorSelectionMode(SelectionMode mode)
{
    Q_D(QtTreeSelectionManager);
    if (d->mode != mode) {
        d->mode = mode;
        if (!d->anchorSelections.isEmpty())
            emit selectionsChanged(d->anchorSelections);
    }
}

void QtTreeSelectionManager::setSelected(const QtTreeSelection &selection, SelectionMode mode)
{
    Q_D(QtTreeSelectionManager);
    const QtTreeSelection previous = d->selections;
    switch (mode) {
    case Select:
        d->selections += selection;
        break;
    case Deselect:
        d->selections -= selection;
        break;
    case Toggle: { // ### optimize
            QtTreeSelection intersection = d->selections & selection;
            d->selections -= intersection;
            d->selections += (selection - intersection);
        }
    default:
        break;
    }
    emit selectionsChanged(previous + selection);
}

void QtTreeSelectionManager::setAnchorSelection(const QtTreeSelection &selection)
{
    Q_D(QtTreeSelectionManager);
    const QtTreeSelection previous = d->anchorSelections;
    d->anchorSelections = selection;
    emit selectionsChanged(previous + selection);
}

bool QtTreeSelectionManager::isSelected(const QtTreeModelBase::iterator_base &it) const
{
    Q_D(const QtTreeSelectionManager);
    if (d->anchorSelections.contains(it)) {
        switch (d->mode) {
        case Select:
            return true;
        case Deselect:
            return false;
        case Toggle:
            return !d->selections.contains(it);
        default:
            break;
        }
    }
    return d->selections.contains(it);
}

void QtTreeSelectionManager::clearSelections()
{
    Q_D(QtTreeSelectionManager);
    QtTreeSelection changed = d->selections + d->anchorSelections;
    d->selections.clear();
    d->anchorSelections.clear();
    emit selectionsChanged(changed);
}

QT_END_NAMESPACE

#include "moc_qtreeselectionmanager.cpp"