File indexing completed on 2024-05-12 05:12:48

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "uistatesaver.h"
0008 
0009 #include <KConfigGroup>
0010 
0011 #include <QComboBox>
0012 #include <QHeaderView>
0013 #include <QSplitter>
0014 #include <QTabWidget>
0015 #include <QTreeView>
0016 
0017 using namespace AkonadiConsole;
0018 
0019 struct Saver {
0020     static void process(QSplitter *splitter, KConfigGroup &config)
0021     {
0022         if (splitter->sizes().count(0) == splitter->sizes().count()) {
0023             return;
0024         }
0025         config.writeEntry(splitter->objectName(), splitter->sizes());
0026     }
0027 
0028     static void process(QTabWidget *tab, KConfigGroup &config)
0029     {
0030         config.writeEntry(tab->objectName(), tab->currentIndex());
0031     }
0032 
0033     static void process(QTreeView *tv, KConfigGroup &config)
0034     {
0035         config.writeEntry(tv->objectName(), tv->header()->saveState());
0036     }
0037 
0038     static void process(QComboBox *cb, KConfigGroup &config)
0039     {
0040         config.writeEntry(cb->objectName(), cb->currentIndex());
0041     }
0042 };
0043 
0044 struct Restorer {
0045     static void process(QSplitter *splitter, const KConfigGroup &config)
0046     {
0047         const QList<int> sizes = config.readEntry(splitter->objectName(), QList<int>());
0048         if (!sizes.isEmpty() && splitter->count() == sizes.count() && sizes.count() != sizes.count(0)) {
0049             splitter->setSizes(sizes);
0050         }
0051     }
0052 
0053     static void process(QTabWidget *tab, const KConfigGroup &config)
0054     {
0055         const int index = config.readEntry(tab->objectName(), -1);
0056         if (index >= 0 && index < tab->count()) {
0057             tab->setCurrentIndex(index);
0058         }
0059     }
0060 
0061     static void process(QTreeView *tv, const KConfigGroup &config)
0062     {
0063         const QByteArray state = config.readEntry(tv->objectName(), QByteArray());
0064         if (!state.isEmpty()) {
0065             tv->header()->restoreState(state);
0066         }
0067     }
0068 
0069     static void process(QComboBox *cb, const KConfigGroup &config)
0070     {
0071         const int index = config.readEntry(cb->objectName(), -1);
0072         if (index >= 0 && index < cb->count()) {
0073             cb->setCurrentIndex(index);
0074         }
0075     }
0076 };
0077 
0078 #define PROCESS_TYPE(T)                                                                                                                                        \
0079     {                                                                                                                                                          \
0080         T *obj = qobject_cast<T *>(w);                                                                                                                         \
0081         if (obj) {                                                                                                                                             \
0082             Op::process(obj, config);                                                                                                                          \
0083             continue;                                                                                                                                          \
0084         }                                                                                                                                                      \
0085     }
0086 
0087 template<typename Op, typename Config>
0088 static void processWidgets(QWidget *widget, Config config)
0089 {
0090     QList<QWidget *> widgets = widget->findChildren<QWidget *>();
0091     widgets << widget;
0092     for (QWidget *w : std::as_const(widgets)) {
0093         if (w->objectName().isEmpty()) {
0094             continue;
0095         }
0096         PROCESS_TYPE(QSplitter);
0097         PROCESS_TYPE(QTabWidget);
0098         PROCESS_TYPE(QTreeView);
0099         PROCESS_TYPE(QComboBox);
0100     }
0101 }
0102 
0103 #undef PROCESS_TYPE
0104 
0105 void UiStateSaver::saveState(QWidget *widget, KConfigGroup &config)
0106 {
0107     processWidgets<Saver, KConfigGroup &>(widget, config);
0108 }
0109 
0110 void UiStateSaver::restoreState(QWidget *widget, const KConfigGroup &config)
0111 {
0112     processWidgets<Restorer, const KConfigGroup &>(widget, config);
0113 }