QtPass
2.0.0
Developer documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
windowstatestore.cpp
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
8
#include "
windowstatestore.h
"
9
#include "
qtpasssettings.h
"
10
11
#include <QCursor>
12
#include <QDialog>
13
#include <QGuiApplication>
14
#include <QScreen>
15
#include <QWidget>
16
17
namespace
{
18
24
auto
onWayland() ->
bool
{
25
return
QGuiApplication::platformName().startsWith(QLatin1String(
"wayland"
));
26
}
27
28
}
// namespace
29
30
namespace
WindowStateStore
{
31
32
auto
restore
(QWidget &window,
const
QString &key) ->
bool
{
33
const
QByteArray geometry =
QtPassSettings::getDialogGeometry
(key);
34
if
(!geometry.isEmpty() && window.restoreGeometry(geometry)) {
35
return
true
;
36
}
37
centreOnCursorScreen
(window);
38
return
false
;
39
}
40
41
void
save
(
const
QWidget &window,
const
QString &key) {
42
QtPassSettings::setDialogGeometry
(key, window.saveGeometry());
43
}
44
45
void
attach
(QDialog &dialog,
const
QString &key) {
46
restore
(dialog, key);
47
QObject::connect(&dialog, &QDialog::finished, &dialog,
48
[&dialog, key] {
save
(dialog, key); });
49
}
50
51
void
centreOnCursorScreen
(QWidget &window) {
52
if
(onWayland()) {
53
return
;
54
}
55
QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
56
if
(screen ==
nullptr
) {
57
screen = QGuiApplication::primaryScreen();
58
}
59
if
(screen ==
nullptr
) {
60
return
;
61
}
62
QRect frame = window.frameGeometry();
63
frame.moveCenter(screen->availableGeometry().center());
64
window.move(frame.topLeft());
65
}
66
67
}
// namespace WindowStateStore
QtPassSettings::setDialogGeometry
static void setDialogGeometry(const QString &key, const QByteArray &geometry)
Save dialog geometry.
Definition
qtpasssettings.cpp:164
QtPassSettings::getDialogGeometry
static auto getDialogGeometry(const QString &key, const QByteArray &defaultValue=QVariant().toByteArray()) -> QByteArray
Get saved dialog geometry.
Definition
qtpasssettings.cpp:157
WindowStateStore
Persist and restore a top-level window's geometry, one way for the main window and every dialog.
Definition
windowstatestore.cpp:30
WindowStateStore::save
void save(const QWidget &window, const QString &key)
Save window's geometry under key.
Definition
windowstatestore.cpp:41
WindowStateStore::restore
auto restore(QWidget &window, const QString &key) -> bool
Restore window's geometry saved under key.
Definition
windowstatestore.cpp:32
WindowStateStore::centreOnCursorScreen
void centreOnCursorScreen(QWidget &window)
Move window so its frame is centred on the screen under the mouse pointer (the primary screen when th...
Definition
windowstatestore.cpp:51
WindowStateStore::attach
void attach(QDialog &dialog, const QString &key)
restore() now, save() whenever the dialog finishes.
Definition
windowstatestore.cpp:45
qtpasssettings.h
QtPassSettings, the QSettings singleton behind every saved setting, the profiles and the Pass backend...
windowstatestore.h
WindowStateStore: saves and restores top-level window geometry through QtPassSettings,...
src
windowstatestore.cpp
Generated by
1.18.0