KWidgetsAddons

kviewstatemaintainerbase.cpp
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
14class 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
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
36void KViewStateMaintainerBasePrivate::slotModelAboutToBeReset()
37{
38 Q_Q(KViewStateMaintainerBase);
39 q->saveState();
40}
41
42void KViewStateMaintainerBasePrivate::slotModelReset()
43{
44 Q_Q(KViewStateMaintainerBase);
45 q->restoreState();
46}
47
48KViewStateMaintainerBase::KViewStateMaintainerBase(QObject *parent)
49 : QObject(parent)
50 , d_ptr(new KViewStateMaintainerBasePrivate(this))
51{
52}
53
54KViewStateMaintainerBase::~KViewStateMaintainerBase() = default;
55
56QItemSelectionModel *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
65void 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(d->m_selectionModel->model(), &QAbstractItemModel::modelAboutToBeReset, this, [d]() {
80 d->slotModelAboutToBeReset();
81 });
82 d->m_viewModelResetConnection = connect(d->m_selectionModel->model(), &QAbstractItemModel::modelReset, this, [d]() {
83 d->slotModelReset();
84 });
85}
86
87QAbstractItemView *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
96void 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(d->m_view->model(), &QAbstractItemModel::modelAboutToBeReset, this, [d]() {
112 d->slotModelAboutToBeReset();
113 });
114 d->m_selectionModelResetConnection = connect(d->m_view->model(), &QAbstractItemModel::modelReset, this, [d]() {
115 d->slotModelReset();
116 });
117 }
118}
119
120#include "moc_kviewstatemaintainerbase.cpp"
void modelAboutToBeReset()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.