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 saveSize = [q, this]() {
34 KWindowConfig::saveWindowSize(window, configGroup);
35 if (!timerId) {
36 timerId = q->startTimer(std::chrono::seconds(30));
37 }
38 };
39 const auto savePosition = [q, this]() {
40 KWindowConfig::saveWindowPosition(window, configGroup);
41 if (!timerId) {
42 timerId = q->startTimer(std::chrono::seconds(30));
43 }
44 };
45
46 QObject::connect(window, &QWindow::widthChanged, q, saveSize);
47 QObject::connect(window, &QWindow::heightChanged, q, saveSize);
48 QObject::connect(window, &QWindow::xChanged, q, savePosition);
49 QObject::connect(window, &QWindow::yChanged, q, savePosition);
50}
51
52void KWindowStateSaverPrivate::initWidget(QObject *widget, KWindowStateSaver *q)
53{
54 if (!window && windowHandleCallback) {
55 window = windowHandleCallback();
56 }
57 if (window) {
58 init(q);
59 } else {
60 widget->installEventFilter(q);
61 }
62}
63
65 : QObject(window)
66 , d(new KWindowStateSaverPrivate)
67{
68 Q_ASSERT(window);
69 d->window = window;
70 d->configGroup = configGroup;
71 d->init(this);
72}
73
75 : QObject(window)
76 , d(new KWindowStateSaverPrivate)
77{
78 Q_ASSERT(window);
79 d->window = window;
80 d->configGroup = KConfigGroup(KSharedConfig::openStateConfig(), configGroupName);
81 d->init(this);
82}
83
84KWindowStateSaver::~KWindowStateSaver()
85{
86 delete d;
87}
88
89void KWindowStateSaver::timerEvent(QTimerEvent *event)
90{
91 killTimer(event->timerId());
92 d->configGroup.sync();
93 d->timerId = 0;
94}
95
96bool KWindowStateSaver::eventFilter(QObject *watched, QEvent *event)
97{
98 // QEvent::PlatformSurface would give us a valid window, but if there are
99 // intial resizings (explicitly or via layout constraints) those would then
100 // already overwrite our restored values. So wait until all that is done
101 // and only restore afterwards.
102 if (event->type() == QEvent::ShowToParent && !d->window) {
103 watched->removeEventFilter(this);
104 d->window = d->windowHandleCallback();
105 d->init(this);
106 }
107
108 return QObject::eventFilter(watched, event);
109}
110
111void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const KConfigGroup &configGroup)
112{
113 d = new KWindowStateSaverPrivate;
114 d->windowHandleCallback = windowHandleCallback;
115 d->configGroup = configGroup;
116 d->initWidget(widget, this);
117}
118
119void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const QString &configGroupName)
120{
121 d = new KWindowStateSaverPrivate;
122 d->windowHandleCallback = windowHandleCallback;
123 d->configGroup = KConfigGroup(KSharedConfig::openStateConfig(), configGroupName);
124 d->initWidget(widget, this);
125}
126
127#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 Sat Dec 21 2024 17:01:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.