File indexing completed on 2024-10-13 03:39: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 #include "statesaverwidget.h"
0009 
0010 #include <QApplication>
0011 #include <QSplitter>
0012 #include <QTreeView>
0013 
0014 #include <KConfig>
0015 #include <KConfigGroup>
0016 #include <ksharedconfig.h>
0017 
0018 #include "dynamictreemodel.h"
0019 #include "dynamictreewidget.h"
0020 
0021 QModelIndex DynamicTreeStateSaver::indexFromConfigString(const QAbstractItemModel *model, const QString &key) const
0022 {
0023     QModelIndexList list = model->match(model->index(0, 0), DynamicTreeModel::DynamicTreeModelId, key.toInt(), 1, Qt::MatchRecursive);
0024     if (list.isEmpty()) {
0025         return QModelIndex();
0026     }
0027     return list.first();
0028 }
0029 
0030 QString DynamicTreeStateSaver::indexToConfigString(const QModelIndex &index) const
0031 {
0032     return index.data(DynamicTreeModel::DynamicTreeModelId).toString();
0033 }
0034 
0035 DynamicTreeStateSaver::DynamicTreeStateSaver(QObject *parent)
0036     : KViewStateSaver(parent)
0037 {
0038 }
0039 
0040 StateSaverWidget::StateSaverWidget(QWidget *parent, Qt::WindowFlags f)
0041     : QWidget(parent, f)
0042 {
0043     QSplitter *splitter = new QSplitter(this);
0044     QHBoxLayout *layout = new QHBoxLayout(this);
0045     layout->addWidget(splitter);
0046 
0047     DynamicTreeModel *model = new DynamicTreeModel(this);
0048 
0049     DynamicTreeWidget *dynamicTreeWidget = new DynamicTreeWidget(model, splitter);
0050 
0051     m_view = new QTreeView(splitter);
0052     m_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
0053     m_view->setModel(model);
0054 
0055     connect(model, SIGNAL(modelAboutToBeReset()), SLOT(saveState()));
0056     connect(model, SIGNAL(modelReset()), SLOT(restoreState()));
0057     connect(qApp, SIGNAL(aboutToQuit()), SLOT(saveState()));
0058 
0059     restoreState();
0060 }
0061 
0062 StateSaverWidget::~StateSaverWidget()
0063 {
0064     saveState();
0065 }
0066 
0067 void StateSaverWidget::saveState()
0068 {
0069     DynamicTreeStateSaver saver;
0070     saver.setView(m_view);
0071 
0072     KConfigGroup cfg(KSharedConfig::openConfig(), "ExampleViewState");
0073     saver.saveState(cfg);
0074     cfg.sync();
0075 }
0076 
0077 void StateSaverWidget::restoreState()
0078 {
0079     DynamicTreeStateSaver *saver = new DynamicTreeStateSaver;
0080     saver->setView(m_view);
0081     KConfigGroup cfg(KSharedConfig::openConfig(), "ExampleViewState");
0082     saver->restoreState(cfg);
0083 }
0084 
0085 #include "moc_statesaverwidget.cpp"