Commit 9c05d4e8 authored by Laurent Montel's avatar Laurent Montel 😁

Move class from libkdepim here (only uses here + akonadiconsole)

parent 4ed28b81
Pipeline #33639 passed with stage
in 6 minutes and 52 seconds
......@@ -64,6 +64,7 @@ set(kaddressbook_LIB_SRCS
contactswitcher.cpp
globalcontactmodel.cpp
mainwidget.cpp
uistatesaver.cpp
manageshowcollectionproperties.cpp
modelcolumnmanager.cpp
stylecontactlistdelegate.cpp
......
......@@ -26,7 +26,7 @@
#include <KaddressbookGrantlee/GrantleeContactGroupFormatter>
#include <GrantleeTheme/GrantleeThemeManager>
#include <Libkdepim/UiStateSaver>
#include "uistatesaver.h"
#include <PimCommonAkonadi/ImapAclAttribute>
#include <PimCommonAkonadi/MailUtil>
......@@ -412,7 +412,7 @@ void MainWidget::delayedInit()
setViewMode(0); // get default from settings
const KConfigGroup group(Settings::self()->config(), "UiState_ContactView");
KPIM::UiStateSaver::restoreState(mItemView, group);
KAddressBook::UiStateSaver::restoreState(mItemView, group);
mXmlGuiClient->actionCollection()->action(QStringLiteral("options_show_qrcodes"))->setChecked(showQRCodes());
......@@ -433,7 +433,7 @@ MainWidget::~MainWidget()
saveSplitterStates();
KConfigGroup group(Settings::self()->config(), "UiState_ContactView");
KPIM::UiStateSaver::saveState(mItemView, group);
KAddressBook::UiStateSaver::saveState(mItemView, group);
saveState();
delete mGrantleeThemeManager;
......@@ -928,8 +928,8 @@ void MainWidget::saveSplitterStates() const
const QString groupName = QStringLiteral("UiState_MainWidgetSplitter_%1").arg(currentMode);
//qCDebug(KADDRESSBOOK_LOG) << "saving to group" << groupName;
KConfigGroup group(Settings::self()->config(), groupName);
KPIM::UiStateSaver::saveState(mMainWidgetSplitter1, group);
KPIM::UiStateSaver::saveState(mMainWidgetSplitter2, group);
KAddressBook::UiStateSaver::saveState(mMainWidgetSplitter1, group);
KAddressBook::UiStateSaver::saveState(mMainWidgetSplitter2, group);
}
void MainWidget::restoreSplitterStates()
......@@ -944,8 +944,8 @@ void MainWidget::restoreSplitterStates()
const QString groupName = QStringLiteral("UiState_MainWidgetSplitter_%1").arg(currentMode);
//qCDebug(KADDRESSBOOK_LOG) << "restoring from group" << groupName;
KConfigGroup group(Settings::self()->config(), groupName);
KPIM::UiStateSaver::restoreState(mMainWidgetSplitter1, group);
KPIM::UiStateSaver::restoreState(mMainWidgetSplitter2, group);
KAddressBook::UiStateSaver::restoreState(mMainWidgetSplitter1, group);
KAddressBook::UiStateSaver::restoreState(mMainWidgetSplitter2, group);
}
void MainWidget::initGrantleeThemeName()
......
/*
SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "uistatesaver.h"
#include <KConfigGroup>
#include <QComboBox>
#include <QHeaderView>
#include <QSplitter>
#include <QTabWidget>
#include <QTreeView>
using namespace KAddressBook;
struct Saver {
static void process(QSplitter *splitter, KConfigGroup &config)
{
if (splitter->sizes().count(0) == splitter->sizes().count()) {
return;
}
config.writeEntry(splitter->objectName(), splitter->sizes());
}
static void process(QTabWidget *tab, KConfigGroup &config)
{
config.writeEntry(tab->objectName(), tab->currentIndex());
}
static void process(QTreeView *tv, KConfigGroup &config)
{
config.writeEntry(tv->objectName(), tv->header()->saveState());
}
static void process(QComboBox *cb, KConfigGroup &config)
{
config.writeEntry(cb->objectName(), cb->currentIndex());
}
};
struct Restorer {
static void process(QSplitter *splitter, const KConfigGroup &config)
{
const QList<int> sizes = config.readEntry(splitter->objectName(), QList<int>());
if (!sizes.isEmpty() && splitter->count() == sizes.count() && sizes.count() != sizes.count(0)) {
splitter->setSizes(sizes);
}
}
static void process(QTabWidget *tab, const KConfigGroup &config)
{
const int index = config.readEntry(tab->objectName(), -1);
if (index >= 0 && index < tab->count()) {
tab->setCurrentIndex(index);
}
}
static void process(QTreeView *tv, const KConfigGroup &config)
{
const QByteArray state = config.readEntry(tv->objectName(), QByteArray());
if (!state.isEmpty()) {
tv->header()->restoreState(state);
}
}
static void process(QComboBox *cb, const KConfigGroup &config)
{
const int index = config.readEntry(cb->objectName(), -1);
if (index >= 0 && index < cb->count()) {
cb->setCurrentIndex(index);
}
}
};
#define PROCESS_TYPE(T) \
{ \
T *obj = qobject_cast<T *>(w); \
if (obj) { \
Op::process(obj, config); \
continue; \
} \
}
template<typename Op, typename Config> static void processWidgets(QWidget *widget, Config config)
{
QList<QWidget *> widgets = widget->findChildren<QWidget *>();
widgets << widget;
for (QWidget *w : qAsConst(widgets)) {
if (w->objectName().isEmpty()) {
continue;
}
PROCESS_TYPE(QSplitter);
PROCESS_TYPE(QTabWidget);
PROCESS_TYPE(QTreeView);
PROCESS_TYPE(QComboBox);
}
}
#undef PROCESS_TYPE
void UiStateSaver::saveState(QWidget *widget, KConfigGroup &config)
{
processWidgets<Saver, KConfigGroup &>(widget, config);
}
void UiStateSaver::restoreState(QWidget *widget, const KConfigGroup &config)
{
processWidgets<Restorer, const KConfigGroup &>(widget, config);
}
/*
SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef UISTATESAVER_H
#define UISTATESAVER_H
#include "kaddressbook_export.h"
class QWidget;
class KConfigGroup;
namespace KAddressBook {
/**
* @short Methods to save and restore the UI state of an application.
*
* This namespace provides methods that automatically save and restore
* the state of various UI elements to/from a configuration group.
*
* The following widgets are supported so far:
* - QSplitter
* - QTabWidget
* - QTreeView
* - QComboBox
*
* @note The widgets need to have set an objectName, otherwise they are ignored
* on processing.
*
* @author Volker Krause <vkrause@kde.org>
* @since 4.5
*/
namespace UiStateSaver {
/**
* Saves the state of @p widget and all its sub-widgets to @p config.
* @param widget The top-level widget which state should be saved.
* @param config The config group the settings should be written to.
*/
KADDRESSBOOK_EXPORT void saveState(QWidget *widget, KConfigGroup &config);
/**
* Restores the UI state of @p widget and all its sub-widgets from @p config.
* @param widget The top-level widget which state should be restored.
* @param config The config gorup the settings should be read from.
*/
KADDRESSBOOK_EXPORT void restoreState(QWidget *widget, const KConfigGroup &config);
}
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment