Commit 892754f9 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Move this class here

parent 918cecff
Pipeline #33640 passed with stage
in 10 minutes and 26 seconds
......@@ -78,7 +78,7 @@ find_package(KF5Akonadi ${AKONADI_VERSION} CONFIG REQUIRED)
find_package(KF5Contacts ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5CalendarCore ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5Mime ${KMIME_LIB_VERSION} CONFIG REQUIRED)
find_package(KF5Libkdepim ${LIBKDEPIM_LIB_VERSION} CONFIG REQUIRED) # Because of KPIM::KCheckComboBox, KPIM::UiStateSaver
find_package(KF5Libkdepim ${LIBKDEPIM_LIB_VERSION} CONFIG REQUIRED) # Because of KPIM::KCheckComboBox
if (NOT WIN32)
find_package(KF5AkonadiContact ${AKONADI_CONTACT_VERSION} CONFIG REQUIRED)
......
......@@ -41,6 +41,7 @@ set(libakonadiconsole_SRCS
notificationmonitor.cpp
querydebugger.cpp
tagpropertiesdialog.cpp
uistatesaver.cpp
${libakonadiconsole_tracker_SRCS}
)
......
......@@ -9,7 +9,7 @@
#include "mainwindow.h"
#include "config-akonadiconsole.h"
#include <Libkdepim/UiStateSaver>
#include "uistatesaver.h"
#include <KActionCollection>
#include <KConfigGroup>
......@@ -27,7 +27,7 @@ MainWindow::MainWindow(QWidget *parent)
KStandardAction::quit(qApp, &QApplication::quit, actionCollection());
setupGUI(Keys /*| ToolBar | StatusBar*/ | Save | Create, QStringLiteral("akonadiconsoleui.rc"));
KPIM::UiStateSaver::restoreState(this, KConfigGroup(KSharedConfig::openConfig(), "UiState"));
AkonadiConsole::UiStateSaver::restoreState(this, KConfigGroup(KSharedConfig::openConfig(), "UiState"));
KMessageBox::information(this, QStringLiteral("<p>Akonadi Console is purely a development tool. "
"It allows you to view and change internal data structures of Akonadi. "
"You should only change data in here if you know what you are doing, otherwise "
......@@ -44,7 +44,7 @@ MainWindow::~MainWindow()
void MainWindow::closeEvent(QCloseEvent *event)
{
KConfigGroup config(KSharedConfig::openConfig(), "UiState");
KPIM::UiStateSaver::saveState(this, config);
AkonadiConsole::UiStateSaver::saveState(this, config);
KSharedConfig::openConfig()->sync();
KXmlGuiWindow::closeEvent(event);
}
/*
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 AkonadiConsole;
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
class QWidget;
class KConfigGroup;
namespace AkonadiConsole {
/**
* @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.
*/
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.
*/
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