KConfig

kwindowconfig.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2012 Benjamin Port <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 
8 #include "kwindowconfig.h"
9 
10 #include <QScreen>
11 #include <QWindow>
12 
13 static const char s_initialSizePropertyName[] = "_kconfig_initial_size";
14 static const char s_initialScreenSizePropertyName[] = "_kconfig_initial_screen_size";
15 
17 {
18  // QWindow::screen() shouldn't return null, but it sometimes does due to bugs.
19  if (!window || !window->screen()) {
20  return;
21  }
22  const QRect desk = window->screen()->geometry();
23 
24  const QSize sizeToSave = window->size();
25  const bool isMaximized = window->windowState() & Qt::WindowMaximized;
26 
27  const QString screenMaximizedString(QStringLiteral("Window-Maximized %1x%2").arg(desk.height()).arg(desk.width()));
28  // Save size only if window is not maximized
29  if (!isMaximized) {
30  const QSize defaultSize(window->property(s_initialSizePropertyName).toSize());
31  const QSize defaultScreenSize(window->property(s_initialScreenSizePropertyName).toSize());
32  const bool sizeValid = defaultSize.isValid() && defaultScreenSize.isValid();
33  if (!sizeValid || (sizeValid && (defaultSize != sizeToSave || defaultScreenSize != desk.size()))) {
34  const QString wString(QStringLiteral("Width %1").arg(desk.width()));
35  const QString hString(QStringLiteral("Height %1").arg(desk.height()));
36  config.writeEntry(wString, sizeToSave.width(), options);
37  config.writeEntry(hString, sizeToSave.height(), options);
38  }
39  }
40  if ((isMaximized == false) && !config.hasDefault(screenMaximizedString)) {
41  config.revertToDefault(screenMaximizedString);
42  } else {
43  config.writeEntry(screenMaximizedString, isMaximized, options);
44  }
45 
46 }
47 
49 {
50  if (!window) {
51  return;
52  }
53 
54  const QRect desk = window->screen()->geometry();
55 
56  const int width = config.readEntry(QStringLiteral("Width %1").arg(desk.width()), window->size().width());
57  const int height = config.readEntry(QStringLiteral("Height %1").arg(desk.height()), window->size().height());
58  const bool isMaximized = config.readEntry(QStringLiteral("Window-Maximized %1x%2").arg(desk.height()).arg(desk.width()), false);
59 
60  // Check default size
61  const QSize defaultSize(window->property(s_initialSizePropertyName).toSize());
62  const QSize defaultScreenSize(window->property(s_initialScreenSizePropertyName).toSize());
63  if (!defaultSize.isValid() || !defaultScreenSize.isValid()) {
64  window->setProperty(s_initialSizePropertyName, window->size());
65  window->setProperty(s_initialScreenSizePropertyName, desk.size());
66  }
67 
68  // If window is maximized set maximized state and in all case set the size
69  window->resize(width, height);
70  if (isMaximized) {
72  }
73 }
virtual QSize size() const const override
QSize size() const const
int width() const const
bool hasDefault(const QString &key) const
Whether a default is specified for an entry in either the system wide configuration file or the globa...
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Writes a value to the configuration object.
int height() const const
QVariant property(const char *name) const const
QSize toSize() const const
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
void setWindowState(Qt::WindowState state)
int width() const const
void resize(const QSize &newSize)
int height() const const
Qt::WindowState windowState() const const
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
Restores the dialog&#39;s size from the configuration according to the screen size.
bool setProperty(const char *name, const QVariant &value)
WindowMaximized
QScreen * screen() const const
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:236
void revertToDefault(const QString &key)
Reverts an entry to the default settings.
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
Saves the window&#39;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-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:46:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.