~ubuntu-branches/ubuntu/trusty/quassel/trusty-updates

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
/***************************************************************************
 *   Copyright (C) 2005-2014 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "selectionmodelsynchronizer.h"

#include <QAbstractItemModel>
#include <QAbstractProxyModel>

#include <QDebug>

SelectionModelSynchronizer::SelectionModelSynchronizer(QAbstractItemModel *parent)
    : QObject(parent),
    _model(parent),
    _selectionModel(parent),
    _changeCurrentEnabled(true),
    _changeSelectionEnabled(true)
{
    connect(&_selectionModel, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
        this, SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
    connect(&_selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
        this, SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
}


bool SelectionModelSynchronizer::checkBaseModel(QItemSelectionModel *selectionModel)
{
    if (!selectionModel)
        return false;

    const QAbstractItemModel *baseModel = selectionModel->model();
    const QAbstractProxyModel *proxyModel = 0;
    while ((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
        baseModel = proxyModel->sourceModel();
        if (baseModel == model())
            break;
    }
    return baseModel == model();
}


void SelectionModelSynchronizer::synchronizeSelectionModel(QItemSelectionModel *selectionModel)
{
    if (!checkBaseModel(selectionModel)) {
        qWarning() << "cannot Synchronize SelectionModel" << selectionModel << "which has a different baseModel()";
        return;
    }

    if (_selectionModels.contains(selectionModel)) {
        selectionModel->setCurrentIndex(mapFromSource(currentIndex(), selectionModel), QItemSelectionModel::Current);
        selectionModel->select(mapSelectionFromSource(currentSelection(), selectionModel), QItemSelectionModel::ClearAndSelect);
        return;
    }

    connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
        this, SLOT(syncedCurrentChanged(QModelIndex, QModelIndex)));
    connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
        this, SLOT(syncedSelectionChanged(QItemSelection, QItemSelection)));

    connect(selectionModel, SIGNAL(destroyed(QObject *)), this, SLOT(selectionModelDestroyed(QObject *)));

    _selectionModels << selectionModel;
}


void SelectionModelSynchronizer::removeSelectionModel(QItemSelectionModel *model)
{
    disconnect(model, 0, this, 0);
    disconnect(this, 0, model, 0);
    selectionModelDestroyed(model);
}


void SelectionModelSynchronizer::selectionModelDestroyed(QObject *object)
{
    QItemSelectionModel *model = static_cast<QItemSelectionModel *>(object);
    QSet<QItemSelectionModel *>::iterator iter = _selectionModels.begin();
    while (iter != _selectionModels.end()) {
        if (*iter == model) {
            iter = _selectionModels.erase(iter);
        }
        else {
            iter++;
        }
    }
}


void SelectionModelSynchronizer::syncedCurrentChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous);

    if (!_changeCurrentEnabled)
        return;

    QItemSelectionModel *selectionModel = qobject_cast<QItemSelectionModel *>(sender());
    Q_ASSERT(selectionModel);
    QModelIndex newSourceCurrent = mapToSource(current, selectionModel);
    if (newSourceCurrent.isValid() && newSourceCurrent != currentIndex())
        setCurrentIndex(newSourceCurrent);
}


void SelectionModelSynchronizer::syncedSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    Q_UNUSED(selected);
    Q_UNUSED(deselected);

    if (!_changeSelectionEnabled)
        return;

    QItemSelectionModel *selectionModel = qobject_cast<QItemSelectionModel *>(sender());
    Q_ASSERT(selectionModel);

    QItemSelection mappedSelection = selectionModel->selection();
    QItemSelection currentSelectionMapped = mapSelectionFromSource(currentSelection(), selectionModel);

    QItemSelection checkSelection = currentSelectionMapped;
    checkSelection.merge(mappedSelection, QItemSelectionModel::Deselect);
    if (checkSelection.isEmpty()) {
        // that means the new selection contains the current selection (currentSel - newSel = {})
        checkSelection = mappedSelection;
        checkSelection.merge(currentSelectionMapped, QItemSelectionModel::Deselect);
        if (checkSelection.isEmpty()) {
            // that means the current selection contains the new selection (newSel - currentSel = {})
            // -> currentSel == newSel
            return;
        }
    }
    setCurrentSelection(mapSelectionToSource(mappedSelection, selectionModel));
}


QModelIndex SelectionModelSynchronizer::mapFromSource(const QModelIndex &sourceIndex, const QItemSelectionModel *selectionModel)
{
    Q_ASSERT(selectionModel);

    QModelIndex mappedIndex = sourceIndex;

    // make a list of all involved proxies, wie have to traverse backwards
    QList<const QAbstractProxyModel *> proxyModels;
    const QAbstractItemModel *baseModel = selectionModel->model();
    const QAbstractProxyModel *proxyModel = 0;
    while ((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
        if (baseModel == model())
            break;
        proxyModels << proxyModel;
        baseModel = proxyModel->sourceModel();
    }

    // now traverse it;
    for (int i = proxyModels.count() - 1; i >= 0; i--) {
        mappedIndex = proxyModels[i]->mapFromSource(mappedIndex);
    }

    return mappedIndex;
}


QItemSelection SelectionModelSynchronizer::mapSelectionFromSource(const QItemSelection &sourceSelection, const QItemSelectionModel *selectionModel)
{
    Q_ASSERT(selectionModel);

    QItemSelection mappedSelection = sourceSelection;

    // make a list of all involved proxies, wie have to traverse backwards
    QList<const QAbstractProxyModel *> proxyModels;
    const QAbstractItemModel *baseModel = selectionModel->model();
    const QAbstractProxyModel *proxyModel = 0;
    while ((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
        if (baseModel == model())
            break;
        proxyModels << proxyModel;
        baseModel = proxyModel->sourceModel();
    }

    // now traverse it;
    for (int i = proxyModels.count() - 1; i >= 0; i--) {
        mappedSelection = proxyModels[i]->mapSelectionFromSource(mappedSelection);
    }
    return mappedSelection;
}


QModelIndex SelectionModelSynchronizer::mapToSource(const QModelIndex &index, QItemSelectionModel *selectionModel)
{
    Q_ASSERT(selectionModel);

    QModelIndex sourceIndex = index;
    const QAbstractItemModel *baseModel = selectionModel->model();
    const QAbstractProxyModel *proxyModel = 0;
    while ((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
        sourceIndex = proxyModel->mapToSource(sourceIndex);
        baseModel = proxyModel->sourceModel();
        if (baseModel == model())
            break;
    }
    return sourceIndex;
}


QItemSelection SelectionModelSynchronizer::mapSelectionToSource(const QItemSelection &selection, QItemSelectionModel *selectionModel)
{
    Q_ASSERT(selectionModel);

    QItemSelection sourceSelection = selection;
    const QAbstractItemModel *baseModel = selectionModel->model();
    const QAbstractProxyModel *proxyModel = 0;
    while ((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
        sourceSelection = proxyModel->mapSelectionToSource(sourceSelection);
        baseModel = proxyModel->sourceModel();
        if (baseModel == model())
            break;
    }
    return sourceSelection;
}


void SelectionModelSynchronizer::setCurrentIndex(const QModelIndex &index)
{
    _selectionModel.setCurrentIndex(index, QItemSelectionModel::Current);
}


void SelectionModelSynchronizer::setCurrentSelection(const QItemSelection &selection)
{
    _selectionModel.select(selection, QItemSelectionModel::ClearAndSelect);
}


void SelectionModelSynchronizer::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous);

    _changeCurrentEnabled = false;
    QSet<QItemSelectionModel *>::iterator iter = _selectionModels.begin();
    while (iter != _selectionModels.end()) {
        (*iter)->setCurrentIndex(mapFromSource(current, (*iter)), QItemSelectionModel::Current);
        iter++;
    }
    _changeCurrentEnabled = true;
}


void SelectionModelSynchronizer::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    Q_UNUSED(selected);
    Q_UNUSED(deselected);

    _changeSelectionEnabled = false;
    QSet<QItemSelectionModel *>::iterator iter = _selectionModels.begin();
    while (iter != _selectionModels.end()) {
        (*iter)->select(mapSelectionFromSource(currentSelection(), (*iter)), QItemSelectionModel::ClearAndSelect);
        iter++;
    }
    _changeSelectionEnabled = true;
}