From f446af2aa592997f6bc4aa3b5559cf477f9259f8 Mon Sep 17 00:00:00 2001 From: Volker Krause Date: Mon, 14 Feb 2022 18:12:24 +0100 Subject: Add KWindowStateSaver This is basically the C++ counter-part to https://invent.kde.org/frameworks/kconfig/-/merge_requests/94 and allows to easily retrofit window size persistence on existing windows/ dialogs, replacing e.g. code like https://invent.kde.org/pim/pimcommon/-/blob/master/src/pimcommon/widgets/kpimprintpreviewdialog.cpp. This is a bit more complicated than one might expect, as KWindowConfig works with QWindows, but that's something freshly created QWidget windows/ dialogs don't have yet. Additionally, we are in a library here that doesn't depend on Qt::Widgets. To overcome this we move the widget-dependent code (basically just a call to QWidget::windowHandle()) to inline template code (and thus into the consumer), use std::function's type erasure to pass it into the library code, and an event filter on the widget to wait for the QWindow to become available. --- autotests/kwindowstatesavertest.cpp | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 autotests/kwindowstatesavertest.cpp (limited to 'autotests/kwindowstatesavertest.cpp') diff --git a/autotests/kwindowstatesavertest.cpp b/autotests/kwindowstatesavertest.cpp new file mode 100644 index 00000000..a8b67218 --- /dev/null +++ b/autotests/kwindowstatesavertest.cpp @@ -0,0 +1,90 @@ +/* + SPDX-FileCopyrightText: 2022 Volker Krause + SPDX-License-Identifier: LGPL-2.0-or-later +*/ + +#include "kwindowstatesaver.h" +#include "kconfiggroup.h" +#include "ksharedconfig.h" + +#include +#include +#include + +#include + +class KWindowStateSaverTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void testTopLevelDialog(); + void testSubDialog(); +}; + +void KWindowStateSaverTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); +} + +void KWindowStateSaverTest::testTopLevelDialog() +{ + auto cfg = KSharedConfig::openStateConfig(); + cfg->deleteGroup("topLevelDialogTest"); + QSize dlgSize(720, 720); + + { + QFontDialog dlg; + new KWindowStateSaver(&dlg, "topLevelDialogTest"); + dlg.show(); + QTest::qWait(10); // give the window time to show up, so we simulate a user-triggered resize + dlg.resize(dlgSize); + QTest::qWait(500); // give the state saver time to trigger + QCOMPARE(dlg.size(), dlgSize); + } + + QVERIFY(cfg->hasGroup("topLevelDialogTest")); + + { + QFontDialog dlg; + new KWindowStateSaver(&dlg, "topLevelDialogTest"); + dlg.show(); + QTest::qWait(100); // give the window time to show up properly + QCOMPARE(dlg.size(), dlgSize); + } +} + +void KWindowStateSaverTest::testSubDialog() +{ + QWidget mainWindow; + mainWindow.show(); + QTest::qWait(10); + + auto cfg = KSharedConfig::openStateConfig(); + cfg->deleteGroup("subDialogTest"); + QSize dlgSize(700, 500); + + { + auto dlg = new QFontDialog(&mainWindow); + new KWindowStateSaver(dlg, "subDialogTest"); + dlg->show(); + QTest::qWait(10); // give the window time to show up, so we simulate a user-triggered resize + dlg->resize(dlgSize); + QTest::qWait(500); // give the state saver time to trigger + QCOMPARE(dlg->size(), dlgSize); + delete dlg; + } + + QVERIFY(cfg->hasGroup("subDialogTest")); + + { + auto dlg = new QFontDialog(&mainWindow); + new KWindowStateSaver(dlg, "subDialogTest"); + dlg->show(); + QTest::qWait(100); // give the window time to show up properly + QCOMPARE(dlg->size(), dlgSize); + } +} + +QTEST_MAIN(KWindowStateSaverTest) +#include "kwindowstatesavertest.moc" -- cgit v1.2.1