1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "client-export.h"
25 #include <QItemSelectionModel>
28 class QAbstractItemModel;
30 class CLIENT_EXPORT SelectionModelSynchronizer : public QObject
35 SelectionModelSynchronizer(QAbstractItemModel* parent = nullptr);
37 void synchronizeSelectionModel(QItemSelectionModel* selectionModel);
38 void removeSelectionModel(QItemSelectionModel* selectionModel);
40 inline QAbstractItemModel* model() { return _model; }
41 inline QItemSelectionModel* selectionModel() const { return const_cast<QItemSelectionModel*>(&_selectionModel); }
42 inline QModelIndex currentIndex() const { return _selectionModel.currentIndex(); }
43 inline QItemSelection currentSelection() const { return _selectionModel.selection(); }
46 void syncedCurrentChanged(const QModelIndex& current, const QModelIndex& previous);
47 void syncedSelectionChanged(const QItemSelection& selected, const QItemSelection& previous);
49 void setCurrentIndex(const QModelIndex& index);
50 void setCurrentSelection(const QItemSelection& selection);
52 void currentChanged(const QModelIndex& current, const QModelIndex& previous);
53 void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
55 void selectionModelDestroyed(QObject* object);
58 QAbstractItemModel* _model;
59 QItemSelectionModel _selectionModel;
60 bool _changeCurrentEnabled{true};
61 bool _changeSelectionEnabled{true};
63 bool checkBaseModel(QItemSelectionModel* model);
64 QModelIndex mapFromSource(const QModelIndex& sourceIndex, const QItemSelectionModel* selectionModel);
65 QItemSelection mapSelectionFromSource(const QItemSelection& sourceSelection, const QItemSelectionModel* selectionModel);
66 QModelIndex mapToSource(const QModelIndex& index, QItemSelectionModel* selectionModel);
67 QItemSelection mapSelectionToSource(const QItemSelection& selection, QItemSelectionModel* selectionModel);
69 QSet<QItemSelectionModel*> _selectionModels;