diff --git a/CHANGELOG.md b/CHANGELOG.md index e56b5a40..0690dfd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * KIconThemes migration has been completed. * The MeshMC News section is now displayed via KF6 Syndication. * KFormat migration has been completed. +* KMainWindow migration has been completed. ## Removed diff --git a/launcher/Application.cpp b/launcher/Application.cpp index f2890a92..39d0fa26 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -41,6 +41,7 @@ #include "ui/pages/global/CustomCommandsPage.h" #include "ui/pages/global/AppearancePage.h" +#include "ui/WindowState.h" #include "ui/themes/AppearanceManager.h" #include "ui/themes/ThemedIcons.h" @@ -1876,10 +1877,11 @@ MainWindow* Application::showMainWindow(bool minimized) m_mainWindow->activateWindow(); } else { m_mainWindow = new MainWindow(); - m_mainWindow->restoreState(QByteArray::fromBase64( - APPLICATION->globalSettings()->mainWindowState().toUtf8())); - m_mainWindow->restoreGeometry(QByteArray::fromBase64( - APPLICATION->globalSettings()->mainWindowGeometry().toUtf8())); + WindowState::setup( + m_mainWindow, QStringLiteral("MainWindow"), + dynamic_cast(m_settings.get()), + QStringLiteral("MainWindowGeometry"), + QStringLiteral("MainWindowState")); if (minimized) { m_mainWindow->showMinimized(); } else { diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 19f58d00..dc360db3 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -128,6 +128,23 @@ set_property(TEST SettingsBinding_test APPEND PROPERTY ENVIRONMENT "QT_QPA_PLATFORM=offscreen" ) +add_unit_test(WindowState + SOURCES ui/WindowState_test.cpp + LIBS MeshMC_logic + ) +set_property(TEST WindowState_test APPEND PROPERTY ENVIRONMENT + "QT_QPA_PLATFORM=offscreen" + ) + +add_unit_test(NewsFeed + SOURCES news/NewsFeed_test.cpp + LIBS MeshMC_logic + ) +# QTEST_MAIN makes a QApplication, which wants a display; none is needed. +set_property(TEST NewsFeed_test APPEND PROPERTY ENVIRONMENT + "QT_QPA_PLATFORM=offscreen" + ) + add_unit_test(Translations SOURCES translations/Translations_test.cpp LIBS MeshMC_logic diff --git a/launcher/settings/GlobalSettings.kcfg b/launcher/settings/GlobalSettings.kcfg index 5b92eefc..8ccbc884 100644 --- a/launcher/settings/GlobalSettings.kcfg +++ b/launcher/settings/GlobalSettings.kcfg @@ -242,18 +242,6 @@ false - - - - - - - - - - - - diff --git a/launcher/settings/GlobalSettingsSetup_test.cpp b/launcher/settings/GlobalSettingsSetup_test.cpp index ba2d7109..b24037d6 100644 --- a/launcher/settings/GlobalSettingsSetup_test.cpp +++ b/launcher/settings/GlobalSettingsSetup_test.cpp @@ -58,9 +58,9 @@ class GlobalSettingsSetupTest : public QObject void test_EverySettingIsThere() { Fixture f; - // 77 now: a setting that is added brings this up, one that is lost + // 73 now: a setting that is added brings this up, one that is lost // would make a caller's get() return nothing. - QVERIFY(f.settings.allSettings().size() >= 77); + QVERIFY(f.settings.allSettings().size() >= 73); for (const char* id : {"InstanceDir", "JavaPath", "ProxyType", "MinMemAlloc", "InstSortMode", "CurseForgeAPIKey", "PasteEEAPIKey", @@ -163,18 +163,18 @@ class GlobalSettingsSetupTest : public QObject auto* typed = typedGlobalSettings(&f.settings); int changed = 0; QVariant seen; - connect(f.settings.getSetting("MainWindowGeometry").get(), + connect(f.settings.getSetting("NewInstanceGeometry").get(), &Setting::SettingChanged, this, [&](const Setting&, const QVariant& value) { ++changed; seen = value; }); - typed->setMainWindowGeometry(QStringLiteral("AAEC")); + typed->setNewInstanceGeometry(QStringLiteral("AAEC")); typed->setSelectedInstance(QStringLiteral("abc")); typed->save(); QCOMPARE(changed, 1); QCOMPARE(seen.toString(), QString("AAEC")); - QCOMPARE(f.settings.get("MainWindowGeometry").toString(), + QCOMPARE(f.settings.get("NewInstanceGeometry").toString(), QString("AAEC")); QCOMPARE(f.settings.get("SelectedInstance").toString(), QString("abc")); diff --git a/launcher/ui/CMakeLists.txt b/launcher/ui/CMakeLists.txt index 2c5df316..32a1a7db 100644 --- a/launcher/ui/CMakeLists.txt +++ b/launcher/ui/CMakeLists.txt @@ -11,6 +11,8 @@ target_sources(MeshMC_logic PRIVATE MacMenuBar.cpp InstanceWindow.h InstanceWindow.cpp + WindowState.h + WindowState.cpp ) add_subdirectory(setupwizard) diff --git a/launcher/ui/InstanceWindow.cpp b/launcher/ui/InstanceWindow.cpp index ca11b34d..63fffd07 100644 --- a/launcher/ui/InstanceWindow.cpp +++ b/launcher/ui/InstanceWindow.cpp @@ -20,6 +20,8 @@ #include "InstanceWindow.h" #include "settings/GlobalSettings.h" +#include "settings/KConfigSettingsObject.h" +#include "ui/WindowState.h" #include "Application.h" #include @@ -39,7 +41,7 @@ #include InstanceWindow::InstanceWindow(InstancePtr instance, QWidget* parent) - : QMainWindow(parent), m_instance(instance) + : KMainWindow(parent), m_instance(instance) { setAttribute(Qt::WA_DeleteOnClose); @@ -114,15 +116,12 @@ InstanceWindow::InstanceWindow(InstancePtr instance, QWidget* parent) m_container->addButtons(horizontalLayout); } - // restore window state - { - auto base64State = - APPLICATION->globalSettings()->consoleWindowState().toUtf8(); - restoreState(QByteArray::fromBase64(base64State)); - auto base64Geometry = - APPLICATION->globalSettings()->consoleWindowGeometry().toUtf8(); - restoreGeometry(QByteArray::fromBase64(base64Geometry)); - } + // restore window state; every console window has the one of the last + WindowState::setup( + this, QStringLiteral("ConsoleWindow"), + dynamic_cast(APPLICATION->settings().get()), + QStringLiteral("ConsoleWindowGeometry"), + QStringLiteral("ConsoleWindowState")); // set up instance and launch process recognition { @@ -210,11 +209,9 @@ void InstanceWindow::closeEvent(QCloseEvent* event) return; } - APPLICATION->globalSettings()->setConsoleWindowState( - QString::fromLatin1(saveState().toBase64())); - APPLICATION->globalSettings()->setConsoleWindowGeometry( - QString::fromLatin1(saveGeometry().toBase64())); - APPLICATION->globalSettings()->save(); + // What KMainWindow::closeEvent() saves, without it quitting the + // application with the last window: Application decides that. + saveAutoSaveSettings(); emit isClosing(); event->accept(); } diff --git a/launcher/ui/InstanceWindow.h b/launcher/ui/InstanceWindow.h index 2fb7c38f..d08dc951 100644 --- a/launcher/ui/InstanceWindow.h +++ b/launcher/ui/InstanceWindow.h @@ -20,7 +20,7 @@ #pragma once -#include +#include #include #include "LaunchController.h" @@ -32,7 +32,7 @@ class QPushButton; class PageContainer; -class InstanceWindow : public QMainWindow, public BasePageContainer +class InstanceWindow : public KMainWindow, public BasePageContainer { Q_OBJECT diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 1b739fd2..a3118651 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -413,8 +413,12 @@ void MainWindow::setupUi() } MainWindow::MainWindow(QWidget* parent) - : QMainWindow(parent), ui(new Ui::MainWindow) + : KMainWindow(parent), ui(new Ui::MainWindow) { + // KMainWindow deletes itself when it is closed. This one is closed to be + // hidden, and Application keeps it for as long as the launcher runs. + setAttribute(Qt::WA_DeleteOnClose, false); + setupUi(); // OSX magic. @@ -716,7 +720,7 @@ MainWindow::~MainWindow() {} QMenu* MainWindow::createPopupMenu() { - QMenu* filteredMenu = QMainWindow::createPopupMenu(); + QMenu* filteredMenu = KMainWindow::createPopupMenu(); filteredMenu->removeAction(ui->mainToolBar->toggleViewAction()); filteredMenu->addAction(ui->actionLockToolbars); return filteredMenu; @@ -752,7 +756,7 @@ void MainWindow::showEvent(QShowEvent* event) * so the user cannot hide it by hand and this cannot be overruling * them. */ updateMenuBarVisibility(); - QMainWindow::showEvent(event); + KMainWindow::showEvent(event); } void MainWindow::updateMenuBarVisibility() @@ -780,7 +784,7 @@ void MainWindow::keyReleaseEvent(QKeyEvent* event) ui->menuBar->setVisible(!ui->menuBar->isVisible()); return; } - QMainWindow::keyReleaseEvent(event); + KMainWindow::keyReleaseEvent(event); } #endif @@ -1192,7 +1196,7 @@ bool MainWindow::eventFilter(QObject* obj, QEvent* ev) } } } - return QMainWindow::eventFilter(obj, ev); + return KMainWindow::eventFilter(obj, ev); } void MainWindow::updateNewsLabel() @@ -1900,12 +1904,10 @@ void MainWindow::on_actionViewSelectedModsFolder_triggered() void MainWindow::closeEvent(QCloseEvent* event) { - // Save the window state and geometry. - APPLICATION->globalSettings()->setMainWindowState( - QString::fromLatin1(saveState().toBase64())); - APPLICATION->globalSettings()->setMainWindowGeometry( - QString::fromLatin1(saveGeometry().toBase64())); - APPLICATION->globalSettings()->save(); + // What KMainWindow::closeEvent() saves, without what else it does: it + // quits the application with its last window, and that is Application's + // to decide (see on_windowClose()). + saveAutoSaveSettings(); event->accept(); emit isClosing(); } @@ -1915,7 +1917,7 @@ void MainWindow::changeEvent(QEvent* event) if (event->type() == QEvent::LanguageChange) { retranslateUi(); } - QMainWindow::changeEvent(event); + KMainWindow::changeEvent(event); } void MainWindow::instanceActivated(QModelIndex index) diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 4b99f98c..742fb55c 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -22,7 +22,7 @@ #include -#include +#include #include #include #include @@ -52,7 +52,7 @@ class InstanceView; class KonamiCode; class InstanceTask; -class MainWindow : public QMainWindow +class MainWindow : public KMainWindow { Q_OBJECT @@ -260,7 +260,7 @@ class MainWindow : public QMainWindow /*! * Toggles whether the toolbars can be dragged between dock areas. * Persisted via the "ToolbarsLocked" setting; the resulting layout itself - * is persisted by QMainWindow::saveState() in closeEvent(). + * is persisted by KMainWindow, with the rest of the window state, in closeEvent(). */ void lockToolbars(bool state); diff --git a/launcher/ui/WindowState.cpp b/launcher/ui/WindowState.cpp new file mode 100644 index 00000000..9e0020ca --- /dev/null +++ b/launcher/ui/WindowState.cpp @@ -0,0 +1,77 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "WindowState.h" + +#include "settings/KConfigSettingsObject.h" + +#include + +#include +#include +#include + +namespace WindowState +{ + void setup(KMainWindow* window, const QString& group, + KConfigSettingsObject* legacy, const QString& geometryKey, + const QString& stateKey) + { + // Window size and state go to a group of their own: KMainWindow uses + // "MainWindow" for all of its windows unless it is told otherwise, + // which would make the console window start like the main window. + // Has to come first. + window->setStateConfigGroup(group); + + // Whether the window saved anything of its own, which is asked before + // setAutoSaveSettings() makes the group. + const bool saved = window->stateConfigGroup().exists(); + window->setAutoSaveSettings(group, true); + + QByteArray legacyGeometry; + QByteArray legacyState; + if (legacy) { + KConfigGroup top = legacy->config()->group(QString()); + legacyGeometry = QByteArray::fromBase64( + top.readEntry(geometryKey, QString()).toUtf8()); + legacyState = QByteArray::fromBase64( + top.readEntry(stateKey, QString()).toUtf8()); + + if (top.hasKey(geometryKey) || top.hasKey(stateKey)) { + top.deleteEntry(geometryKey); + top.deleteEntry(stateKey); + legacy->config()->sync(); + } + } + + if (saved) { + window->applyMainWindowSettings(window->autoSaveConfigGroup()); + return; + } + + // The state first, then the geometry, as it always was. + if (!legacyState.isEmpty()) { + window->restoreState(legacyState); + } + if (!legacyGeometry.isEmpty()) { + window->restoreGeometry(legacyGeometry); + } + } +} // namespace WindowState diff --git a/launcher/ui/WindowState.h b/launcher/ui/WindowState.h new file mode 100644 index 00000000..b20fef3e --- /dev/null +++ b/launcher/ui/WindowState.h @@ -0,0 +1,53 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#pragma once + +#include + +class KMainWindow; +class KConfigSettingsObject; + +/*! + * How a main window of the application remembers where it was and what it + * looked like, by way of KMainWindow. + */ +namespace WindowState +{ + /*! + * Makes \p window keep its size, its position and the layout of its + * toolbars under \p group of KDE's state configuration, and puts it back + * the way it was. + * + * The window has to be finished (its toolbars made), and not shown yet. + * It saves itself when KMainWindow::saveAutoSaveSettings() is called, which + * its closeEvent() does: KMainWindow::closeEvent() would do that too, + * but it also quits the application when it is the last window, which + * MeshMC decides for itself. + * + * Earlier versions kept geometry and state in the settings, as the text + * saveGeometry() and saveState() make. If \p legacy has those (\p + * geometryKey and \p stateKey), and the window has not saved anything of its + * own yet, it takes them, once. They are removed from \p legacy either way. + */ + void setup(KMainWindow* window, const QString& group, + KConfigSettingsObject* legacy, const QString& geometryKey, + const QString& stateKey); +} // namespace WindowState diff --git a/launcher/ui/WindowState_test.cpp b/launcher/ui/WindowState_test.cpp new file mode 100644 index 00000000..98c11ffa --- /dev/null +++ b/launcher/ui/WindowState_test.cpp @@ -0,0 +1,209 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "settings/KConfigSettingsObject.h" +#include "ui/WindowState.h" + +namespace +{ + // saveAutoSaveSettings() is protected: this is what closeEvent() calls. + class TestWindow : public KMainWindow + { + public: + TestWindow() + { + setAttribute(Qt::WA_DeleteOnClose, false); + auto* tools = new QToolBar(QStringLiteral("tools"), this); + tools->setObjectName(QStringLiteral("tools")); // saveState() wants it + addToolBar(tools); + setCentralWidget(new QWidget(this)); + } + void save() + { + saveAutoSaveSettings(); + } + }; + + QByteArray slurp(const QString& path) + { + QFile file(path); + return file.open(QIODevice::ReadOnly) ? file.readAll() : QByteArray(); + } +} // namespace + +// How the main window and the console window come back the way they were. +class WindowStateTest : public QObject +{ + Q_OBJECT + + // the legacy file of a window that had the given size, in the form + // earlier versions wrote it + static QString legacyFile(const QTemporaryDir& dir, const QSize& size) + { + TestWindow window; + window.resize(size); + window.show(); + const QString path = dir.filePath("meshmc.cfg"); + QFile file(path); + file.open(QIODevice::WriteOnly); + file.write("MainWindowGeometry=" + window.saveGeometry().toBase64() + + "\nMainWindowState=" + window.saveState().toBase64() + + "\nInstanceDir=inst\n"); + return path; + } + + private slots: + // The state of the windows of the test is kept like the real one is, so it + // has to be gone before and after, or a second run starts from the first. + static void forgetTheWindows() + { + auto config = KSharedConfig::openStateConfig(); + for (const char* group : + {"SavedWindow", "LegacyWindow", "OwnWindow", "KindA", "KindB", + "QuitWindow"}) { + config->deleteGroup(QString::fromLatin1(group)); + } + config->sync(); + } + + void initTestCase() + { + QStandardPaths::setTestModeEnabled(true); + KLocalizedString::setApplicationDomain("meshmc"); + forgetTheWindows(); + } + + void cleanupTestCase() + { + forgetTheWindows(); + } + + void test_AWindowComesBackTheWayItWasSaved() + { + const QString group = QStringLiteral("SavedWindow"); + { + TestWindow window; + WindowState::setup(&window, group, nullptr, QString(), QString()); + window.resize(512, 384); + window.show(); + window.save(); + } + + TestWindow again; + WindowState::setup(&again, group, nullptr, QString(), QString()); + QCOMPARE(again.size(), QSize(512, 384)); + } + + void test_TheSettingsOfEarlierVersionsAreTakenOnceAndRemoved() + { + QTemporaryDir dir; + const QString path = legacyFile(dir, QSize(600, 420)); + KConfigSettingsObject legacy(path); + + TestWindow window; + WindowState::setup(&window, QStringLiteral("LegacyWindow"), &legacy, + QStringLiteral("MainWindowGeometry"), + QStringLiteral("MainWindowState")); + QCOMPARE(window.size(), QSize(600, 420)); + + const QByteArray written = slurp(path); + QVERIFY(!written.contains("MainWindowGeometry")); + QVERIFY(!written.contains("MainWindowState")); + QVERIFY(written.contains("InstanceDir=inst")); + } + + void test_WhatTheWindowSavedItselfWinsOverTheOldSettings() + { + const QString group = QStringLiteral("OwnWindow"); + { + TestWindow window; + WindowState::setup(&window, group, nullptr, QString(), QString()); + window.resize(500, 400); + window.show(); + window.save(); + } + + QTemporaryDir dir; + const QString path = legacyFile(dir, QSize(900, 700)); + KConfigSettingsObject legacy(path); + + TestWindow window; + WindowState::setup(&window, group, &legacy, + QStringLiteral("MainWindowGeometry"), + QStringLiteral("MainWindowState")); + QCOMPARE(window.size(), QSize(500, 400)); + // and they are gone all the same + QVERIFY(!slurp(path).contains("MainWindowGeometry")); + } + + void test_EveryKindOfWindowHasItsOwn() + { + { + TestWindow main; + WindowState::setup(&main, QStringLiteral("KindA"), nullptr, + QString(), QString()); + main.resize(500, 400); + main.show(); + main.save(); + } + + // the console window does not start like the main window + TestWindow console; + WindowState::setup(&console, QStringLiteral("KindB"), nullptr, + QString(), QString()); + QVERIFY(console.size() != QSize(500, 400)); + } + + void test_ClosingDoesNotQuitTheApplication() + { + // KMainWindow::closeEvent() would; the windows of MeshMC do not call + // it. This is the one thing the application depends on that a window + // could get wrong by being a KMainWindow. + QSignalSpy quit(qApp, &QCoreApplication::aboutToQuit); + { + TestWindow window; + WindowState::setup(&window, QStringLiteral("QuitWindow"), nullptr, + QString(), QString()); + window.show(); + window.save(); + window.close(); + } + QTest::qWait(50); + QCOMPARE(quit.count(), 0); + } +}; + +QTEST_MAIN(WindowStateTest) + +#include "WindowState_test.moc"