File indexing completed on 2024-03-24 15:26:01

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KVIEWSTATEMAINTAINER_H
0009 #define KVIEWSTATEMAINTAINER_H
0010 
0011 #include <KViewStateMaintainerBase>
0012 
0013 #include <QAbstractItemView>
0014 
0015 #include <KConfigGroup>
0016 
0017 /**
0018  * @class KViewStateMaintainer kviewstatemaintainer.h KViewStateMaintainer
0019  *
0020  * @brief Encapsulates the maintenance of state between resets of QAbstractItemModel
0021  *
0022  * @code
0023  *   m_collectionViewStateMaintainer = new KViewStateMaintainer<Akonadi::ETMViewStateSaver>(KSharedConfig::openConfig()->group("collectionView"));
0024  *   m_collectionViewStateMaintainer->setView(m_collectionView);
0025  *
0026  *   m_collectionCheckStateMaintainer = new KViewStateMaintainer<Akonadi::ETMViewStateSaver>(KSharedConfig::openConfig()->group("collectionCheckState"));
0027  *   m_collectionCheckStateMaintainer->setSelectionModel(m_checkableProxy->selectionModel());
0028  * @endcode
0029  *
0030  * @see KConfigViewStateSaver
0031  */
0032 template<typename StateSaver>
0033 class KViewStateMaintainer : public KViewStateMaintainerBase
0034 {
0035     typedef StateSaver StateRestorer;
0036 
0037 public:
0038     KViewStateMaintainer(const KConfigGroup &configGroup, QObject *parent = nullptr)
0039         : KViewStateMaintainerBase(parent)
0040         , m_configGroup(configGroup)
0041     {
0042     }
0043 
0044     /* reimp */ void saveState()
0045     {
0046         StateSaver saver;
0047         saver.setView(view());
0048         saver.setSelectionModel(selectionModel());
0049         saver.saveState(m_configGroup);
0050         m_configGroup.sync();
0051     }
0052 
0053     /* reimp */ void restoreState()
0054     {
0055         StateRestorer *restorer = new StateRestorer;
0056         restorer->setView(view());
0057         restorer->setSelectionModel(selectionModel());
0058         restorer->restoreState(m_configGroup);
0059     }
0060 
0061 private:
0062     KConfigGroup m_configGroup;
0063 };
0064 
0065 #endif