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 
12 class KWindowStateSaverPrivate
13 {
14 public:
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 
24 void 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 
44 void 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 
66 KWindowStateSaver::KWindowStateSaver(QWindow *window, const QString &configGroupName)
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 
76 KWindowStateSaver::~KWindowStateSaver()
77 {
78  delete d;
79 }
80 
81 void 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->timerId = 0;
87 }
88 
89 bool KWindowStateSaver::eventFilter(QObject *watched, QEvent *event)
90 {
91  // QEvent::PlatformSurface would give us a valid window, but if there are
92  // intial resizings (explicitly or via layout constraints) those would then
93  // already overwrite our restored values. So wait until all that is done
94  // and only restore afterwards.
95  if (event->type() == QEvent::ShowToParent && !d->window) {
96  watched->removeEventFilter(this);
97  d->window = d->windowHandleCallback();
98  d->init(this);
99  }
100 
101  return QObject::eventFilter(watched, event);
102 }
103 
104 void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const KConfigGroup &configGroup)
105 {
106  d = new KWindowStateSaverPrivate;
107  d->windowHandleCallback = windowHandleCallback;
108  d->configGroup = configGroup;
109  d->initWidget(widget, this);
110 }
111 
112 void KWindowStateSaver::initWidget(QObject *widget, const std::function<QWindow *()> &windowHandleCallback, const QString &configGroupName)
113 {
114  d = new KWindowStateSaverPrivate;
115  d->windowHandleCallback = windowHandleCallback;
116  d->configGroup = KConfigGroup(KSharedConfig::openStateConfig(), configGroupName);
117  d->initWidget(widget, this);
118 }
119 
120 #include "moc_kwindowstatesaver.cpp"
void removeEventFilter(QObject *obj)
KCONFIGGUI_EXPORT void restoreWindowPosition(QWindow *window, const KConfigGroup &config)
Restores the window's screen position from the configuration and calls restoreWindowScreenPosition.
int startTimer(int interval, Qt::TimerType timerType)
void heightChanged(int arg)
QWidget * window(QObject *job)
void yChanged(int arg)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Saves and restores a window size and (when possible) position.
virtual bool eventFilter(QObject *watched, QEvent *event)
virtual bool event(QEvent *e)
void xChanged(int arg)
void killTimer(int id)
void installEventFilter(QObject *filterObj)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Creates a KSharedConfig object to manipulate a configuration file suitable for storing state informat...
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.
void widthChanged(int arg)
KWindowStateSaver(QWindow *window, const KConfigGroup &configGroup)
Create a new window state saver for window.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
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 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 ...
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 04:07:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.