1 | /* |
2 | SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> |
3 | SPDX-FileContributor: Stephen Kelly <stephen@kdab.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #include "kviewstatemaintainerbase.h" |
9 | |
10 | #include <QAbstractItemView> |
11 | #include <QItemSelectionModel> |
12 | #include <QPointer> |
13 | |
14 | class KViewStateMaintainerBasePrivate |
15 | { |
16 | KViewStateMaintainerBasePrivate(KViewStateMaintainerBase *qq) |
17 | : q_ptr(qq) |
18 | { |
19 | } |
20 | |
21 | Q_DECLARE_PUBLIC(KViewStateMaintainerBase) |
22 | KViewStateMaintainerBase *const q_ptr; |
23 | |
24 | void slotModelAboutToBeReset(); |
25 | void slotModelReset(); |
26 | |
27 | QPointer<QAbstractItemView> m_view; |
28 | QMetaObject::Connection m_viewModelAboutToBeResetConnection; |
29 | QMetaObject::Connection m_viewModelResetConnection; |
30 | |
31 | QPointer<QItemSelectionModel> m_selectionModel; |
32 | QMetaObject::Connection m_selectionModelAboutToBeResetConnection; |
33 | QMetaObject::Connection m_selectionModelResetConnection; |
34 | }; |
35 | |
36 | void KViewStateMaintainerBasePrivate::slotModelAboutToBeReset() |
37 | { |
38 | Q_Q(KViewStateMaintainerBase); |
39 | q->saveState(); |
40 | } |
41 | |
42 | void KViewStateMaintainerBasePrivate::slotModelReset() |
43 | { |
44 | Q_Q(KViewStateMaintainerBase); |
45 | q->restoreState(); |
46 | } |
47 | |
48 | KViewStateMaintainerBase::KViewStateMaintainerBase(QObject *parent) |
49 | : QObject(parent) |
50 | , d_ptr(new KViewStateMaintainerBasePrivate(this)) |
51 | { |
52 | } |
53 | |
54 | KViewStateMaintainerBase::~KViewStateMaintainerBase() = default; |
55 | |
56 | QItemSelectionModel *KViewStateMaintainerBase::selectionModel() const |
57 | { |
58 | Q_D(const KViewStateMaintainerBase); |
59 | if (d->m_selectionModel.isNull()) { |
60 | return nullptr; |
61 | } |
62 | return d->m_selectionModel.data(); |
63 | } |
64 | |
65 | void KViewStateMaintainerBase::setSelectionModel(QItemSelectionModel *selectionModel) |
66 | { |
67 | Q_D(KViewStateMaintainerBase); |
68 | if (selectionModel == d->m_selectionModel) { |
69 | return; |
70 | } |
71 | |
72 | d->m_selectionModel = selectionModel; |
73 | |
74 | if (d->m_view && d->m_view->model()) { |
75 | disconnect(d->m_selectionModelAboutToBeResetConnection); |
76 | disconnect(d->m_selectionModelResetConnection); |
77 | } |
78 | |
79 | d->m_viewModelAboutToBeResetConnection = connect(sender: d->m_selectionModel->model(), signal: &QAbstractItemModel::modelAboutToBeReset, context: this, slot: [d]() { |
80 | d->slotModelAboutToBeReset(); |
81 | }); |
82 | d->m_viewModelResetConnection = connect(sender: d->m_selectionModel->model(), signal: &QAbstractItemModel::modelReset, context: this, slot: [d]() { |
83 | d->slotModelReset(); |
84 | }); |
85 | } |
86 | |
87 | QAbstractItemView *KViewStateMaintainerBase::view() const |
88 | { |
89 | Q_D(const KViewStateMaintainerBase); |
90 | if (d->m_view.isNull()) { |
91 | return nullptr; |
92 | } |
93 | return d->m_view.data(); |
94 | } |
95 | |
96 | void KViewStateMaintainerBase::setView(QAbstractItemView *view) |
97 | { |
98 | Q_D(KViewStateMaintainerBase); |
99 | if (view == d->m_view) { |
100 | return; |
101 | } |
102 | |
103 | d->m_view = view; |
104 | |
105 | if (d->m_selectionModel && d->m_selectionModel->model()) { |
106 | disconnect(d->m_viewModelAboutToBeResetConnection); |
107 | disconnect(d->m_viewModelResetConnection); |
108 | } |
109 | |
110 | if (d->m_view && d->m_view->model()) { |
111 | d->m_selectionModelAboutToBeResetConnection = connect(sender: d->m_view->model(), signal: &QAbstractItemModel::modelAboutToBeReset, context: this, slot: [d]() { |
112 | d->slotModelAboutToBeReset(); |
113 | }); |
114 | d->m_selectionModelResetConnection = connect(sender: d->m_view->model(), signal: &QAbstractItemModel::modelReset, context: this, slot: [d]() { |
115 | d->slotModelReset(); |
116 | }); |
117 | } |
118 | } |
119 | |
120 | #include "moc_kviewstatemaintainerbase.cpp" |
121 | |