KConfig

kwindowstatesaver.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "kwindowstatesaver.h"
7#include "ksharedconfig.h"
8#include "kwindowconfig.h"
9
10#include <QWindow>
11
12class KWindowStateSaverPrivate
13{
14public:
15 QWindow *window = nullptr;
16 KConfigGroup configGroup;
17 std::function<QWindow *()> windowHandleCallback;
18 int timerId = 0;
19
20 void init(KWindowStateSaver *q);
21 void initWidget(QObject *widget, KWindowStateSaver *q);
22};
23
24void KWindowStateSaverPrivate::init(KWindowStateSaver *q)
25{
26 if (!window) {
27 return;
28 }
29
30 KWindowConfig::restoreWindowSize(window, configGroup);
31 KWindowConfig::restoreWindowPosition(window, configGroup);
32
33 const auto deferredSave = [q, this]() {
34 if (!timerId) {
35 timerId = q->startTimer(250);
36 }
37 };
38 QObject::connect(window, &QWindow::widthChanged, q, deferredSave);
39 QObject::connect(window, &QWindow::heightChanged, q, deferredSave);
40 QObject::connect(window, &QWindow::xChanged, q, deferredSave);
41 QObject::connect(window, &QWindow::yChanged, q, deferredSave);
42}
43
44void KWindowStateSaverPrivate::initWidget(QObject *widget, KWindowStateSaver *q)
45{
46 if (!window && windowHandleCallback) {
47 window = windowHandleCallback();
48 }
49 if (window) {
50 init(q);
51 } else {
52 widget->installEventFilter(q);
53 }
54}
55
57 : QObject(window)
58 , d(new KWindowStateSaverPrivate)
59{
60 Q_ASSERT(window);
61 d->window = window;
62 d->configGroup = configGroup;
63 d->init(this);
64}
65
67 : QObject(window)
68 , d(new KWindowStateSaverPrivate)
69{
70 Q_ASSERT(window);
71 d->window = window;
72 d->configGroup = KConfigGroup(KSharedConfig::openStateConfig(), configGroupName);
73 d->init(this);
74}
75
76KWindowStateSaver::~KWindowStateSaver()
77{
78 delete d;
79}
80
81void KWindowStateSaver::timerEvent(QTimerEvent *event)
82{
83 killTimer(event->timerId());
84 KWindowConfig::saveWindowPosition(d->window, d->configGroup);
85 KWindowConfig::saveWindowSize(d->window, d->configGroup);
86 d->configGroup.sync();
87 d->timerId = 0;
88}
89
90bool KWindowStateSaver::eventFilter(QObject *watched, QEvent *event)
91{
92 // QEvent::PlatformSurface would give us a valid window, but if there are
93 // intial resizings (explicitly or via layout constraints) those would then
94 // already overwrite our restored values. So wait until all that is done
95 // and only restore afterwards.
96 if (event->type() == QEvent::ShowToParent && !d->window) {
97 watched->removeEventFilter(this);
98 d->window = d->windowHandleCallback();
99 d->init(this);
100 }
101
102 return QObject::eventFilter(watched, event);
103}
104
105void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const KConfigGroup &configGroup)
106{
107 d = new KWindowStateSaverPrivate;
108 d->windowHandleCallback = windowHandleCallback;
109 d->configGroup = configGroup;
110 d->initWidget(widget, this);
111}
112
113void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const QString &configGroupName)
114{
115 d = new KWindowStateSaverPrivate;
116 d->windowHandleCallback = windowHandleCallback;
117 d->configGroup = KConfigGroup(KSharedConfig::openStateConfig(), configGroupName);
118 d->initWidget(widget, this);
119}
120
121#include "moc_kwindowstatesaver.cpp"
A class for one specific group in a KConfig object.
bool sync() override
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Creates a KSharedConfig object to manipulate a configuration file suitable for storing state informat...
Saves and restores a window size and (when possible) position.
KWindowStateSaver(QWindow *window, const KConfigGroup &configGroup)
Create a new window state saver for window.
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
Saves the window's size dependent on the screen dimension either to the global or application config ...
KCONFIGGUI_EXPORT void saveWindowPosition(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
Saves the window's position either to the global or application config file.
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
Restores the dialog's size from the configuration according to the screen size.
KCONFIGGUI_EXPORT void restoreWindowPosition(QWindow *window, const KConfigGroup &config)
Restores the window's screen position from the configuration and calls restoreWindowScreenPosition.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
void killTimer(int id)
void removeEventFilter(QObject *obj)
int startTimer(int interval, Qt::TimerType timerType)
void heightChanged(int arg)
void widthChanged(int arg)
void xChanged(int arg)
void yChanged(int arg)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.